2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "SettingsParserTest.h"
11 #include <driver_settings.h>
14 #include <cppunit/TestCaller.h>
15 #include <cppunit/TestSuite.h>
17 #include "SettingsParser.h"
20 SettingsParserTest::SettingsParserTest()
25 SettingsParserTest::~SettingsParserTest()
30 // #pragma mark - conditions
34 SettingsParserTest::TestConditionsEmpty()
37 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if {\n"
39 CPPUNIT_ASSERT(message
.IsEmpty());
44 SettingsParserTest::TestConditionsMultiLine()
47 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if {\n"
51 CPPUNIT_ASSERT_EQUAL(2, message
.CountNames(B_ANY_TYPE
));
54 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("safemode", &subMessage
));
55 CPPUNIT_ASSERT(subMessage
.IsEmpty());
57 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("file_exists",
59 CPPUNIT_ASSERT_EQUAL(BString("one"),
60 BString(subMessage
.GetString("args", 0, "-")));
61 CPPUNIT_ASSERT_EQUAL(1, subMessage
.CountNames(B_ANY_TYPE
));
66 SettingsParserTest::TestConditionsFlat()
69 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if safemode\n", message
));
70 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
73 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("safemode", &args
));
74 CPPUNIT_ASSERT(args
.IsEmpty());
79 SettingsParserTest::TestConditionsFlatWithNot()
82 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if not safemode\n", message
));
83 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
86 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("not",
90 CPPUNIT_ASSERT_EQUAL(B_OK
, subMessage
.FindMessage("safemode", &args
));
91 CPPUNIT_ASSERT(args
.IsEmpty());
96 SettingsParserTest::TestConditionsFlatWithArgs()
99 CPPUNIT_ASSERT_EQUAL(B_OK
,
100 _ParseCondition("if file_exists one\n", message
));
101 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
104 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("file_exists", &args
));
105 CPPUNIT_ASSERT_EQUAL(BString("one"),
106 BString(args
.GetString("args", 0, "-")));
107 CPPUNIT_ASSERT_EQUAL(1, args
.CountNames(B_ANY_TYPE
));
112 SettingsParserTest::TestConditionsFlatWithNotAndArgs()
115 CPPUNIT_ASSERT_EQUAL(B_OK
,
116 _ParseCondition("if not file_exists one\n", message
));
117 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
120 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("not",
124 CPPUNIT_ASSERT_EQUAL(B_OK
, subMessage
.FindMessage("file_exists", &args
));
125 CPPUNIT_ASSERT_EQUAL(BString("one"),
126 BString(args
.GetString("args", 0, "-")));
127 CPPUNIT_ASSERT_EQUAL(1, args
.CountNames(B_ANY_TYPE
));
132 SettingsParserTest::TestConditionsMultiLineFlatNot()
135 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if {\n"
140 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("not",
142 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
145 CPPUNIT_ASSERT_EQUAL(B_OK
, subMessage
.FindMessage("safemode", &args
));
146 CPPUNIT_ASSERT_EQUAL(1, subMessage
.CountNames(B_ANY_TYPE
));
147 CPPUNIT_ASSERT(args
.IsEmpty());
152 SettingsParserTest::TestConditionsMultiLineFlatNotWithArgs()
155 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if {\n"
156 "\tnot file_exists one two\n"
160 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("not",
164 CPPUNIT_ASSERT_EQUAL(B_OK
, subMessage
.FindMessage("file_exists", &args
));
165 CPPUNIT_ASSERT_EQUAL(BString("one"),
166 BString(args
.GetString("args", 0, "-")));
167 CPPUNIT_ASSERT_EQUAL(BString("two"),
168 BString(args
.GetString("args", 1, "-")));
169 CPPUNIT_ASSERT_EQUAL(1, args
.CountNames(B_ANY_TYPE
));
170 CPPUNIT_ASSERT_EQUAL(2, _ArrayCount(args
, "args"));
175 SettingsParserTest::TestConditionsMultiLineNot()
178 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseCondition("if {\n"
185 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("not", &subMessage
));
188 CPPUNIT_ASSERT_EQUAL(B_OK
, subMessage
.FindMessage("safemode", &args
));
189 CPPUNIT_ASSERT(args
.IsEmpty());
193 // #pragma mark - events
197 SettingsParserTest::TestEventsEmpty()
200 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseEvent("on {\n"
202 CPPUNIT_ASSERT(message
.IsEmpty());
207 SettingsParserTest::TestEventsMultiLine()
210 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseEvent("on {\n"
211 "\tfile_created one\n"
214 CPPUNIT_ASSERT_EQUAL(2, message
.CountNames(B_ANY_TYPE
));
217 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("demand", &subMessage
));
218 CPPUNIT_ASSERT(subMessage
.IsEmpty());
220 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("file_created",
222 CPPUNIT_ASSERT_EQUAL(BString("one"),
223 BString(subMessage
.GetString("args", 0, "-")));
224 CPPUNIT_ASSERT_EQUAL(1, subMessage
.CountNames(B_ANY_TYPE
));
229 SettingsParserTest::TestEventsFlat()
232 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseEvent("on demand\n", message
));
233 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
236 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("demand", &args
));
237 CPPUNIT_ASSERT(args
.IsEmpty());
242 SettingsParserTest::TestEventsFlatWithArgs()
245 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseEvent("on file_created one\n", message
));
246 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
249 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("file_created", &args
));
250 CPPUNIT_ASSERT_EQUAL(BString("one"),
251 BString(args
.GetString("args", 0, "-")));
252 CPPUNIT_ASSERT_EQUAL(1, args
.CountNames(B_ANY_TYPE
));
256 // #pragma mark - environment
260 SettingsParserTest::TestEnvironmentMultiLine()
263 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseName("env", "env {\n"
264 "from_script SetupEnvironment\n"
267 CPPUNIT_ASSERT_EQUAL(2, message
.CountNames(B_ANY_TYPE
));
269 CPPUNIT_ASSERT_EQUAL(BString("SetupEnvironment"),
270 BString(message
.GetString("from_script", "-")));
271 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(message
, "from_script"));
273 CPPUNIT_ASSERT_EQUAL(BString("well,"),
274 BString(message
.GetString("TEST", 0, "-")));
275 CPPUNIT_ASSERT_EQUAL(BString("yes"),
276 BString(message
.GetString("TEST", 1, "-")));
277 CPPUNIT_ASSERT_EQUAL(2, _ArrayCount(message
, "TEST"));
282 SettingsParserTest::TestEnvironmentFlat()
285 CPPUNIT_ASSERT_EQUAL(B_OK
, _ParseName("env", "env SetupEnvironment\n",
287 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
289 CPPUNIT_ASSERT_EQUAL(BString("SetupEnvironment"),
290 BString(message
.GetString("from_script", "-")));
291 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(message
, "from_script"));
295 // #pragma mark - run
299 SettingsParserTest::TestRunFlat()
301 SettingsParser parser
;
303 CPPUNIT_ASSERT_EQUAL(B_OK
, parser
.Parse("run me", jobs
));
304 CPPUNIT_ASSERT_EQUAL(1, jobs
.CountNames(B_ANY_TYPE
));
307 CPPUNIT_ASSERT_EQUAL(B_OK
, jobs
.FindMessage("run", &message
));
308 CPPUNIT_ASSERT_EQUAL(BString("me"),
309 BString(message
.GetString("target", "-")));
310 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(message
, "target"));
311 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
316 SettingsParserTest::TestRunMultiLine()
318 SettingsParser parser
;
320 status_t status
= parser
.Parse("run {\n"
324 CPPUNIT_ASSERT_EQUAL(B_OK
, status
);
325 CPPUNIT_ASSERT_EQUAL(1, jobs
.CountNames(B_ANY_TYPE
));
328 CPPUNIT_ASSERT_EQUAL(B_OK
, jobs
.FindMessage("run", &message
));
329 CPPUNIT_ASSERT_EQUAL(BString("me"),
330 BString(message
.GetString("target", 0, "-")));
331 CPPUNIT_ASSERT_EQUAL(BString("you"),
332 BString(message
.GetString("target", 1, "-")));
333 CPPUNIT_ASSERT_EQUAL(2, _ArrayCount(message
, "target"));
334 CPPUNIT_ASSERT_EQUAL(1, message
.CountNames(B_ANY_TYPE
));
339 SettingsParserTest::TestRunIfThenElseFlat()
341 SettingsParser parser
;
343 status_t status
= parser
.Parse("run {\n"
348 CPPUNIT_ASSERT_EQUAL(B_OK
, status
);
351 CPPUNIT_ASSERT_EQUAL(B_OK
, jobs
.FindMessage("run", &message
));
352 CPPUNIT_ASSERT_EQUAL(3, message
.CountNames(B_ANY_TYPE
));
355 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("then", &then
));
356 CPPUNIT_ASSERT_EQUAL(BString("this"),
357 BString(then
.GetString("target", "-")));
358 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(then
, "target"));
359 CPPUNIT_ASSERT_EQUAL(1, then
.CountNames(B_ANY_TYPE
));
362 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("else", &otherwise
));
363 CPPUNIT_ASSERT_EQUAL(BString("that"),
364 BString(otherwise
.GetString("target", "-")));
365 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(otherwise
, "target"));
366 CPPUNIT_ASSERT_EQUAL(1, otherwise
.CountNames(B_ANY_TYPE
));
371 SettingsParserTest::TestRunIfThenElseMultiLine()
373 SettingsParser parser
;
375 status_t status
= parser
.Parse("run {\n"
386 CPPUNIT_ASSERT_EQUAL(B_OK
, status
);
389 CPPUNIT_ASSERT_EQUAL(B_OK
, jobs
.FindMessage("run", &message
));
390 CPPUNIT_ASSERT_EQUAL(3, message
.CountNames(B_ANY_TYPE
));
393 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("then", &then
));
394 CPPUNIT_ASSERT_EQUAL(BString("this"),
395 BString(then
.GetString("target", "-")));
396 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(then
, "target"));
397 CPPUNIT_ASSERT_EQUAL(1, then
.CountNames(B_ANY_TYPE
));
400 CPPUNIT_ASSERT_EQUAL(B_OK
, message
.FindMessage("else", &otherwise
));
401 CPPUNIT_ASSERT_EQUAL(BString("that"),
402 BString(otherwise
.GetString("target", "-")));
403 CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(otherwise
, "target"));
404 CPPUNIT_ASSERT_EQUAL(1, otherwise
.CountNames(B_ANY_TYPE
));
412 SettingsParserTest::AddTests(BTestSuite
& parent
)
414 CppUnit::TestSuite
& suite
= *new CppUnit::TestSuite("SettingsParserTest");
417 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
418 "SettingsParserTest::TestConditionsEmpty",
419 &SettingsParserTest::TestConditionsEmpty
));
420 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
421 "SettingsParserTest::TestConditionsMultiLine",
422 &SettingsParserTest::TestConditionsMultiLine
));
423 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
424 "SettingsParserTest::TestConditionsFlat",
425 &SettingsParserTest::TestConditionsFlat
));
426 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
427 "SettingsParserTest::TestConditionsFlatWithNot",
428 &SettingsParserTest::TestConditionsFlatWithNot
));
429 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
430 "SettingsParserTest::TestConditionsFlatWithArgs",
431 &SettingsParserTest::TestConditionsFlatWithArgs
));
432 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
433 "SettingsParserTest::TestConditionsFlatWithNotAndArgs",
434 &SettingsParserTest::TestConditionsFlatWithNotAndArgs
));
435 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
436 "SettingsParserTest::TestConditionsMultiLineFlatNot",
437 &SettingsParserTest::TestConditionsMultiLineFlatNot
));
438 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
439 "SettingsParserTest::TestConditionsMultiLineFlatNotWithArgs",
440 &SettingsParserTest::TestConditionsMultiLineFlatNotWithArgs
));
441 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
442 "SettingsParserTest::TestConditionsMultiLineNot",
443 &SettingsParserTest::TestConditionsMultiLineNot
));
446 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
447 "SettingsParserTest::TestEventsEmpty",
448 &SettingsParserTest::TestEventsEmpty
));
449 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
450 "SettingsParserTest::TestEventsMultiLine",
451 &SettingsParserTest::TestEventsMultiLine
));
452 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
453 "SettingsParserTest::TestEventsFlat",
454 &SettingsParserTest::TestEventsFlat
));
455 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
456 "SettingsParserTest::TestEventsFlatWithArgs",
457 &SettingsParserTest::TestEventsFlatWithArgs
));
460 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
461 "SettingsParserTest::TestEnvironmentMultiLine",
462 &SettingsParserTest::TestEnvironmentMultiLine
));
463 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
464 "SettingsParserTest::TestEnvironmentFlat",
465 &SettingsParserTest::TestEnvironmentFlat
));
468 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
469 "SettingsParserTest::TestRunFlat",
470 &SettingsParserTest::TestRunFlat
));
471 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
472 "SettingsParserTest::TestRunMultiLine",
473 &SettingsParserTest::TestRunMultiLine
));
474 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
475 "SettingsParserTest::TestRunIfThenElseFlat",
476 &SettingsParserTest::TestRunIfThenElseFlat
));
477 suite
.addTest(new CppUnit::TestCaller
<SettingsParserTest
>(
478 "SettingsParserTest::TestRunIfThenElseMultiLine",
479 &SettingsParserTest::TestRunIfThenElseMultiLine
));
481 parent
.addTest("SettingsParserTest", &suite
);
486 SettingsParserTest::_ParseCondition(const char* text
, BMessage
& message
)
488 return _ParseName("if", text
, message
);
493 SettingsParserTest::_ParseEvent(const char* text
, BMessage
& message
)
495 return _ParseName("on", text
, message
);
500 SettingsParserTest::_ParseName(const char* name
, const char* text
,
503 SettingsParser parser
;
504 BString
input("job A {\n");
505 input
<< text
<< "\n}\n";
508 status_t status
= parser
.Parse(input
, jobs
);
513 status
= jobs
.FindMessage("job", 0, &job
);
517 CPPUNIT_ASSERT_EQUAL(2, job
.CountNames(B_ANY_TYPE
));
518 CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job
.GetString("name")));
520 return job
.FindMessage(name
, &message
);
525 SettingsParserTest::_ArrayCount(BMessage
& message
, const char* name
)
528 if (message
.GetInfo(name
, NULL
, &found
, NULL
) != B_OK
)