launch_daemon: Added condition tests.

This commit is contained in:
Axel Dörfler
2015-07-22 20:42:54 +02:00
parent 17ac467a6e
commit 0e347cb203
4 changed files with 210 additions and 1 deletions
+170
View File
@@ -0,0 +1,170 @@
/*
* Copyright 2015, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "ConditionsTest.h"
#include <stdlib.h>
#include <driver_settings.h>
#include <String.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include "Conditions.h"
#include "SettingsParser.h"
class TestConditionContext : public ConditionContext {
public:
bool IsSafeMode() const
{ return false; }
};
static TestConditionContext sConditionContext;
ConditionsTest::ConditionsTest()
{
}
ConditionsTest::~ConditionsTest()
{
}
void
ConditionsTest::TestSafemode()
{
Condition* condition = _Condition("safemode");
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
class SafemodeConditionContext : public ConditionContext {
public:
bool IsSafeMode() const
{
return true;
}
} safemodeContext;
CPPUNIT_ASSERT(condition->Test(safemodeContext));
}
void
ConditionsTest::TestFileExists()
{
Condition* condition = _Condition("file_exists /boot");
CPPUNIT_ASSERT(condition->Test(sConditionContext));
condition = _Condition("file_exists /boot/don't fool me!");
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
}
void
ConditionsTest::TestOr()
{
Condition* condition = _Condition("or {\n"
"file_exists /boot\n"
"}\n");
CPPUNIT_ASSERT(condition->Test(sConditionContext));
condition = _Condition("or {\n"
"file_exists /nowhere\n"
"}\n");
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
condition = _Condition("or {\n"
"file_exists /boot\n"
"file_exists /nowhere\n"
"}\n");
CPPUNIT_ASSERT(condition->Test(sConditionContext));
}
void
ConditionsTest::TestAnd()
{
Condition* condition = _Condition("and {\n"
"file_exists /boot\n"
"}\n");
CPPUNIT_ASSERT(condition->Test(sConditionContext));
condition = _Condition("and {\n"
"file_exists /nowhere\n"
"}\n");
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
condition = _Condition("and {\n"
"file_exists /boot\n"
"file_exists /nowhere\n"
"}\n");
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
}
void
ConditionsTest::TestNot()
{
Condition* condition = _Condition("not safemode");
CPPUNIT_ASSERT(condition->Test(sConditionContext));
class SafemodeConditionContext : public ConditionContext {
public:
bool IsSafeMode() const
{
return true;
}
} safemodeContext;
CPPUNIT_ASSERT(!condition->Test(safemodeContext));
}
/*static*/ void
ConditionsTest::AddTests(BTestSuite& parent)
{
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("ConditionsTest");
suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
"ConditionsTest::TestSafemode", &ConditionsTest::TestSafemode));
suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
"ConditionsTest::TestFileExists", &ConditionsTest::TestFileExists));
suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
"ConditionsTest::TestOr", &ConditionsTest::TestOr));
suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
"ConditionsTest::TestAnd", &ConditionsTest::TestAnd));
suite.addTest(new CppUnit::TestCaller<ConditionsTest>(
"ConditionsTest::TestNot", &ConditionsTest::TestNot));
parent.addTest("ConditionsTest", &suite);
}
Condition*
ConditionsTest::_Condition(const char* string)
{
SettingsParser parser;
BString input("job A {\nif {");
input << string << "\n}\n}\n";
BMessage jobs;
CPPUNIT_ASSERT_EQUAL(B_OK, parser.Parse(input, jobs));
BMessage job;
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("job", 0, &job));
CPPUNIT_ASSERT_EQUAL(2, job.CountNames(B_ANY_TYPE));
CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job.GetString("name")));
BMessage message;
CPPUNIT_ASSERT_EQUAL(B_OK, job.FindMessage("if", &message));
Condition* condition = Conditions::FromMessage(message);
CPPUNIT_ASSERT(condition != NULL);
return condition;
}
+34
View File
@@ -0,0 +1,34 @@
/*
* Copyright 2015, Axel Dörfler, [email protected].
* Distributed under the terms of the MIT License.
*/
#ifndef CONDITIONS_TEST_H
#define CONDITIONS_TEST_H
#include <TestCase.h>
#include <TestSuite.h>
class Condition;
class ConditionsTest : public CppUnit::TestCase {
public:
ConditionsTest();
virtual ~ConditionsTest();
void TestSafemode();
void TestFileExists();
void TestOr();
void TestAnd();
void TestNot();
static void AddTests(BTestSuite& suite);
private:
Condition* _Condition(const char* string);
};
#endif // CONDITIONS_TEST_H
+4 -1
View File
@@ -2,7 +2,7 @@ SubDir HAIKU_TOP src tests servers launch ;
AddSubDirSupportedPlatforms libbe_test ;
UsePrivateHeaders app shared support ;
UsePrivateHeaders app shared storage support ;
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src servers launch ] ;
@@ -12,5 +12,8 @@ UnitTestLib liblaunch_daemontest.so :
SettingsParserTest.cpp
SettingsParser.cpp
ConditionsTest.cpp
Conditions.cpp
: be libshared.a [ TargetLibstdc++ ] [ TargetLibsupc++ ]
;
@@ -7,6 +7,7 @@
#include <TestSuite.h>
#include <TestSuiteAddon.h>
#include "ConditionsTest.h"
#include "SettingsParserTest.h"
@@ -16,6 +17,7 @@ getTestSuite()
BTestSuite* suite = new BTestSuite("LaunchDaemon");
SettingsParserTest::AddTests(*suite);
ConditionsTest::AddTests(*suite);
return suite;
}