Skip to content

Commit 6463541

Browse files
committed
adds basic initial test suite for expression_tree_leaf_node
1 parent 95a1a54 commit 6463541

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

include/attwoodn/expression_tree.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ namespace attwoodn::expression_tree {
180180
~expression_tree_leaf_node() override {};
181181

182182
bool evaluate(const Obj& obj) override {
183-
if (!member_func_ && !member_var_) {
183+
if (member_func_ && member_var_) {
184+
throw std::runtime_error("expression_tree_leaf_node has both a member function reference " +
185+
std::string("and member variable reference. Only one is permitted"));
186+
}
187+
188+
else if (!member_func_ && !member_var_) {
184189
throw std::runtime_error("expression_tree_leaf_node has a nullptr for both member function reference " +
185190
std::string("and member variable reference. At least one is required"));
186191
}
@@ -199,7 +204,7 @@ namespace attwoodn::expression_tree {
199204

200205
else return false;
201206

202-
return Op(actual_value, comp_value_);
207+
return logical_op_(actual_value, comp_value_);
203208
}
204209

205210
private:

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
if(BUILD_TESTING)
2-
add_executable( expression_tree_test test.cpp )
3-
add_test( expression_tree_test ${EXECUTABLE_OUTPUT_PATH}/expression_tree_test )
2+
add_executable( expression_tree_leaf_node_test expression_tree_leaf_node.cpp )
3+
add_test( expression_tree_leaf_node_test ${EXECUTABLE_OUTPUT_PATH}/expression_tree_leaf_node_test )
44

55
add_executable( operators_test operators.cpp )
66
add_test( operators_test ${EXECUTABLE_OUTPUT_PATH}/operators_test )
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <attwoodn/expression_tree.hpp>
2+
#include <iostream>
3+
#include <cassert>
4+
5+
using namespace attwoodn::expression_tree;
6+
7+
struct test_fixture {
8+
std::string some_string;
9+
};
10+
11+
template<class T> struct type_id{typedef T type;};
12+
13+
// template<typename Obj, typename CompValue, typename Op = typename type_id<bool (*)(CompValue*, CompValue*)>::type>
14+
// node::expression_tree_leaf_node<Obj, Op, CompValue>* make_leaf_node( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
15+
// return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
16+
// }
17+
18+
template<typename Obj, typename CompValue, typename Op = typename type_id<bool (*)(CompValue, CompValue)>::type>
19+
node::expression_tree_leaf_node<Obj, Op, CompValue>* make_leaf_node( CompValue Obj::* member_var, Op op, CompValue comp_value ) {
20+
return new node::expression_tree_leaf_node<Obj, Op, CompValue>( member_var, op, comp_value );
21+
}
22+
23+
int main(int argc, char** argv) {
24+
test_fixture fixture;
25+
fixture.some_string = "hello world!";
26+
27+
auto node = make_leaf_node(&test_fixture::some_string, &op::equals, std::string("hello world!"));
28+
assert(node->evaluate(fixture));
29+
30+
fixture.some_string = "hey, world!";
31+
assert(!node->evaluate(fixture));
32+
33+
return EXIT_SUCCESS;
34+
}

tests/test.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)