2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "ConditionsTest.h"
11 #include <driver_settings.h>
14 #include <cppunit/TestCaller.h>
15 #include <cppunit/TestSuite.h>
17 #include "Conditions.h"
18 #include "SettingsParser.h"
21 class TestConditionContext
: public ConditionContext
{
23 bool IsSafeMode() const
25 bool BootVolumeIsReadOnly() const
30 static TestConditionContext sConditionContext
;
33 ConditionsTest::ConditionsTest()
38 ConditionsTest::~ConditionsTest()
44 ConditionsTest::TestEmpty()
46 Condition
* condition
= _Condition("");
47 CPPUNIT_ASSERT(condition
== NULL
);
52 ConditionsTest::TestSafemode()
54 Condition
* condition
= _Condition("safemode");
55 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
56 CPPUNIT_ASSERT(condition
->IsConstant(sConditionContext
));
58 class SafemodeConditionContext
: public TestConditionContext
{
60 bool IsSafeMode() const
65 CPPUNIT_ASSERT(condition
->Test(safemodeContext
));
66 CPPUNIT_ASSERT(condition
->IsConstant(safemodeContext
));
71 ConditionsTest::TestFileExists()
73 Condition
* condition
= _Condition("file_exists /boot");
74 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
75 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
77 condition
= _Condition("file_exists /boot/don't fool me!");
78 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
83 ConditionsTest::TestOr()
85 Condition
* condition
= _Condition("or {\n"
88 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
89 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
91 condition
= _Condition("or {\n"
92 "file_exists /nowhere\n"
94 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
95 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
97 condition
= _Condition("or {\n"
99 "file_exists /nowhere\n"
101 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
102 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
104 condition
= _Condition("or {\n"
106 "file_exists /boot\n"
108 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
109 CPPUNIT_ASSERT(condition
->IsConstant(sConditionContext
));
111 condition
= _Condition("or {\n"
113 "file_exists /boot\n"
115 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
116 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
121 ConditionsTest::TestAnd()
123 Condition
* condition
= _Condition("and {\n"
124 "file_exists /boot\n"
126 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
127 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
129 condition
= _Condition("and {\n"
130 "file_exists /nowhere\n"
132 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
133 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
135 condition
= _Condition("and {\n"
136 "file_exists /boot\n"
137 "file_exists /nowhere\n"
139 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
140 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
142 condition
= _Condition("and {\n"
144 "file_exists /nowhere\n"
146 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
147 CPPUNIT_ASSERT(condition
->IsConstant(sConditionContext
));
149 condition
= _Condition("and {\n"
151 "file_exists /nowhere\n"
153 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
154 CPPUNIT_ASSERT(!condition
->IsConstant(sConditionContext
));
156 condition
= _Condition("and {\n"
159 CPPUNIT_ASSERT(!condition
->Test(sConditionContext
));
160 CPPUNIT_ASSERT(condition
->IsConstant(sConditionContext
));
165 ConditionsTest::TestNot()
167 Condition
* condition
= _Condition("not safemode");
168 CPPUNIT_ASSERT(condition
->Test(sConditionContext
));
170 class SafemodeConditionContext
: public TestConditionContext
{
172 bool IsSafeMode() const
177 CPPUNIT_ASSERT(!condition
->Test(safemodeContext
));
183 ConditionsTest::AddTests(BTestSuite
& parent
)
185 CppUnit::TestSuite
& suite
= *new CppUnit::TestSuite("ConditionsTest");
187 suite
.addTest(new CppUnit::TestCaller
<ConditionsTest
>(
188 "ConditionsTest::TestEmpty", &ConditionsTest::TestEmpty
));
189 suite
.addTest(new CppUnit::TestCaller
<ConditionsTest
>(
190 "ConditionsTest::TestSafemode", &ConditionsTest::TestSafemode
));
191 suite
.addTest(new CppUnit::TestCaller
<ConditionsTest
>(
192 "ConditionsTest::TestFileExists", &ConditionsTest::TestFileExists
));
193 suite
.addTest(new CppUnit::TestCaller
<ConditionsTest
>(
194 "ConditionsTest::TestOr", &ConditionsTest::TestOr
));
195 suite
.addTest(new CppUnit::TestCaller
<ConditionsTest
>(
196 "ConditionsTest::TestAnd", &ConditionsTest::TestAnd
));
197 suite
.addTest(new CppUnit::TestCaller
<ConditionsTest
>(
198 "ConditionsTest::TestNot", &ConditionsTest::TestNot
));
200 parent
.addTest("ConditionsTest", &suite
);
205 ConditionsTest::_Condition(const char* string
)
207 SettingsParser parser
;
208 BString
input("job A {\nif {");
209 input
<< string
<< "\n}\n}\n";
212 CPPUNIT_ASSERT_EQUAL(B_OK
, parser
.Parse(input
, jobs
));
215 CPPUNIT_ASSERT_EQUAL(B_OK
, jobs
.FindMessage("job", 0, &job
));
216 CPPUNIT_ASSERT_EQUAL(2, job
.CountNames(B_ANY_TYPE
));
217 CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job
.GetString("name")));
220 CPPUNIT_ASSERT_EQUAL(B_OK
, job
.FindMessage("if", &message
));
222 Condition
* condition
= Conditions::FromMessage(message
);
223 if (string
[0] != '\0')
224 CPPUNIT_ASSERT(condition
!= NULL
);