-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_parser_test.cc
100 lines (73 loc) · 3.92 KB
/
config_parser_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
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "gtest/gtest.h"
#include "config_parser.h"
class NginxConfigParserStringTest : public ::testing::Test {
protected:
bool ParseString(const std::string config_string) {
std::stringstream config_stream(config_string);
return parser.Parse(&config_stream, &out_config);
}
NginxConfigParser parser;
NginxConfig out_config;
};
TEST(NginxConfigParserTest, SimpleConfigFile) {
NginxConfigParser parser;
NginxConfig out_config;
EXPECT_TRUE(parser.Parse("example_config", &out_config)) << "Example config file can be parsed";
EXPECT_FALSE(parser.Parse("non_existent_config", &out_config)) << "Nonexistent config file cannot be parsed";
}
TEST(NginxConfigStatementTest, ToString) {
NginxConfigStatement statement;
statement.tokens.push_back("foo");
statement.tokens.push_back("bar");
EXPECT_EQ("foo bar;\n", statement.ToString(0));
}
TEST_F(NginxConfigParserStringTest, SimpleStatementConfig) {
ASSERT_TRUE(ParseString("foo bar;"));
ASSERT_EQ(1, out_config.statements.size()) << "Config has only one statement.";
EXPECT_EQ("foo", out_config.statements[0]->tokens[0]) << "foo is the first token.";
}
TEST_F(NginxConfigParserStringTest, SimpleInvalidStatementsConfig) {
EXPECT_FALSE(ParseString("foo bar")) << "Config missing semicolon.";
EXPECT_FALSE(ParseString("foo bar;;")) << "Config with repeated semicolons.";
EXPECT_FALSE(ParseString("foo 'bar;")) << "Config missing closed single quote.";
EXPECT_FALSE(ParseString("foo \"bar;")) << "Config missing closed double quote.";
EXPECT_FALSE(ParseString("foo 'bar\";")) << "Config with improper matching quote.";
EXPECT_FALSE(ParseString("")) << "Empty string config.";
EXPECT_FALSE(ParseString(" ")) << "White space config.";
EXPECT_FALSE(ParseString("{")) << "Config with only opening curly brace.";
EXPECT_FALSE(ParseString("}")) << "Config with only ending curly brace.";
EXPECT_FALSE(ParseString("{}")) << "Config with matching curly braces but no child_block statements.";
}
TEST_F(NginxConfigParserStringTest, MultipleStatementsConfig) {
ASSERT_TRUE(ParseString("foo bar {foo bar; } foobar;"));
ASSERT_EQ(2, out_config.statements.size()) << "Config with two statements: one with child block and one normal.";
EXPECT_EQ("foo bar {\n foo bar;\n}\n", out_config.statements[0]->ToString(0));
}
TEST_F(NginxConfigParserStringTest, InnerStatementsConfig) {
ASSERT_TRUE(ParseString("foo bar {foo barr; bar foo;} foobar;"));
ASSERT_EQ(2, out_config.statements.size());
ASSERT_EQ(2, out_config.statements[0]->child_block->statements.size()) << "Child block has two statements.";
EXPECT_EQ("bar foo;\n", out_config.statements[0]->child_block->statements[1]->ToString(0));
EXPECT_EQ("foo barr;\n", out_config.statements[0]->child_block->statements[0]->ToString(0));
}
TEST_F(NginxConfigParserStringTest, CurlyConfigs) {
EXPECT_TRUE(ParseString("foo bar {foo bar; foo bar;}"));
EXPECT_EQ(1, out_config.statements.size());
EXPECT_EQ(2, out_config.statements[0]->child_block->statements.size());
}
TEST_F(NginxConfigParserStringTest, EmbedCurlyConfigs) {
ASSERT_TRUE(ParseString("foo bar { foo bar {fooo bar;} }"));
ASSERT_EQ(1, out_config.statements.size());
EXPECT_EQ(1, out_config.statements[0]->child_block->statements.size());
EXPECT_EQ(1, out_config.statements[0]->child_block->statements[0]->child_block->statements.size());
}
TEST_F(NginxConfigParserStringTest, UnbalancedCurlyConfigs) {
EXPECT_FALSE(ParseString("foo bar {foo bar; ")) << "Missing closing curly brace.";
EXPECT_FALSE(ParseString("foo bar {foo bar {foo bar; }"));
EXPECT_FALSE(ParseString("foo bar foo bar; } } }"));
}
TEST_F(NginxConfigParserStringTest, EmptyChildBlock) {
EXPECT_TRUE(ParseString("foo bar {} ")) << "Empty child block is valid.";
ASSERT_EQ(1, out_config.statements.size());
EXPECT_EQ(0, out_config.statements[0]->child_block->statements.size());
}