Better logging in tokenstream
This commit is contained in:
+23
-8
@@ -3,12 +3,12 @@
|
||||
#include <string.h>
|
||||
|
||||
static void test_tokenstream_open_fail(void) {
|
||||
TokenStream* ts = tokenstream_open(NULL);
|
||||
TokenStream* ts = tokenstream_open(NULL, NULL);
|
||||
if (ts != NULL) fail("expected NULL for NULL buffer");
|
||||
}
|
||||
|
||||
static void test_tokenstream_simple_keyword(void) {
|
||||
TokenStream* ts = tokenstream_open("module");
|
||||
TokenStream* ts = tokenstream_open("test.c", "module");
|
||||
|
||||
Token t = tokenstream_next(ts);
|
||||
if (t.token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
@@ -20,7 +20,7 @@ static void test_tokenstream_simple_keyword(void) {
|
||||
}
|
||||
|
||||
static void test_tokenstream_keywords_and_symbols(void) {
|
||||
TokenStream* ts = tokenstream_open("module main; import stdio;");
|
||||
TokenStream* ts = tokenstream_open("test.c", "module main; import stdio;");
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (main)");
|
||||
@@ -34,7 +34,7 @@ static void test_tokenstream_keywords_and_symbols(void) {
|
||||
}
|
||||
|
||||
static void test_tokenstream_parentheses_and_brackets(void) {
|
||||
TokenStream* ts = tokenstream_open("()[]");
|
||||
TokenStream* ts = tokenstream_open("test.c", "()[]");
|
||||
|
||||
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");
|
||||
@@ -46,7 +46,7 @@ static void test_tokenstream_parentheses_and_brackets(void) {
|
||||
}
|
||||
|
||||
static void test_tokenstream_comma(void) {
|
||||
TokenStream* ts = tokenstream_open("a,b,c");
|
||||
TokenStream* ts = tokenstream_open("test.c", "a,b,c");
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected a");
|
||||
if (tokenstream_next(ts).token != TOKEN_COMMA) fail("expected comma");
|
||||
@@ -59,7 +59,7 @@ static void test_tokenstream_comma(void) {
|
||||
}
|
||||
|
||||
static void test_tokenstream_whitespace_ignored(void) {
|
||||
TokenStream* ts = tokenstream_open(" module \n\t import ; ");
|
||||
TokenStream* ts = tokenstream_open("test.c", " module \n\t import ; ");
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
if (tokenstream_next(ts).token != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
|
||||
@@ -70,7 +70,7 @@ static void test_tokenstream_whitespace_ignored(void) {
|
||||
}
|
||||
|
||||
static void test_tokenstream_void_function_signature(void) {
|
||||
TokenStream* ts = tokenstream_open("void main()");
|
||||
TokenStream* ts = tokenstream_open("test.c", "void main()");
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_VOID) fail("expected TOKEN_VOID");
|
||||
if (tokenstream_next(ts).token != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
||||
@@ -81,8 +81,23 @@ static void test_tokenstream_void_function_signature(void) {
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_unknown_token(void) {
|
||||
TokenStream* ts = tokenstream_open("test.c", "%");
|
||||
|
||||
if (tokenstream_next(ts).token != TOKEN_UNKNOWN) fail("expected TOKEN_UNKNOWN");
|
||||
|
||||
assert_log(
|
||||
"--- test.c ---\n"
|
||||
"1| %\n"
|
||||
" ^\n"
|
||||
" unexpected token '%'\n",
|
||||
"expected error message for unknown token");
|
||||
|
||||
tokenstream_close(ts);
|
||||
}
|
||||
|
||||
static void test_tokenstream_info(void) {
|
||||
TokenStream* ts = tokenstream_open("module main;");
|
||||
TokenStream* ts = tokenstream_open("test.c", "module main;");
|
||||
|
||||
Token t1 = tokenstream_next(ts);
|
||||
if (t1.token != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||
|
||||
Reference in New Issue
Block a user