Fix minor issues
This commit is contained in:
parent
143ac3399c
commit
aed02b3e27
1 changed files with 24 additions and 15 deletions
39
src/main.ts
39
src/main.ts
|
@ -143,10 +143,8 @@ export function showContext(c: Context) {
|
||||||
return show(c || 'doctest')
|
return show(c || 'doctest')
|
||||||
}
|
}
|
||||||
|
|
||||||
const ava: ShowScript = {
|
function showScript(script: Script, c: Context, before_end=(t: string) => '') {
|
||||||
showImports: 'import {test as __test} from "ava"',
|
const t = `t`
|
||||||
showScript(script, c) {
|
|
||||||
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
|
||||||
|
@ -154,20 +152,24 @@ const ava: ShowScript = {
|
||||||
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}})`
|
return `__test(${showContext(c)}, ${t} => {${body}${before_end(t)}})`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const ava: ShowScript = {
|
||||||
|
showImports: 'import {test as __test} from "ava"',
|
||||||
|
showScript: showScript
|
||||||
}
|
}
|
||||||
|
|
||||||
const tape: ShowScript = {
|
const tape: ShowScript = {
|
||||||
showImports: 'import * as __test from "tape"',
|
showImports: 'import * as __test from "tape"',
|
||||||
showScript: ava.showScript
|
showScript: (s, c) => showScript(s, c, t => `\n;${t}.end()`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const showScriptInstances = {ava, tape}
|
const showScriptInstances = {ava, tape}
|
||||||
|
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
|
|
||||||
function instrument(d: ShowScript, file: string): void {
|
function instrument(d: ShowScript, file: string, mode?: 'watch'): void {
|
||||||
const {base, ext, ...u} = path.parse(file)
|
const {base, ext, ...u} = path.parse(file)
|
||||||
if (base.includes('doctest')) {
|
if (base.includes('doctest')) {
|
||||||
return
|
return
|
||||||
|
@ -175,18 +177,25 @@ function instrument(d: ShowScript, file: string): void {
|
||||||
const buffer = fs.readFileSync(file, {encoding: 'utf8'})
|
const buffer = fs.readFileSync(file, {encoding: 'utf8'})
|
||||||
const tests = Doctests(d, buffer)
|
const tests = Doctests(d, buffer)
|
||||||
const outfile = path.format({...u, ext: '.doctest' + ext})
|
const outfile = path.format({...u, ext: '.doctest' + ext})
|
||||||
console.log('Writing', outfile)
|
if (tests.length == 0) {
|
||||||
fs.writeFileSync(outfile, buffer + '\n' + d.showImports + '\n' + tests)
|
console.error('No doctests found in', file)
|
||||||
|
} else {
|
||||||
|
console.error('Writing', outfile)
|
||||||
|
if (mode == 'watch') {
|
||||||
|
console.log(outfile)
|
||||||
|
}
|
||||||
|
fs.writeFileSync(outfile, buffer + '\n' + d.showImports + '\n' + tests)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Doctests(d: ShowScript, buffer: string): string {
|
function Doctests(d: ShowScript, buffer: string): string[] {
|
||||||
let s = ''
|
const out: string[] = []
|
||||||
for (const c of Comments(buffer)) {
|
for (const c of Comments(buffer)) {
|
||||||
for (const script of extractScripts(c.comment)) {
|
for (const script of extractScripts(c.comment)) {
|
||||||
s += '\n' + d.showScript(script, c.context)
|
out.push(d.showScript(script, c.context))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
|
@ -205,8 +214,8 @@ function main() {
|
||||||
}
|
}
|
||||||
files.forEach(file => instrument(d, file))
|
files.forEach(file => instrument(d, file))
|
||||||
if (opts.w == true || opts.watch == true) {
|
if (opts.w == true || opts.watch == true) {
|
||||||
const watcher = chokidar.watch(files)
|
const watcher = chokidar.watch(files, {ignored: '*.doctest.*'})
|
||||||
watcher.on('change', file => instrument(d, file))
|
watcher.on('change', file => instrument(d, file, 'watch'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue