Refactor launch server tests
* Move SafemodeConditionContext definition from method to the class because GCC2 can't handle it (possibly because the method is defined in-line). Change-Id: If432ddf7fdb044ff5d5b8ca87a20b60489bb7cd7 Reviewed-on: https://review.haiku-os.org/c/haiku/+/10682 Reviewed-by: waddlesplash <[email protected]> Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
@@ -4,15 +4,14 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "ConditionsTest.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <driver_settings.h>
|
||||
#include <String.h>
|
||||
|
||||
#include <cppunit/TestCaller.h>
|
||||
#include <cppunit/TestSuite.h>
|
||||
#include <TestSuiteAddon.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "Conditions.h"
|
||||
#include "SettingsParser.h"
|
||||
@@ -30,30 +29,38 @@ public:
|
||||
static TestConditionContext sConditionContext;
|
||||
|
||||
|
||||
ConditionsTest::ConditionsTest()
|
||||
{
|
||||
}
|
||||
class ConditionsTest : public CppUnit::TestFixture {
|
||||
CPPUNIT_TEST_SUITE(ConditionsTest);
|
||||
CPPUNIT_TEST(TestEmpty);
|
||||
CPPUNIT_TEST(TestSafemode);
|
||||
CPPUNIT_TEST(TestFileExists);
|
||||
CPPUNIT_TEST(TestOr);
|
||||
CPPUNIT_TEST(TestAnd);
|
||||
CPPUNIT_TEST(TestNot);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
Condition* _Condition(const char* string)
|
||||
{
|
||||
SettingsParser parser;
|
||||
BString input("job A {\nif {");
|
||||
input << string << "\n}\n}\n";
|
||||
|
||||
ConditionsTest::~ConditionsTest()
|
||||
{
|
||||
}
|
||||
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, (int)job.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job.GetString("name")));
|
||||
|
||||
void
|
||||
ConditionsTest::TestEmpty()
|
||||
{
|
||||
Condition* condition = _Condition("");
|
||||
CPPUNIT_ASSERT(condition == NULL);
|
||||
}
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, job.FindMessage("if", &message));
|
||||
|
||||
|
||||
void
|
||||
ConditionsTest::TestSafemode()
|
||||
{
|
||||
Condition* condition = _Condition("safemode");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
Condition* condition = Conditions::FromMessage(message);
|
||||
if (string[0] != '\0')
|
||||
CPPUNIT_ASSERT(condition != NULL);
|
||||
return condition;
|
||||
}
|
||||
|
||||
class SafemodeConditionContext : public TestConditionContext {
|
||||
public:
|
||||
@@ -61,166 +68,124 @@ ConditionsTest::TestSafemode()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
} safemodeContext;
|
||||
CPPUNIT_ASSERT(condition->Test(safemodeContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(safemodeContext));
|
||||
}
|
||||
};
|
||||
|
||||
public:
|
||||
void TestEmpty()
|
||||
{
|
||||
Condition* condition = _Condition("");
|
||||
CPPUNIT_ASSERT(condition == NULL);
|
||||
}
|
||||
|
||||
void TestSafemode()
|
||||
{
|
||||
Condition* condition = _Condition("safemode");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
|
||||
SafemodeConditionContext safemodeContext;
|
||||
CPPUNIT_ASSERT(condition->Test(safemodeContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(safemodeContext));
|
||||
}
|
||||
|
||||
void TestFileExists()
|
||||
{
|
||||
Condition* condition = _Condition("file_exists /boot");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("file_exists /boot/don't fool me!");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
}
|
||||
|
||||
void TestOr()
|
||||
{
|
||||
Condition* condition = _Condition("or {\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"file_exists /boot\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"not safemode\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"safemode\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
}
|
||||
|
||||
void TestAnd()
|
||||
{
|
||||
Condition* condition = _Condition("and {\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"file_exists /boot\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"safemode\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"not safemode\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"safemode\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
}
|
||||
|
||||
void TestNot()
|
||||
{
|
||||
Condition* condition = _Condition("not safemode");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
|
||||
SafemodeConditionContext safemodeContext;
|
||||
CPPUNIT_ASSERT(!condition->Test(safemodeContext));
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void
|
||||
ConditionsTest::TestFileExists()
|
||||
{
|
||||
Condition* condition = _Condition("file_exists /boot");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(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));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"file_exists /boot\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"not safemode\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("or {\n"
|
||||
"safemode\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ConditionsTest::TestAnd()
|
||||
{
|
||||
Condition* condition = _Condition("and {\n"
|
||||
"file_exists /boot\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"file_exists /boot\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"safemode\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"not safemode\n"
|
||||
"file_exists /nowhere\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(!condition->IsConstant(sConditionContext));
|
||||
|
||||
condition = _Condition("and {\n"
|
||||
"safemode\n"
|
||||
"}\n");
|
||||
CPPUNIT_ASSERT(!condition->Test(sConditionContext));
|
||||
CPPUNIT_ASSERT(condition->IsConstant(sConditionContext));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ConditionsTest::TestNot()
|
||||
{
|
||||
Condition* condition = _Condition("not safemode");
|
||||
CPPUNIT_ASSERT(condition->Test(sConditionContext));
|
||||
|
||||
class SafemodeConditionContext : public TestConditionContext {
|
||||
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::TestEmpty", &ConditionsTest::TestEmpty));
|
||||
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);
|
||||
if (string[0] != '\0')
|
||||
CPPUNIT_ASSERT(condition != NULL);
|
||||
return condition;
|
||||
}
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ConditionsTest, getTestSuiteName());
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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 TestEmpty();
|
||||
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,22 +4,11 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <TestSuite.h>
|
||||
#include <TestSuiteAddon.h>
|
||||
|
||||
#include "ConditionsTest.h"
|
||||
#include "SettingsParserTest.h"
|
||||
#include "UtilityTest.h"
|
||||
|
||||
|
||||
BTestSuite*
|
||||
getTestSuite()
|
||||
const char*
|
||||
getTestSuiteName()
|
||||
{
|
||||
BTestSuite* suite = new BTestSuite("LaunchDaemon");
|
||||
|
||||
SettingsParserTest::AddTests(*suite);
|
||||
ConditionsTest::AddTests(*suite);
|
||||
UtilityTest::AddTests(*suite);
|
||||
|
||||
return suite;
|
||||
return "LaunchDaemon";
|
||||
}
|
||||
|
||||
@@ -4,529 +4,411 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "SettingsParserTest.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <driver_settings.h>
|
||||
#include <String.h>
|
||||
|
||||
#include <cppunit/TestCaller.h>
|
||||
#include <cppunit/TestSuite.h>
|
||||
#include <TestSuiteAddon.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "SettingsParser.h"
|
||||
|
||||
|
||||
SettingsParserTest::SettingsParserTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SettingsParserTest::~SettingsParserTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - conditions
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsEmpty()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT(message.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsMultiLine()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tsafemode\n"
|
||||
"\tfile_exists one\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(2, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("safemode", &subMessage));
|
||||
CPPUNIT_ASSERT(subMessage.IsEmpty());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_exists",
|
||||
&subMessage));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(subMessage.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, subMessage.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsFlat()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if safemode\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsFlatWithNot()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if not safemode\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsFlatWithArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK,
|
||||
_ParseCondition("if file_exists one\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_exists", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, args.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsFlatWithNotAndArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK,
|
||||
_ParseCondition("if not file_exists one\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("file_exists", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, args.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsMultiLineFlatNot()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tnot safemode\n"
|
||||
"}\n", message));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(1, subMessage.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsMultiLineFlatNotWithArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tnot file_exists one two\n"
|
||||
"}\n", message));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("file_exists", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("two"),
|
||||
BString(args.GetString("args", 1, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, args.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT_EQUAL(2, _ArrayCount(args, "args"));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestConditionsMultiLineNot()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tnot {\n"
|
||||
"\t\tsafemode\n"
|
||||
"\t}\n"
|
||||
"}\n", message));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not", &subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - events
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestEventsEmpty()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on {\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT(message.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestEventsMultiLine()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on {\n"
|
||||
"\tfile_created one\n"
|
||||
"\tdemand\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(2, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("demand", &subMessage));
|
||||
CPPUNIT_ASSERT(subMessage.IsEmpty());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_created",
|
||||
&subMessage));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(subMessage.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, subMessage.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestEventsFlat()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on demand\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("demand", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestEventsFlatWithArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on file_created one\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_created", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, args.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - environment
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestEnvironmentMultiLine()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseName("env", "env {\n"
|
||||
"from_script SetupEnvironment\n"
|
||||
"TEST well, yes\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(2, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(BString("SetupEnvironment"),
|
||||
BString(message.GetString("from_script", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(message, "from_script"));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(BString("well,"),
|
||||
BString(message.GetString("TEST", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("yes"),
|
||||
BString(message.GetString("TEST", 1, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(2, _ArrayCount(message, "TEST"));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestEnvironmentFlat()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseName("env", "env SetupEnvironment\n",
|
||||
message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(BString("SetupEnvironment"),
|
||||
BString(message.GetString("from_script", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(message, "from_script"));
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark - run
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestRunFlat()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, parser.Parse("run me", jobs));
|
||||
CPPUNIT_ASSERT_EQUAL(1, jobs.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("me"),
|
||||
BString(message.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(message, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestRunMultiLine()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse("run {\n"
|
||||
"\tme\n"
|
||||
"\tyou\n"
|
||||
"}\n", jobs);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, status);
|
||||
CPPUNIT_ASSERT_EQUAL(1, jobs.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("me"),
|
||||
BString(message.GetString("target", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("you"),
|
||||
BString(message.GetString("target", 1, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(2, _ArrayCount(message, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, message.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestRunIfThenElseFlat()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse("run {\n"
|
||||
"\tif safemode\n"
|
||||
"\tthen this\n"
|
||||
"\telse that\n"
|
||||
"}\n", jobs);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, status);
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(3, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage then;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("then", &then));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("this"),
|
||||
BString(then.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(then, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, then.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage otherwise;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("else", &otherwise));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("that"),
|
||||
BString(otherwise.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(otherwise, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, otherwise.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsParserTest::TestRunIfThenElseMultiLine()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse("run {\n"
|
||||
"\tif {\n"
|
||||
"\t\tread_only\n"
|
||||
"\t}\n"
|
||||
"\tthen {\n"
|
||||
"\t\tthis\n"
|
||||
"\t}\n"
|
||||
"\telse {\n"
|
||||
"\t\tthat\n"
|
||||
"\t}\n"
|
||||
"}\n", jobs);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, status);
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(3, message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage then;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("then", &then));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("this"),
|
||||
BString(then.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(then, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, then.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage otherwise;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("else", &otherwise));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("that"),
|
||||
BString(otherwise.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, _ArrayCount(otherwise, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, otherwise.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
|
||||
// #pragma mark -
|
||||
|
||||
|
||||
/*static*/ void
|
||||
SettingsParserTest::AddTests(BTestSuite& parent)
|
||||
{
|
||||
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("SettingsParserTest");
|
||||
|
||||
// Conditions
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsEmpty",
|
||||
&SettingsParserTest::TestConditionsEmpty));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsMultiLine",
|
||||
&SettingsParserTest::TestConditionsMultiLine));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsFlat",
|
||||
&SettingsParserTest::TestConditionsFlat));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsFlatWithNot",
|
||||
&SettingsParserTest::TestConditionsFlatWithNot));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsFlatWithArgs",
|
||||
&SettingsParserTest::TestConditionsFlatWithArgs));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsFlatWithNotAndArgs",
|
||||
&SettingsParserTest::TestConditionsFlatWithNotAndArgs));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsMultiLineFlatNot",
|
||||
&SettingsParserTest::TestConditionsMultiLineFlatNot));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsMultiLineFlatNotWithArgs",
|
||||
&SettingsParserTest::TestConditionsMultiLineFlatNotWithArgs));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestConditionsMultiLineNot",
|
||||
&SettingsParserTest::TestConditionsMultiLineNot));
|
||||
|
||||
// Events
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestEventsEmpty",
|
||||
&SettingsParserTest::TestEventsEmpty));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestEventsMultiLine",
|
||||
&SettingsParserTest::TestEventsMultiLine));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestEventsFlat",
|
||||
&SettingsParserTest::TestEventsFlat));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestEventsFlatWithArgs",
|
||||
&SettingsParserTest::TestEventsFlatWithArgs));
|
||||
|
||||
// Environment
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestEnvironmentMultiLine",
|
||||
&SettingsParserTest::TestEnvironmentMultiLine));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestEnvironmentFlat",
|
||||
&SettingsParserTest::TestEnvironmentFlat));
|
||||
|
||||
// Run
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestRunFlat",
|
||||
&SettingsParserTest::TestRunFlat));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestRunMultiLine",
|
||||
&SettingsParserTest::TestRunMultiLine));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestRunIfThenElseFlat",
|
||||
&SettingsParserTest::TestRunIfThenElseFlat));
|
||||
suite.addTest(new CppUnit::TestCaller<SettingsParserTest>(
|
||||
"SettingsParserTest::TestRunIfThenElseMultiLine",
|
||||
&SettingsParserTest::TestRunIfThenElseMultiLine));
|
||||
|
||||
parent.addTest("SettingsParserTest", &suite);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
SettingsParserTest::_ParseCondition(const char* text, BMessage& message)
|
||||
{
|
||||
return _ParseName("if", text, message);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
SettingsParserTest::_ParseEvent(const char* text, BMessage& message)
|
||||
{
|
||||
return _ParseName("on", text, message);
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
SettingsParserTest::_ParseName(const char* name, const char* text,
|
||||
BMessage& message)
|
||||
{
|
||||
SettingsParser parser;
|
||||
BString input("job A {\n");
|
||||
input << text << "\n}\n";
|
||||
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse(input, jobs);
|
||||
if (status != B_OK)
|
||||
return status;
|
||||
|
||||
BMessage job;
|
||||
status = jobs.FindMessage("job", 0, &job);
|
||||
if (status != B_OK)
|
||||
return status;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(2, job.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job.GetString("name")));
|
||||
|
||||
return job.FindMessage(name, &message);
|
||||
}
|
||||
|
||||
|
||||
int32
|
||||
SettingsParserTest::_ArrayCount(BMessage& message, const char* name)
|
||||
{
|
||||
int32 found;
|
||||
if (message.GetInfo(name, NULL, &found, NULL) != B_OK)
|
||||
return 0;
|
||||
|
||||
return found;
|
||||
}
|
||||
class SettingsParserTest : public CppUnit::TestFixture {
|
||||
CPPUNIT_TEST_SUITE(SettingsParserTest);
|
||||
CPPUNIT_TEST(TestConditionsEmpty);
|
||||
CPPUNIT_TEST(TestConditionsMultiLine);
|
||||
CPPUNIT_TEST(TestConditionsFlat);
|
||||
CPPUNIT_TEST(TestConditionsFlatWithNot);
|
||||
CPPUNIT_TEST(TestConditionsFlatWithArgs);
|
||||
CPPUNIT_TEST(TestConditionsFlatWithNotAndArgs);
|
||||
CPPUNIT_TEST(TestConditionsMultiLineFlatNot);
|
||||
CPPUNIT_TEST(TestConditionsMultiLineFlatNotWithArgs);
|
||||
CPPUNIT_TEST(TestConditionsMultiLineNot);
|
||||
CPPUNIT_TEST(TestEventsEmpty);
|
||||
CPPUNIT_TEST(TestEventsMultiLine);
|
||||
CPPUNIT_TEST(TestEventsFlat);
|
||||
CPPUNIT_TEST(TestEventsFlatWithArgs);
|
||||
CPPUNIT_TEST(TestEnvironmentMultiLine);
|
||||
CPPUNIT_TEST(TestEnvironmentFlat);
|
||||
CPPUNIT_TEST(TestRunFlat);
|
||||
CPPUNIT_TEST(TestRunMultiLine);
|
||||
CPPUNIT_TEST(TestRunIfThenElseFlat);
|
||||
CPPUNIT_TEST(TestRunIfThenElseMultiLine);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
status_t _ParseCondition(const char* text, BMessage& message)
|
||||
{
|
||||
return _ParseName("if", text, message);
|
||||
}
|
||||
|
||||
status_t _ParseEvent(const char* text, BMessage& message)
|
||||
{
|
||||
return _ParseName("on", text, message);
|
||||
}
|
||||
|
||||
status_t _ParseName(const char* name, const char* text, BMessage& message)
|
||||
{
|
||||
SettingsParser parser;
|
||||
BString input("job A {\n");
|
||||
input << text << "\n}\n";
|
||||
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse(input, jobs);
|
||||
if (status != B_OK)
|
||||
return status;
|
||||
|
||||
BMessage job;
|
||||
status = jobs.FindMessage("job", 0, &job);
|
||||
if (status != B_OK)
|
||||
return status;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)job.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("A"), BString(job.GetString("name")));
|
||||
|
||||
return job.FindMessage(name, &message);
|
||||
}
|
||||
|
||||
int32 _ArrayCount(BMessage& message, const char* name)
|
||||
{
|
||||
int32 found;
|
||||
if (message.GetInfo(name, NULL, &found, NULL) != B_OK)
|
||||
return 0;
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
public:
|
||||
void TestConditionsEmpty()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT(message.IsEmpty());
|
||||
}
|
||||
|
||||
void TestConditionsMultiLine()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tsafemode\n"
|
||||
"\tfile_exists one\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("safemode", &subMessage));
|
||||
CPPUNIT_ASSERT(subMessage.IsEmpty());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_exists",
|
||||
&subMessage));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(subMessage.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)subMessage.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestConditionsFlat()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if safemode\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
void TestConditionsFlatWithNot()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if not safemode\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
void TestConditionsFlatWithArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK,
|
||||
_ParseCondition("if file_exists one\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_exists", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)args.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestConditionsFlatWithNotAndArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK,
|
||||
_ParseCondition("if not file_exists one\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("file_exists", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)args.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestConditionsMultiLineFlatNot()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tnot safemode\n"
|
||||
"}\n", message));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)subMessage.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
void TestConditionsMultiLineFlatNotWithArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tnot file_exists one two\n"
|
||||
"}\n", message));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not",
|
||||
&subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("file_exists", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("two"),
|
||||
BString(args.GetString("args", 1, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)args.CountNames(B_ANY_TYPE));
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)_ArrayCount(args, "args"));
|
||||
}
|
||||
|
||||
void TestConditionsMultiLineNot()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseCondition("if {\n"
|
||||
"\tnot {\n"
|
||||
"\t\tsafemode\n"
|
||||
"\t}\n"
|
||||
"}\n", message));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("not", &subMessage));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, subMessage.FindMessage("safemode", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
void TestEventsEmpty()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on {\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT(message.IsEmpty());
|
||||
}
|
||||
|
||||
void TestEventsMultiLine()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on {\n"
|
||||
"\tfile_created one\n"
|
||||
"\tdemand\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage subMessage;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("demand", &subMessage));
|
||||
CPPUNIT_ASSERT(subMessage.IsEmpty());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_created",
|
||||
&subMessage));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(subMessage.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)subMessage.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestEventsFlat()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on demand\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("demand", &args));
|
||||
CPPUNIT_ASSERT(args.IsEmpty());
|
||||
}
|
||||
|
||||
void TestEventsFlatWithArgs()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseEvent("on file_created one\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage args;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("file_created", &args));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("one"),
|
||||
BString(args.GetString("args", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)args.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestEnvironmentMultiLine()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseName("env", "env {\n"
|
||||
"from_script SetupEnvironment\n"
|
||||
"TEST well, yes\n"
|
||||
"}\n", message));
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(BString("SetupEnvironment"),
|
||||
BString(message.GetString("from_script", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(message, "from_script"));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(BString("well,"),
|
||||
BString(message.GetString("TEST", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("yes"),
|
||||
BString(message.GetString("TEST", 1, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)_ArrayCount(message, "TEST"));
|
||||
}
|
||||
|
||||
void TestEnvironmentFlat()
|
||||
{
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, _ParseName("env", "env SetupEnvironment\n",
|
||||
message));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(BString("SetupEnvironment"),
|
||||
BString(message.GetString("from_script", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(message, "from_script"));
|
||||
}
|
||||
|
||||
void TestRunFlat()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, parser.Parse("run me", jobs));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)jobs.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("me"),
|
||||
BString(message.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(message, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestRunMultiLine()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse("run {\n"
|
||||
"\tme\n"
|
||||
"\tyou\n"
|
||||
"}\n", jobs);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, status);
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)jobs.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("me"),
|
||||
BString(message.GetString("target", 0, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("you"),
|
||||
BString(message.GetString("target", 1, "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(2, (int)_ArrayCount(message, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)message.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestRunIfThenElseFlat()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse("run {\n"
|
||||
"\tif safemode\n"
|
||||
"\tthen this\n"
|
||||
"\telse that\n"
|
||||
"}\n", jobs);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, status);
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(3, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage then;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("then", &then));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("this"),
|
||||
BString(then.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(then, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)then.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage otherwise;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("else", &otherwise));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("that"),
|
||||
BString(otherwise.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(otherwise, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)otherwise.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
|
||||
void TestRunIfThenElseMultiLine()
|
||||
{
|
||||
SettingsParser parser;
|
||||
BMessage jobs;
|
||||
status_t status = parser.Parse("run {\n"
|
||||
"\tif {\n"
|
||||
"\t\tread_only\n"
|
||||
"\t}\n"
|
||||
"\tthen {\n"
|
||||
"\t\tthis\n"
|
||||
"\t}\n"
|
||||
"\telse {\n"
|
||||
"\t\tthat\n"
|
||||
"\t}\n"
|
||||
"}\n", jobs);
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, status);
|
||||
|
||||
BMessage message;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, jobs.FindMessage("run", &message));
|
||||
CPPUNIT_ASSERT_EQUAL(3, (int)message.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage then;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("then", &then));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("this"),
|
||||
BString(then.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(then, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)then.CountNames(B_ANY_TYPE));
|
||||
|
||||
BMessage otherwise;
|
||||
CPPUNIT_ASSERT_EQUAL(B_OK, message.FindMessage("else", &otherwise));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("that"),
|
||||
BString(otherwise.GetString("target", "-")));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)_ArrayCount(otherwise, "target"));
|
||||
CPPUNIT_ASSERT_EQUAL(1, (int)otherwise.CountNames(B_ANY_TYPE));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SettingsParserTest, getTestSuiteName());
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef SETTINGS_PARSER_TEST_H
|
||||
#define SETTINGS_PARSER_TEST_H
|
||||
|
||||
|
||||
#include <TestCase.h>
|
||||
#include <TestSuite.h>
|
||||
|
||||
#include <Message.h>
|
||||
|
||||
|
||||
class SettingsParserTest : public CppUnit::TestCase {
|
||||
public:
|
||||
SettingsParserTest();
|
||||
virtual ~SettingsParserTest();
|
||||
|
||||
void TestConditionsEmpty();
|
||||
void TestConditionsMultiLine();
|
||||
void TestConditionsFlat();
|
||||
void TestConditionsFlatWithNot();
|
||||
void TestConditionsFlatWithArgs();
|
||||
void TestConditionsFlatWithNotAndArgs();
|
||||
void TestConditionsMultiLineFlatNot();
|
||||
void TestConditionsMultiLineFlatNotWithArgs();
|
||||
void TestConditionsMultiLineNot();
|
||||
|
||||
void TestEventsEmpty();
|
||||
void TestEventsMultiLine();
|
||||
void TestEventsFlat();
|
||||
void TestEventsFlatWithArgs();
|
||||
|
||||
void TestEnvironmentMultiLine();
|
||||
void TestEnvironmentFlat();
|
||||
|
||||
void TestRunFlat();
|
||||
void TestRunMultiLine();
|
||||
void TestRunIfThenElseFlat();
|
||||
void TestRunIfThenElseMultiLine();
|
||||
|
||||
static void AddTests(BTestSuite& suite);
|
||||
|
||||
private:
|
||||
status_t _ParseCondition(const char* text,
|
||||
BMessage& message);
|
||||
status_t _ParseEvent(const char* text,
|
||||
BMessage& message);
|
||||
status_t _ParseName(const char* name, const char* text,
|
||||
BMessage& message);
|
||||
int32 _ArrayCount(BMessage& message,
|
||||
const char* name);
|
||||
};
|
||||
|
||||
|
||||
#endif // SETTINGS_PARSER_TEST_H
|
||||
@@ -4,53 +4,39 @@
|
||||
*/
|
||||
|
||||
|
||||
#include "UtilityTest.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <cppunit/TestCaller.h>
|
||||
#include <cppunit/TestSuite.h>
|
||||
#include <TestSuiteAddon.h>
|
||||
#include <cppunit/TestFixture.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
#include "Utility.h"
|
||||
|
||||
|
||||
UtilityTest::UtilityTest()
|
||||
{
|
||||
}
|
||||
class UtilityTest : public CppUnit::TestFixture {
|
||||
CPPUNIT_TEST_SUITE(UtilityTest);
|
||||
CPPUNIT_TEST(TestTranslatePath);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
public:
|
||||
void TestTranslatePath()
|
||||
{
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/boot/home/test"),
|
||||
Utility::TranslatePath("$HOME/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/boot/home/test"),
|
||||
Utility::TranslatePath("${HOME}/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("--/boot/home--"),
|
||||
Utility::TranslatePath("--${HOME}--"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("$(HOME)/test"),
|
||||
Utility::TranslatePath("$(HOME)/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/boot/home/test"),
|
||||
Utility::TranslatePath("~/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("~baron/test"),
|
||||
Utility::TranslatePath("~baron/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/~/test"),
|
||||
Utility::TranslatePath("/~/test"));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
UtilityTest::~UtilityTest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
UtilityTest::TestTranslatePath()
|
||||
{
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/boot/home/test"),
|
||||
Utility::TranslatePath("$HOME/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/boot/home/test"),
|
||||
Utility::TranslatePath("${HOME}/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("--/boot/home--"),
|
||||
Utility::TranslatePath("--${HOME}--"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("$(HOME)/test"),
|
||||
Utility::TranslatePath("$(HOME)/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/boot/home/test"),
|
||||
Utility::TranslatePath("~/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("~baron/test"),
|
||||
Utility::TranslatePath("~baron/test"));
|
||||
CPPUNIT_ASSERT_EQUAL(BString("/~/test"),
|
||||
Utility::TranslatePath("/~/test"));
|
||||
}
|
||||
|
||||
|
||||
/*static*/ void
|
||||
UtilityTest::AddTests(BTestSuite& parent)
|
||||
{
|
||||
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UtilityTest");
|
||||
|
||||
suite.addTest(new CppUnit::TestCaller<UtilityTest>(
|
||||
"UtilityTest::TestTranslatePath", &UtilityTest::TestTranslatePath));
|
||||
|
||||
parent.addTest("UtilityTest", &suite);
|
||||
}
|
||||
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(UtilityTest, getTestSuiteName());
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef UTILITY_TEST_H
|
||||
#define UTILITY_TEST_H
|
||||
|
||||
|
||||
#include <TestCase.h>
|
||||
#include <TestSuite.h>
|
||||
|
||||
|
||||
class UtilityTest : public CppUnit::TestCase {
|
||||
public:
|
||||
UtilityTest();
|
||||
virtual ~UtilityTest();
|
||||
|
||||
void TestTranslatePath();
|
||||
|
||||
static void AddTests(BTestSuite& suite);
|
||||
};
|
||||
|
||||
|
||||
#endif // UTILITY_TEST_H
|
||||
Reference in New Issue
Block a user