1 #include <msp/datafile/except.h>
2 #include <msp/datafile/parser.h>
3 #include <msp/datafile/statement.h>
4 #include <msp/io/memory.h>
5 #include <msp/test/test.h>
9 class ParserTests: public Test::RegisteredTest<ParserTests>
14 static const char *get_name() { return "Parser"; }
22 void unterminated_comment();
23 void missing_semicolon();
28 ParserTests::ParserTests()
30 add(&ParserTests::keyword_only, "Keyword only");
31 add(&ParserTests::arguments, "Arguments");
32 add(&ParserTests::substatements, "Substatements");
33 add(&ParserTests::empty, "Empty input");
34 add(&ParserTests::comments, "Comments");
35 add(&ParserTests::unterminated_comment, "Unterminated comment").expect_throw<DataFile::data_error>();
36 add(&ParserTests::missing_semicolon, "Missing semicolon").expect_throw<DataFile::data_error>();
37 add(&ParserTests::bad_string, "Bad string").expect_throw<DataFile::data_error>();
38 add(&ParserTests::bad_float, "Bad float").expect_throw<DataFile::data_error>();
41 void ParserTests::keyword_only()
43 static const char input[] =
47 IO::Memory mem(input, sizeof(input)-1);
48 DataFile::Parser parser(mem, "-");
49 DataFile::Statement st = parser.parse();
50 EXPECT_EQUAL(st.keyword, "foo");
52 EXPECT_EQUAL(st.keyword, "bar");
54 EXPECT_EQUAL(st.keyword, "quux");
57 void ParserTests::arguments()
59 static const char input[] =
64 IO::Memory mem(input, sizeof(input)-1);
65 DataFile::Parser parser(mem, "-");
66 DataFile::Statement st = parser.parse();
67 EXPECT_EQUAL(st.keyword, "foo");
68 EXPECT_EQUAL(st.args.size(), 2);
69 EXPECT_EQUAL(st.args[0].get_signature(), DataFile::IntType::signature);
70 EXPECT_EQUAL(st.args[1].get_signature(), DataFile::BoolType::signature);
72 EXPECT_EQUAL(st.keyword, "bar");
73 EXPECT_EQUAL(st.args.size(), 1);
74 EXPECT_EQUAL(st.args[0].get_signature(), DataFile::FloatType::signature);
76 EXPECT_EQUAL(st.keyword, "quux");
77 EXPECT_EQUAL(st.args.size(), 2);
78 EXPECT_EQUAL(st.args[0].get_signature(), DataFile::SymbolType::signature);
79 EXPECT_EQUAL(st.args[1].get_signature(), DataFile::StringType::signature);
82 void ParserTests::substatements()
84 static const char input[] =
87 IO::Memory mem(input, sizeof(input)-1);
88 DataFile::Parser parser(mem, "-");
89 DataFile::Statement st = parser.parse();
90 EXPECT_EQUAL(st.keyword, "top");
91 EXPECT_EQUAL(st.sub.size(), 1);
92 EXPECT_EQUAL(st.sub.front().keyword, "sub");
95 void ParserTests::empty()
97 static const char input[] = { 0 };
99 IO::Memory mem(input, 0U);
100 DataFile::Parser parser(mem, "-");
101 DataFile::Statement st = parser.parse();
105 void ParserTests::comments()
107 static const char input[] =
113 "/* block comment with embedded // double\n"
116 "// line comment with embedded /* slash star\n"
119 IO::Memory mem(input, sizeof(input)-1);
120 DataFile::Parser parser(mem, "-");
121 DataFile::Statement st = parser.parse();
122 EXPECT_EQUAL(st.keyword, "a");
124 EXPECT_EQUAL(st.keyword, "b");
126 EXPECT_EQUAL(st.keyword, "c");
128 EXPECT_EQUAL(st.keyword, "d");
131 void ParserTests::unterminated_comment()
133 static const char input[] =
136 IO::Memory mem(input, sizeof(input)-1);
137 DataFile::Parser parser(mem, "-");
141 void ParserTests::missing_semicolon()
143 static const char input[] =
146 IO::Memory mem(input, sizeof(input)-1);
147 DataFile::Parser parser(mem, "-");
151 void ParserTests::bad_string()
153 static const char input[] =
156 IO::Memory mem(input, sizeof(input)-1);
157 DataFile::Parser parser(mem, "-");
161 void ParserTests::bad_float()
163 static const char input[] =
166 IO::Memory mem(input, sizeof(input)-1);
167 DataFile::Parser parser(mem, "-");