test: improve test coverage (#7113)

This commit is contained in:
Joshua Chen 2022-04-05 14:09:19 +08:00 committed by GitHub
parent 4194925da9
commit e610a4ac00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 235 additions and 61 deletions

View file

@ -74,7 +74,20 @@ bbbbb",
}
`;
exports[`parseLines respects language 1`] = `
exports[`parseLines respects language: html 1`] = `
{
"code": "aaaa
{/* highlight-next-line */}
bbbbb
dddd",
"highlightLines": [
0,
3,
],
}
`;
exports[`parseLines respects language: js 1`] = `
{
"code": "# highlight-next-line
aaaaa
@ -83,31 +96,47 @@ bbbbb",
}
`;
exports[`parseLines respects language 2`] = `
exports[`parseLines respects language: jsx 1`] = `
{
"code": "/* highlight-next-line */
aaaaa
bbbbb",
"highlightLines": [],
}
`;
exports[`parseLines respects language 3`] = `
{
"code": "// highlight-next-line
aaaa
/* highlight-next-line */
"code": "aaaa
bbbbb
ccccc
<!-- highlight-next-line -->
dddd",
"highlightLines": [
4,
0,
1,
],
}
`;
exports[`parseLines respects language 4`] = `
exports[`parseLines respects language: md 1`] = `
{
"code": "---
aaa: boo
---
aaaa
<div>
foo
</div>
bbbbb
dddd
\`\`\`js
// highlight-next-line
console.log(\\"preserved\\");
\`\`\`",
"highlightLines": [
1,
7,
11,
],
}
`;
exports[`parseLines respects language: none 1`] = `
{
"code": "aaaa
bbbbb
@ -121,3 +150,27 @@ dddd",
],
}
`;
exports[`parseLines respects language: py 1`] = `
{
"code": "/* highlight-next-line */
aaaaa
bbbbb",
"highlightLines": [],
}
`;
exports[`parseLines respects language: py 2`] = `
{
"code": "// highlight-next-line
aaaa
/* highlight-next-line */
bbbbb
ccccc
<!-- highlight-next-line -->
dddd",
"highlightLines": [
4,
],
}
`;

View file

@ -140,7 +140,7 @@ bbbbb`,
'',
'js',
),
).toMatchSnapshot();
).toMatchSnapshot('js');
expect(
parseLines(
`/* highlight-next-line */
@ -149,7 +149,7 @@ bbbbb`,
'',
'py',
),
).toMatchSnapshot();
).toMatchSnapshot('py');
expect(
parseLines(
`// highlight-next-line
@ -163,7 +163,7 @@ dddd`,
'',
'py',
),
).toMatchSnapshot();
).toMatchSnapshot('py');
expect(
parseLines(
`// highlight-next-line
@ -177,6 +177,57 @@ dddd`,
'',
'',
),
).toMatchSnapshot();
).toMatchSnapshot('none');
expect(
parseLines(
`// highlight-next-line
aaaa
{/* highlight-next-line */}
bbbbb
<!-- highlight-next-line -->
dddd`,
'',
'jsx',
),
).toMatchSnapshot('jsx');
expect(
parseLines(
`// highlight-next-line
aaaa
{/* highlight-next-line */}
bbbbb
<!-- highlight-next-line -->
dddd`,
'',
'html',
),
).toMatchSnapshot('html');
expect(
parseLines(
`---
# highlight-next-line
aaa: boo
---
aaaa
<div>
{/* highlight-next-line */}
foo
</div>
bbbbb
<!-- highlight-next-line -->
dddd
\`\`\`js
// highlight-next-line
console.log("preserved");
\`\`\`
`,
'',
'md',
),
).toMatchSnapshot('md');
});
});

View file

@ -141,29 +141,29 @@ export function parseLines(
for (let lineNumber = 0; lineNumber < lines.length; ) {
const line = lines[lineNumber]!;
const match = line.match(directiveRegex);
if (match !== null) {
const directive = match.slice(1).find((item) => item !== undefined);
switch (directive) {
case 'highlight-next-line':
highlightRange += `${lineNumber},`;
break;
case 'highlight-start':
highlightBlockStart = lineNumber;
break;
case 'highlight-end':
highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
break;
default:
break;
}
lines.splice(lineNumber, 1);
} else {
if (!match) {
// lines without directives are unchanged
lineNumber += 1;
continue;
}
const directive = match.slice(1).find((item) => item !== undefined);
switch (directive) {
case 'highlight-next-line':
highlightRange += `${lineNumber},`;
break;
case 'highlight-start':
highlightBlockStart = lineNumber;
break;
case 'highlight-end':
highlightRange += `${highlightBlockStart!}-${lineNumber - 1},`;
break;
default:
break;
}
lines.splice(lineNumber, 1);
}
const highlightLines = rangeParser(highlightRange);
code = lines.join('\n');