| | #include "tests.h"
|
| |
|
| | #include "peg-parser.h"
|
| |
|
| | #include <string>
|
| | #include <sstream>
|
| | #include <iomanip>
|
| | #include <cctype>
|
| |
|
| | static void assert_result_equal(testing & t, common_peg_parse_result_type expected, common_peg_parse_result_type actual) {
|
| | t.assert_equal(common_peg_parse_result_type_name(expected), common_peg_parse_result_type_name(actual));
|
| | }
|
| |
|
| | static std::string hex_dump(const std::string& str) {
|
| | std::ostringstream oss;
|
| | for (unsigned char c : str) {
|
| | if (std::isprint(c)) {
|
| | oss << c;
|
| | } else {
|
| | oss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c);
|
| | }
|
| | }
|
| | return oss.str();
|
| | }
|
| |
|
| | void test_unicode(testing &t) {
|
| | struct test_case {
|
| | std::string input;
|
| | std::string expected_text;
|
| | common_peg_parse_result_type expected_result;
|
| | };
|
| |
|
| | t.test("any", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {"Hello", "Hello", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("Caf\xC3\xA9"), std::string("Caf\xC3\xA9"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xF0\x9F\x9A\x80"), std::string("\xF0\x9F\x9A\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("Caf\xC3"), "Caf", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | {std::string("\xE4\xBD"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | {std::string("\xF0\x9F\x9A"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| |
|
| |
|
| | {std::string("\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("Hello\x80World"), "Hello", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.sequence({p.one_or_more(p.any()), p.end()});
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, true);
|
| | auto result = parser.parse(ctx);
|
| |
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| |
|
| | if (result.success() || result.need_more_input()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("char classes", [](testing &t) {
|
| | t.test("unicode range U+4E00-U+9FFF (CJK)", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("\xE4\xB8\x80"), std::string("\xE4\xB8\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xE4\xBD\xA0"), std::string("\xE4\xBD\xA0"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xE5\xA5\xBD"), std::string("\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xE9\xBF\xBF"), std::string("\xE9\xBF\xBF"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {"a", "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("\xE4\xB7\xBF"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("\xEA\x80\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("\xE4\xB8"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | {std::string("\xE5\xA5"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.sequence({p.chars(R"([\u4E00-\u9FFF])"), p.end()});
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, true);
|
| | auto result = parser.parse(ctx);
|
| |
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| |
|
| | if (result.success() || result.need_more_input()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("unicode range U+1F600-U+1F64F (emoticons)", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("\xF0\x9F\x98\x80"), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xF0\x9F\x98\x81"), std::string("\xF0\x9F\x98\x81"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xF0\x9F\x99\x8F"), std::string("\xF0\x9F\x99\x8F"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xF0\x9F\x97\xBF"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("\xF0\x9F\x99\x90"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("\xF0\x9F\x9A\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("\xF0\x9F\x98"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | {std::string("\xF0\x9F"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.sequence({p.chars(R"([\U0001F600-\U0001F64F])"), p.end()});
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, true);
|
| | auto result = parser.parse(ctx);
|
| |
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| |
|
| | if (result.success() || result.need_more_input()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("mixed unicode ranges", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("\xE4\xB8\x80"), std::string("\xE4\xB8\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | {std::string("\xE4\xBD\xA0"), std::string("\xE4\xBD\xA0"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xF0\x9F\x98\x80"), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {"5", "5", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {"a", "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | {std::string("\xF0\x9F\x9A\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("\xE4\xB8"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | {std::string("\xF0\x9F\x98"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.sequence({p.chars(R"([\u4E00-\u9FFF\U0001F600-\U0001F64F0-9])"), p.end()});
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, true);
|
| | auto result = parser.parse(ctx);
|
| |
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| |
|
| | if (result.success() || result.need_more_input()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| | });
|
| |
|
| | t.test("until parser", [](testing &t) {
|
| | t.test("ASCII delimiter with Unicode content", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("\xE4\xBD\xA0\xE5\xA5\xBD</tag>"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xF0\x9F\x98\x80</tag>"), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!</tag>"), std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.until("</tag>");
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, false);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| | if (result.success()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("incomplete UTF-8 at end", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("content\xF0\x9F\x98"), std::string("content"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| |
|
| |
|
| | {std::string("hello\xE4\xB8"), std::string("hello"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| |
|
| |
|
| | {std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.until("</tag>");
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, true);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| | if (result.success() || result.need_more_input()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("malformed UTF-8", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("Hello\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("Hello\x80World"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | };
|
| |
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.until("</tag>");
|
| | });
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | common_peg_parse_context ctx(tc.input, false);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| | });
|
| | }
|
| | });
|
| | });
|
| |
|
| | t.test("json_string parser", [](testing &t) {
|
| | t.test("valid UTF-8 characters", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {"Hello World\"", "Hello World", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("Caf\xC3\xA9\""), std::string("Caf\xC3\xA9"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xE4\xBD\xA0\xE5\xA5\xBD\""), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xF0\x9F\x98\x80\""), std::string("\xF0\x9F\x98\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!\""), std::string("Hello \xE4\xB8\x96\xE7\x95\x8C!"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | };
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.sequence({p.json_string_content(), p.literal("\"")});
|
| | });
|
| |
|
| | common_peg_parse_context ctx(tc.input, false);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| | if (result.success()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start - 1);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("incomplete UTF-8", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("Caf\xC3"), std::string("Caf"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| |
|
| |
|
| | {std::string("Hello\xE4\xB8"), std::string("Hello"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| |
|
| |
|
| | {std::string("Text\xF0\x9F\x98"), std::string("Text"), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| |
|
| |
|
| | {std::string("\xE4\xBD"), std::string(""), COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT},
|
| | };
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.json_string_content();
|
| | });
|
| |
|
| | common_peg_parse_context ctx(tc.input, true);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| | if (result.need_more_input()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("malformed UTF-8", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {std::string("Hello\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("Hello\x80World"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| |
|
| |
|
| | {std::string("\xC0\x80"), "", COMMON_PEG_PARSE_RESULT_FAIL},
|
| | };
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.json_string_content();
|
| | });
|
| |
|
| | common_peg_parse_context ctx(tc.input, false);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| | });
|
| | }
|
| | });
|
| |
|
| | t.test("escape sequences with UTF-8", [](testing &t) {
|
| | std::vector<test_case> test_cases {
|
| |
|
| | {"Hello\\u0041\"", "Hello\\u0041", COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xE4\xBD\xA0\\n\xE5\xA5\xBD\""), std::string("\xE4\xBD\xA0\\n\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| |
|
| |
|
| | {std::string("\xE4\xBD\xA0\\\"\xE5\xA5\xBD\""), std::string("\xE4\xBD\xA0\\\"\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS},
|
| | };
|
| |
|
| | for (size_t i = 0; i < test_cases.size(); i++) {
|
| | const auto & tc = test_cases[i];
|
| | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input);
|
| |
|
| | t.test(test_name, [&](testing &t) {
|
| | auto parser = build_peg_parser([](common_peg_parser_builder& p) {
|
| | return p.sequence({p.json_string_content(), p.literal("\"")});
|
| | });
|
| |
|
| | common_peg_parse_context ctx(tc.input, false);
|
| | auto result = parser.parse(ctx);
|
| |
|
| | assert_result_equal(t, tc.expected_result, result.type);
|
| |
|
| | if (result.success()) {
|
| | std::string matched = tc.input.substr(result.start, result.end - result.start - 1);
|
| | t.assert_equal(tc.expected_text, matched);
|
| | }
|
| | });
|
| | }
|
| | });
|
| | });
|
| | }
|
| |
|