tree-sitter/spec/runtime/arithmetic_spec.cpp

65 lines
2.3 KiB
C++
Raw Normal View History

#include "spec_helper.h"
2014-01-08 18:35:16 -08:00
extern ts_parse_config ts_parse_config_arithmetic;
2013-12-17 13:14:41 -08:00
START_TEST
2013-12-17 13:14:41 -08:00
describe("arithmetic", []() {
ts_document *doc;
2014-01-09 13:31:30 -08:00
before_each([&]() {
doc = ts_document_make();
ts_document_set_parser(doc, ts_parse_config_arithmetic);
});
after_each([&]() {
ts_document_free(doc);
2014-01-09 13:31:30 -08:00
});
it("parses variables", [&]() {
ts_document_set_text(doc, "x");
AssertThat(string(ts_document_string(doc)), Equals(
"(expression (term (factor (variable))))"));
2014-01-09 13:31:30 -08:00
});
it("parses numbers", [&]() {
ts_document_set_text(doc, "5");
AssertThat(string(ts_document_string(doc)), Equals(
"(expression (term (factor (number))))"));
});
2014-01-09 13:31:30 -08:00
it("parses products of variables", [&]() {
ts_document_set_text(doc, "x + y");
AssertThat(string(ts_document_string(doc)), Equals(
2014-01-23 13:00:08 -08:00
"(expression (term (factor (variable))) (plus) (term (factor (variable))))"));
ts_document_set_text(doc, "x * y");
AssertThat(string(ts_document_string(doc)), Equals(
2014-01-23 13:00:08 -08:00
"(expression (term (factor (variable)) (times) (factor (variable))))"));
});
it("parses complex trees", [&]() {
ts_document_set_text(doc, "x * y + z * a");
AssertThat(string(ts_document_string(doc)), Equals(
2014-01-23 13:00:08 -08:00
"(expression (term (factor (variable)) (times) (factor (variable))) (plus) (term (factor (variable)) (times) (factor (variable))))"));
ts_document_set_text(doc, "x * (y + z)");
AssertThat(string(ts_document_string(doc)), Equals(
"(expression (term (factor (variable)) (times) (factor (expression (term (factor (variable))) (plus) (term (factor (variable)))))))"));
});
describe("error recovery", [&]() {
it("recovers from errors at the top level", [&]() {
ts_document_set_text(doc, "x * * y");
AssertThat(string(ts_document_string(doc)), Equals("(ERROR)"));
});
it("recovers from errors in parenthesized expressions", [&]() {
ts_document_set_text(doc, "x + (y * + z) * 5");
AssertThat(string(ts_document_string(doc)), Equals(
"(expression (term (factor (variable))) (plus) (term (factor (ERROR)) (times) (factor (number))))"));
});
});
});
END_TEST