Skip to content

Commit 14ec27b

Browse files
committed
Updated test
1 parent 6eb3ac0 commit 14ec27b

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

test/additionalProperties.test.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('additionalProperties', (t) => {
7+
t.plan(1)
8+
const stringify = build({
9+
title: 'additionalProperties',
10+
type: 'object',
11+
properties: {
12+
str: {
13+
type: 'string'
14+
}
15+
},
16+
additionalProperties: {
17+
type: 'string'
18+
}
19+
})
20+
21+
let obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: {a: true} }
22+
t.equal('{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}', stringify(obj))
23+
})
24+
25+
test('additionalProperties should not change properties', (t) => {
26+
t.plan(1)
27+
const stringify = build({
28+
title: 'patternProperties should not change properties',
29+
type: 'object',
30+
properties: {
31+
foo: {
32+
type: 'string'
33+
}
34+
},
35+
additionalProperties: {
36+
type: 'number'
37+
}
38+
})
39+
40+
const obj = { foo: '42', ofoo: 42 }
41+
t.equal('{"ofoo":42,"foo":"42"}', stringify(obj))
42+
})
43+
44+
test('additionalProperties should not change properties and patternProperties', (t) => {
45+
t.plan(1)
46+
const stringify = build({
47+
title: 'patternProperties should not change properties',
48+
type: 'object',
49+
properties: {
50+
foo: {
51+
type: 'string'
52+
}
53+
},
54+
patternProperties: {
55+
foo: {
56+
type: 'string'
57+
}
58+
},
59+
additionalProperties: {
60+
type: 'number'
61+
}
62+
})
63+
64+
const obj = { foo: '42', ofoo: 42, test: '42' }
65+
t.equal('{"ofoo":"42","test":42,"foo":"42"}', stringify(obj))
66+
})
67+
68+
test('additionalProperties - string coerce', (t) => {
69+
t.plan(1)
70+
const stringify = build({
71+
title: 'check string coerce',
72+
type: 'object',
73+
properties: {},
74+
additionalProperties: {
75+
type: 'string'
76+
}
77+
})
78+
79+
const obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
80+
t.equal('{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}', stringify(obj))
81+
})
82+
83+
test('additionalProperties - number coerce', (t) => {
84+
t.plan(1)
85+
const stringify = build({
86+
title: 'check number coerce',
87+
type: 'object',
88+
properties: {},
89+
additionalProperties: {
90+
type: 'number'
91+
}
92+
})
93+
94+
const obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
95+
t.equal('{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}', stringify(obj))
96+
})
97+
98+
test('additionalProperties - boolean coerce', (t) => {
99+
t.plan(1)
100+
const stringify = build({
101+
title: 'check boolean coerce',
102+
type: 'object',
103+
properties: {},
104+
additionalProperties: {
105+
type: 'boolean'
106+
}
107+
})
108+
109+
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } }
110+
t.equal('{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}', stringify(obj))
111+
})
112+
113+
test('additionalProperties - object coerce', (t) => {
114+
t.plan(1)
115+
const stringify = build({
116+
title: 'check object coerce',
117+
type: 'object',
118+
properties: {},
119+
additionalProperties: {
120+
type: 'object',
121+
properties: {
122+
answer: {
123+
type: 'number'
124+
}
125+
}
126+
}
127+
})
128+
129+
const obj = { objfoo: { answer: 42 } }
130+
t.equal('{"objfoo":{"answer":42}}', stringify(obj))
131+
})
132+
133+
test('additionalProperties - array coerce', (t) => {
134+
t.plan(1)
135+
const stringify = build({
136+
title: 'check array coerce',
137+
type: 'object',
138+
properties: {},
139+
additionalProperties: {
140+
type: 'array',
141+
items: {
142+
type: 'string'
143+
}
144+
}
145+
})
146+
147+
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
148+
t.equal('{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}', stringify(obj))
149+
})
150+
151+
test('additionalProperties - throw on unknown type', (t) => {
152+
t.plan(1)
153+
const stringify = build({
154+
title: 'check array coerce',
155+
type: 'object',
156+
properties: {},
157+
additionalProperties: {
158+
type: 'strangetype'
159+
}
160+
})
161+
162+
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
163+
try {
164+
stringify(obj)
165+
t.fail()
166+
} catch (e) {
167+
t.pass()
168+
}
169+
})

0 commit comments

Comments
 (0)