2014-03-08 15:26:27 -08:00
|
|
|
#include "runtime_spec_helper.h"
|
2014-01-08 18:35:16 -08:00
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
extern ts_parse_config ts_parse_config_arithmetic;
|
2013-12-17 13:14:41 -08:00
|
|
|
|
2013-12-28 15:09:52 -08:00
|
|
|
START_TEST
|
2013-12-17 13:14:41 -08:00
|
|
|
|
2013-12-28 15:09:52 -08:00
|
|
|
describe("arithmetic", []() {
|
2014-02-27 00:38:08 -08:00
|
|
|
ts_document *doc;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-09 13:31:30 -08:00
|
|
|
before_each([&]() {
|
2014-02-27 00:38:08 -08:00
|
|
|
doc = ts_document_make();
|
|
|
|
|
ts_document_set_parser(doc, ts_parse_config_arithmetic);
|
|
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-27 00:38:08 -08:00
|
|
|
after_each([&]() {
|
|
|
|
|
ts_document_free(doc);
|
2014-01-09 13:31:30 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-09 13:31:30 -08:00
|
|
|
it("parses variables", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals(
|
2014-01-22 23:04:29 -08:00
|
|
|
"(expression (term (factor (variable))))"));
|
2014-01-09 13:31:30 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-30 18:54:39 -08:00
|
|
|
it("parses numbers", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "5");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals(
|
2014-01-30 18:54:39 -08:00
|
|
|
"(expression (term (factor (number))))"));
|
|
|
|
|
});
|
2014-01-09 13:31:30 -08:00
|
|
|
|
|
|
|
|
it("parses products of variables", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x + y");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals(
|
2014-01-23 13:00:08 -08:00
|
|
|
"(expression (term (factor (variable))) (plus) (term (factor (variable))))"));
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x * y");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals(
|
2014-01-23 13:00:08 -08:00
|
|
|
"(expression (term (factor (variable)) (times) (factor (variable))))"));
|
2014-01-22 23:04:29 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-22 23:04:29 -08:00
|
|
|
it("parses complex trees", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x * y + z * a");
|
2014-02-27 00:38:08 -08:00
|
|
|
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))))"));
|
2014-01-22 23:04:29 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x * (y + z)");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals(
|
2014-01-28 22:09:37 -08:00
|
|
|
"(expression (term (factor (variable)) (times) (factor (expression (term (factor (variable))) (plus) (term (factor (variable)))))))"));
|
2013-12-28 15:09:52 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-27 00:38:08 -08:00
|
|
|
describe("error recovery", [&]() {
|
|
|
|
|
it("recovers from errors at the top level", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x * * y");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(ERROR)"));
|
|
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-27 00:38:08 -08:00
|
|
|
it("recovers from errors in parenthesized expressions", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "x + (y * + z) * 5");
|
2014-02-27 00:38:08 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals(
|
|
|
|
|
"(expression (term (factor (variable))) (plus) (term (factor (ERROR)) (times) (factor (number))))"));
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-12-28 15:09:52 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
END_TEST
|