tree-sitter/spec/compiler/build_tables/rule_transitions_spec.cpp

131 lines
3.5 KiB
C++
Raw Normal View History

2013-12-28 23:26:20 -08:00
#include "spec_helper.h"
2014-01-11 15:14:17 -08:00
#include "rule_transitions.h"
using build_tables::rule_transitions;
2014-01-11 17:08:32 -08:00
using namespace rules;
2013-12-28 23:26:20 -08:00
START_TEST
describe("rule transitions", []() {
2014-01-11 17:08:32 -08:00
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');
2013-12-28 23:26:20 -08:00
it("handles symbols", [&]() {
AssertThat(
2014-01-11 15:14:17 -08:00
rule_transitions(symbol1),
2014-01-11 17:08:32 -08:00
Equals(transition_map<Rule, Rule>({
{ symbol1, blank() }
2013-12-28 23:26:20 -08:00
})));
});
it("handles characters", [&]() {
AssertThat(
2014-01-11 15:14:17 -08:00
rule_transitions(char1),
2014-01-11 17:08:32 -08:00
Equals(transition_map<Rule, Rule>({
{ char1, blank() }
2013-12-28 23:26:20 -08:00
})));
});
it("handles character classes", [&]() {
2014-01-11 17:08:32 -08:00
auto rule = character(CharClassDigit);
2013-12-28 23:26:20 -08:00
AssertThat(
2014-01-11 15:14:17 -08:00
rule_transitions(rule),
2014-01-11 17:08:32 -08:00
Equals(transition_map<Rule, Rule>({
{ rule, blank() }
2013-12-28 23:26:20 -08:00
})));
});
it("handles choices", [&]() {
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(choice({ symbol1, symbol2 })),
Equals(transition_map<Rule, Rule>({
{ symbol1, blank() },
{ symbol2, blank() }
2013-12-28 23:26:20 -08:00
})));
});
it("handles sequences", [&]() {
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(seq({ symbol1, symbol2 })),
Equals(transition_map<Rule, Rule>({
2013-12-28 23:26:20 -08:00
{ symbol1, symbol2 }
})));
});
it("handles_long_sequences", [&]() {
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(seq({
2013-12-28 23:26:20 -08:00
symbol1,
symbol2,
symbol3,
symbol4
})),
2014-01-11 17:08:32 -08:00
Equals(transition_map<Rule, Rule>({
{ symbol1, seq({ symbol2, symbol3, symbol4 }) }
2013-12-28 23:26:20 -08:00
})));
});
it("handles choices with common starting symbols", [&]() {
AssertThat(
2014-01-11 15:14:17 -08:00
rule_transitions(
2014-01-11 17:08:32 -08:00
choice({
seq({ symbol1, symbol2 }),
seq({ symbol1, symbol3 }) })),
Equals(transition_map<Rule, Rule>({
{ symbol1, choice({ symbol2, symbol3 }) }
2013-12-28 23:26:20 -08:00
})));
});
it("handles strings", [&]() {
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(str("bad")),
Equals(transition_map<Rule, Rule>({
{ character('b'), seq({ character('a'), character('d') })
2013-12-28 23:26:20 -08:00
}
})));
});
it("handles patterns", [&]() {
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(pattern("a|b")),
Equals(transition_map<Rule, Rule>({
{ character('a'), blank() },
{ character('b'), blank() }
2013-12-28 23:26:20 -08:00
})));
});
it("handles repeats", [&]() {
2014-01-11 17:08:32 -08:00
rule_ptr rule = repeat(str("ab"));
2013-12-28 23:26:20 -08:00
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(rule),
Equals(transition_map<Rule, Rule>({
2013-12-28 23:26:20 -08:00
{
2014-01-11 17:08:32 -08:00
character('a'),
seq({
character('b'),
choice({
rule,
blank()
2013-12-28 23:26:20 -08:00
})
})
}})));
2014-01-11 17:08:32 -08:00
rule = repeat(str("a"));
2013-12-28 23:26:20 -08:00
AssertThat(
2014-01-11 17:08:32 -08:00
rule_transitions(rule),
Equals(transition_map<Rule, Rule>({
2013-12-28 23:26:20 -08:00
{
2014-01-11 17:08:32 -08:00
character('a'),
choice({
rule,
blank()
2013-12-28 23:26:20 -08:00
})
}})));
});
});
END_TEST