Commits

David Mugnai committed 1bcfcd4

expr: interpolation rule preliminary tests

Comments (0)

Files changed (3)

                         return std::string(x.data(), x.size());
                     }, qi::_1)
                 ];
+
+
+                this->expression.name("expression");
+                this->term.name("expression term");
+                this->factor.name("expression factor");
+                this->operand.name("operand");
+                this->function_call.name("function_call");
+                this->symbol.name("symbol");
+                this->interpolation.name("interpolation");
+                this->string_value.name("string");
+                this->number_value.name("number");
+                this->identifier.name("identifier");
+
+                //qi::debug(this->expression);
+                //qi::debug(this->term);
+                //qi::debug(this->factor);
+                //qi::debug(this->operand);
+                //qi::debug(this->function_call);
+                //qi::debug(this->symbol);
+                //qi::debug(this->interpolation);
+                //qi::debug(this->string_value);
+                //qi::debug(this->number_value);
+                //qi::debug(this->identifier);
             }
 
             ast::Node const* binary_operation(char op, ast::Node const* first, ast::Node const* second) {
                 }
                 return output;
             }
-
-            void enable_debug() {
-                this->expression.name("expression");
-                this->term.name("expression term");
-                this->factor.name("expression factor");
-                this->operand.name("operand");
-                this->function_call.name("function_call");
-                this->symbol.name("symbol");
-                this->string_value.name("string");
-                this->number_value.name("number");
-                this->identifier.name("identifier");
-
-                qi::debug(this->expression);
-                qi::debug(this->term);
-                qi::debug(this->factor);
-                qi::debug(this->operand);
-                qi::debug(this->function_call);
-                qi::debug(this->symbol);
-                qi::debug(this->string_value);
-                qi::debug(this->number_value);
-                qi::debug(this->identifier);
-            }
         };
 
         ast::Node const* parse(std::string const& src) {
             ExprGrammar<std::string::const_iterator> g;
-            //g.enable_debug();
             auto start = src.begin();
             auto end = src.end();
             ast::Node const* a = nullptr;

tests/test_expr.cpp

 using expr::parser::ExprGrammar;
 
 struct InterpolationRuleTest : slam::test::SingleGrammarRuleTest<ExprGrammar<string::const_iterator>> {
+    using base_type = slam::test::SingleGrammarRuleTest<ExprGrammar<string::const_iterator>>;
+    void SetUp() {
+        base_type::SetUp();
+        this->context->put("a", 42);
+    }
     using RType = Node const*;
-    std::vector<tuple<string, bool, RType>> tests_list = {
-        make_tuple("#{1}", true, RType{})
+    std::vector<tuple<string, bool, double>> tests_list = {
+        make_tuple("#{1}", true, 1),
+        make_tuple("#{1+2}", true, 3),
+        make_tuple("#{ 2 + 2 }", true, 4),
+        make_tuple("#{a*2}", true, 84),
+        make_tuple("#{a+a}", true, 84),
+        make_tuple("#{a+a/2}", true, 63),
+        make_tuple("#{(a+a)/0.5}", true, 168)
     };
 };
 TEST_F(InterpolationRuleTest, test) {
 #include <tuple>
 #include "gtest/gtest.h"
 #include <boost/spirit/include/qi.hpp>
+#include "expr.h"
 
 namespace slam { namespace test {
 
 template<typename TGrammar>
 struct SingleGrammarRuleTest : ::testing::Test {
     TGrammar grammar;
+    expr::context::Context* context;
+
+    virtual void SetUp() {
+        context = new expr::context::Context();
+    }
+    virtual void TearDown() {
+        delete context;
+        context = nullptr;
+    }
 
     template<typename T>
     tuple<bool, typename T::attr_type> parse(string const& input, T rule) {
     template<typename T, typename R>
     void run_test(T const& tests_list, R const& rule) {
         for(auto& test : tests_list) {
-            auto result = this->parse(get<0>(test), rule);
-            auto expected = get<1>(test);
-            auto success = get<0>(result);
-            EXPECT_EQ(expected, success) << " input --> " << get<0>(test);
-            if(expected && success) {
-                EXPECT_EQ(get<2>(test), get<1>(result));
+            auto parsing_result = this->parse(get<0>(test), rule);
+            auto success = get<0>(parsing_result);
+
+            EXPECT_EQ(get<1>(test), success) << " input --> " << get<0>(test);
+            if(get<1>(test) && success) {
+                auto node = get<1>(parsing_result);
+
+                auto expected_value = get<2>(test);
+                auto value = node->evaluate(*this->context);
+
+                using value_type = decltype(expected_value);
+                value_type* evaluated_value = boost::get<value_type>(&value);
+
+                EXPECT_NE(evaluated_value, nullptr);
+                EXPECT_EQ(expected_value, *evaluated_value);
             }
         }
     }