OILS / cpp / frontend_match_test.cc View on Github | oilshell.org

87 lines, 54 significant
1#include "cpp/frontend_match.h"
2
3#include "_gen/frontend/id_kind.asdl_c.h"
4#include "vendor/greatest.h"
5
6using id_kind_asdl::Id;
7
8TEST lexer_test() {
9 // Need lex_mode_e
10 // auto tup = match::OneToken(lex_mode__ShCommand, StrFromC("cd /tmp"), 0);
11
12 match::SimpleLexer* lex = match::BraceRangeLexer(StrFromC("{-1..22}"));
13
14 auto toks = lex->Tokens();
15 for (int i = 0; i < len(toks); i++) {
16 auto* t = toks->at(i);
17 int id = t->at0();
18 if (id == id__Eol_Tok) {
19 break;
20 }
21 log("id = %d", id);
22 log("val = %s", t->at1()->data_);
23 }
24
25 match::SimpleLexer* lex2 = match::BraceRangeLexer(kEmptyString);
26 auto t = lex2->Next();
27 int id = t.at0();
28 ASSERT_EQ(Id::Eol_Tok, id);
29
30 PASS();
31}
32
33TEST func_test() {
34 ASSERT_EQ(Id::BoolUnary_G, match::BracketUnary(StrFromC("-G")));
35 ASSERT_EQ(Id::Undefined_Tok, match::BracketUnary(StrFromC("-Gz")));
36 ASSERT_EQ(Id::Undefined_Tok, match::BracketUnary(StrFromC("")));
37
38 ASSERT_EQ(Id::BoolBinary_NEqual, match::BracketBinary(StrFromC("!=")));
39 ASSERT_EQ(Id::Undefined_Tok, match::BracketBinary(StrFromC("")));
40
41 ASSERT_EQ(Id::Op_LParen, match::BracketOther(StrFromC("(")));
42
43 // This still works, but can't it overflow a buffer?
44 BigStr* s = StrFromC("!= ");
45 BigStr* stripped = s->strip();
46
47 ASSERT_EQ(3, len(s));
48 ASSERT_EQ(2, len(stripped));
49
50 ASSERT_EQ(Id::BoolBinary_NEqual, match::BracketBinary(stripped));
51
52 ASSERT(match::IsValidVarName(StrFromC("a")));
53 ASSERT(!match::IsValidVarName(StrFromC("01")));
54 ASSERT(!match::IsValidVarName(StrFromC("!!")));
55 ASSERT(!match::IsValidVarName(kEmptyString));
56
57 ASSERT(match::ShouldHijack(StrFromC("#!/bin/bash\n")));
58 ASSERT(!match::ShouldHijack(StrFromC("/bin/bash\n")));
59
60 PASS();
61}
62
63TEST for_test_coverage() {
64 (void)match::GlobLexer(kEmptyString);
65 (void)match::EchoLexer(kEmptyString);
66 (void)match::HistoryTokens(kEmptyString);
67 (void)match::Ps1Tokens(kEmptyString);
68
69 PASS();
70}
71
72GREATEST_MAIN_DEFS();
73
74int main(int argc, char** argv) {
75 gHeap.Init();
76
77 GREATEST_MAIN_BEGIN();
78
79 RUN_TEST(lexer_test);
80 RUN_TEST(func_test);
81 RUN_TEST(for_test_coverage);
82
83 gHeap.CleanProcessExit();
84
85 GREATEST_MAIN_END(); /* display results */
86 return 0;
87}