Clean up specs

This commit is contained in:
Max Brunsfeld 2014-01-11 17:08:32 -08:00
parent 8881214f0d
commit f342067293
7 changed files with 71 additions and 72 deletions

View file

@ -2,7 +2,8 @@
#include <functional>
#include "build_tables/perform.h"
using namespace tree_sitter::rules;
using build_tables::perform;
using namespace rules;
typedef unordered_set<ParseAction> parse_actions;
typedef unordered_set<LexAction> lex_actions;
@ -35,7 +36,7 @@ describe("building parse and lex tables", []() {
{ "right-paren", str(")") }
});
pair<ParseTable, LexTable> tables = build_tables::perform(grammar, lex_grammar);
pair<ParseTable, LexTable> tables = perform(grammar, lex_grammar);
ParseTable table = tables.first;
LexTable lex_table = tables.second;

View file

@ -2,125 +2,126 @@
#include "rule_transitions.h"
using build_tables::rule_transitions;
using namespace rules;
START_TEST
describe("rule 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("4");
rules::rule_ptr char1 = rules::character('a');
rule_ptr symbol1 = sym("1");
rule_ptr symbol2 = sym("2");
rule_ptr symbol3 = sym("3");
rule_ptr symbol4 = sym("4");
rule_ptr char1 = character('a');
it("handles symbols", [&]() {
AssertThat(
rule_transitions(symbol1),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::blank() }
Equals(transition_map<Rule, Rule>({
{ symbol1, blank() }
})));
});
it("handles characters", [&]() {
AssertThat(
rule_transitions(char1),
Equals(transition_map<rules::Rule, rules::Rule>({
{ char1, rules::blank() }
Equals(transition_map<Rule, Rule>({
{ char1, blank() }
})));
});
it("handles character classes", [&]() {
auto rule = rules::character(CharClassDigit);
auto rule = character(CharClassDigit);
AssertThat(
rule_transitions(rule),
Equals(transition_map<rules::Rule, rules::Rule>({
{ rule, rules::blank() }
Equals(transition_map<Rule, Rule>({
{ rule, blank() }
})));
});
it("handles choices", [&]() {
AssertThat(
rule_transitions(rules::choice({ symbol1, symbol2 })),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::blank() },
{ symbol2, rules::blank() }
rule_transitions(choice({ symbol1, symbol2 })),
Equals(transition_map<Rule, Rule>({
{ symbol1, blank() },
{ symbol2, blank() }
})));
});
it("handles sequences", [&]() {
AssertThat(
rule_transitions(rules::seq({ symbol1, symbol2 })),
Equals(transition_map<rules::Rule, rules::Rule>({
rule_transitions(seq({ symbol1, symbol2 })),
Equals(transition_map<Rule, Rule>({
{ symbol1, symbol2 }
})));
});
it("handles_long_sequences", [&]() {
AssertThat(
rule_transitions(rules::seq({
rule_transitions(seq({
symbol1,
symbol2,
symbol3,
symbol4
})),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::seq({ symbol2, symbol3, symbol4 }) }
Equals(transition_map<Rule, Rule>({
{ symbol1, seq({ symbol2, symbol3, symbol4 }) }
})));
});
it("handles choices with common starting symbols", [&]() {
AssertThat(
rule_transitions(
rules::choice({
rules::seq({ symbol1, symbol2 }),
rules::seq({ symbol1, symbol3 }) })),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::choice({ symbol2, symbol3 }) }
choice({
seq({ symbol1, symbol2 }),
seq({ symbol1, symbol3 }) })),
Equals(transition_map<Rule, Rule>({
{ symbol1, choice({ symbol2, symbol3 }) }
})));
});
it("handles strings", [&]() {
AssertThat(
rule_transitions(rules::str("bad")),
Equals(transition_map<rules::Rule, rules::Rule>({
{ rules::character('b'), rules::seq({ rules::character('a'), rules::character('d') })
rule_transitions(str("bad")),
Equals(transition_map<Rule, Rule>({
{ character('b'), seq({ character('a'), character('d') })
}
})));
});
it("handles patterns", [&]() {
AssertThat(
rule_transitions(rules::pattern("a|b")),
Equals(transition_map<rules::Rule, rules::Rule>({
{ rules::character('a'), rules::blank() },
{ rules::character('b'), rules::blank() }
rule_transitions(pattern("a|b")),
Equals(transition_map<Rule, Rule>({
{ character('a'), blank() },
{ character('b'), blank() }
})));
});
it("handles repeats", [&]() {
rules::rule_ptr repeat = rules::repeat(rules::str("ab"));
rule_ptr rule = repeat(str("ab"));
AssertThat(
rule_transitions(repeat),
Equals(transition_map<rules::Rule, rules::Rule>({
rule_transitions(rule),
Equals(transition_map<Rule, Rule>({
{
rules::character('a'),
rules::seq({
rules::character('b'),
rules::choice({
repeat,
rules::blank()
character('a'),
seq({
character('b'),
choice({
rule,
blank()
})
})
}})));
repeat = rules::repeat(rules::str("a"));
rule = repeat(str("a"));
AssertThat(
rule_transitions(repeat),
Equals(transition_map<rules::Rule, rules::Rule>({
rule_transitions(rule),
Equals(transition_map<Rule, Rule>({
{
rules::character('a'),
rules::choice({
repeat,
rules::blank()
character('a'),
choice({
rule,
blank()
})
}})));
});

View file

@ -3,8 +3,8 @@
START_TEST
using namespace tree_sitter::rules;
using tree_sitter::prepare_grammar::perform;
using prepare_grammar::perform;
using namespace rules;
describe("preparing a grammar", []() {
it("extracts character-based subtrees into a separate grammar", [&]() {

View file

@ -1,7 +1,7 @@
#include "spec_helper.h"
#include "rules.h"
using namespace tree_sitter::rules;
using namespace rules;
START_TEST

View file

@ -1,25 +1,23 @@
#include "spec_helper.h"
#include "rule_transitions.h"
using namespace rules;
START_TEST
describe("Rules", []() {
rules::rule_ptr symbol1 = rules::sym("1");
rules::rule_ptr symbol2 = rules::sym("2");
rules::rule_ptr symbol3 = rules::sym("3");
describe("constructing rules", []() {
rule_ptr symbol1 = sym("1");
rule_ptr symbol2 = sym("2");
rule_ptr symbol3 = sym("3");
describe("construction", [&]() {
it("constructs binary trees", [&]() {
AssertThat(
rules::seq({ symbol1, symbol2, symbol3 }),
EqualsPointer(
rules::seq({ rules::seq({ symbol1, symbol2 }), symbol3 })));
AssertThat(
rules::choice({ symbol1, symbol2, symbol3 }),
EqualsPointer(
rules::choice({ rules::choice({ symbol1, symbol2 }), symbol3 })));
});
it("constructs binary trees", [&]() {
AssertThat(
seq({ symbol1, symbol2, symbol3 }),
EqualsPointer(seq({ seq({ symbol1, symbol2 }), symbol3 })));
AssertThat(
choice({ symbol1, symbol2, symbol3 }),
EqualsPointer(choice({ choice({ symbol1, symbol2 }), symbol3 })));
});
});

View file

@ -70,7 +70,6 @@ namespace snowhouse {
inline EqualsPointerConstraint<ExpectedType> EqualsPointer(const ExpectedType& expected) {
return EqualsPointerConstraint<ExpectedType>(expected);
}
}
string src_dir();

View file

@ -2,7 +2,7 @@
#include "rules.h"
using namespace tree_sitter;
using namespace tree_sitter::rules;
using namespace rules;
namespace test_grammars {
Grammar arithmetic() {