Skip to content

Commit b34b0f3

Browse files
Added tests for YAML.parse() and YAML.stringify()
1 parent 321e852 commit b34b0f3

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

tests/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
<!-- Tests -->
2929
<script src="specs/dereference.spec.js"></script>
30+
<script src="specs/yaml.spec.js"></script>
3031

3132
<script>
3233
mocha.run();

tests/specs/yaml.spec.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
'use strict';
2+
3+
describe('YAML object', function() {
4+
describe('parse', function() {
5+
it('should parse an object',
6+
function() {
7+
var obj = $RefParser.YAML.parse(
8+
'title: person\n' +
9+
'required:\n' +
10+
' - name\n' +
11+
' - age\n' +
12+
'properties:\n' +
13+
' name:\n' +
14+
' type: string\n' +
15+
' age:\n' +
16+
' type: number'
17+
);
18+
19+
expect(obj).to.deep.equal({
20+
title: 'person',
21+
required: ['name', 'age'],
22+
properties: {
23+
name: {
24+
type: 'string'
25+
},
26+
age: {
27+
type: 'number'
28+
}
29+
}
30+
});
31+
}
32+
);
33+
34+
it('should parse a string',
35+
function() {
36+
var str = $RefParser.YAML.parse('hello, world');
37+
expect(str).to.equal('hello, world');
38+
}
39+
);
40+
41+
it('should parse a number',
42+
function() {
43+
var str = $RefParser.YAML.parse('42');
44+
expect(str).to.be.a('number').equal(42);
45+
}
46+
);
47+
});
48+
49+
describe('stringify', function() {
50+
it('should stringify an object',
51+
function() {
52+
var yaml = $RefParser.YAML.stringify({
53+
title: 'person',
54+
required: ['name', 'age'],
55+
properties: {
56+
name: {
57+
type: 'string'
58+
},
59+
age: {
60+
type: 'number'
61+
}
62+
}
63+
});
64+
65+
expect(yaml).to.equal(
66+
'title: person\n' +
67+
'required:\n' +
68+
' - name\n' +
69+
' - age\n' +
70+
'properties:\n' +
71+
' name:\n' +
72+
' type: string\n' +
73+
' age:\n' +
74+
' type: number\n'
75+
);
76+
}
77+
);
78+
79+
it('should support a custom indent (as a string)',
80+
function() {
81+
var yaml = $RefParser.YAML.stringify({
82+
title: 'person',
83+
required: ['name', 'age'],
84+
properties: {
85+
name: {
86+
type: 'string'
87+
},
88+
age: {
89+
type: 'number'
90+
}
91+
}
92+
}, null, ' ');
93+
94+
expect(yaml).to.equal(
95+
'title: person\n' +
96+
'required:\n' +
97+
' - name\n' +
98+
' - age\n' +
99+
'properties:\n' +
100+
' name:\n' +
101+
' type: string\n' +
102+
' age:\n' +
103+
' type: number\n'
104+
);
105+
}
106+
);
107+
108+
it('should support a custom indent (as a number)',
109+
function() {
110+
var yaml = $RefParser.YAML.stringify({
111+
title: 'person',
112+
required: ['name', 'age'],
113+
properties: {
114+
name: {
115+
type: 'string'
116+
},
117+
age: {
118+
type: 'number'
119+
}
120+
}
121+
}, null, 10);
122+
123+
expect(yaml).to.equal(
124+
'title: person\n' +
125+
'required:\n' +
126+
' - name\n' +
127+
' - age\n' +
128+
'properties:\n' +
129+
' name:\n' +
130+
' type: string\n' +
131+
' age:\n' +
132+
' type: number\n'
133+
);
134+
}
135+
);
136+
137+
it('should stringify a string',
138+
function() {
139+
var yaml = $RefParser.YAML.stringify('hello, world');
140+
expect(yaml).to.equal('\'hello, world\'\n');
141+
}
142+
);
143+
144+
it('should stringify a number',
145+
function() {
146+
var yaml = $RefParser.YAML.stringify(42);
147+
expect(yaml).to.equal('42\n');
148+
}
149+
);
150+
});
151+
});

0 commit comments

Comments
 (0)