1// These are defined by the test:
2// errors (boolean)
3// encoder (function)
4// ranges (array)
5// expect (function)
6
7function encode(input, expected, desc) {
8    // tests whether a Unicode character is converted to an equivalent byte sequence by href
9    // input: a Unicode character
10    // expected: expected byte sequence
11    // desc: what's being tested
12    subsetTest(test, function() {
13        var a = document.createElement("a"); // <a> uses document encoding for URL's query
14        a.href = "https://example.com/?" + input;
15        result = a.search.substr(1); // remove leading "?"
16        assert_equals(normalizeStr(result), normalizeStr(expected));
17    }, desc);
18}
19
20// set up a simple array of unicode codepoints that are not encoded
21var codepoints = [];
22
23for (var range of ranges) {
24    for (var i = range[0]; i < range[1]; i++) {
25        result = encoder(String.fromCodePoint(i));
26        var success = !!result;
27        if (errors) {
28          success = !success;
29        }
30        if (success) {
31            var item = {};
32            codepoints.push(item);
33            item.cp = i;
34            item.expected = expect(result, i);
35            item.desc = range[2] ? range[2] + " " : "";
36        }
37    }
38}
39
40// run the tests
41for (var x = 0; x < codepoints.length; x++) {
42    encode(
43        String.fromCodePoint(codepoints[x].cp),
44        codepoints[x].expected,
45        codepoints[x].desc +
46            " U+" +
47            codepoints[x].cp.toString(16).toUpperCase() +
48            " " +
49            String.fromCodePoint(codepoints[x].cp) +
50            " " +
51            codepoints[x].expected
52    );
53}
54
55// NOTES
56// this test relies on support for String.fromCodePoint, which appears to be supported by major desktop browsers
57// the tests exclude ASCII characters
58