-
Notifications
You must be signed in to change notification settings - Fork 43
/
stringify.js
71 lines (69 loc) · 2.8 KB
/
stringify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict'
const test = require('tap').test
const TOML = require('..')
const roundtrip = {
'toJSON is not a function': {obj: {a: {toJSON: 'EXAMPLE'}}, toml: `[a]\ntoJSON = "EXAMPLE"\n`},
'array of arrays': {obj: {a: [[5], [23]]},
toml: 'a = [ [ 5 ], [ 23 ] ]\n',
'array of tables': {obj: {a: [{b: 5}, {b: 23}]}, toml: '[[a]]\nb = 5\n\n[[a]]\nb = 23\n'}},
'inline objects': {obj: {a: [[{a: 23}, {}]]}, toml: `a = [ [ { a = 23 }, { } ] ]\n`},
'keys with quotes': {obj: {'a"b': 123}, toml: `"a\\"b" = 123\n`},
'multiline values ending in quotes': {obj: {a: 'abc\n"def"'}, toml: `a = """\nabc\n"def"\\\n"""\n`}
}
const good = {
'toJSON overrides': {obj: {a: {toJSON: () => 'EXAMPLE'}}, toml: `a = "EXAMPLE"\n`},
'toJSON on the top level object': {obj: {toJSON: () => ({c: 23})}, toml: `c = 23\n`},
'toJSON on top level returns null, get null': {obj: {toJSON: () => null}, toml: null},
'null is removed': {obj: {a: null, b: 'hi'}, toml: `b = "hi"\n`},
'undefined is removed': {obj: {a: undefined, b: 'hi'}, toml: `b = "hi"\n`},
'NaN is NOT removed': {obj: {a: NaN, b: 'hi'}, toml: `a = nan\nb = "hi"\n`},
'null is removed from arrays': {obj: {a: [null]}, toml: `a = [ ]\n`},
'undefined is removed from arrays': {obj: {a: [23, undefined]}, toml: `a = [ 23 ]\n`},
'NaN is NOT removed from arrays': {obj: {a: [NaN, 23]}, toml: `a = [ nan, 23 ]\n`},
'Invalid Date in scalar': {obj: {a: 1, b: new Date('BAD')}, toml: 'a = 1\n'},
'Invalid Date in list': {obj: {a: [new Date('nope')]}, toml: `a = [ ]\n`},
'infinity': {obj: {a: Infinity}, toml: `a = inf\n`},
'-infinity': {obj: {a: -Infinity}, toml: `a = -inf\n`},
'-0': {obj: {a: -0}, toml: 'a = -0.0\n'},
'multiline': {obj: {a: [ 'abc', 'ghi', 'abc', 'ghi', 'abc', 'ghi', 'abc', 'ghi', 'abc' ]}, toml: 'a = [\n "abc",\n "ghi",\n "abc",\n "ghi",\n "abc",\n "ghi",\n "abc",\n "ghi",\n "abc"\n]\n'}
}
const bad = {
'stringify null': null,
'stringify undefined': undefined,
'stringify number': 23,
'stringify date': new Date(),
'stringify bool': true,
'stringify array': [1, 2, 3]
}
test('stringify', t => {
Object.keys(bad).forEach(msg => {
try {
const result = TOML.stringify(bad[msg])
t.comment(result)
t.fail(msg)
} catch (err) {
t.comment(err.message)
t.pass(msg)
}
})
Object.keys(good).forEach(msg => {
try {
const result = TOML.stringify(good[msg].obj)
t.is(result, good[msg].toml, msg)
} catch (err) {
t.comment(err.message)
t.fail(msg)
}
})
Object.keys(roundtrip).forEach(msg => {
try {
const result = TOML.stringify(roundtrip[msg].obj)
t.is(result, roundtrip[msg].toml, msg)
t.isDeeply(TOML.parse(result), roundtrip[msg].obj, msg + ' roundtrip')
} catch (err) {
t.comment(err.message)
t.fail(msg)
}
})
t.done()
})