Make tests run again, use tape instead of ava
This commit is contained in:
parent
9ac5a6bb12
commit
a7d180c4dc
4 changed files with 216 additions and 2164 deletions
17
package.json
17
package.json
|
@ -9,10 +9,9 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"test": "tsc && ts-node src/main.ts --ava src/*ts && ava dist/test/*js dist/src/*doctest.js",
|
"test": "ts-node src/main.ts --tape src/*ts && ts-node node_modules/.bin/tape test/*.ts src/*doctest*.ts | tap-diff",
|
||||||
"test:watch": "tsc --watch & ts-node src/main.ts --watch --ava src/*ts & tsc --watch & ava --watch dist/test dist/src/*doctest*",
|
"doctest:watch": "ts-node src/main.ts --tape --watch src/*.ts | while read file; do echo tape $file; ts-node $file | tap-diff; done",
|
||||||
"coverage": "tsc && nyc ava dist/test",
|
"prettier": "rm -v -f {src,test}/*doctest.ts && prettier --list-different --write src/*ts* test/*ts*"
|
||||||
"prettier": "prettier --list-different --write src/*ts* test/*ts*"
|
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -36,15 +35,15 @@
|
||||||
"typescript": "^2.7.1"
|
"typescript": "^2.7.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chokidar": "^1.7.4",
|
"@types/chokidar": "^1.7.5",
|
||||||
"@types/minimist": "^1.2.0",
|
"@types/minimist": "^1.2.0",
|
||||||
"@types/node": "^9.4.5",
|
"@types/node": "^9.4.6",
|
||||||
"@types/tape": "^4.2.31",
|
"@types/tape": "^4.2.31",
|
||||||
"ava": "^0.25.0",
|
|
||||||
"faucet": "^0.0.1",
|
"faucet": "^0.0.1",
|
||||||
"prettier": "^1.10.2",
|
"prettier": "^1.11.0",
|
||||||
"tap-diff": "^0.1.1",
|
"tap-diff": "^0.1.1",
|
||||||
"tape": "^4.8.0"
|
"tape": "^4.9.0",
|
||||||
|
"ts-node": "^5.0.0"
|
||||||
},
|
},
|
||||||
"prettier": {
|
"prettier": {
|
||||||
"printWidth": 100,
|
"printWidth": 100,
|
||||||
|
|
45
src/main.ts
45
src/main.ts
|
@ -32,8 +32,7 @@ export interface Comment {
|
||||||
|
|
||||||
export function Comments(s: string): Comment[] {
|
export function Comments(s: string): Comment[] {
|
||||||
const out: Comment[] = []
|
const out: Comment[] = []
|
||||||
function add_comment(c: string, context: string | null) {
|
function add_comment(c: string, context: string | null) {}
|
||||||
}
|
|
||||||
|
|
||||||
function traverse(node: ts.Node) {
|
function traverse(node: ts.Node) {
|
||||||
const jsdocs = (node as any).jsDoc || []
|
const jsdocs = (node as any).jsDoc || []
|
||||||
|
@ -49,7 +48,7 @@ export function Comments(s: string): Comment[] {
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// console.dir(node)
|
// console.dir(node)
|
||||||
context =ts.isConstructorDeclaration(node) ? 'constructor' : null
|
context = ts.isConstructorDeclaration(node) ? 'constructor' : null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jsdocs.forEach((doc: ts.JSDoc) => {
|
jsdocs.forEach((doc: ts.JSDoc) => {
|
||||||
|
@ -74,9 +73,7 @@ export function Comments(s: string): Comment[] {
|
||||||
is_doctest('// true') // => false
|
is_doctest('// true') // => false
|
||||||
|
|
||||||
*/
|
*/
|
||||||
const is_doctest = (s: string) => s.match( /\/\/[ \t]*=>/) != null
|
const is_doctest = (s: string) => s.match(/\/\/[ \t]*=>/) != null
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
||||||
|
@ -98,23 +95,21 @@ const doctest_rhs = (s: string) => s.match(/^\s*\/\/[ \t]*=>([^\n]*)/m)
|
||||||
export function extractScript(s: string): Script {
|
export function extractScript(s: string): Script {
|
||||||
const pwoc = ts.createPrinter({removeComments: true})
|
const pwoc = ts.createPrinter({removeComments: true})
|
||||||
const ast = ts.createSourceFile('_.ts', s, ts.ScriptTarget.Latest)
|
const ast = ts.createSourceFile('_.ts', s, ts.ScriptTarget.Latest)
|
||||||
return ast.statements.map(
|
return ast.statements.map((stmt, i): Statement | Equality => {
|
||||||
(stmt, i): Statement | Equality => {
|
|
||||||
if (ts.isExpressionStatement(stmt)) {
|
if (ts.isExpressionStatement(stmt)) {
|
||||||
const next = ast.statements[i+1] // zip with next
|
const next = ast.statements[i + 1] // zip with next
|
||||||
const [a, z] = next ? [next.pos, next.end] : [stmt.end, ast.end]
|
const [a, z] = next ? [next.pos, next.end] : [stmt.end, ast.end]
|
||||||
const after = ast.text.slice(a, z)
|
const after = ast.text.slice(a, z)
|
||||||
const m = doctest_rhs(after)
|
const m = doctest_rhs(after)
|
||||||
if (m && m[1]) {
|
if (m && m[1]) {
|
||||||
const lhs = pwoc.printNode(ts.EmitHint.Expression, stmt.expression, ast)
|
const lhs = pwoc.printNode(ts.EmitHint.Expression, stmt.expression, ast)
|
||||||
const rhs = m[1].trim()
|
const rhs = m[1].trim()
|
||||||
return { tag: '==', lhs, rhs }
|
return {tag: '==', lhs, rhs}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {tag: 'Statement', stmt: pwoc.printNode(ts.EmitHint.Unspecified, stmt, ast)}
|
return {tag: 'Statement', stmt: pwoc.printNode(ts.EmitHint.Unspecified, stmt, ast)}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractScripts(docstring: string): Script[] {
|
export function extractScripts(docstring: string): Script[] {
|
||||||
|
@ -143,26 +138,28 @@ export function showContext(c: Context) {
|
||||||
return show(c || 'doctest')
|
return show(c || 'doctest')
|
||||||
}
|
}
|
||||||
|
|
||||||
function showScript(script: Script, c: Context, before_end=(t: string) => '') {
|
function showScript(script: Script, c: Context, before_end = (t: string) => '') {
|
||||||
const t = `t`
|
const t = `t`
|
||||||
const body = script.map(s => {
|
const body = script
|
||||||
|
.map(s => {
|
||||||
if (s.tag == 'Statement') {
|
if (s.tag == 'Statement') {
|
||||||
return s.stmt
|
return s.stmt
|
||||||
} else {
|
} else {
|
||||||
return `${t}.deepEqual(${s.lhs}, ${s.rhs}, ${show(s.rhs)})`
|
return `${t}.deepEqual(${s.lhs}, ${s.rhs}, ${show(s.rhs)})`
|
||||||
}
|
}
|
||||||
}).join('\n')
|
})
|
||||||
|
.join('\n')
|
||||||
return `__test(${showContext(c)}, ${t} => {${body}${before_end(t)}})`
|
return `__test(${showContext(c)}, ${t} => {${body}${before_end(t)}})`
|
||||||
}
|
}
|
||||||
|
|
||||||
const ava: ShowScript = {
|
const ava: ShowScript = {
|
||||||
showImports: 'import {test as __test} from "ava"',
|
showImports: 'import {test as __test} from "ava"',
|
||||||
showScript: showScript
|
showScript: showScript,
|
||||||
}
|
}
|
||||||
|
|
||||||
const tape: ShowScript = {
|
const tape: ShowScript = {
|
||||||
showImports: 'import * as __test from "tape"',
|
showImports: 'import * as __test from "tape"',
|
||||||
showScript: (s, c) => showScript(s, c, t => `\n;${t}.end()`)
|
showScript: (s, c) => showScript(s, c, t => `\n;${t}.end()`),
|
||||||
}
|
}
|
||||||
|
|
||||||
const showScriptInstances = {ava, tape}
|
const showScriptInstances = {ava, tape}
|
||||||
|
@ -203,13 +200,19 @@ function main() {
|
||||||
const d = showScriptInstances[opts.tape == true ? 'tape' : 'ava']
|
const d = showScriptInstances[opts.tape == true ? 'tape' : 'ava']
|
||||||
const files = opts._
|
const files = opts._
|
||||||
if (files.length == 0) {
|
if (files.length == 0) {
|
||||||
console.error(`No files specified!
|
console.error(
|
||||||
|
`No files specified!
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
[-w|--watch] [--ava|--tape] files globs...
|
[-w|--watch] [--ava|--tape] files globs...
|
||||||
|
|
||||||
Your options were:`, opts)
|
Your options were:`,
|
||||||
|
opts,
|
||||||
|
`
|
||||||
|
From:`,
|
||||||
|
process.argv
|
||||||
|
)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
files.forEach(file => instrument(d, file))
|
files.forEach(file => instrument(d, file))
|
||||||
|
@ -219,5 +222,5 @@ function main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~process.argv[1].indexOf('test-worker.js') || main()
|
// if we are checking the doctests of this very file we don't need to run main
|
||||||
|
~process.argv[1].indexOf('doctest') || main()
|
||||||
|
|
11
test/test.ts
11
test/test.ts
|
@ -1,5 +1,5 @@
|
||||||
import * as main from '../src/main'
|
import * as main from '../src/main'
|
||||||
import test from 'ava'
|
import * as test from 'tape'
|
||||||
|
|
||||||
test('tests', t => {
|
test('tests', t => {
|
||||||
t.plan(1)
|
t.plan(1)
|
||||||
|
@ -35,8 +35,6 @@ test('tests', t => {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const c = (comment: string, context: string | null) => ({comment, context})
|
const c = (comment: string, context: string | null) => ({comment, context})
|
||||||
|
|
||||||
test('modules and namespace', t => {
|
test('modules and namespace', t => {
|
||||||
|
@ -108,12 +106,7 @@ test('class', t => {
|
||||||
p: Array<number>
|
p: Array<number>
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [
|
t.deepEqual(cs, [c('C ', 'C'), c('constructor ', 'constructor'), c('m ', 'm'), c('p ', 'p')])
|
||||||
c('C ', 'C'),
|
|
||||||
c('constructor ', 'constructor'),
|
|
||||||
c('m ', 'm'),
|
|
||||||
c('p ', 'p'),
|
|
||||||
])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('interface', t => {
|
test('interface', t => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue