| 1 |
import { test } from 'tap' |
| 2 |
import { Document, type Scalar, parse, stringify } from 'yaml' |
| 3 |
|
| 4 |
import { sharedSymbol, symbol } from '.' |
| 5 |
|
| 6 |
test('parse shared', t => { |
| 7 |
const res: symbol = parse(`!symbol/shared foo`, { |
| 8 |
customTags: [sharedSymbol, symbol] |
| 9 |
}) |
| 10 |
t.type(res, 'symbol') |
| 11 |
t.same(Symbol.keyFor(res), 'foo') |
| 12 |
t.end() |
| 13 |
}) |
| 14 |
|
| 15 |
test('stringify shared', t => { |
| 16 |
const doc = new Document<Scalar, false>(Symbol.for('some\nsymbol'), { |
| 17 |
customTags: [sharedSymbol, symbol] |
| 18 |
}) |
| 19 |
t.equal(doc.toString(), '!symbol/shared |-\nsome\nsymbol\n') |
| 20 |
|
| 21 |
doc.contents.value = Symbol('foo') |
| 22 |
t.throws(() => doc.toString(), { name: 'TypeError' }) |
| 23 |
|
| 24 |
doc.contents.value = 42 |
| 25 |
t.throws(() => doc.toString(), { name: 'TypeError' }) |
| 26 |
|
| 27 |
t.throws(() => |
| 28 |
stringify(Symbol('some\nsymbol'), { customTags: [sharedSymbol] }) |
| 29 |
) |
| 30 |
|
| 31 |
t.end() |
| 32 |
}) |
| 33 |
|
| 34 |
test('parse private', t => { |
| 35 |
const res: symbol = parse(`!symbol foo`, { |
| 36 |
customTags: [sharedSymbol, symbol] |
| 37 |
}) |
| 38 |
t.type(res, 'symbol') |
| 39 |
t.notOk(Symbol.keyFor(res)) |
| 40 |
t.end() |
| 41 |
}) |
| 42 |
|
| 43 |
test('stringify private', t => { |
| 44 |
const doc = new Document<Scalar, false>(Symbol('some\nsymbol'), { |
| 45 |
customTags: [sharedSymbol, symbol] |
| 46 |
}) |
| 47 |
t.equal(doc.toString(), '!symbol |-\nsome\nsymbol\n') |
| 48 |
|
| 49 |
doc.contents.value = Symbol.for('foo') |
| 50 |
t.equal(doc.toString(), '!symbol foo\n') |
| 51 |
|
| 52 |
doc.contents.value = 'foo' |
| 53 |
t.throws(() => doc.toString(), { name: 'TypeError' }) |
| 54 |
|
| 55 |
t.end() |
| 56 |
}) |