1 #include <msp/datafile/dataerror.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"; }
21 void unterminated_comment();
22 void missing_semicolon();
27 ParserTests::ParserTests()
29 add(&ParserTests::keyword_only, "Keyword only");
30 add(&ParserTests::arguments, "Arguments");
31 add(&ParserTests::substatements, "Substatements");
32 add(&ParserTests::empty, "Empty input");
33 add(&ParserTests::unterminated_comment, "Unterminated comment").expect_throw<DataFile::data_error>();
34 add(&ParserTests::missing_semicolon, "Missing semicolon").expect_throw<DataFile::data_error>();
35 add(&ParserTests::bad_string, "Bad string").expect_throw<DataFile::data_error>();
36 add(&ParserTests::bad_float, "Bad float").expect_throw<DataFile::data_error>();
39 void ParserTests::keyword_only()
41 static const char input[] =
45 IO::Memory mem(input, sizeof(input)-1);
46 DataFile::Parser parser(mem, "-");
47 DataFile::Statement st = parser.parse();
48 EXPECT_EQUAL(st.keyword, "foo");
50 EXPECT_EQUAL(st.keyword, "bar");
52 EXPECT_EQUAL(st.keyword, "quux");
55 void ParserTests::arguments()
57 static const char input[] =
62 IO::Memory mem(input, sizeof(input)-1);
63 DataFile::Parser parser(mem, "-");
64 DataFile::Statement st = parser.parse();
65 EXPECT_EQUAL(st.keyword, "foo");
66 EXPECT_EQUAL(st.args.size(), 2);
67 EXPECT_EQUAL(st.args[0].get_signature(), DataFile::IntType::signature);
68 EXPECT_EQUAL(st.args[1].get_signature(), DataFile::BoolType::signature);
70 EXPECT_EQUAL(st.keyword, "bar");
71 EXPECT_EQUAL(st.args.size(), 1);
72 EXPECT_EQUAL(st.args[0].get_signature(), DataFile::FloatType::signature);
74 EXPECT_EQUAL(st.keyword, "quux");
75 EXPECT_EQUAL(st.args.size(), 2);
76 EXPECT_EQUAL(st.args[0].get_signature(), DataFile::SymbolType::signature);
77 EXPECT_EQUAL(st.args[1].get_signature(), DataFile::StringType::signature);
80 void ParserTests::substatements()
82 static const char input[] =
85 IO::Memory mem(input, sizeof(input)-1);
86 DataFile::Parser parser(mem, "-");
87 DataFile::Statement st = parser.parse();
88 EXPECT_EQUAL(st.keyword, "top");
89 EXPECT_EQUAL(st.sub.size(), 1);
90 EXPECT_EQUAL(st.sub.front().keyword, "sub");
93 void ParserTests::empty()
95 static const char input[] = { 0 };
97 IO::Memory mem(input, 0U);
98 DataFile::Parser parser(mem, "-");
99 DataFile::Statement st = parser.parse();
103 void ParserTests::unterminated_comment()
105 static const char input[] =
108 IO::Memory mem(input, sizeof(input)-1);
109 DataFile::Parser parser(mem, "-");
113 void ParserTests::missing_semicolon()
115 static const char input[] =
118 IO::Memory mem(input, sizeof(input)-1);
119 DataFile::Parser parser(mem, "-");
123 void ParserTests::bad_string()
125 static const char input[] =
128 IO::Memory mem(input, sizeof(input)-1);
129 DataFile::Parser parser(mem, "-");
133 void ParserTests::bad_float()
135 static const char input[] =
138 IO::Memory mem(input, sizeof(input)-1);
139 DataFile::Parser parser(mem, "-");