| 24 | } |
| 25 | |
| 26 | void test_unicode(testing &t) { |
| 27 | struct test_case { |
| 28 | std::string input; |
| 29 | std::string expected_text; |
| 30 | common_peg_parse_result_type expected_result; |
| 31 | }; |
| 32 | |
| 33 | t.test("any", [](testing &t) { |
| 34 | std::vector<test_case> test_cases { |
| 35 | // Valid UTF-8 sequences |
| 36 | {"Hello", "Hello", COMMON_PEG_PARSE_RESULT_SUCCESS}, |
| 37 | {std::string("Caf\xC3\xA9"), std::string("Caf\xC3\xA9"), COMMON_PEG_PARSE_RESULT_SUCCESS}, |
| 38 | {std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), std::string("\xE4\xBD\xA0\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS}, |
| 39 | {std::string("\xF0\x9F\x9A\x80"), std::string("\xF0\x9F\x9A\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS}, |
| 40 | |
| 41 | // Incomplete UTF-8 sequences (partial bytes at end) |
| 42 | {std::string("Caf\xC3"), "Caf", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, |
| 43 | {std::string("\xE4\xBD"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, |
| 44 | {std::string("\xF0\x9F\x9A"), "", COMMON_PEG_PARSE_RESULT_NEED_MORE_INPUT}, |
| 45 | |
| 46 | // Invalid/malformed UTF-8 sequences |
| 47 | {std::string("\xFF\xFE"), "", COMMON_PEG_PARSE_RESULT_FAIL}, |
| 48 | {std::string("Hello\x80World"), "Hello", COMMON_PEG_PARSE_RESULT_FAIL}, |
| 49 | {std::string("\xC3\x28"), "", COMMON_PEG_PARSE_RESULT_FAIL}, |
| 50 | }; |
| 51 | |
| 52 | auto parser = build_peg_parser([](common_peg_parser_builder& p) { |
| 53 | return p.sequence({p.one_or_more(p.any()), p.end()}); |
| 54 | }); |
| 55 | |
| 56 | for (size_t i = 0; i < test_cases.size(); i++) { |
| 57 | const auto & tc = test_cases[i]; |
| 58 | std::string test_name = "case " + std::to_string(i) + ": " + hex_dump(tc.input); |
| 59 | |
| 60 | t.test(test_name, [&](testing &t) { |
| 61 | common_peg_parse_context ctx(tc.input, COMMON_PEG_PARSE_FLAG_LENIENT); |
| 62 | auto result = parser.parse(ctx); |
| 63 | |
| 64 | // Assert result type matches |
| 65 | assert_result_equal(t, tc.expected_result, result.type); |
| 66 | |
| 67 | // Assert matched text if success or need_more_input |
| 68 | if (result.success() || result.need_more_input()) { |
| 69 | std::string matched = tc.input.substr(result.start, result.end - result.start); |
| 70 | t.assert_equal(tc.expected_text, matched); |
| 71 | } |
| 72 | }); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | t.test("char classes", [](testing &t) { |
| 77 | t.test("unicode range U+4E00-U+9FFF (CJK)", [](testing &t) { |
| 78 | std::vector<test_case> test_cases { |
| 79 | // Within range - CJK Unified Ideographs |
| 80 | {std::string("\xE4\xB8\x80"), std::string("\xE4\xB8\x80"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+4E00 |
| 81 | {std::string("\xE4\xBD\xA0"), std::string("\xE4\xBD\xA0"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+4F60 |
| 82 | {std::string("\xE5\xA5\xBD"), std::string("\xE5\xA5\xBD"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+597D |
| 83 | {std::string("\xE9\xBF\xBF"), std::string("\xE9\xBF\xBF"), COMMON_PEG_PARSE_RESULT_SUCCESS}, // U+9FFF |
nothing calls this directly
no test coverage detected