|
import { tagMatches, createFormatString } from "./"; |
|
import * as util from "util"; |
|
|
|
function printFailure(message: string) { |
|
console.error(`\x1b[31m❌ ${message}\x1b[0m`); |
|
} |
|
|
|
function printObjectFailure(label: string, value: any, expected: any) { |
|
console.error(`\n\x1b[31m❌ ${label}\x1b[0m.\n`); |
|
console.error("Expected", expected, "but got", value); |
|
console.error(); |
|
} |
|
|
|
function printPass(message: string) { |
|
console.log(`\x1b[32m✔ ${message}\x1b[0m`); |
|
} |
|
|
|
let failed = false; |
|
|
|
function assert(assertion: boolean, label: string) { |
|
if (!assertion) { |
|
failed = true; |
|
printFailure(label); |
|
} else { |
|
printPass(label); |
|
} |
|
} |
|
|
|
function assertDeepEquals(value: any, expected: any, label: string) { |
|
if (!util.isDeepStrictEqual(value, expected)) { |
|
failed = true; |
|
printObjectFailure(label, value, expected); |
|
} else { |
|
printPass(label); |
|
} |
|
} |
|
|
|
function testTagMatches() { |
|
assert( |
|
!tagMatches({ tag: "something", queryString: "another" }), |
|
"Should not be able to match" |
|
); |
|
assert( |
|
tagMatches({ tag: "something", queryString: "something" }), |
|
"Should be able to match by equality" |
|
); |
|
assert( |
|
tagMatches({ tag: "something", queryString: "something,another" }), |
|
"Should be able to match, given two queries" |
|
); |
|
assert( |
|
tagMatches({ tag: "something", queryString: "another,something" }), |
|
"Should be able to match, given two queries, regardless of order" |
|
); |
|
assert( |
|
tagMatches({ tag: "something", queryString: "another,something,foo" }), |
|
"Should be able to match, given three queries, regardless of order" |
|
); |
|
assert( |
|
tagMatches({ tag: "something:another", queryString: "something:another" }), |
|
"Should be able to match with colon" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another", |
|
queryString: "something:another,hello", |
|
}), |
|
"Should be able to match with colon, given multiple queries" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another", |
|
queryString: "hello,something:another", |
|
}), |
|
"Should be able to match with colon, given multiple queries, regardless of order" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another", |
|
queryString: "foo:bar,something,something:another", |
|
}), |
|
"Should be able to match with colon, given multiple queries, mixing and matching" |
|
); |
|
assert( |
|
!tagMatches({ |
|
tag: "something:another", |
|
queryString: "something:foo", |
|
}), |
|
"Should not match given prefix" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another", |
|
queryString: "something:*", |
|
}), |
|
"Glob matching should match" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another", |
|
queryString: "something:foo,something:*", |
|
}), |
|
"Useless glob example. Should still work" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another", |
|
queryString: "another:*,something:*", |
|
}), |
|
"Multiple globs can still work in the querystring" |
|
); |
|
assert( |
|
!tagMatches({ |
|
tag: "something:another", |
|
queryString: "another:*,foo:*", |
|
}), |
|
"If none of the globs match, then the globs do not match" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another:yetanother", |
|
queryString: "something:another:yetanother", |
|
}), |
|
"Should match tags with three components" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another:yetanother", |
|
queryString: "something:another:*", |
|
}), |
|
"Should match wildcard for three components" |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another:yetanother", |
|
queryString: "something:*", |
|
}), |
|
'Should match for everything prefixed with "something"' |
|
); |
|
assert( |
|
tagMatches({ |
|
tag: "something:another:yetanother:foobar", |
|
queryString: "*", |
|
}), |
|
"Should catch all" |
|
); |
|
} |
|
|
|
function testCreateFormatString() { |
|
assertDeepEquals( |
|
createFormatString(), |
|
{ params: [], formatString: "" }, |
|
"Empty" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("Some string"), |
|
{ params: ["Some string"], formatString: "%O" }, |
|
"Single string" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("Some string", "another string"), |
|
{ |
|
params: ["Some string", "another string"], |
|
formatString: "%O %O", |
|
}, |
|
"Two strings" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("%s", "some string"), |
|
{ |
|
params: ["some string"], |
|
formatString: "%s", |
|
}, |
|
"Format string" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("%s %s", "some", "string"), |
|
{ |
|
params: ["some", "string"], |
|
formatString: "%s %s", |
|
}, |
|
"Format string with two params" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("%s", "some", "string"), |
|
{ |
|
params: ["some", "string"], |
|
formatString: "%s %O", |
|
}, |
|
"Format string with one expected param, and an excessive param" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("Some preamble %s", "some", "string"), |
|
{ |
|
params: ["some", "string"], |
|
formatString: "Some preamble %s %O", |
|
}, |
|
"Format string with one expected param, the original string, and an excessive param" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("%s %s", "some", "string", true), |
|
{ |
|
params: ["some", "string", true], |
|
formatString: "%s %s %O", |
|
}, |
|
"Format string with two expected params, and an extra param" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString( |
|
"Some preamble %s an intermission %s", |
|
"some", |
|
"string", |
|
true |
|
), |
|
{ |
|
params: ["some", "string", true], |
|
formatString: "Some preamble %s an intermission %s %O", |
|
}, |
|
"Format string with two expected params, the original string, and an extra param" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString("%s %s", "some", "string", true, false, 10, 42), |
|
{ |
|
params: ["some", "string", true, false, 10, 42], |
|
formatString: "%s %s %O %O %O %O", |
|
}, |
|
"Format string with two expected params, and an extra param" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString( |
|
"Some preamble %s an intermission %s", |
|
"some", |
|
"string", |
|
true, |
|
false, |
|
10, |
|
42 |
|
), |
|
{ |
|
params: ["some", "string", true, false, 10, 42], |
|
formatString: "Some preamble %s an intermission %s %O %O %O %O", |
|
}, |
|
"Format string with two expected params, the original string, and an extra param" |
|
); |
|
|
|
assertDeepEquals( |
|
createFormatString(true, "hello", "world"), |
|
{ |
|
params: [true, "hello", "world"], |
|
formatString: "%o %o %o", |
|
}, |
|
"Format string with object at the start" |
|
); |
|
} |
|
|
|
const tests: { test: () => void; label: string }[] = [ |
|
{ test: testTagMatches, label: "tagMatches" }, |
|
{ test: testCreateFormatString, label: "createFormatString" }, |
|
]; |
|
|
|
for (const { test, label } of tests) { |
|
console.log(label); |
|
console.log(); |
|
|
|
test(); |
|
|
|
console.log(); |
|
} |
|
|
|
if (failed) { |
|
process.exit(1); |
|
} |