Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
wpt_tests.cpp
Go to the documentation of this file.
1#include "gtest/gtest.h"
2#include <cstdlib>
3#include <filesystem>
4#include <fstream>
5#include <iostream>
6#include <sstream>
7#include <string>
8
9#include "ada/idna/to_ascii.h"
10#include "simdjson.h"
11
12using namespace simdjson;
13
14class IdnaTestV2 : public ::testing::Test {
15 protected:
16 // Global function to get filename from environment or use default
17 static std::string GetTestV2Filename() {
18 const char* env_file = std::getenv("IDNA_TEST_V2_FILE");
19 return env_file ? std::string(env_file) : "fixtures/IdnaTestV2.json";
20 }
21};
22
23// Test for IdnaTestV2 JSON file
24TEST_F(IdnaTestV2, ToAscii) {
25 std::string filename = GetTestV2Filename();
26
27 // Skip test if file doesn't exist
28 if (!std::filesystem::exists(filename)) {
29 GTEST_SKIP() << "Test file not found: " << filename;
30 }
31
32 ondemand::parser parser;
33
34 // Load the JSON file
35 padded_string json;
36 try {
37 json = padded_string::load(filename);
38 std::cout << " loaded " << filename << " (" << json.size() << " kB)"
39 << std::endl;
40 } catch (const simdjson_error& e) {
41 FAIL() << "Failed to load JSON file: " << e.what();
42 }
43
44 ondemand::document doc;
45 try {
46 doc = parser.iterate(json);
47 } catch (const simdjson_error& e) {
48 FAIL() << "Failed to parse JSON: " << e.what();
49 }
50
51 // Process each test case
52 for (auto element : doc.get_array()) {
53 if (element.type() == ondemand::json_type::string) {
54 continue; // Skip string elements (comments)
55 }
56
57 ondemand::object object;
58 try {
59 object = element.get_object();
60 } catch (const simdjson_error& e) {
61 ADD_FAILURE() << "Failed to get object from array element: " << e.what();
62 continue;
63 }
64
65 auto json_string =
66 std::string(std::string_view(simdjson::to_json_string(object)));
67
68 // Process comment if available (for debugging)
69 ondemand::value comment;
70 if (object["comment"].get(comment) == simdjson::SUCCESS) {
71 std::string_view comment_string;
72 if (comment.get_string().get(comment_string) == simdjson::SUCCESS) {
73 // Doesn't see useful to print all comments:
74 // std::cout << " comment: " << comment_string << std::endl;
75 }
76 }
77
78 // Get input and process with IDNA
79 std::string_view input;
80 try {
81 input = object["input"].get_string();
82 } catch (const simdjson_error& e) {
83 // We assume that the input is valid Unicode.
84 // However, the test cases may contain invalid Unicode strings.
85 // We skip the test case if the input is invalid.
86#if UNICODE16
87 ADD_FAILURE()
88#else
89 std::cout
90#endif
91 << "Failed to get input string: " << e.what()
92 << " for test case: " << json_string;
93 continue;
94 }
95
96 // Create scoped trace for better test failures
97 SCOPED_TRACE("Test case: " + json_string);
98
99 // Process input with IDNA
100 std::string output = ada::idna::to_ascii(input);
102 output = "";
103 }
104
105 // Check expected output
106 auto expected_output = object["output"];
107
108 if (expected_output.is_null()) {
109 // For null expected output, we expect empty string result
110 if (!output.empty()) {
111 std::cout << "\n Test case: " + json_string +
112 "\n Got output: " + output
113 << std::endl;
114 EXPECT_TRUE(output.empty())
115 << "Expected null output but got: " << output;
116 }
117 } else if (expected_output.type() == ondemand::json_type::string) {
118 std::string_view str_expected_output;
119 try {
120 str_expected_output = expected_output.get_string();
121 } catch (const simdjson_error& e) {
122 ADD_FAILURE() << "Failed to get expected output string: " << e.what();
123 continue;
124 }
125
126 if (str_expected_output != output) {
127 std::cout << "\n Test case: " + json_string +
128 "\n Got output: " + output
129 << std::endl;
130 EXPECT_EQ(str_expected_output, output)
131 << "Output doesn't match expected value for input: " << input;
132 }
133 }
134 }
135}
136
137// WPT toascii.json — assorted IDNA / domain-to-ASCII cases that IdnaTestV2 may
138// not cover. Vendored from web-platform-tests (also used by Ada).
139// Mirrors Ada's unicode::to_ascii: to_ascii + forbidden-domain-code-point
140// filter. See
141// https://github.com/web-platform-tests/wpt/blob/master/url/resources/toascii.json
142TEST(ToAsciiWpt, ToAscii) {
143 const char* filename = "fixtures/toascii.json";
144 if (!std::filesystem::exists(filename)) {
145 GTEST_SKIP() << "Test file not found: " << filename;
146 }
147
148 ondemand::parser parser;
149 padded_string json;
150 try {
151 json = padded_string::load(filename);
152 std::cout << " loaded " << filename << " (" << json.size() << " B)"
153 << std::endl;
154 } catch (const simdjson_error& e) {
155 FAIL() << "Failed to load JSON file: " << e.what();
156 }
157
158 ondemand::document doc;
159 try {
160 doc = parser.iterate(json);
161 } catch (const simdjson_error& e) {
162 FAIL() << "Failed to parse JSON: " << e.what();
163 }
164
165 size_t cases = 0;
166 for (auto element : doc.get_array()) {
167 if (element.type() == ondemand::json_type::string) {
168 continue; // comments
169 }
170
171 ondemand::object object;
172 try {
173 object = element.get_object();
174 } catch (const simdjson_error& e) {
175 ADD_FAILURE() << "Failed to get object: " << e.what();
176 continue;
177 }
178
179 auto json_string =
180 std::string(std::string_view(simdjson::to_json_string(object)));
181
182 std::string_view input;
183 try {
184 input = object["input"].get_string();
185 } catch (const simdjson_error& e) {
186 // Invalid Unicode in the JSON string — skip (Ada does the same).
187 std::cout << " skip invalid input encoding: " << e.what()
188 << " for: " << json_string << std::endl;
189 continue;
190 }
191
192 SCOPED_TRACE("Test case: " + json_string);
193
194 std::string output = ada::idna::to_ascii(input);
196 output = "";
197 }
198
199 auto expected_output = object["output"];
200 ++cases;
201
202 if (expected_output.is_null()) {
203 EXPECT_TRUE(output.empty())
204 << "Expected failure (null) for input but got: " << output
205 << " case: " << json_string;
206 } else if (expected_output.type() == ondemand::json_type::string) {
207 std::string_view str_expected_output;
208 try {
209 str_expected_output = expected_output.get_string();
210 } catch (const simdjson_error& e) {
211 ADD_FAILURE() << "Failed to get expected output string: " << e.what();
212 continue;
213 }
214 EXPECT_EQ(output, str_expected_output)
215 << "toascii mismatch for case: " << json_string;
216
217 // Idempotency on successful results: applying to_ascii again is stable.
218 if (!output.empty()) {
219 std::string again = ada::idna::to_ascii(output);
221 again = "";
222 }
223 EXPECT_EQ(again, output) << "to_ascii not idempotent for: " << output;
224 }
225 }
226 }
227 EXPECT_GT(cases, 50u) << "expected toascii.json to contribute many cases";
228 std::cout << " ran " << cases << " toascii.json cases" << std::endl;
229}
static std::string GetTestV2Filename()
Definition wpt_tests.cpp:17
std::string to_ascii(std::string_view ut8_string)
bool contains_forbidden_domain_code_point(std::string_view ascii_string)
TEST(ToAsciiWpt, ToAscii)
TEST_F(IdnaTestV2, ToAscii)
Definition wpt_tests.cpp:24