Skip to content

Commit

Permalink
test: add WPT for URLPattern
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Lemire <[email protected]>
  • Loading branch information
anonrig and lemire committed Jan 30, 2025
1 parent 9659662 commit 31e323f
Show file tree
Hide file tree
Showing 15 changed files with 3,308 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Last update:
- resources: https://github.com/web-platform-tests/wpt/tree/1e140d63ec/resources
- streams: https://github.com/web-platform-tests/wpt/tree/bc9dcbbf1a/streams
- url: https://github.com/web-platform-tests/wpt/tree/a23788b77a/url
- urlpattern: https://github.com/web-platform-tests/wpt/tree/1b56d89a26/urlpattern
- user-timing: https://github.com/web-platform-tests/wpt/tree/5ae85bf826/user-timing
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/wpt/urlpattern/WEB_FEATURES.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
features:
- name: urlpattern
files: "**"
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
[
{
"component": "pathname",
"left": { "pathname": "/foo/a" },
"right": { "pathname": "/foo/b" },
"expected": -1
},
{
"component": "pathname",
"left": { "pathname": "/foo/b" },
"right": { "pathname": "/foo/bar" },
"expected": -1
},
{
"component": "pathname",
"left": { "pathname": "/foo/bar" },
"right": { "pathname": "/foo/:bar" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/" },
"right": { "pathname": "/foo/:bar" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/:bar" },
"right": { "pathname": "/foo/*" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/{bar}" },
"right": { "pathname": "/foo/(bar)" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/{bar}" },
"right": { "pathname": "/foo/{bar}+" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/{bar}+" },
"right": { "pathname": "/foo/{bar}?" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/{bar}?" },
"right": { "pathname": "/foo/{bar}*" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/(123)" },
"right": { "pathname": "/foo/(12)" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/:b" },
"right": { "pathname": "/foo/:a" },
"expected": 0
},
{
"component": "pathname",
"left": { "pathname": "*/foo" },
"right": { "pathname": "*" },
"expected": 1
},
{
"component": "port",
"left": { "port": "9" },
"right": { "port": "100" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "foo/:bar?/baz" },
"right": { "pathname": "foo/{:bar}?/baz" },
"expected": -1
},
{
"component": "pathname",
"left": { "pathname": "foo/:bar?/baz" },
"right": { "pathname": "foo{/:bar}?/baz" },
"expected": 0
},
{
"component": "pathname",
"left": { "pathname": "foo/:bar?/baz" },
"right": { "pathname": "fo{o/:bar}?/baz" },
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "foo/:bar?/baz" },
"right": { "pathname": "foo{/:bar/}?baz" },
"expected": -1
},
{
"component": "pathname",
"left": "https://a.example.com/b?a",
"right": "https://b.example.com/a?b",
"expected": 1
},
{
"component": "pathname",
"left": { "pathname": "/foo/{bar}/baz" },
"right": { "pathname": "/foo/bar/baz" },
"expected": 0
},
{
"component": "protocol",
"left": { "protocol": "a" },
"right": { "protocol": "b" },
"expected": -1
},
{
"component": "username",
"left": { "username": "a" },
"right": { "username": "b" },
"expected": -1
},
{
"component": "password",
"left": { "password": "a" },
"right": { "password": "b" },
"expected": -1
},
{
"component": "hostname",
"left": { "hostname": "a" },
"right": { "hostname": "b" },
"expected": -1
},
{
"component": "search",
"left": { "search": "a" },
"right": { "search": "b" },
"expected": -1
},
{
"component": "hash",
"left": { "hash": "a" },
"right": { "hash": "b" },
"expected": -1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function runTests(data) {
for (let entry of data) {
test(function() {
const left = new URLPattern(entry.left);
const right = new URLPattern(entry.right);

assert_equals(URLPattern.compareComponent(entry.component, left, right), entry.expected);

// We have to coerce to an integer here in order to avoid asserting
// that `+0` is `-0`.
const reverse_expected = ~~(entry.expected * -1);
assert_equals(URLPattern.compareComponent(entry.component, right, left), reverse_expected, "reverse order");

assert_equals(URLPattern.compareComponent(entry.component, left, left), 0, "left equality");
assert_equals(URLPattern.compareComponent(entry.component, right, right), 0, "right equality");
}, `Component: ${entry.component} ` +
`Left: ${JSON.stringify(entry.left)} ` +
`Right: ${JSON.stringify(entry.right)}`);
}
}

promise_test(async function() {
const response = await fetch('resources/urlpattern-compare-test-data.json');
const data = await response.json();
runTests(data);
}, 'Loading data...');
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test(() => {
assert_implements('hasRegExpGroups' in URLPattern.prototype, "hasRegExpGroups is not implemented");
assert_false(new URLPattern({}).hasRegExpGroups, "match-everything pattern");
for (let component of ['protocol', 'username', 'password', 'hostname', 'port', 'pathname', 'search', 'hash']) {
assert_false(new URLPattern({[component]: '*'}).hasRegExpGroups, `wildcard in ${component}`);
assert_false(new URLPattern({[component]: ':foo'}).hasRegExpGroups, `segment wildcard in ${component}`);
assert_false(new URLPattern({[component]: ':foo?'}).hasRegExpGroups, `optional segment wildcard in ${component}`);
assert_true(new URLPattern({[component]: ':foo(hi)'}).hasRegExpGroups, `named regexp group in ${component}`);
assert_true(new URLPattern({[component]: '(hi)'}).hasRegExpGroups, `anonymous regexp group in ${component}`);
if (component !== 'protocol' && component !== 'port') {
// These components are more narrow in what they accept in any case.
assert_false(new URLPattern({[component]: 'a-{:hello}-z-*-a'}).hasRegExpGroups, `wildcards mixed in with fixed text and wildcards in ${component}`);
assert_true(new URLPattern({[component]: 'a-(hi)-z-(lo)-a'}).hasRegExpGroups, `regexp groups mixed in with fixed text and wildcards in ${component}`);
}
}
assert_false(new URLPattern({pathname: '/a/:foo/:baz?/b/*'}).hasRegExpGroups, "complex pathname with no regexp");
assert_true(new URLPattern({pathname: '/a/:foo/:baz([a-z]+)?/b/*'}).hasRegExpGroups, "complex pathname with regexp");
}, '');
Loading

0 comments on commit 31e323f

Please sign in to comment.