-
Notifications
You must be signed in to change notification settings - Fork 576
/
stringify_test.ts
134 lines (119 loc) · 3.1 KB
/
stringify_test.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Ported from js-yaml v3.13.1:
// https://github.com/nodeca/js-yaml/commit/665aadda42349dcae869f12040d9b10ef18d12da
// Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license.
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../../testing/asserts.ts";
import { stringify } from "./stringify.ts";
import { YAMLError } from "./error.ts";
import { DEFAULT_SCHEMA, EXTENDED_SCHEMA } from "./schema/mod.ts";
import { Type } from "./type.ts";
Deno.test({
name: "stringified correctly",
fn(): void {
const FIXTURE = {
foo: {
bar: true,
test: [
"a",
"b",
{
a: false,
},
{
a: false,
},
],
},
test: "foobar",
};
const ASSERTS = `foo:
bar: true
test:
- a
- b
- a: false
- a: false
test: foobar
`;
assertEquals(stringify(FIXTURE), ASSERTS);
},
});
Deno.test({
name:
"`!!js/*` yaml types are not handled in default schemas while stringifying",
fn(): void {
const object = { undefined: undefined };
assertThrows(
() => stringify(object),
YAMLError,
"unacceptable kind of an object to dump",
);
},
});
Deno.test({
name:
"`!!js/*` yaml types are correctly handled with extended schema while stringifying",
fn(): void {
const object = {
regexp: {
simple: /foobar/,
modifiers: /foobar/im,
},
undefined: undefined,
};
const expected = `regexp:
simple: !<tag:yaml.org,2002:js/regexp> /foobar/
modifiers: !<tag:yaml.org,2002:js/regexp> /foobar/im
undefined: !<tag:yaml.org,2002:js/undefined> ''
`;
assertEquals(stringify(object, { schema: EXTENDED_SCHEMA }), expected);
},
});
Deno.test({
name:
"`!!js/function` yaml type is correctly handled with extended schema while stringifying",
fn(): void {
const func = function foobar() {
return "hello world!";
};
const expected = `function: !<tag:yaml.org,2002:js/function> |-
${func.toString().split("\n").map((line) => ` ${line}`).join("\n")}
`;
assertEquals(
stringify({ function: func }, { schema: EXTENDED_SCHEMA }),
expected,
);
},
});
Deno.test({
name: "`!*` yaml user defined types are supported while stringifying",
fn(): void {
const PointYamlType = new Type("!point", {
kind: "sequence",
resolve(data) {
return data !== null && data?.length === 3;
},
construct(data) {
const [x, y, z] = data;
return { x, y, z };
},
predicate(object: unknown) {
return !!(object && typeof object === "object" && "x" in object &&
"y" in object && "z" in object);
},
represent(point) {
return [point.x, point.y, point.z];
},
});
const SPACE_SCHEMA = DEFAULT_SCHEMA.extend({ explicit: [PointYamlType] });
const object = {
point: { x: 1, y: 2, z: 3 },
};
const expected = `point: !<!point>${" "}
- 1
- 2
- 3
`;
assertEquals(stringify(object, { schema: SPACE_SCHEMA }), expected);
},
});