headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / servers / launch / ConditionsTest.cpp
blob48b3f56d9ff12511635f3ded2d8ea4ca9746b0fe
1 /*
2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "ConditionsTest.h"
9 #include <stdlib.h>
11 #include <driver_settings.h>
12 #include <String.h>
14 #include <cppunit/TestCaller.h>
15 #include <cppunit/TestSuite.h>
17 #include "Conditions.h"
18 #include "SettingsParser.h"
21 class TestConditionContext : public ConditionContext {
22 public:
23 bool IsSafeMode() const
24 { return false; }
25 bool BootVolumeIsReadOnly() const
26 { return false; }
30 static TestConditionContext sConditionContext;
33 ConditionsTest::ConditionsTest()
38 ConditionsTest::~ConditionsTest()
43 void
44 ConditionsTest::TestEmpty()
46 Condition* condition = _Condition("");
47 CPPUNIT_ASSERT(condition == NULL);
51 void
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 {
59 public:
60 bool IsSafeMode() const
62 return true;
64 } safemodeContext;
65 CPPUNIT_ASSERT(condition->Test(safemodeContext));
66 CPPUNIT_ASSERT(condition->IsConstant(safemodeContext));
70 void
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));
82 void
83 ConditionsTest::TestOr()
85 Condition* condition = _Condition("or {\n"
86 "file_exists /boot\n"
87 "}\n");
88 CPPUNIT_ASSERT(condition->Test(sConditionContext));
89 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
91 condition = _Condition("or {\n"
92 "file_exists /nowhere\n"
93 "}\n");
94 CPPUNIT_ASSERT(!condition->Test(sConditionContext));
95 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
97 condition = _Condition("or {\n"
98 "file_exists /boot\n"
99 "file_exists /nowhere\n"
100 "}\n");
101 CPPUNIT_ASSERT(condition->Test(sConditionContext));
102 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
104 condition = _Condition("or {\n"
105 "not safemode\n"
106 "file_exists /boot\n"
107 "}\n");
108 CPPUNIT_ASSERT(condition->Test(sConditionContext));
109 CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
111 condition = _Condition("or {\n"
112 "safemode\n"
113 "file_exists /boot\n"
114 "}\n");
115 CPPUNIT_ASSERT(condition->Test(sConditionContext));
116 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
120 void
121 ConditionsTest::TestAnd()
123 Condition* condition = _Condition("and {\n"
124 "file_exists /boot\n"
125 "}\n");
126 CPPUNIT_ASSERT(condition->Test(sConditionContext));
127 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
129 condition = _Condition("and {\n"
130 "file_exists /nowhere\n"
131 "}\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"
138 "}\n");
139 CPPUNIT_ASSERT(!condition->Test(sConditionContext));
140 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
142 condition = _Condition("and {\n"
143 "safemode\n"
144 "file_exists /nowhere\n"
145 "}\n");
146 CPPUNIT_ASSERT(!condition->Test(sConditionContext));
147 CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
149 condition = _Condition("and {\n"
150 "not safemode\n"
151 "file_exists /nowhere\n"
152 "}\n");
153 CPPUNIT_ASSERT(!condition->Test(sConditionContext));
154 CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
156 condition = _Condition("and {\n"
157 "safemode\n"
158 "}\n");
159 CPPUNIT_ASSERT(!condition->Test(sConditionContext));
160 CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
164 void
165 ConditionsTest::TestNot()
167 Condition* condition = _Condition("not safemode");
168 CPPUNIT_ASSERT(condition->Test(sConditionContext));
170 class SafemodeConditionContext : public TestConditionContext {
171 public:
172 bool IsSafeMode() const
174 return true;
176 } safemodeContext;
177 CPPUNIT_ASSERT(!condition->Test(safemodeContext));
182 /*static*/ void
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);
204 Condition*
205 ConditionsTest::_Condition(const char* string)
207 SettingsParser parser;
208 BString input("job A {\nif {");
209 input << string << "\n}\n}\n";
211 BMessage jobs;
212 CPPUNIT_ASSERT_EQUAL(B_OK, parser.Parse(input, jobs));
214 BMessage job;
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")));
219 BMessage message;
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);
225 return condition;