skopy/tests/lexer.h

71 lines
1.5 KiB
C

#ifndef SK_TEST_LEXER_H
#define SK_TEST_LEXER_H
#include <CUnit/CUnit.h>
#include <lexer.h>
#include <token.h>
static void test_lexer(char const* source, int count, ...)
{
struct lexer lexer;
lexer_init(&lexer, source);
va_list lst;
va_start(lst, count);
for (int i=0; i<count; i++)
{
struct token* tok = lexer_try_new_next(&lexer);
CU_ASSERT_FATAL(tok != NULL);
struct str tok_str;
str_init(&tok_str);
token_str(tok, &tok_str);
char* oracle = va_arg(lst, char*);
if (strcmp(oracle, tok_str.value) != 0)
{
fprintf(stderr, "%s != %s\n", oracle, tok_str.value);
}
CU_ASSERT_STRING_EQUAL(tok_str.value, oracle);
str_free(&tok_str);
token_free(tok);
free(tok);
}
va_end(lst);
lexer_free(&lexer);
}
static void test_lexer_int()
{
test_lexer(" 4 -23 720 ", 3,
"INT[4]",
"INT[-23]",
"INT[720]"
);
test_lexer("+-*/^%()", 8,
"ADD", "SUB", "MUL", "DIV",
"POW", "MOD", "OPAR", "CPAR"
);
}
static void test_lexer_assert()
{
test_lexer("assert 2 eq 1", 4,
"ASSERT",
"INT[2]",
"ASSERT_EQ",
"INT[1]"
);
}
void register_lexer()
{
CU_pSuite suite = CU_add_suite("Lexer", 0, 0);
CU_add_test(suite, "Integers", test_lexer_int);
CU_add_test(suite, "Assertions", test_lexer_assert);
}
#endif