| 1 |
import { test } from 'tap' |
| 2 |
import { parse, stringify } from 'yaml' |
| 3 |
|
| 4 |
import { classTag, functionTag } from '.' |
| 5 |
|
| 6 |
test('parse valid', t => { |
| 7 |
const res: { new (): any } = parse( |
| 8 |
`!class |- |
| 9 |
class X extends Y { |
| 10 |
constructor() { |
| 11 |
this.a = 1 |
| 12 |
throw new Error('bad idea to actually run this') |
| 13 |
} |
| 14 |
z = 3 |
| 15 |
} |
| 16 |
`, |
| 17 |
{ customTags: [classTag, functionTag] } |
| 18 |
) |
| 19 |
t.type(res, 'function') |
| 20 |
t.equal( |
| 21 |
res.toString(), |
| 22 |
`class X extends Y { |
| 23 |
constructor() { |
| 24 |
this.a = 1 |
| 25 |
throw new Error('bad idea to actually run this') |
| 26 |
} |
| 27 |
z = 3 |
| 28 |
}` |
| 29 |
) |
| 30 |
t.equal(res.name, 'X') |
| 31 |
const inst = new res() |
| 32 |
t.notMatch( |
| 33 |
inst, |
| 34 |
{ |
| 35 |
a: Number, |
| 36 |
z: Number |
| 37 |
}, |
| 38 |
'does not actually run the code specified' |
| 39 |
) |
| 40 |
t.end() |
| 41 |
}) |
| 42 |
|
| 43 |
test('unnamed class', t => { |
| 44 |
// it's actually kind of tricky to get a class that V8 won't |
| 45 |
// assign *some* sort of intelligible name to. It has to never be |
| 46 |
// assigned to any variable, or directly pass as an argument to |
| 47 |
// a named function at its point of creation, hence this line noise. |
| 48 |
const res = stringify((() => class {})(), { |
| 49 |
customTags: [classTag, functionTag] |
| 50 |
}) |
| 51 |
t.equal( |
| 52 |
res, |
| 53 |
`!class |- |
| 54 |
class {\n } |
| 55 |
` |
| 56 |
) |
| 57 |
t.end() |
| 58 |
}) |
| 59 |
|
| 60 |
test('parse completely empty value', t => { |
| 61 |
const src = `!class |-\n` |
| 62 |
const res: { new (): any } = parse(src, { customTags: [classTag] }) |
| 63 |
t.type(res, 'function') |
| 64 |
t.equal(res.name, undefined) |
| 65 |
t.equal(res.toString(), '') |
| 66 |
t.end() |
| 67 |
}) |
| 68 |
|
| 69 |
class Foo extends Boolean {} |
| 70 |
test('stringify a class', t => { |
| 71 |
const res = stringify(Foo, { customTags: [classTag, functionTag] }) |
| 72 |
// don't test the actual class body, because that will break |
| 73 |
// if/when TypeScript is updated. |
| 74 |
t.ok( |
| 75 |
res.startsWith(`!class |- |
| 76 |
class Foo extends Boolean {`), |
| 77 |
'shows class toString value' |
| 78 |
) |
| 79 |
t.end() |
| 80 |
}) |
| 81 |
|
| 82 |
test('stringify not a class for identify coverage', t => { |
| 83 |
const res = stringify(() => {}, { customTags: [classTag, functionTag] }) |
| 84 |
t.equal( |
| 85 |
res, |
| 86 |
`!function |- |
| 87 |
"" |
| 88 |
() => { } |
| 89 |
` |
| 90 |
) |
| 91 |
t.end() |
| 92 |
}) |