58 lines
1.4 KiB
C
58 lines
1.4 KiB
C
#include <assert.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "../include/sync/lexer.h"
|
|
|
|
void test_tokenize_simple_assignment(void) {
|
|
const char *src = "x = 42;";
|
|
Lexer lexer;
|
|
lexer_init(&lexer, src);
|
|
|
|
Token t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_IDENTIFIER && strncmp(t.start, "x", t.length) == 0);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_OPERATOR && strncmp(t.start, "=", t.length) == 0);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_NUMBER && strncmp(t.start, "42", t.length) == 0);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_SEMICOLON);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_EOF);
|
|
}
|
|
|
|
void test_tokenize_function_call(void) {
|
|
const char *src = "print(x);";
|
|
Lexer lexer;
|
|
lexer_init(&lexer, src);
|
|
|
|
Token t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_IDENTIFIER && strncmp(t.start, "print", t.length) == 0);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_LPAREN);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_IDENTIFIER && strncmp(t.start, "x", t.length) == 0);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_RPAREN);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_SEMICOLON);
|
|
|
|
t = lexer_next(&lexer);
|
|
assert(t.type == TOKEN_EOF);
|
|
}
|
|
|
|
int main(void) {
|
|
test_tokenize_simple_assignment();
|
|
test_tokenize_function_call();
|
|
|
|
printf("All lexer tests passed.\n");
|
|
return 0;
|
|
}
|