Skip to content

Commit cbc14de

Browse files
committed
Updated test - patternProperties
1 parent 3b8da57 commit cbc14de

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

test.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,112 @@ test('missing values', (t) => {
316316
t.equal('{"str":"string","val":"value"}', stringify({ str: 'string', val: 'value' }))
317317
t.equal('{"str":"string","num":42,"val":"value"}', stringify({ str: 'string', num: 42, val: 'value' }))
318318
})
319+
320+
test('patternProperties', (t) => {
321+
t.plan(7)
322+
let stringify = build({
323+
title: 'patternProperties',
324+
type: 'object',
325+
properties: {
326+
str: {
327+
type: 'string'
328+
}
329+
},
330+
patternProperties: {
331+
'foo': {
332+
type: 'string'
333+
}
334+
}
335+
})
336+
337+
let obj = { str: 'test', foo: 42, ofoo: true, foof: 'string', objfoo: {a: true}, notMe: false }
338+
t.equal('{"foo":"42","ofoo":"true","foof":"string","objfoo":"[object Object]","str":"test"}', stringify(obj))
339+
340+
stringify = build({
341+
title: 'patternProperties should not change properties',
342+
type: 'object',
343+
properties: {
344+
foo: {
345+
type: 'string'
346+
}
347+
},
348+
patternProperties: {
349+
foo: {
350+
type: 'number'
351+
}
352+
}
353+
})
354+
355+
obj = { foo: '42', ofoo: 42 }
356+
t.equal('{"ofoo":42,"foo":"42"}', stringify(obj))
357+
358+
stringify = build({
359+
title: 'check string coerce',
360+
type: 'object',
361+
properties: {},
362+
patternProperties: {
363+
foo: {
364+
type: 'string'
365+
}
366+
}
367+
})
368+
369+
obj = { foo: true, ofoo: 42, arrfoo: ['array', 'test'], objfoo: { a: 'world' } }
370+
t.equal('{"foo":"true","ofoo":"42","arrfoo":"array,test","objfoo":"[object Object]"}', stringify(obj))
371+
372+
stringify = build({
373+
title: 'check number coerce',
374+
type: 'object',
375+
properties: {},
376+
patternProperties: {
377+
foo: {
378+
type: 'number'
379+
}
380+
}
381+
})
382+
383+
obj = { foo: true, ofoo: '42', xfoo: 'string', arrfoo: [1, 2], objfoo: { num: 42 } }
384+
t.equal('{"foo":1,"ofoo":42,"xfoo":null,"arrfoo":null,"objfoo":null}', stringify(obj))
385+
386+
stringify = build({
387+
title: 'check boolean coerce',
388+
type: 'object',
389+
properties: {},
390+
patternProperties: {
391+
foo: {
392+
type: 'boolean'
393+
}
394+
}
395+
})
396+
397+
obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { a: true } }
398+
t.equal('{"foo":true,"ofoo":false,"arrfoo":true,"objfoo":true}', stringify(obj))
399+
400+
stringify = build({
401+
title: 'check object coerce',
402+
type: 'object',
403+
properties: {},
404+
patternProperties: {
405+
foo: {
406+
type: 'object'
407+
}
408+
}
409+
})
410+
411+
obj = { foo: true, ofoo: '42', arrfoo: [1, 2], objfoo: { answer: 42 } }
412+
t.equal('{"foo":{},"ofoo":{},"arrfoo":{},"objfoo":{}}', stringify(obj))
413+
414+
stringify = build({
415+
title: 'check array coerce',
416+
type: 'object',
417+
properties: {},
418+
patternProperties: {
419+
foo: {
420+
type: 'array'
421+
}
422+
}
423+
})
424+
425+
obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
426+
t.equal('{"foo":[],"ofoo":[],"arrfoo":[],"objfoo":[]}', stringify(obj))
427+
})

0 commit comments

Comments
 (0)