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

183 lines
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"
2014-01-11 17:08:32 -08:00
using namespace rules;
using namespace build_tables;
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 sequences whose left sides can be blank", [&]() {
AssertThat(
rule_transitions(seq({
choice({
sym("x"),
blank(),
}),
seq({
sym("x"),
sym("y")
})
})), Equals(transition_map<Rule, Rule>({
{ sym("x"), choice({ seq({ sym("x"), sym("y") }), sym("y"), }) }
})));
});
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
})
}})));
});
2014-01-30 13:04:10 -08:00
describe("regression tests (somewhat redundant, should maybe be deleted later)", []() {
it("handles sequences that start with repeating characters", [&]() {
auto rule = seq({
choice({
repeat(character({ '"' }, false)),
blank(),
}),
character('"'),
});
AssertThat(rule_transitions(rule), Equals(transition_map<Rule, Rule>({
{ character({ '"' }, false), seq({
choice({
repeat(character({ '"' }, false)),
blank(),
}),
character('"'), }) },
{ character('"'), blank() },
})));
});
});
2013-12-28 23:26:20 -08:00
});
describe("checking if rules can be blank", [&]() {
it("handles sequences", [&]() {
rule_ptr rule = seq({
choice({
str("x"),
blank(),
}),
str("y"),
});
AssertThat(rule_can_be_blank(rule), Equals(false));
});
});
2013-12-28 23:26:20 -08:00
END_TEST