doctest-ts/test/test.ts

153 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as main from '../src/main'
import * as test from 'tape'
test('tests', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
t.deepEqual(
2018-02-14 16:14:36 +01:00
main.extractScripts(`*
foo // => 1
`),
[[{tag: '==', lhs: `foo`, rhs: `1`}]]
)
})
test('tests', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
t.deepEqual(
2018-02-14 16:14:36 +01:00
main.extractScripts(`*
a
2018-02-14 16:14:36 +01:00
b // => 1 + 2 + 3
c // => 1
d
2018-02-14 16:14:36 +01:00
*/`),
[
[
{tag: 'Statement', stmt: 'a;'},
2018-02-14 16:14:36 +01:00
{tag: '==', lhs: 'b', rhs: '1 + 2 + 3'},
{tag: '==', lhs: 'c', rhs: '1'},
{tag: 'Statement', stmt: 'd;'},
],
]
)
})
2018-02-14 16:14:36 +01:00
const c = (comment: string, context: string | null) => ({comment, context})
test('modules and namespace', t => {
t.plan(1)
const cs = main.Comments(`
2018-02-14 16:14:36 +01:00
/** m */
namespace m {}
/** ns */
namespace ns {}
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('m ', 'm'), c('ns ', 'ns')])
})
test('const', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** u */
const u = 1
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('u ', 'u')])
})
test('const object', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** k */
const k = {
/** a */
a: 1,
/** b */
b(x: string) { return x+x }
}
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('k ', 'k'), c('a ', 'a'), c('b ', 'b')])
})
test('object deconstruction', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** hello */
const {u, v} = {u: 1, v: 2}
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('hello ', null)])
})
test('function', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** v */
function v(s: string): number {
return s.length + 1
}
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('v ', 'v')])
})
test('class', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** C */
class C<A> {
/** constructor */
constructor() {}
/** m */
m(s: Array<number>): Array<string> {
}
/** p */
p: Array<number>
}
`)
t.deepEqual(cs, [c('C ', 'C'), c('constructor ', 'constructor'), c('m ', 'm'), c('p ', 'p')])
})
test('interface', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** I */
interface I<A> {
/** i */
i: A,
/** j */
j(a: A): string
}
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('I ', 'I'), c('i ', 'i'), c('j ', 'j')])
})
test('type', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
/** T */
type T = number
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('T ', 'T')])
})
test('anywhere', t => {
2018-02-14 16:14:36 +01:00
t.plan(1)
const cs = main.Comments(`
const $ = () => {
/** test1 */
const w = 1
/** test2 */
function f(x) {
return x * x
}
/** test3 */
return f(f(w))
}
`)
2018-02-14 16:14:36 +01:00
t.deepEqual(cs, [c('test1 ', 'w'), c('test2 ', 'f')])
})