/
0571.js
114 lines (87 loc) · 2.92 KB
/
0571.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'use strict';
const assert = require('assert');
const yaml = require('../../');
describe('Undefined', function () {
let undef = new yaml.Type('!undefined', {
kind: 'scalar',
resolve: () => true,
construct: () => {},
predicate: object => typeof object === 'undefined',
represent: () => ''
});
let undef_schema = yaml.DEFAULT_SCHEMA.extend(undef);
it('Should replace undefined with null in collections', function () {
let str;
str = yaml.dump([ undefined, 1, undefined, null, 2 ], { flowLevel: 0 });
assert(str.match(/^\[/));
assert.deepStrictEqual(
yaml.load(str),
[ null, 1, null, null, 2 ]
);
str = yaml.dump([ undefined, 1, undefined, null, 2 ], { flowLevel: -1 });
assert(str.match(/^- /));
assert.deepStrictEqual(
yaml.load(str),
[ null, 1, null, null, 2 ]
);
});
it('Should remove keys with undefined in mappings', function () {
let str;
str = yaml.dump({ t: undefined, foo: 1, bar: undefined, baz: null }, { flowLevel: 0 });
assert(str.match(/^\{/));
assert.deepStrictEqual(
yaml.load(str),
{ foo: 1, baz: null }
);
str = yaml.dump({ t: undefined, foo: 1, bar: undefined, baz: null }, { flowLevel: -1 });
assert(str.match(/^foo:/));
assert.deepStrictEqual(
yaml.load(str),
{ foo: 1, baz: null }
);
});
it("Should serialize top-level undefined to ''", function () {
assert.strictEqual(yaml.dump(undefined), '');
});
it('Should serialize undefined if schema is available', function () {
assert.deepStrictEqual(
yaml.load(
yaml.dump([ 1, undefined, null, 2 ], { schema: undef_schema }),
{ schema: undef_schema }
),
[ 1, undefined, null, 2 ]
);
assert.deepStrictEqual(
yaml.load(
yaml.dump({ foo: 1, bar: undefined, baz: null }, { schema: undef_schema }),
{ schema: undef_schema }
),
{ foo: 1, bar: undefined, baz: null }
);
});
it('Should respect null formatting', function () {
assert.strictEqual(
yaml.dump([ undefined ], { styles: { '!!null': 'uppercase' } }),
'- NULL\n'
);
});
it('Should return an error if neither null nor undefined schemas are available', function () {
assert.throws(() => {
yaml.dump([ 'foo', undefined, 'bar' ], { schema: yaml.FAILSAFE_SCHEMA });
}, /unacceptable kind of an object to dump/);
});
it('Should skip leading values correctly', function () {
assert.strictEqual(
yaml.dump([ () => {}, 'a' ], { flowLevel: 0, skipInvalid: true }),
'[a]\n');
assert.strictEqual(
yaml.dump([ () => {}, 'a' ], { flowLevel: -1, skipInvalid: true }),
'- a\n');
assert.strictEqual(
yaml.dump({ a: () => {}, b: 'a' }, { flowLevel: 0, skipInvalid: true }),
'{b: a}\n');
assert.strictEqual(
yaml.dump({ a: () => {}, b: 'a' }, { flowLevel: -1, skipInvalid: true }),
'b: a\n');
});
});