forked from oils-for-unix/oils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrontend_match_test.cc
87 lines (63 loc) · 2.16 KB
/
frontend_match_test.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "cpp/frontend_match.h"
#include "_gen/frontend/id_kind.asdl_c.h"
#include "vendor/greatest.h"
using id_kind_asdl::Id;
TEST lexer_test() {
// Need lex_mode_e
// auto tup = match::OneToken(lex_mode__ShCommand, StrFromC("cd /tmp"), 0);
match::SimpleLexer* lex = match::BraceRangeLexer(StrFromC("{-1..22}"));
List<Tuple2<Id_t, Str*>*>* toks = lex->Tokens();
for (int i = 0; i < len(toks); i++) {
auto* t = toks->index_(i);
int id = t->at0();
if (id == id__Eol_Tok) {
break;
}
log("id = %d", id);
log("val = %s", t->at1()->data_);
}
match::SimpleLexer* lex2 = match::BraceRangeLexer(kEmptyString);
auto t = lex2->Next();
int id = t.at0();
ASSERT_EQ(Id::Eol_Tok, id);
PASS();
}
TEST func_test() {
ASSERT_EQ(Id::BoolUnary_G, match::BracketUnary(StrFromC("-G")));
ASSERT_EQ(Id::Undefined_Tok, match::BracketUnary(StrFromC("-Gz")));
ASSERT_EQ(Id::Undefined_Tok, match::BracketUnary(StrFromC("")));
ASSERT_EQ(Id::BoolBinary_NEqual, match::BracketBinary(StrFromC("!=")));
ASSERT_EQ(Id::Undefined_Tok, match::BracketBinary(StrFromC("")));
ASSERT_EQ(Id::Op_LParen, match::BracketOther(StrFromC("(")));
// This still works, but can't it overflow a buffer?
Str* s = StrFromC("!= ");
Str* stripped = s->strip();
ASSERT_EQ(3, len(s));
ASSERT_EQ(2, len(stripped));
ASSERT_EQ(Id::BoolBinary_NEqual, match::BracketBinary(stripped));
ASSERT(match::IsValidVarName(StrFromC("a")));
ASSERT(!match::IsValidVarName(StrFromC("01")));
ASSERT(!match::IsValidVarName(StrFromC("!!")));
ASSERT(!match::IsValidVarName(kEmptyString));
ASSERT(match::ShouldHijack(StrFromC("#!/bin/bash\n")));
ASSERT(!match::ShouldHijack(StrFromC("/bin/bash\n")));
PASS();
}
TEST for_test_coverage() {
(void)match::GlobLexer(kEmptyString);
(void)match::EchoLexer(kEmptyString);
(void)match::HistoryTokens(kEmptyString);
(void)match::Ps1Tokens(kEmptyString);
PASS();
}
GREATEST_MAIN_DEFS();
int main(int argc, char** argv) {
gHeap.Init();
GREATEST_MAIN_BEGIN();
RUN_TEST(lexer_test);
RUN_TEST(func_test);
RUN_TEST(for_test_coverage);
gHeap.CleanProcessExit();
GREATEST_MAIN_END(); /* display results */
return 0;
}