Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp committed Feb 19, 2024
1 parent 5071139 commit dc7bd73
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
8 changes: 7 additions & 1 deletion samples/basic/test/each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ describe('testing', (a) => {
${[]} | ${'b'} | ${'b'}
${{}} | ${'b'} | ${'[object Object]b'}
${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
`('returns $expected when $a is added $b', ({ a, b, expected }) => {
`('table1: returns $expected when $a is added $b', ({ a, b, expected }) => {
expect(a + b).toBe(expected)
})
test.each`
a | b | expected
${{v: 1}} | ${{v: 1}} | ${2}
`('table2: returns $expected when $a.v is added $b.v', ({ a, b, expected }) => {
expect(a.v + b.v).toBe(expected)
})
test.each([
{ input: 1, add: 1, sum: 2 },
{ input: 2, add: 2, sum: 4 },
Expand Down
4 changes: 3 additions & 1 deletion src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ export class TestRunner {

if (testPattern) {
let argsValue = testPattern
if (isWindows && !customStartProcess)
if (isWindows && !customStartProcess) {
// Wrap the test pattern in quotes to ensure it is treated as a single argument
argsValue = `"${argsValue.replace(/"/g, '\\"')}"`
}

args.push('-t', argsValue)
}
Expand Down
42 changes: 27 additions & 15 deletions src/pure/testName.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
export function transformTestPattern({ testName, isEach }: { testName: string; isEach: boolean }): string {
// Vitest's test name pattern is a regex, so we need to escape any special regex characters.
// Additionally, when a custom start process is not used on Windows, child_process.spawn is used with shell: true.
// That disables automatic quoting/escaping of arguments, requiring us to manually perform that here as well.
let result = testName.replace(/[$^+?()[\]"]/g, '\\$&')

Check failure on line 1 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Too many blank lines at the beginning of file. Max of 0 allowed
function escapeRegExp(str: string) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\\\^\|\.\$]/g, "\\$&");

Check failure on line 3 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote

Check failure on line 3 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Extra semicolon
}

const kReplacers = new Map<string, string>([
['%i', '\\d+?'],
['%#', '\\d+?'],
['%d', '[\\d.eE+-]+?'],
['%f', '[\\d.eE+-]+?'],
['%s', '.+?'],
['%j', '.+?'],
['%o', '.+?'],
['%%', '%'],
])

export function transformTestPattern(
{ testName, isEach }:

Check failure on line 18 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Should have line breaks between items, in node FunctionDeclaration
{ testName: string; isEach: boolean }): string {

Check failure on line 19 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 2 spaces but found 4
// https://vitest.dev/api/#test-each
// replace vitest's table test placeholder and treat it as regex
let result = testName;

Check failure on line 22 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Extra semicolon
if (isEach) {
// replace \$value, \$obj.a with .+?
result = result.replace(/\\\$[a-zA-Z_.]+/g, '.+?')
// Integer or index of test case
result = result.replace(/%[i#]/g, () => '\\d+?')
// Float
result = result.replace(/%[df]/g, () => '[\\d.eE+-]+?')
// Arbitrary string
result = result.replace(/%[sjo]/g, () => '.+?')
// Single percent sign
result = result.replace(/%%/g, () => '%')
// Replace object access patterns ($value, $obj.a) with %s first
result = result.replace(/\$[a-zA-Z_.]+/g, '%s')
result = escapeRegExp(result)
// Replace percent placeholders with their respective regex
result = result.replace(/%[i#dfsjo%]/g, (m) => kReplacers.get(m) || m)

Check failure on line 28 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected parentheses around single function argument having a body with no curly braces
} else {

Check failure on line 29 in src/pure/testName.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Closing curly brace appears on the same line as the subsequent block
result = escapeRegExp(result)
}
return result
}
12 changes: 9 additions & 3 deletions test/pure/testName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ describe('testName', () => {
['$^+?()[]', '\\$\\^\\+\\?\\(\\)\\[\\]'],
['$value', '.+?'],
['$obj_name.a', '.+?'],
['%%', '%'],
['%d = %f', '[\\d.eE+-]+? = [\\d.eE+-]+?'],
['%j = %o', '.+? = .+?'],
['test %i', 'test \\d+?'],
])('isEach=true, %s', (input, expected) => {
])('isEach=true, value=%s', (input, expected) => {
expect(transformTestPattern({
testName: input,
isEach: true,
Expand All @@ -19,9 +22,12 @@ describe('testName', () => {
['test', 'test'],
['$^+?()[]', '\\$\\^\\+\\?\\(\\)\\[\\]'],
['$value', '\\$value'],
['$obj_name.a', '\\$obj_name.a'],
['$obj_name.a', '\\$obj_name\\.a'],
['%%', '%%'],
['%d = %f', '%d = %f'],
['%j = %o', '%j = %o'],
['test %i', 'test %i'],
])('isEach=false %s', (input, expected) => {
])('isEach=false, value=%s', (input, expected) => {
expect(transformTestPattern({
testName: input,
isEach: false,
Expand Down

0 comments on commit dc7bd73

Please sign in to comment.