tree-sitter/spec/compiler/rules/rules_spec.cpp

146 lines
4.9 KiB
C++
Raw Normal View History

2013-11-07 13:24:01 -08:00
#include "spec_helper.h"
2013-12-18 20:58:05 -08:00
#include "transitions.h"
2013-11-05 22:15:19 -08:00
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");
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 }),
EqualsPointer(
rules::seq({ rules::seq({ symbol1, symbol2 }), symbol3 })));
AssertThat(
rules::choice({ symbol1, symbol2, symbol3 }),
EqualsPointer(
rules::choice({ rules::choice({ symbol1, symbol2 }), symbol3 })));
});
});
describe("transitions", [&]() {
it("handles symbols", [&]() {
2013-11-05 22:15:19 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(symbol1),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::blank() }
})));
});
it("handles characters", [&]() {
2013-11-07 08:22:56 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(char1),
Equals(transition_map<rules::Rule, rules::Rule>({
{ char1, rules::blank() }
})));
});
2013-11-05 22:15:19 -08:00
it("handles character classes", [&]() {
auto rule = rules::character(CharClassDigit);
2013-11-20 19:00:20 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(rule),
Equals(transition_map<rules::Rule, rules::Rule>({
2013-11-20 19:00:20 -08:00
{ rule, rules::blank() }
})));
});
2013-11-20 19:00:20 -08:00
it("handles choices", [&]() {
2013-11-05 22:15:19 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(rules::choice({ symbol1, symbol2 })),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::blank() },
{ symbol2, rules::blank() }
})));
});
2013-11-05 22:15:19 -08:00
it("handles sequences", [&]() {
2013-11-05 22:15:19 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(rules::seq({ symbol1, symbol2 })),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, symbol2 }
})));
});
it("handles_long_sequences", [&]() {
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(rules::seq({
symbol1,
symbol2,
symbol3,
symbol4
2013-12-18 20:58:05 -08:00
})),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::seq({ symbol2, symbol3, symbol4 }) }
})));
});
it("handles choices with common starting symbols", [&]() {
2013-11-05 22:15:19 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(
rules::choice({
rules::seq({ symbol1, symbol2 }),
rules::seq({ symbol1, symbol3 }) })),
Equals(transition_map<rules::Rule, rules::Rule>({
{ symbol1, rules::choice({ symbol2, symbol3 }) }
})));
});
2013-11-07 18:30:00 -08:00
it("handles strings", [&]() {
2013-11-07 18:30:00 -08:00
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(rules::str("bad")),
Equals(transition_map<rules::Rule, rules::Rule>({
{
rules::character('b'),
rules::seq({ rules::character('a'), rules::character('d') })
}
})));
});
it("handles patterns", [&]() {
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(rules::pattern("a|b")),
Equals(transition_map<rules::Rule, rules::Rule>({
{ rules::character('a'), rules::blank() },
{ rules::character('b'), rules::blank() }
})));
});
2013-11-15 08:46:45 -08:00
it("handles repeats", [&]() {
2013-11-15 08:46:45 -08:00
rules::rule_ptr repeat = rules::repeat(rules::str("ab"));
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(repeat),
Equals(transition_map<rules::Rule, rules::Rule>({
2013-11-15 08:46:45 -08:00
{
rules::character('a'),
rules::seq({
rules::character('b'),
rules::choice({
repeat,
rules::blank()
})
})
}})));
2013-11-20 19:00:20 -08:00
repeat = rules::repeat(rules::str("a"));
AssertThat(
2013-12-18 20:58:05 -08:00
rules::transitions(repeat),
Equals(transition_map<rules::Rule, rules::Rule>({
2013-11-20 19:00:20 -08:00
{
rules::character('a'),
rules::choice({
repeat,
rules::blank()
})
}})));
});
});
});
END_TEST