| 1 |
import { test } from 'tap' |
| 2 |
import { Document, parse, stringify, Tags, type Scalar } from 'yaml' |
| 3 |
|
| 4 |
import { bigint } from '.' |
| 5 |
|
| 6 |
const customTags = (tags: Tags) => ([bigint] as Tags).concat(tags) |
| 7 |
|
| 8 |
test('parse with n', t => { |
| 9 |
const res: bigint = parse(`!bigint 123n`, { |
| 10 |
customTags: [bigint] |
| 11 |
}) |
| 12 |
t.type(res, 'bigint') |
| 13 |
t.equal(Number(res), 123) |
| 14 |
t.equal(res, 123n) |
| 15 |
t.end() |
| 16 |
}) |
| 17 |
|
| 18 |
test('parse without n', t => { |
| 19 |
const res: bigint = parse(`!bigint 123`, { customTags }) |
| 20 |
t.type(res, 'bigint') |
| 21 |
t.equal(Number(res), 123) |
| 22 |
t.equal(res, 123n) |
| 23 |
t.end() |
| 24 |
}) |
| 25 |
|
| 26 |
test('negative zero is zero', t => { |
| 27 |
const pos: bigint = parse(`!bigint 0`, { customTags }) |
| 28 |
const neg: bigint = parse(`!bigint -0`, { customTags }) |
| 29 |
t.equal(pos, 0n, 'pos equals zero') |
| 30 |
t.equal(neg, 0n, 'neg equals zero') |
| 31 |
t.equal(pos, neg, 'pos equals neg') |
| 32 |
t.end() |
| 33 |
}) |
| 34 |
|
| 35 |
test('parse hex, octal, binary', t => { |
| 36 |
const cases = [ |
| 37 |
'0b11011110101011011011111011101111', |
| 38 |
'0B11011110101011011011111011101111', |
| 39 |
'0b11011110101011011011111011101111n', |
| 40 |
'0B11011110101011011011111011101111n', |
| 41 |
'0o33653337357', |
| 42 |
'0o33653337357n', |
| 43 |
'3735928559', |
| 44 |
'3735928559n', |
| 45 |
'0xDeAdBeEf', |
| 46 |
'0xDeAdBeEfn', |
| 47 |
'0xDEADBEEF', |
| 48 |
'0xDEADBEEFn', |
| 49 |
'0XDEADBEEF', |
| 50 |
'0XDEADBEEFn', |
| 51 |
'0x0000DEADBEEF', |
| 52 |
'0x0000DEADBEEFn', |
| 53 |
'0xdeadbeef', |
| 54 |
'0xdeadbeefn', |
| 55 |
'0000000003735928559' |
| 56 |
] |
| 57 |
for (const c of cases) { |
| 58 |
const res: bigint = parse(`!bigint ${c}`, { customTags }) |
| 59 |
t.equal(res, 0xdeadbeefn, `${c} value`) |
| 60 |
t.type(res, 'bigint', `${c} typeof`) |
| 61 |
} |
| 62 |
t.end() |
| 63 |
}) |
| 64 |
|
| 65 |
test('parse invalid', t => { |
| 66 |
const opt = { customTags } |
| 67 |
t.throws(() => parse('!bigint not a number\n', opt)) |
| 68 |
t.throws(() => parse('!bigint 123.456\n', opt)) |
| 69 |
t.throws(() => parse('!bigint 123x\n', opt)) |
| 70 |
t.throws(() => parse('!bigint 0xBAD1DEAN\n', opt), 'n must be lowercase') |
| 71 |
t.throws(() => parse('!bigint 0b012', opt), '2 is invalid binary digit') |
| 72 |
t.throws(() => parse('!bigint 0o018', opt), '8 is invalid octal digit') |
| 73 |
t.end() |
| 74 |
}) |
| 75 |
|
| 76 |
test('empty string is 0n', t => { |
| 77 |
t.equal(parse('!bigint ""', { customTags }), 0n) |
| 78 |
t.end() |
| 79 |
}) |
| 80 |
|
| 81 |
test('stringify', t => { |
| 82 |
const doc = new Document<Scalar, false>(123n, { customTags }) |
| 83 |
t.equal(doc.toString(), '!bigint 123n\n') |
| 84 |
|
| 85 |
doc.contents.value = Object(42n) |
| 86 |
t.equal(doc.toString(), '!bigint 42n\n') |
| 87 |
|
| 88 |
doc.contents.value = 42 |
| 89 |
t.throws(() => doc.toString(), { name: 'TypeError' }) |
| 90 |
|
| 91 |
t.equal( |
| 92 |
stringify( |
| 93 |
[0n, 123, 123n, BigInt('123'), Object(123n), Object(BigInt(123)), -123n], |
| 94 |
{ |
| 95 |
customTags |
| 96 |
} |
| 97 |
), |
| 98 |
`- !bigint 0n |
| 99 |
- 123 |
| 100 |
- !bigint 123n |
| 101 |
- !bigint 123n |
| 102 |
- !bigint 123n |
| 103 |
- !bigint 123n |
| 104 |
- !bigint -123n |
| 105 |
` |
| 106 |
) |
| 107 |
|
| 108 |
t.end() |
| 109 |
}) |