From 5fbb8d971242c58ce81dc8f4c2d2f1cd44035613 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Fri, 30 Jul 2021 22:10:55 +0000 Subject: [PATCH] Bug 1722377 [wpt PR 29791] - URLPattern: Implement compareComponent() method., a=testonly Automatic update from web-platform-tests URLPattern: Implement compareComponent() method. This CL adds a prototype URLPattern.compareComponent() to provide a natural ordering to URLPattern pattern strings. This was based on feedback from routing framework authors and there is some discussion in: https://github.com/WICG/urlpattern/issues/61 The general algorithm is to compare the component patterns Part by Part. The PartType, Modifier, and text contents are compared for each Part, but group names are not considered. The end result is a mostly lexicographical ordering based on fixed text. Matching groups and modifiers are ordered such that more restrictive patterns are greater. Bug: 1232795 Change-Id: I8474cd7d7689e657c9c74c552ad630cdcdd86c95 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3052630 Commit-Queue: Ben Kelly Reviewed-by: Jeremy Roman Cr-Commit-Position: refs/heads/master@{#906025} -- wpt-commits: 2f4642d50f5c868a281cf3f20c8c7122e1f6b89a wpt-pr: 29791 --- .../urlpattern-compare-test-data.json | 116 ++++++++++++++++++ .../resources/urlpattern-compare-tests.js | 26 ++++ .../urlpattern/urlpattern-compare.any.js | 2 + .../urlpattern-compare.https.any.js | 2 + 4 files changed, 146 insertions(+) create mode 100644 testing/web-platform/tests/urlpattern/resources/urlpattern-compare-test-data.json create mode 100644 testing/web-platform/tests/urlpattern/resources/urlpattern-compare-tests.js create mode 100644 testing/web-platform/tests/urlpattern/urlpattern-compare.any.js create mode 100644 testing/web-platform/tests/urlpattern/urlpattern-compare.https.any.js diff --git a/testing/web-platform/tests/urlpattern/resources/urlpattern-compare-test-data.json b/testing/web-platform/tests/urlpattern/resources/urlpattern-compare-test-data.json new file mode 100644 index 0000000000000..54dd80a91a234 --- /dev/null +++ b/testing/web-platform/tests/urlpattern/resources/urlpattern-compare-test-data.json @@ -0,0 +1,116 @@ +[ + { + "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 + } +] diff --git a/testing/web-platform/tests/urlpattern/resources/urlpattern-compare-tests.js b/testing/web-platform/tests/urlpattern/resources/urlpattern-compare-tests.js new file mode 100644 index 0000000000000..b92b53ee45cd9 --- /dev/null +++ b/testing/web-platform/tests/urlpattern/resources/urlpattern-compare-tests.js @@ -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...'); diff --git a/testing/web-platform/tests/urlpattern/urlpattern-compare.any.js b/testing/web-platform/tests/urlpattern/urlpattern-compare.any.js new file mode 100644 index 0000000000000..8db38adfd41d4 --- /dev/null +++ b/testing/web-platform/tests/urlpattern/urlpattern-compare.any.js @@ -0,0 +1,2 @@ +// META: global=window,worker +// META: script=resources/urlpattern-compare-tests.js diff --git a/testing/web-platform/tests/urlpattern/urlpattern-compare.https.any.js b/testing/web-platform/tests/urlpattern/urlpattern-compare.https.any.js new file mode 100644 index 0000000000000..8db38adfd41d4 --- /dev/null +++ b/testing/web-platform/tests/urlpattern/urlpattern-compare.https.any.js @@ -0,0 +1,2 @@ +// META: global=window,worker +// META: script=resources/urlpattern-compare-tests.js