Make tests run again, use tape instead of ava

This commit is contained in:
Dan Rosén 2018-02-28 10:07:09 +01:00
parent 9ac5a6bb12
commit a7d180c4dc
4 changed files with 216 additions and 2164 deletions

View file

@ -9,10 +9,9 @@
},
"scripts": {
"build": "tsc",
"test": "tsc && ts-node src/main.ts --ava src/*ts && ava dist/test/*js dist/src/*doctest.js",
"test:watch": "tsc --watch & ts-node src/main.ts --watch --ava src/*ts & tsc --watch & ava --watch dist/test dist/src/*doctest*",
"coverage": "tsc && nyc ava dist/test",
"prettier": "prettier --list-different --write src/*ts* test/*ts*"
"test": "ts-node src/main.ts --tape src/*ts && ts-node node_modules/.bin/tape test/*.ts src/*doctest*.ts | tap-diff",
"doctest:watch": "ts-node src/main.ts --tape --watch src/*.ts | while read file; do echo tape $file; ts-node $file | tap-diff; done",
"prettier": "rm -v -f {src,test}/*doctest.ts && prettier --list-different --write src/*ts* test/*ts*"
},
"repository": {
"type": "git",
@ -36,15 +35,15 @@
"typescript": "^2.7.1"
},
"devDependencies": {
"@types/chokidar": "^1.7.4",
"@types/chokidar": "^1.7.5",
"@types/minimist": "^1.2.0",
"@types/node": "^9.4.5",
"@types/node": "^9.4.6",
"@types/tape": "^4.2.31",
"ava": "^0.25.0",
"faucet": "^0.0.1",
"prettier": "^1.10.2",
"prettier": "^1.11.0",
"tap-diff": "^0.1.1",
"tape": "^4.8.0"
"tape": "^4.9.0",
"ts-node": "^5.0.0"
},
"prettier": {
"printWidth": 100,

View file

@ -32,8 +32,7 @@ export interface Comment {
export function Comments(s: string): 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) {
const jsdocs = (node as any).jsDoc || []
@ -42,15 +41,15 @@ export function Comments(s: string): Comment[] {
try {
context = (node as any).name.escapedText || null
} catch (e) {
try {
const decls = (node as any).declarationList.declarations
if (decls.length == 1) {
context = decls[0].name.escapedText || null
try {
const decls = (node as any).declarationList.declarations
if (decls.length == 1) {
context = decls[0].name.escapedText || null
}
} catch (e) {
// console.dir(node)
context = ts.isConstructorDeclaration(node) ? 'constructor' : null
}
} catch (e) {
// console.dir(node)
context =ts.isConstructorDeclaration(node) ? 'constructor' : null
}
}
jsdocs.forEach((doc: ts.JSDoc) => {
out.push({comment: doc.comment || '', context})
@ -74,9 +73,7 @@ export function Comments(s: string): Comment[] {
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 {
const pwoc = ts.createPrinter({removeComments: true})
const ast = ts.createSourceFile('_.ts', s, ts.ScriptTarget.Latest)
return ast.statements.map(
(stmt, i): Statement | Equality => {
if (ts.isExpressionStatement(stmt)) {
const next = ast.statements[i+1] // zip with next
const [a, z] = next ? [next.pos, next.end] : [stmt.end, ast.end]
const after = ast.text.slice(a, z)
const m = doctest_rhs(after)
if (m && m[1]) {
const lhs = pwoc.printNode(ts.EmitHint.Expression, stmt.expression, ast)
const rhs = m[1].trim()
return { tag: '==', lhs, rhs }
}
}
return {tag: 'Statement', stmt: pwoc.printNode(ts.EmitHint.Unspecified, stmt, ast)}
})
return ast.statements.map((stmt, i): Statement | Equality => {
if (ts.isExpressionStatement(stmt)) {
const next = ast.statements[i + 1] // zip with next
const [a, z] = next ? [next.pos, next.end] : [stmt.end, ast.end]
const after = ast.text.slice(a, z)
const m = doctest_rhs(after)
if (m && m[1]) {
const lhs = pwoc.printNode(ts.EmitHint.Expression, stmt.expression, ast)
const rhs = m[1].trim()
return {tag: '==', lhs, rhs}
}
}
return {tag: 'Statement', stmt: pwoc.printNode(ts.EmitHint.Unspecified, stmt, ast)}
})
}
export function extractScripts(docstring: string): Script[] {
@ -143,26 +138,28 @@ export function showContext(c: Context) {
return show(c || 'doctest')
}
function showScript(script: Script, c: Context, before_end=(t: string) => '') {
const t = `t`
const body = script.map(s => {
function showScript(script: Script, c: Context, before_end = (t: string) => '') {
const t = `t`
const body = script
.map(s => {
if (s.tag == 'Statement') {
return s.stmt
} else {
return `${t}.deepEqual(${s.lhs}, ${s.rhs}, ${show(s.rhs)})`
}
}).join('\n')
return `__test(${showContext(c)}, ${t} => {${body}${before_end(t)}})`
}
})
.join('\n')
return `__test(${showContext(c)}, ${t} => {${body}${before_end(t)}})`
}
const ava: ShowScript = {
showImports: 'import {test as __test} from "ava"',
showScript: showScript
showScript: showScript,
}
const tape: ShowScript = {
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}
@ -203,13 +200,19 @@ function main() {
const d = showScriptInstances[opts.tape == true ? 'tape' : 'ava']
const files = opts._
if (files.length == 0) {
console.error(`No files specified!
console.error(
`No files specified!
Usage:
[-w|--watch] [--ava|--tape] files globs...
Your options were:`, opts)
Your options were:`,
opts,
`
From:`,
process.argv
)
process.exit(1)
}
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()

View file

@ -1,5 +1,5 @@
import * as main from '../src/main'
import test from 'ava'
import * as test from 'tape'
test('tests', t => {
t.plan(1)
@ -35,8 +35,6 @@ test('tests', t => {
)
})
const c = (comment: string, context: string | null) => ({comment, context})
test('modules and namespace', t => {
@ -108,12 +106,7 @@ test('class', t => {
p: Array<number>
}
`)
t.deepEqual(cs, [
c('C ', 'C'),
c('constructor ', 'constructor'),
c('m ', 'm'),
c('p ', 'p'),
])
t.deepEqual(cs, [c('C ', 'C'), c('constructor ', 'constructor'), c('m ', 'm'), c('p ', 'p')])
})
test('interface', t => {

2267
yarn.lock

File diff suppressed because it is too large Load diff