From b9dc92c36bffe26496b80ba7d7e8f2638345e6f5 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 25 May 2015 20:44:23 -0700 Subject: [PATCH] Add EqualsTree matcher --- spec/runtime/helpers/tree_helpers.cc | 12 ++++++++++++ spec/runtime/helpers/tree_helpers.h | 10 ++++++++++ spec/runtime/parse_stack_spec.cc | 26 +++++++++----------------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/spec/runtime/helpers/tree_helpers.cc b/spec/runtime/helpers/tree_helpers.cc index 80d51ab6..2e4c7f98 100644 --- a/spec/runtime/helpers/tree_helpers.cc +++ b/spec/runtime/helpers/tree_helpers.cc @@ -6,3 +6,15 @@ TSTree ** tree_array(std::vector trees) { result[i] = trees[i]; return result; } + +EqualsTree::EqualsTree(const TSTree *expected, const char **symbol_names) + : expected(expected), symbol_names(symbol_names) {} + +bool EqualsTree::Matches(const TSTree *actual) const { + return ts_tree_eq(actual, expected); +} + +std::ostream &operator<<(std::ostream &stream, const EqualsTree &matcher) { + stream << std::string("equals tree: ") << std::string(ts_tree_string(matcher.expected, matcher.symbol_names)); + return stream; +} diff --git a/spec/runtime/helpers/tree_helpers.h b/spec/runtime/helpers/tree_helpers.h index fc0e5773..e024c666 100644 --- a/spec/runtime/helpers/tree_helpers.h +++ b/spec/runtime/helpers/tree_helpers.h @@ -3,7 +3,17 @@ #include "runtime/tree.h" #include +#include TSTree ** tree_array(std::vector trees); +struct EqualsTree { + EqualsTree(const TSTree *expected, const char **symbol_names); + bool Matches(const TSTree *actual) const; + const TSTree *expected; + const char **symbol_names; +}; + +std::ostream &operator<<(std::ostream &stream, const EqualsTree &matcher); + #endif // HELPERS_TREE_HELPERS_H_ diff --git a/spec/runtime/parse_stack_spec.cc b/spec/runtime/parse_stack_spec.cc index 834a6030..dad6e61d 100644 --- a/spec/runtime/parse_stack_spec.cc +++ b/spec/runtime/parse_stack_spec.cc @@ -70,12 +70,9 @@ describe("ParseStack", [&]() { ParseStackNode *head = ts_parse_stack_head(stack, 0); AssertThat(head->state, Equals(103)); - AssertThat( - ts_tree_eq( - head->tree, - ts_tree_make_node(symbol4, 2, tree_array({ trees[1], trees[2] }), false) - ), - IsTrue()); + AssertThat(head->tree, Fulfills(EqualsTree( + ts_tree_make_node(symbol4, 2, tree_array({ trees[1], trees[2] }), false), + names))); AssertThat(head->successor_count, Equals(1)); head = head->successors[0]; @@ -94,12 +91,9 @@ describe("ParseStack", [&]() { ParseStackNode *head = ts_parse_stack_head(stack, 0); AssertThat(head->state, Equals(103)); - AssertThat( - ts_tree_eq( - head->tree, - ts_tree_make_node(symbol4, 3, tree_array({ trees[0], trees[1], trees[2] }), false) - ), - IsTrue()); + AssertThat(head->tree, Fulfills(EqualsTree( + ts_tree_make_node(symbol4, 3, tree_array({ trees[0], trees[1], trees[2] }), false), + names))); AssertThat(head->successor_count, Equals(1)); head = head->successors[0]; @@ -198,11 +192,9 @@ describe("ParseStack", [&]() { AssertThat(ts_parse_stack_head_count(stack), Equals(1)); ParseStackNode *head = ts_parse_stack_head(stack, 0); AssertThat(head->state, Equals(stateG)); - AssertThat( - ts_tree_eq( - head->tree, - ts_tree_make_node(symbol5, 1, tree_array({ trees[4] }), false)), - IsTrue()); + AssertThat(head->tree, Fulfills(EqualsTree( + ts_tree_make_node(symbol5, 1, tree_array({ trees[4] }), false), + names))); AssertThat(head->successor_count, Equals(2)); }); });