182 lines
5.6 KiB
C++
182 lines
5.6 KiB
C++
// #include "core/bts_core/utest/utest.h"
|
|
|
|
// #include "../IPropertyEvaluator.h"
|
|
// #include "../ExpressionEvaluator.h"
|
|
// #include "../ParserExceptions.h"
|
|
|
|
// #include "core/bts_core/utils/BTS_SharedPtr.h"
|
|
// #include <utility>
|
|
|
|
// using namespace bts::expression;
|
|
|
|
// namespace bts {
|
|
|
|
// // Simple property evaluator for testing
|
|
// class EvaluatesToFoo : public IPropertyEvaluator
|
|
// {
|
|
// public:
|
|
// EvaluatesToFoo()
|
|
// { }
|
|
// ~EvaluatesToFoo()
|
|
// { }
|
|
// BTS_NO_COPY(EvaluatesToFoo);
|
|
|
|
// bool get_value(const std::string& prop, Value* out_value) OVERRIDE
|
|
// {
|
|
// bool valid = prop.substr(0,3) == "Foo";
|
|
// if (valid && out_value != nullptr)
|
|
// {
|
|
// if (prop == "Foo.number")
|
|
// {
|
|
// *out_value = Value(42.0);
|
|
// }
|
|
// else if (prop == "Foo.list")
|
|
// {
|
|
// std::vector<Value> values;
|
|
// values.push_back(Value(std::string("foo")));
|
|
// values.push_back(Value(std::string("bar")));
|
|
// values.push_back(Value(std::string("baz")));
|
|
// *out_value = Value(values);
|
|
// }
|
|
// else if (prop == "Foo.bool")
|
|
// {
|
|
// *out_value = Value(true);
|
|
// }
|
|
// else
|
|
// {
|
|
// // Foo.*
|
|
// *out_value = Value(std::string("Foo"));
|
|
// }
|
|
// }
|
|
|
|
// return valid;
|
|
// }
|
|
// };
|
|
|
|
// class EvaluatesRecursively : public IPropertyEvaluator
|
|
// {
|
|
// public:
|
|
// EvaluatesRecursively()
|
|
// : _e(nullptr)
|
|
// { }
|
|
// ~EvaluatesRecursively()
|
|
// { }
|
|
// BTS_NO_COPY(EvaluatesRecursively);
|
|
|
|
// void set_evaluator(ExpressionEvaluator* e)
|
|
// {
|
|
// _e = e;
|
|
// }
|
|
|
|
// bool get_value(const std::string& prop, Value* out_value) OVERRIDE
|
|
// {
|
|
// std::string recur = "42 == " + prop;
|
|
// if (out_value)
|
|
// {
|
|
// *out_value = Value(_e->eval(recur));
|
|
// }
|
|
// return true;
|
|
// }
|
|
|
|
// ExpressionEvaluator* _e;
|
|
// };
|
|
|
|
// struct expression_tests : public TestSuite
|
|
// {
|
|
// expression_tests() { }
|
|
// ~expression_tests() { }
|
|
// BTS_NO_COPY(expression_tests);
|
|
|
|
// void setup()
|
|
// {
|
|
// }
|
|
|
|
// void teardown()
|
|
// {
|
|
// }
|
|
|
|
// void simple_operator_test()
|
|
// {
|
|
// ExpressionEvaluator e(BTS_SharedPtr<IPropertyEvaluator> (new
|
|
// EvaluatesToFoo())); test_assert(e.eval("!!true"));
|
|
// test_assert(e.eval("3 == 3"));
|
|
// test_assert(e.eval("4 > 3"));
|
|
// test_assert(e.eval("3 < 6"));
|
|
// test_assert(e.eval("3 <= 3.5"));
|
|
// test_assert(e.eval("3 in [1,2,3]"));
|
|
// test_assert(!e.eval("3 in [1,2,4]"));
|
|
// test_assert(e.eval("'oo' in Foo.foo"));
|
|
// test_assert(e.eval("'oo' == 'oo'"));
|
|
// test_assert(e.eval("'afoobar' ~= 'foo.*'"));
|
|
// test_assert(!e.eval("'afoobar' ~= '^foo.*'"));
|
|
// }
|
|
|
|
// void test_bool_conversion()
|
|
// {
|
|
// ExpressionEvaluator e(BTS_SharedPtr<IPropertyEvaluator> (new
|
|
// EvaluatesToFoo())); test_assert(e.eval("true"));
|
|
// test_assert(e.eval("1"));
|
|
// test_assert(e.eval("1.5"));
|
|
// test_assert(e.eval("'true string'"));
|
|
// //test_assert(e.eval("[1,2,3]")); // list not valid here - only
|
|
// after "in" test_assert(e.eval("Foo.x"));
|
|
// test_assert(e.eval("Foo.list"));
|
|
// test_assert(!e.eval("''"));
|
|
// }
|
|
|
|
// void property_validator_test()
|
|
// {
|
|
// ExpressionEvaluator e(BTS_SharedPtr<IPropertyEvaluator> (new
|
|
// EvaluatesToFoo())); test_assert(e.check_valid("Foo.bar == 'Foo' or
|
|
// 'x' in [Foo.x, Foo.y, 'z']", nullptr)); std::string bad_property;
|
|
// test_assert(!e.check_valid("Foo.bar == 'Foo' or 'x' in [Fuu.x,
|
|
// Foo.y, 'z']", &bad_property)); test_assert(bad_property == "Fuu.x");
|
|
// }
|
|
|
|
// void more_complex_test()
|
|
// {
|
|
// ExpressionEvaluator e(BTS_SharedPtr<IPropertyEvaluator> (new
|
|
// EvaluatesToFoo()));
|
|
|
|
// test_assert(e.eval("Foo.bar == 'Foo' and 'b' in ['a', 'b', 'c',
|
|
// Foo.bar]")); test_assert(e.eval("Foo.number > 2 or (3.5 <= 7 and
|
|
// Foo.number == 3) or 'x' in ['a', 'b', 'c']"));
|
|
// test_assert(!e.eval("(3.5 <= 7 and Foo.number == 3) or 'x' in ['a',
|
|
// 'b', 'c']")); test_assert(e.eval("'foo' in Foo.list and Foo.number
|
|
// == 42.0")); test_assert(e.eval("!!('oo' in Foo.foo)"));
|
|
// test_assert(e.eval("!('ooo' in Foo.foo)"));
|
|
// test_assert(e.eval("!false in [false, false, false, false, true,
|
|
// Foo.bool]"));
|
|
// }
|
|
|
|
// void recursive_test()
|
|
// {
|
|
// BTS_SharedPtr<EvaluatesRecursively> badprops (new
|
|
// EvaluatesRecursively()); ExpressionEvaluator e(badprops);
|
|
// badprops->set_evaluator(&e);
|
|
|
|
// // Try to get the 'fuzzy'property recursively
|
|
// bool success = false;
|
|
// try
|
|
// {
|
|
// test_assert(e.eval("fuzzy == 42"));
|
|
// }
|
|
// catch(RecursivePropertyGetException)
|
|
// {
|
|
// success = true;
|
|
// }
|
|
// test_assert(success);
|
|
// }
|
|
|
|
// };
|
|
|
|
// BEGIN_TEST_SUITE(expression_tests)
|
|
// ADD_TEST(simple_operator_test);
|
|
// ADD_TEST(test_bool_conversion);
|
|
// ADD_TEST(property_validator_test);
|
|
// ADD_TEST(more_complex_test);
|
|
// ADD_TEST(recursive_test);
|
|
// END_TEST_SUITE
|
|
|
|
// }
|