Skip to content

Commit

Permalink
tests: Add missing tests for complete test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Oct 12, 2020
1 parent 276a4b2 commit c49747f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 11 deletions.
98 changes: 96 additions & 2 deletions src/__fixtures__/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Selector } from "..";
import { Selector, Options } from "..";

export const tests: [string, Selector[][], string][] = [
export const tests: [
selector: string,
expected: Selector[][],
message: string,
options?: Options
][] = [
// Tag names
[
"div",
Expand Down Expand Up @@ -370,6 +375,20 @@ export const tests: [string, Selector[][], string][] = [
"ID starting with a dot",
],

// Pseudo elements
[
"::foo",
[
[
{
type: "pseudo-element",
name: "foo",
},
],
],
"pseudo-element",
],

// Pseudo selectors
[
":foo",
Expand Down Expand Up @@ -410,6 +429,19 @@ export const tests: [string, Selector[][], string][] = [
],
"pseudo selector with data",
],
[
':contains("(a((foo\\\\\\))))")',
[
[
{
type: "pseudo",
name: "contains",
data: "(a((foo\\))))",
},
],
],
"pseudo selector with escaped data",
],
[
":icontains('')",
[
Expand Down Expand Up @@ -698,4 +730,66 @@ export const tests: [string, Selector[][], string][] = [
],
"Long numeric escape (non-BMP)",
],

// Options
[
"fOo[baR]",
[
[
{
name: "fOo",
type: "tag",
},
{
action: "exists",
ignoreCase: false,
name: "baR",
type: "attribute",
value: "",
},
],
],
"XML mode",
{ xmlMode: true },
],
[
"fOo[baR]",
[
[
{
name: "foo",
type: "tag",
},
{
action: "exists",
ignoreCase: false,
name: "baR",
type: "attribute",
value: "",
},
],
],
"`lowerCaseAttributeNames` option",
{ lowerCaseAttributeNames: false },
],
[
"fOo[baR]",
[
[
{
name: "fOo",
type: "tag",
},
{
action: "exists",
ignoreCase: false,
name: "bar",
type: "attribute",
value: "",
},
],
],
"`lowerCaseTags` option",
{ lowerCaseTags: false },
],
];
9 changes: 7 additions & 2 deletions src/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { parse } from ".";
import { tests } from "./__fixtures__/tests";

describe("parse own tests", () => {
for (const [selector, expected, message] of tests) {
test(message, () => expect(parse(selector)).toStrictEqual(expected));
for (const [selector, expected, message, options] of tests) {
test(message, () =>
expect(parse(selector, options)).toStrictEqual(expected)
);
}
});

Expand Down Expand Up @@ -33,6 +35,9 @@ const broken = [
"input[name=foo.baz]",
"input[name=foo[baz]]",
':has("p")',
":has(p",
":foo(p()",
"#",
];

describe("Broken selectors", () => {
Expand Down
6 changes: 2 additions & 4 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,8 @@ function parseSelector(
stripWhitespace(1);
} else {
if (sawWS) {
if (tokens.length > 0) {
ensureNotTraversal();
tokens.push({ type: "descendant" });
}
ensureNotTraversal();
tokens.push({ type: "descendant" });
sawWS = false;
}

Expand Down
6 changes: 4 additions & 2 deletions src/stringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { tests } from "./__fixtures__/tests";

describe("Stringify & re-parse", () => {
describe("Own tests", () => {
for (const [selector, expected, message] of tests) {
for (const [selector, expected, message, options] of tests) {
test(`${message} (${selector})`, () => {
expect(parse(stringify(expected))).toStrictEqual(expected);
expect(parse(stringify(expected), options)).toStrictEqual(
expected
);
});
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const charsToEscape = new Set([
"]",
" ",
"\\",
"(",
")",
]);

/**
Expand Down Expand Up @@ -59,7 +61,7 @@ function stringifyToken(token: Selector): string {
case "pseudo":
if (token.data === null) return `:${escapeName(token.name)}`;
if (typeof token.data === "string") {
return `:${escapeName(token.name)}(${token.data})`;
return `:${escapeName(token.name)}(${escapeName(token.data)})`;
}
return `:${escapeName(token.name)}(${stringify(token.data)})`;

Expand Down

0 comments on commit c49747f

Please sign in to comment.