Add EqualsTree matcher

This commit is contained in:
Max Brunsfeld 2015-05-25 20:44:23 -07:00
parent d793eda5b5
commit b9dc92c36b
3 changed files with 31 additions and 17 deletions

View file

@ -6,3 +6,15 @@ TSTree ** tree_array(std::vector<TSTree *> 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;
}

View file

@ -3,7 +3,17 @@
#include "runtime/tree.h"
#include <vector>
#include <string>
TSTree ** tree_array(std::vector<TSTree *> 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_

View file

@ -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));
});
});