Skip to content

Instantly share code, notes, and snippets.

@Ventero
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ventero/10564913 to your computer and use it in GitHub Desktop.
Save Ventero/10564913 to your computer and use it in GitHub Desktop.
Greasemonkey @match test suite
// Placed in the public domain, where applicable.
// Also licensed under CC0/WTFPL.
function tests() {
// Unloading the module doesn't always work, so changes might require a browser restart.
Components.utils.unload("resource://greasemonkey/third-party/MatchPattern.js");
var scope = Components.utils.import("resource://greasemonkey/third-party/MatchPattern.js");
var tests = [
// format: [pattern, url, shouldMatch, shouldThrow]
// patterns without wildcards
["http://example.org/", "http://example.org/", true],
["http://example.org/", "http://foo.example.org/", false],
["http://example.org/foo", "http://example.org/foo", true],
["http://example.org/foo", "http://example.org/", false],
["http://example.org/", "http://example.org/foo", false],
["http://example.org/foo", "http://example.org/bar", false],
["ftp://example.org/foo", "ftp://example.org/foo", true],
// semi-fixed path
["http://example.org/foo*", "http://example.org/foo", true],
["http://example.org/foo*", "http://foo.example.org/foo", false],
["http://example.org/foo*", "http://example.org/foobar", true],
["http://example.org/foo*", "http://example.org/foo/bar", true],
["http://example.org/foo*", "http://example.org/bar/foo", false],
// any hostname, fixed procotol + path
["http://*/foo", "http://example.org/foo", true],
["http://*/foo", "https://example.org/foo", false],
["http://*/foo", "http://example.com/foo", true],
["http://*/foo", "http://example.org/bar", false],
["http://*/foo", "http://foo.example.org/foo", true],
["http://*/foo", "http://foo.bar.example.org/foo", true],
// any hostname + path, fixed protocol
["http://*/*", "http://example.org/", true],
["http://*/*", "https://example.org/", false],
["http://*/*", "http://example.org/foo", true],
["http://*/*", "http://example.org/foo/bar", true],
["http://*/*", "http://foo.example.org/foo/bar", true],
["http://*/*", "http://foo.bar.example.org/foo/bar", true],
["http://*/*", "https://example.org/foo/bar", false],
["https://*/*", "https://example.org/foo/bar", true],
// any hostname, semi-fixed path, fixed protocol
["http://*/foo*", "http://example.org/foo", true],
["http://*/foo*", "http://foo.example.org/foo", true],
["http://*/foo*", "http://foo.bar.example.org/foo", true],
["http://*/foo*", "http://example.org/", false],
["http://*/foo*", "http://example.org/foo/bar", true],
["http://*/foo*", "https://example.org/foo", false],
["http://*/foo*", "http://example.org/bar", false],
["http://*/foo*", "http://foo.example.org/bar", false],
["http://*/foo*", "http://foo.example.org/bar/foo", false],
// any protocol, fixed hostname and path
["*://example.org/", "http://example.org/", true],
["*://example.org/", "https://example.org/", true],
["*://example.org/foo", "https://example.org/foo", true],
["*://example.org/foo", "https://example.org/bar", false],
// any protocol + hostname, fixed path
["*://*/foo", "https://example.org/foo", true],
["*://*/bar", "https://example.org/bar", true],
["*://*/foo", "http://example.org/bar", false],
["*://*/foo", "http://example.com/bar", false],
["*://*/foo", "http://foo.example.org/bar", false],
// any protocol + path, fixed hostname
["*://example.org/*", "http://example.org/", true],
["*://example.org/*", "http://example.org/foo", true],
["*://example.org/*", "http://example.org/foo/bar", true],
["*://example.org/*", "https://example.org/foo/bar", true],
["*://example.org/*", "https://example.com/foo/bar", false],
["*://example.org/*", "http://foo.example.com/", false],
// full wildcard
["*://*/*", "http://example.org/foo", true],
["*://*/*", "https://example.org/foo", true],
["*://*/*", "https://foo.example.org/foo/bar", true],
["<all_urls>", "http://example.org/foo", true],
["<all_urls>", "https://example.org/foo", true],
["<all_urls>", "https://foo.example.org/foo/bar", true],
// any/no subdomain
["http://*.example.org/", "http://example.org/", true],
["http://*.example.org/", "http://foo.example.org/", true],
["https://*.example.org/", "https://example.org/", true],
["https://*.example.org/", "https://foo.example.org/", true],
["http://*.example.org/", "https://example.org/", false],
["http://*.example.org/", "https://foo.example.org/", false],
["*://*.example.org/", "https://example.org/", true],
["*://*.example.org/", "https://foo.example.org/", true],
["*://*.example.org/", "https://foo.bar.example.org/", true],
["*://*.example.org/", "https://example.org/foo", false],
["*://*.example.org/*", "https://example.org/foo", true],
["*://*.example.org/", "http://example.org/", true],
["*://*.example.org/", "ftp://example.org/", true],
["*://*.example.org/", "http://fooexample.org/", false],
["*://*/foo", "file:///foo", true],
// invalid host for file:// URI
["*://*.example.org/", "file://example.org/", false],
// missing path
["http://example.org", "", false, true],
// * has to be followed by . or /
["http://*example.org", "", false, true],
["http://ex*ample.org", "", false, true],
// no * allowed in hostname after the initial *.
["http://*.*/", "", false, true],
["http://*.*./", "", false, true],
// at least one additional char required in the hostname after *.
["http://*./", "", false, true],
// pattern has to start with *, if it exists
["*://foo*.bar/", "", false, true],
// invalid scheme
["foo://example.org/", "", false, true],
// incorrect scheme separator
["http:/example.org/", "", false, true]
]
var failed = 0;
tests.forEach(function(testcase, idx) {
var reason = "";
var patternStr = testcase[0];
var uri = testcase[1];
var expected = testcase[2];
var shouldThrow = testcase[3];
try {
var match = new MatchPattern(patternStr);
var res = (match.doMatch(uri) === expected);
if (shouldThrow)
reason = patternStr + " should throw, but didn't";
else if (!res)
reason = patternStr + " should " + (expected ? "" : "not ") + "match " + uri;
} catch (e) {
if (!shouldThrow)
reason = patternStr + " threw exception: " + e.message + " at " + e.fileName + ":" + e.lineNumber;
}
if (reason) {
failed++;
console.log("Case %d failed: %s", idx, reason);
}
});
console.log("%d test cases ran, %d failed", tests.length, failed);
}
tests();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment