diff --git a/spec/compiler/rules/pattern_spec.cpp b/spec/compiler/rules/pattern_spec.cpp index b33a12e3..69d1bc94 100644 --- a/spec/compiler/rules/pattern_spec.cpp +++ b/spec/compiler/rules/pattern_spec.cpp @@ -10,30 +10,30 @@ describe("parsing pattern rules", []() { it("parses simple strings", [&]() { Pattern rule("abc"); AssertThat( - rule.to_rule_tree()->to_string(), - Equals(seq({ + rule.to_rule_tree(), + EqualsPointer(seq({ character('a'), character('b'), character('c') - })->to_string())); + }))); }); it("parses character classes", []() { Pattern rule("\\w-\\d"); AssertThat( - rule.to_rule_tree()->to_string(), - Equals(seq({ + rule.to_rule_tree(), + EqualsPointer(seq({ character(CharClassWord), character('-'), character(CharClassDigit) - })->to_string())); + }))); }); it("parses choices", []() { Pattern rule("ab|cd|ef"); AssertThat( - rule.to_rule_tree()->to_string(), - Equals(choice({ + rule.to_rule_tree(), + EqualsPointer(choice({ seq({ character('a'), character('b'), @@ -46,39 +46,39 @@ describe("parsing pattern rules", []() { character('e'), character('f') }) - })->to_string())); + }))); }); it("parses choices in sequences", []() { Pattern rule("(a|b)cd"); AssertThat( - rule.to_rule_tree()->to_string(), - Equals(seq({ + rule.to_rule_tree(), + EqualsPointer(seq({ choice({ character('a'), character('b'), }), character('c'), character('d') - })->to_string())); + }))); }); it("parses special characters when they are escaped", []() { Pattern rule("a\\(b"); AssertThat( - rule.to_rule_tree()->to_string(), - Equals(seq({ + rule.to_rule_tree(), + EqualsPointer(seq({ character('a'), character('('), character('b') - })->to_string())); + }))); }); it("parses repeating rules", []() { Pattern rule("(ab)+(cd)+"); AssertThat( - rule.to_rule_tree()->to_string(), - Equals( + rule.to_rule_tree(), + EqualsPointer( seq({ repeat(seq({ character('a'), @@ -88,7 +88,7 @@ describe("parsing pattern rules", []() { character('c'), character('d') })), - })->to_string() + }) )); }); }); diff --git a/spec/compiler/rules/rules_spec.cpp b/spec/compiler/rules/rules_spec.cpp index 6c3912d9..4424b1c3 100644 --- a/spec/compiler/rules/rules_spec.cpp +++ b/spec/compiler/rules/rules_spec.cpp @@ -4,29 +4,27 @@ START_TEST describe("Rules", []() { - describe("construction", []() { - rules::rule_ptr symbol1 = rules::sym("1"); - rules::rule_ptr symbol2 = rules::sym("2"); - rules::rule_ptr symbol3 = rules::sym("3"); - + rules::rule_ptr symbol1 = rules::sym("1"); + rules::rule_ptr symbol2 = rules::sym("2"); + rules::rule_ptr symbol3 = rules::sym("3"); + rules::rule_ptr symbol4 = rules::sym("4"); + rules::rule_ptr char1 = rules::character('a'); + + describe("construction", [&]() { it("constructs binary trees", [&]() { AssertThat( - rules::seq({ symbol1, symbol2, symbol3 })->to_string(), - Equals(std::string("(seq (seq (sym '1') (sym '2')) (sym '3'))"))); + rules::seq({ symbol1, symbol2, symbol3 }), + EqualsPointer( + rules::seq({ rules::seq({ symbol1, symbol2 }), symbol3 }))); AssertThat( - rules::choice({ symbol1, symbol2, symbol3 })->to_string(), - Equals(std::string("(choice (choice (sym '1') (sym '2')) (sym '3'))"))); + rules::choice({ symbol1, symbol2, symbol3 }), + EqualsPointer( + rules::choice({ rules::choice({ symbol1, symbol2 }), symbol3 }))); }); }); - describe("transitions", []() { - rules::rule_ptr symbol1 = rules::sym("1"); - rules::rule_ptr symbol2 = rules::sym("2"); - rules::rule_ptr symbol3 = rules::sym("3"); - rules::rule_ptr symbol4 = rules::sym("3"); - rules::rule_ptr char1 = rules::character('a'); - + describe("transitions", [&]() { it("handles symbols", [&]() { AssertThat( rules::transitions(symbol1), diff --git a/spec/compiler/spec_helper.h b/spec/compiler/spec_helper.h index 0641f8fa..ea244f14 100644 --- a/spec/compiler/spec_helper.h +++ b/spec/compiler/spec_helper.h @@ -18,10 +18,40 @@ using namespace bandit; #define START_TEST go_bandit([]() { #define END_TEST }); +namespace snowhouse { + template + struct EqualsPointerConstraint : Expression> + { + EqualsPointerConstraint(const ExpectedType& expected) : expected(expected) {} + + template + bool operator()(const ActualType& actual) const { + return *expected == *actual; + } + + ExpectedType expected; + }; + + template + struct Stringizer> + { + static std::string ToString(const EqualsPointerConstraint& constraint) { + std::ostringstream builder; + builder << "equal to pointer " << snowhouse::Stringize(constraint.expected); + return builder.str(); + } + }; + + template + inline EqualsPointerConstraint EqualsPointer(const ExpectedType& expected) { + return EqualsPointerConstraint(expected); + } +} + namespace tree_sitter { namespace lr { - std::ostream& operator<<(std::ostream &stream, const unordered_map> &map); - std::ostream& operator<<(std::ostream &stream, const unordered_map> &map); + std::ostream& operator<<(std::ostream &stream, const unordered_map> &map); + std::ostream& operator<<(std::ostream &stream, const unordered_map> &map); } }