forked from wmorgan/whistlepig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-tokenizer.c
55 lines (40 loc) · 1.15 KB
/
test-tokenizer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "test.h"
#include "tokenizer.lex.h"
#define ASSERT_NEXT_WORD(word) { \
int token_type = yylex(scanner); \
ASSERT_EQUALS_UINT(TOK_WORD, token_type); \
ASSERT(!strcmp(word, yyget_text(scanner))); \
}
#define ASSERT_DONE { \
int token_type = yylex(scanner); \
ASSERT_EQUALS_UINT(TOK_DONE, token_type); \
}
TEST(tokenizes_easy_words) {
yyscan_t scanner;
lexinfo charpos = {0, 0};
yylex_init_extra(&charpos, &scanner);
const char* string = "i love mice";
YY_BUFFER_STATE state = yy_scan_string(string, scanner);
ASSERT_NEXT_WORD("i");
ASSERT_NEXT_WORD("love");
ASSERT_NEXT_WORD("mice");
ASSERT_DONE;
yy_delete_buffer(state, scanner);
yylex_destroy(scanner);
return NO_ERROR;
}
TEST(strips_trailing_punctuation) {
yyscan_t scanner;
lexinfo charpos = {0, 0};
yylex_init_extra(&charpos, &scanner);
const char* string = "hey! this: you're <cool>";
YY_BUFFER_STATE state = yy_scan_string(string, scanner);
ASSERT_NEXT_WORD("hey");
ASSERT_NEXT_WORD("this");
ASSERT_NEXT_WORD("you're");
ASSERT_NEXT_WORD("cool");
ASSERT_DONE;
yy_delete_buffer(state, scanner);
yylex_destroy(scanner);
return NO_ERROR;
}