422203fdab
Since buffer_open_string() always succeeds and tokenstream_open() always succeeds when given a valid buffer, the NULL checks are unnecessary. This simplifies the test code and makes it more readable. All 12 tests still pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
88 lines
3.3 KiB
C
88 lines
3.3 KiB
C
#include "test.h"
|
|
#include "token.h"
|
|
|
|
static void test_tokenstream_open_fail(void) {
|
|
TokenStream* ts = tokenstream_open(NULL);
|
|
if (ts != NULL) fail("expected NULL for NULL buffer");
|
|
}
|
|
|
|
static void test_tokenstream_simple_keyword(void) {
|
|
Buffer* buf = buffer_open_string("module");
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
|
|
Token t = tokenstream_next(ts);
|
|
if (t != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
|
|
|
Token eof = tokenstream_next(ts);
|
|
if (eof != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_keywords_and_symbols(void) {
|
|
Buffer* buf = buffer_open_string("module main; import stdio;");
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
|
|
if (tokenstream_next(ts) != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (main)");
|
|
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
|
if (tokenstream_next(ts) != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (stdio)");
|
|
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_parentheses_and_brackets(void) {
|
|
Buffer* buf = buffer_open_string("()[]");
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
|
|
if (tokenstream_next(ts) != TOKEN_BRACKET_OPEN) fail("expected TOKEN_BRACKET_OPEN");
|
|
if (tokenstream_next(ts) != TOKEN_BRACKET_CLOSE) fail("expected TOKEN_BRACKET_CLOSE");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_comma(void) {
|
|
Buffer* buf = buffer_open_string("a,b,c");
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected a");
|
|
if (tokenstream_next(ts) != TOKEN_COMMA) fail("expected comma");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected b");
|
|
if (tokenstream_next(ts) != TOKEN_COMMA) fail("expected comma");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected c");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_whitespace_ignored(void) {
|
|
Buffer* buf = buffer_open_string(" module \n\t import ; ");
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
|
|
if (tokenstream_next(ts) != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
|
if (tokenstream_next(ts) != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
|
|
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|
|
|
|
static void test_tokenstream_void_function_signature(void) {
|
|
Buffer* buf = buffer_open_string("void main()");
|
|
TokenStream* ts = tokenstream_open(buf);
|
|
|
|
if (tokenstream_next(ts) != TOKEN_VOID) fail("expected TOKEN_VOID");
|
|
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
|
|
if (tokenstream_next(ts) != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
|
|
if (tokenstream_next(ts) != -1) fail("expected EOF");
|
|
|
|
tokenstream_close(ts);
|
|
}
|