2014-03-08 15:26:27 -08:00
|
|
|
#include "runtime_spec_helper.h"
|
2014-01-23 13:44:49 -08:00
|
|
|
|
2014-02-20 13:30:43 -08:00
|
|
|
extern ts_parse_config ts_parse_config_json;
|
2014-01-23 13:44:49 -08:00
|
|
|
|
|
|
|
|
START_TEST
|
|
|
|
|
|
|
|
|
|
describe("json", []() {
|
2014-02-24 18:42:54 -08:00
|
|
|
ts_document *doc;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-23 13:44:49 -08:00
|
|
|
before_each([&]() {
|
2014-02-24 18:42:54 -08:00
|
|
|
doc = ts_document_make();
|
|
|
|
|
ts_document_set_parser(doc, ts_parse_config_json);
|
2014-01-23 13:44:49 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-20 18:38:31 -08:00
|
|
|
after_each([&]() {
|
2014-02-24 18:42:54 -08:00
|
|
|
ts_document_free(doc);
|
2014-02-20 18:38:31 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-23 13:44:49 -08:00
|
|
|
it("parses strings", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "\"\"");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (string))"));
|
2014-02-12 18:35:29 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "\"simple-string\"");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (string))"));
|
2014-02-12 18:35:29 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "\"this is a \\\"string\\\" within a string\"");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (string))"));
|
2014-01-23 13:44:49 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-23 23:56:37 -08:00
|
|
|
it("parses objects", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "{}");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (object))"));
|
2014-02-15 15:43:32 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "{ \"key1\": 1 }");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (object (string) (value (number))))"));
|
2014-02-15 15:43:32 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "{\"key1\": 1, \"key2\": 2 }");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (object (string) (value (number)) (string) (value (number))))"));
|
2014-01-28 13:27:30 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-28 13:27:30 -08:00
|
|
|
it("parses arrays", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "[]");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (array))"));
|
2014-02-15 15:43:32 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "[5]");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (array (value (number))))"));
|
2014-02-15 15:43:32 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "[1, 2, 3]");
|
2014-02-24 18:42:54 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (array (value (number)) (value (number)) (value (number))))"));
|
2014-01-23 23:56:37 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-01 15:44:25 -08:00
|
|
|
describe("tracking the positions of AST nodes", [&]() {
|
|
|
|
|
it("records the widths and offsets of nodes", [&]() {
|
|
|
|
|
ts_document_set_input_string(doc, " [12, 5]");
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-01 15:44:25 -08:00
|
|
|
const ts_tree *tree = ts_document_tree(doc);
|
|
|
|
|
const ts_tree *array = ts_tree_children(tree)[0];
|
|
|
|
|
const ts_tree *number1 = ts_tree_children(array)[0];
|
|
|
|
|
const ts_tree *number2 = ts_tree_children(array)[1];
|
|
|
|
|
|
|
|
|
|
AssertThat(number1->offset, Equals(0));
|
|
|
|
|
AssertThat(number1->size, Equals(2));
|
|
|
|
|
|
|
|
|
|
AssertThat(number2->offset, Equals(1));
|
|
|
|
|
AssertThat(number2->size, Equals(1));
|
|
|
|
|
|
|
|
|
|
AssertThat(array->offset, Equals(2));
|
|
|
|
|
AssertThat(array->size, Equals(7));
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-01 15:44:25 -08:00
|
|
|
AssertThat(tree->offset, Equals(2));
|
|
|
|
|
AssertThat(tree->size, Equals(7));
|
|
|
|
|
});
|
|
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-25 18:32:17 -08:00
|
|
|
describe("errors", [&]() {
|
|
|
|
|
it("reports errors in the top-level node", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "[");
|
2014-02-25 18:32:17 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(ERROR)"));
|
2014-02-26 19:03:43 -08:00
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-26 22:58:45 -08:00
|
|
|
it("reports errors inside of arrays and objects", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "{ \"key1\": 1, 5 }");
|
2014-02-26 19:03:43 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (object (string) (value (number)) (ERROR)))"));
|
2014-02-26 22:58:45 -08:00
|
|
|
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "[1,,2]");
|
2014-02-26 22:58:45 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (array (value (number)) (ERROR) (value (number))))"));
|
|
|
|
|
});
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-26 22:58:45 -08:00
|
|
|
it("reports errors in nested objects", [&]() {
|
2014-02-27 13:30:11 -08:00
|
|
|
ts_document_set_input_string(doc, "{ \"key1\": { \"key2\": 1, 2 }, [, \"key3\": 3 }");
|
2014-02-26 22:58:45 -08:00
|
|
|
AssertThat(string(ts_document_string(doc)), Equals("(value (object (string) (value (object (string) (value (number)) (ERROR))) (ERROR) (string) (value (number))))"));
|
2014-02-25 18:32:17 -08:00
|
|
|
});
|
|
|
|
|
});
|
2014-01-23 13:44:49 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
END_TEST
|