Add EqualsPointer matcher for comparing pointed-to values

This commit is contained in:
Max Brunsfeld 2013-12-28 18:42:00 -08:00
parent d027aa5af6
commit 656f6b0819
3 changed files with 64 additions and 36 deletions

View file

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

View file

@ -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),

View file

@ -18,10 +18,40 @@ using namespace bandit;
#define START_TEST go_bandit([]() {
#define END_TEST });
namespace snowhouse {
template<typename ExpectedType>
struct EqualsPointerConstraint : Expression<EqualsPointerConstraint<ExpectedType>>
{
EqualsPointerConstraint(const ExpectedType& expected) : expected(expected) {}
template<typename ActualType>
bool operator()(const ActualType& actual) const {
return *expected == *actual;
}
ExpectedType expected;
};
template<typename ExpectedType>
struct Stringizer<EqualsPointerConstraint<ExpectedType>>
{
static std::string ToString(const EqualsPointerConstraint<ExpectedType>& constraint) {
std::ostringstream builder;
builder << "equal to pointer " << snowhouse::Stringize(constraint.expected);
return builder.str();
}
};
template<typename ExpectedType>
inline EqualsPointerConstraint<ExpectedType> EqualsPointer(const ExpectedType& expected) {
return EqualsPointerConstraint<ExpectedType>(expected);
}
}
namespace tree_sitter {
namespace lr {
std::ostream& operator<<(std::ostream &stream, const unordered_map<string, unordered_set<ParseAction>> &map);
std::ostream& operator<<(std::ostream &stream, const unordered_map<CharMatch, unordered_set<LexAction>> &map);
std::ostream& operator<<(std::ostream &stream, const unordered_map<string, unordered_set<lr::ParseAction>> &map);
std::ostream& operator<<(std::ostream &stream, const unordered_map<CharMatch, unordered_set<lr::LexAction>> &map);
}
}