Refactor tests
This commit is contained in:
+11
-45
@@ -9,23 +9,19 @@ static void test_tokenstream_open_fail(void) {
|
||||
}
|
||||
|
||||
static void test_tokenstream_simple_keyword(void) {
|
||||
TokenStream* ts;
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
Token t;
|
||||
Token eof;
|
||||
|
||||
ts = tokenstream_open("test.c", "module");
|
||||
|
||||
t = tokenstream_next(ts);
|
||||
if (t.token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
|
||||
eof = tokenstream_next(ts);
|
||||
if (eof.token != TOKEN_EOF) fail("expected EOF");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_keywords_and_symbols(void) {
|
||||
TokenStream* ts = tokenstream_open("test.c", "module main; import stdio;");
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (main)");
|
||||
@@ -34,24 +30,20 @@ static void test_tokenstream_keywords_and_symbols(void) {
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (stdio)");
|
||||
if (tokenstream_next(ts).token != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
||||
if (tokenstream_next(ts).token != TOKEN_EOF) fail("expected EOF");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_parentheses_and_brackets(void) {
|
||||
TokenStream* ts = tokenstream_open("test.c", "()[]");
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
|
||||
if (tokenstream_next(ts).token != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
|
||||
if (tokenstream_next(ts).token != TOKEN_BRACKET_OPEN) fail("expected TOKEN_BRACKET_OPEN");
|
||||
if (tokenstream_next(ts).token != TOKEN_BRACKET_CLOSE) fail("expected TOKEN_BRACKET_CLOSE");
|
||||
if (tokenstream_next(ts).token != TOKEN_EOF) fail("expected EOF");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_comma(void) {
|
||||
TokenStream* ts = tokenstream_open("test.c", "a,b,c");
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected a");
|
||||
if (tokenstream_next(ts).token != TOKEN_COMMA) fail("expected comma");
|
||||
@@ -59,75 +51,49 @@ static void test_tokenstream_comma(void) {
|
||||
if (tokenstream_next(ts).token != TOKEN_COMMA) fail("expected comma");
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected c");
|
||||
if (tokenstream_next(ts).token != TOKEN_EOF) fail("expected EOF");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_whitespace_ignored(void) {
|
||||
TokenStream* ts = tokenstream_open("test.c", " module \n\t import ; ");
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
if (tokenstream_next(ts).token != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
|
||||
if (tokenstream_next(ts).token != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
||||
if (tokenstream_next(ts).token != TOKEN_EOF) fail("expected EOF");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_void_function_signature(void) {
|
||||
TokenStream* ts = tokenstream_open("test.c", "void main()");
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_VOID) fail("expected TOKEN_VOID");
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
||||
if (tokenstream_next(ts).token != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
|
||||
if (tokenstream_next(ts).token != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
|
||||
if (tokenstream_next(ts).token != TOKEN_EOF) fail("expected EOF");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_unknown_token(void) {
|
||||
TokenStream* ts = tokenstream_get_test();
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_UNKNOWN) fail("expected TOKEN_UNKNOWN");
|
||||
|
||||
assert_log_file("expected error message for unknown token");
|
||||
|
||||
tokenstream_close(ts);
|
||||
assert_log_file("expected error message for unknown token");
|
||||
}
|
||||
|
||||
static void test_tokenstream_info(void) {
|
||||
TokenStream* ts;
|
||||
TokenStream* ts = test_get_tokenstream();
|
||||
Token t1;
|
||||
Token t2;
|
||||
char* buf1;
|
||||
char* buf2;
|
||||
|
||||
ts = tokenstream_open("test.c", "module main;");
|
||||
|
||||
t1 = tokenstream_next(ts);
|
||||
if (t1.token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
|
||||
buf1 = malloc((size_t)t1.text.length + 1);
|
||||
if (!buf1) fail("out of memory");
|
||||
memcpy(buf1, t1.text.data, t1.text.length);
|
||||
buf1[t1.text.length] = '\0';
|
||||
assert_str("module", buf1, "info: expected 'module'");
|
||||
assert_string("module", t1.text, "info: expected 'module'");
|
||||
if (t1.location.line != 1) fail("expected line 1");
|
||||
if (t1.location.column_start != 1) fail("expected column 1");
|
||||
|
||||
t2 = tokenstream_next(ts);
|
||||
if (t2.token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
||||
|
||||
buf2 = malloc((size_t)t2.text.length + 1);
|
||||
if (!buf2) { free(buf1); fail("out of memory"); }
|
||||
memcpy(buf2, t2.text.data, t2.text.length);
|
||||
buf2[t2.text.length] = '\0';
|
||||
assert_str("main", buf2, "info: expected 'main'");
|
||||
assert_string("main", t2.text, "info: expected 'main'");
|
||||
if (t2.location.line != 1) fail("expected line 1");
|
||||
if (t2.location.column_start != 8) fail("expected column 8");
|
||||
|
||||
free(buf1);
|
||||
free(buf2);
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user