10e347cb2SAxel Dörfler /*
20e347cb2SAxel Dörfler * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
30e347cb2SAxel Dörfler * Distributed under the terms of the MIT License.
40e347cb2SAxel Dörfler */
50e347cb2SAxel Dörfler
60e347cb2SAxel Dörfler
70e347cb2SAxel Dörfler #include "ConditionsTest.h"
80e347cb2SAxel Dörfler
90e347cb2SAxel Dörfler #include <stdlib.h>
100e347cb2SAxel Dörfler
110e347cb2SAxel Dörfler #include <driver_settings.h>
120e347cb2SAxel Dörfler #include <String.h>
130e347cb2SAxel Dörfler
140e347cb2SAxel Dörfler #include <cppunit/TestCaller.h>
150e347cb2SAxel Dörfler #include <cppunit/TestSuite.h>
160e347cb2SAxel Dörfler
170e347cb2SAxel Dörfler #include "Conditions.h"
180e347cb2SAxel Dörfler #include "SettingsParser.h"
190e347cb2SAxel Dörfler
200e347cb2SAxel Dörfler
210e347cb2SAxel Dörfler class TestConditionContext : public ConditionContext {
220e347cb2SAxel Dörfler public:
IsSafeMode() const230e347cb2SAxel Dörfler bool IsSafeMode() const
240e347cb2SAxel Dörfler { return false; }
BootVolumeIsReadOnly() const253888dd6eSAxel Dörfler bool BootVolumeIsReadOnly() const
263888dd6eSAxel Dörfler { return false; }
270e347cb2SAxel Dörfler };
280e347cb2SAxel Dörfler
290e347cb2SAxel Dörfler
300e347cb2SAxel Dörfler static TestConditionContext sConditionContext;
310e347cb2SAxel Dörfler
320e347cb2SAxel Dörfler
ConditionsTest()330e347cb2SAxel Dörfler ConditionsTest::ConditionsTest()
340e347cb2SAxel Dörfler {
350e347cb2SAxel Dörfler }
360e347cb2SAxel Dörfler
370e347cb2SAxel Dörfler
~ConditionsTest()380e347cb2SAxel Dörfler ConditionsTest::~ConditionsTest()
390e347cb2SAxel Dörfler {
400e347cb2SAxel Dörfler }
410e347cb2SAxel Dörfler
420e347cb2SAxel Dörfler
430e347cb2SAxel Dörfler void
TestEmpty()44*d54bb194SAxel Dörfler ConditionsTest::TestEmpty()
45*d54bb194SAxel Dörfler {
46*d54bb194SAxel Dörfler Condition* condition = _Condition("");
47*d54bb194SAxel Dörfler CPPUNIT_ASSERT(condition == NULL);
48*d54bb194SAxel Dörfler }
49*d54bb194SAxel Dörfler
50*d54bb194SAxel Dörfler
51*d54bb194SAxel Dörfler void
TestSafemode()520e347cb2SAxel Dörfler ConditionsTest::TestSafemode()
530e347cb2SAxel Dörfler {
540e347cb2SAxel Dörfler Condition* condition = _Condition("safemode");
550e347cb2SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
56e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
570e347cb2SAxel Dörfler
583888dd6eSAxel Dörfler class SafemodeConditionContext : public TestConditionContext {
590e347cb2SAxel Dörfler public:
600e347cb2SAxel Dörfler bool IsSafeMode() const
610e347cb2SAxel Dörfler {
620e347cb2SAxel Dörfler return true;
630e347cb2SAxel Dörfler }
640e347cb2SAxel Dörfler } safemodeContext;
650e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition->Test(safemodeContext));
66e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->IsConstant(safemodeContext));
670e347cb2SAxel Dörfler }
680e347cb2SAxel Dörfler
690e347cb2SAxel Dörfler
700e347cb2SAxel Dörfler void
TestFileExists()710e347cb2SAxel Dörfler ConditionsTest::TestFileExists()
720e347cb2SAxel Dörfler {
730e347cb2SAxel Dörfler Condition* condition = _Condition("file_exists /boot");
740e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
75e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
760e347cb2SAxel Dörfler
770e347cb2SAxel Dörfler condition = _Condition("file_exists /boot/don't fool me!");
780e347cb2SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
790e347cb2SAxel Dörfler }
800e347cb2SAxel Dörfler
810e347cb2SAxel Dörfler
820e347cb2SAxel Dörfler void
TestOr()830e347cb2SAxel Dörfler ConditionsTest::TestOr()
840e347cb2SAxel Dörfler {
850e347cb2SAxel Dörfler Condition* condition = _Condition("or {\n"
860e347cb2SAxel Dörfler "file_exists /boot\n"
870e347cb2SAxel Dörfler "}\n");
880e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
89e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
900e347cb2SAxel Dörfler
910e347cb2SAxel Dörfler condition = _Condition("or {\n"
920e347cb2SAxel Dörfler "file_exists /nowhere\n"
930e347cb2SAxel Dörfler "}\n");
940e347cb2SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
95e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
960e347cb2SAxel Dörfler
970e347cb2SAxel Dörfler condition = _Condition("or {\n"
980e347cb2SAxel Dörfler "file_exists /boot\n"
990e347cb2SAxel Dörfler "file_exists /nowhere\n"
1000e347cb2SAxel Dörfler "}\n");
1010e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
102e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
103e0fc09b4SAxel Dörfler
104e0fc09b4SAxel Dörfler condition = _Condition("or {\n"
105e0fc09b4SAxel Dörfler "not safemode\n"
106e0fc09b4SAxel Dörfler "file_exists /boot\n"
107e0fc09b4SAxel Dörfler "}\n");
108e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
109e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
110e0fc09b4SAxel Dörfler
111e0fc09b4SAxel Dörfler condition = _Condition("or {\n"
112e0fc09b4SAxel Dörfler "safemode\n"
113e0fc09b4SAxel Dörfler "file_exists /boot\n"
114e0fc09b4SAxel Dörfler "}\n");
115e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
116e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
1170e347cb2SAxel Dörfler }
1180e347cb2SAxel Dörfler
1190e347cb2SAxel Dörfler
1200e347cb2SAxel Dörfler void
TestAnd()1210e347cb2SAxel Dörfler ConditionsTest::TestAnd()
1220e347cb2SAxel Dörfler {
1230e347cb2SAxel Dörfler Condition* condition = _Condition("and {\n"
1240e347cb2SAxel Dörfler "file_exists /boot\n"
1250e347cb2SAxel Dörfler "}\n");
1260e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
127e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
1280e347cb2SAxel Dörfler
1290e347cb2SAxel Dörfler condition = _Condition("and {\n"
1300e347cb2SAxel Dörfler "file_exists /nowhere\n"
1310e347cb2SAxel Dörfler "}\n");
1320e347cb2SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
133e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
1340e347cb2SAxel Dörfler
1350e347cb2SAxel Dörfler condition = _Condition("and {\n"
1360e347cb2SAxel Dörfler "file_exists /boot\n"
1370e347cb2SAxel Dörfler "file_exists /nowhere\n"
1380e347cb2SAxel Dörfler "}\n");
1390e347cb2SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
140e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
141e0fc09b4SAxel Dörfler
142e0fc09b4SAxel Dörfler condition = _Condition("and {\n"
143e0fc09b4SAxel Dörfler "safemode\n"
144e0fc09b4SAxel Dörfler "file_exists /nowhere\n"
145e0fc09b4SAxel Dörfler "}\n");
146e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
147e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
148e0fc09b4SAxel Dörfler
149e0fc09b4SAxel Dörfler condition = _Condition("and {\n"
150e0fc09b4SAxel Dörfler "not safemode\n"
151e0fc09b4SAxel Dörfler "file_exists /nowhere\n"
152e0fc09b4SAxel Dörfler "}\n");
153e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
154e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
155e0fc09b4SAxel Dörfler
156e0fc09b4SAxel Dörfler condition = _Condition("and {\n"
157e0fc09b4SAxel Dörfler "safemode\n"
158e0fc09b4SAxel Dörfler "}\n");
159e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(sConditionContext));
160e0fc09b4SAxel Dörfler CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
1610e347cb2SAxel Dörfler }
1620e347cb2SAxel Dörfler
1630e347cb2SAxel Dörfler
1640e347cb2SAxel Dörfler void
TestNot()1650e347cb2SAxel Dörfler ConditionsTest::TestNot()
1660e347cb2SAxel Dörfler {
1670e347cb2SAxel Dörfler Condition* condition = _Condition("not safemode");
1680e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition->Test(sConditionContext));
1690e347cb2SAxel Dörfler
1703888dd6eSAxel Dörfler class SafemodeConditionContext : public TestConditionContext {
1710e347cb2SAxel Dörfler public:
1720e347cb2SAxel Dörfler bool IsSafeMode() const
1730e347cb2SAxel Dörfler {
1740e347cb2SAxel Dörfler return true;
1750e347cb2SAxel Dörfler }
1760e347cb2SAxel Dörfler } safemodeContext;
1770e347cb2SAxel Dörfler CPPUNIT_ASSERT(!condition->Test(safemodeContext));
1780e347cb2SAxel Dörfler
1790e347cb2SAxel Dörfler }
1800e347cb2SAxel Dörfler
1810e347cb2SAxel Dörfler
1820e347cb2SAxel Dörfler /*static*/ void
AddTests(BTestSuite & parent)1830e347cb2SAxel Dörfler ConditionsTest::AddTests(BTestSuite& parent)
1840e347cb2SAxel Dörfler {
1850e347cb2SAxel Dörfler CppUnit::TestSuite& suite = *new CppUnit::TestSuite("ConditionsTest");
1860e347cb2SAxel Dörfler
1870e347cb2SAxel Dörfler suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
188*d54bb194SAxel Dörfler "ConditionsTest::TestEmpty", &ConditionsTest::TestEmpty));
189*d54bb194SAxel Dörfler suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
1900e347cb2SAxel Dörfler "ConditionsTest::TestSafemode", &ConditionsTest::TestSafemode));
1910e347cb2SAxel Dörfler suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
1920e347cb2SAxel Dörfler "ConditionsTest::TestFileExists", &ConditionsTest::TestFileExists));
1930e347cb2SAxel Dörfler suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
1940e347cb2SAxel Dörfler "ConditionsTest::TestOr", &ConditionsTest::TestOr));
1950e347cb2SAxel Dörfler suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
1960e347cb2SAxel Dörfler "ConditionsTest::TestAnd", &ConditionsTest::TestAnd));
1970e347cb2SAxel Dörfler suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
1980e347cb2SAxel Dörfler "ConditionsTest::TestNot", &ConditionsTest::TestNot));
1990e347cb2SAxel Dörfler
2000e347cb2SAxel Dörfler parent.addTest("ConditionsTest", &suite);
2010e347cb2SAxel Dörfler }
2020e347cb2SAxel Dörfler
2030e347cb2SAxel Dörfler
2040e347cb2SAxel Dörfler Condition*
_Condition(const char * string)2050e347cb2SAxel Dörfler ConditionsTest::_Condition(const char* string)
2060e347cb2SAxel Dörfler {
2070e347cb2SAxel Dörfler SettingsParser parser;
2080e347cb2SAxel Dörfler BString input("job A {\nif {");
2090e347cb2SAxel Dörfler input << string << "\n}\n}\n";
2100e347cb2SAxel Dörfler
2110e347cb2SAxel Dörfler BMessage jobs;
2120e347cb2SAxel Dörfler CPPUNIT_ASSERT_EQUAL(B_OK, parser.Parse(input, jobs));
2130e347cb2SAxel Dörfler
2140e347cb2SAxel Dörfler BMessage job;
2150e347cb2SAxel Dörfler CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("job", 0, &job));
2160e347cb2SAxel Dörfler CPPUNIT_ASSERT_EQUAL(2, job.CountNames(B_ANY_TYPE));
2170e347cb2SAxel Dörfler CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job.GetString("name")));
2180e347cb2SAxel Dörfler
2190e347cb2SAxel Dörfler BMessage message;
2200e347cb2SAxel Dörfler CPPUNIT_ASSERT_EQUAL(B_OK, job.FindMessage("if", &message));
2210e347cb2SAxel Dörfler
2220e347cb2SAxel Dörfler Condition* condition = Conditions::FromMessage(message);
223*d54bb194SAxel Dörfler if (string[0] != '\0')
2240e347cb2SAxel Dörfler CPPUNIT_ASSERT(condition != NULL);
2250e347cb2SAxel Dörfler return condition;
2260e347cb2SAxel Dörfler }
227