Skip to content

Commit

Permalink
test: refactor how fixtures are structured
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Feb 7, 2020
1 parent bd7ae78 commit a7fb750
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
13 changes: 0 additions & 13 deletions test/example/example.spec.ts

This file was deleted.

File renamed without changes.
18 changes: 18 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import { describe } from 'mocha'
import * as fs from 'fs'
import * as path from 'path'
import testFixtures from './testFixtures'

const dirs = fs.readdirSync(__dirname)
for (const dir of dirs) {
if (fs.statSync(path.join(__dirname, dir)).isDirectory()) {
describe(dir, function() {
testFixtures({
glob: path.join(__dirname, dir, '*.ts'),
transform: require(`../src/${dir}`),
})
})
}
}
63 changes: 36 additions & 27 deletions test/testFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,23 @@ export default function textFixtures({
it(path.basename(fixturePath).replace(/\.js$/, ''), function() {
let source = input
const position = source.indexOf('// position')
let selectionStart
let selectionEnd
if (position >= 0) {
selectionStart = selectionEnd = position
source = source.replace(/^\s*\/\/ position[^\r\n]*(\r\n?|\n)/gm, '')
}
let selectionStart = source.indexOf('/* selectionStart */')
let selectionEnd
if (selectionStart >= 0) {
source = source.replace('/* selectionStart */', '')
selectionEnd = source.indexOf('/* selectionEnd */')
if (selectionEnd < 0) {
throw new Error(
'/* selectionEnd */ must be given if /* selectionStart */ is'
)
} else {
selectionStart = source.indexOf('/* selectionStart */')
if (selectionStart >= 0) {
source = source.replace('/* selectionStart */', '')
selectionEnd = source.indexOf('/* selectionEnd */')
if (selectionEnd < 0) {
throw new Error(
'/* selectionEnd */ must be given if /* selectionStart */ is'
)
}
source = source.replace('/* selectionEnd */', '')
}
source = source.replace('/* selectionEnd */', '')
}
if (selectionStart < 0) selectionStart = position
if (selectionEnd < 0) selectionEnd = position
Expand All @@ -78,23 +81,29 @@ export default function textFixtures({
const report = []
const parser = fixture.parser || defaultParser
const j = parser ? jscodeshift.withParser(parser) : jscodeshift
const result = transform(
{ path: file, source },
{
j,
jscodeshift: j,
stats: (name: string, quantity = 1): void => {
const total = stats[name]
stats[name] = total != null ? total + quantity : quantity
const doTransform = (): string | null | void | undefined =>
transform(
{ path: file, source },
{
j,
jscodeshift: j,
stats: (name: string, quantity = 1): void => {
const total = stats[name]
stats[name] = total != null ? total + quantity : quantity
},
report: (msg: string) => report.push(msg),
},
report: (msg: string) => report.push(msg),
},
options
)
if (!result) expect(result).to.equal(fixture.expected)
else expect(normalize(result)).to.equal(normalize(expected))
if (fixture.stats) expect(stats).to.deep.equal(fixture.stats)
if (fixture.report) expect(report).to.deep.equal(fixture.report)
options
)
if (fixture.expectedError) {
expect(doTransform).to.throw(fixture.expectedError)
} else {
const result = doTransform()
if (!result) expect(result).to.equal(fixture.expected)
else expect(normalize(result)).to.equal(normalize(expected))
if (fixture.stats) expect(stats).to.deep.equal(fixture.stats)
if (fixture.report) expect(report).to.deep.equal(fixture.report)
}
})
}
}

0 comments on commit a7fb750

Please sign in to comment.