Skip to content

Commit e478863

Browse files
authored
improve coverage of serializer.js to 100% (#580)
* improve coverage of date formats * add test for date error * improve coverage for serializer.js to 100%
1 parent 74f9fb4 commit e478863

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

lib/serializer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
55

66
module.exports = class Serializer {
7-
constructor (options = {}) {
8-
switch (options.rounding) {
7+
constructor (options) {
8+
switch (options && options.rounding) {
99
case 'floor':
1010
this.parseInteger = Math.floor
1111
break

test/date.test.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,95 @@ test('non-date format should not affect data serialization (issue #491)', (t) =>
544544
const data = { hello: 123n }
545545
t.equal(stringify(data), '{"hello":"123"}')
546546
})
547+
548+
test('should serialize also an invalid string value, even if it is not a valid date', (t) => {
549+
t.plan(2)
550+
551+
const schema = {
552+
title: 'a date in a string',
553+
type: 'string',
554+
format: 'date-time',
555+
nullable: true
556+
}
557+
const toStringify = 'invalid'
558+
559+
const validate = validator(schema)
560+
const stringify = build(schema)
561+
const output = stringify(toStringify)
562+
563+
t.equal(output, JSON.stringify(toStringify))
564+
t.not(validate(JSON.parse(output)), 'valid schema')
565+
})
566+
567+
test('should throw an error if value can not be transformed to date-time', (t) => {
568+
t.plan(2)
569+
570+
const schema = {
571+
title: 'a date in a string',
572+
type: 'string',
573+
format: 'date-time',
574+
nullable: true
575+
}
576+
const toStringify = true
577+
578+
const validate = validator(schema)
579+
const stringify = build(schema)
580+
581+
t.throws(() => stringify(toStringify), new Error('The value "true" cannot be converted to a date-time.'))
582+
t.not(validate(toStringify))
583+
})
584+
585+
test('should throw an error if value can not be transformed to date', (t) => {
586+
t.plan(2)
587+
588+
const schema = {
589+
title: 'a date in a string',
590+
type: 'string',
591+
format: 'date',
592+
nullable: true
593+
}
594+
const toStringify = true
595+
596+
const validate = validator(schema)
597+
const stringify = build(schema)
598+
599+
t.throws(() => stringify(toStringify), new Error('The value "true" cannot be converted to a date.'))
600+
t.not(validate(toStringify))
601+
})
602+
603+
test('should throw an error if value can not be transformed to time', (t) => {
604+
t.plan(2)
605+
606+
const schema = {
607+
title: 'a time in a string',
608+
type: 'string',
609+
format: 'time',
610+
nullable: true
611+
}
612+
const toStringify = true
613+
614+
const validate = validator(schema)
615+
const stringify = build(schema)
616+
617+
t.throws(() => stringify(toStringify), new Error('The value "true" cannot be converted to a time.'))
618+
t.not(validate(toStringify))
619+
})
620+
621+
test('should serialize also an invalid string value, even if it is not a valid time', (t) => {
622+
t.plan(2)
623+
624+
const schema = {
625+
title: 'a time in a string',
626+
type: 'string',
627+
format: 'time',
628+
nullable: true
629+
}
630+
const toStringify = 'invalid'
631+
632+
const validate = validator(schema)
633+
const stringify = build(schema)
634+
const output = stringify(toStringify)
635+
636+
t.equal(output, JSON.stringify(toStringify))
637+
t.not(validate(JSON.parse(output)), 'valid schema')
638+
})

test/string.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const test = t.test
5+
const build = require('..')
6+
7+
test('serialize short string', (t) => {
8+
t.plan(2)
9+
10+
const schema = {
11+
type: 'string'
12+
}
13+
14+
const input = 'abcd'
15+
const stringify = build(schema)
16+
const output = stringify(input)
17+
18+
t.equal(output, '"abcd"')
19+
t.equal(JSON.parse(output), input)
20+
})
21+
22+
test('serialize short string', (t) => {
23+
t.plan(2)
24+
25+
const schema = {
26+
type: 'string'
27+
}
28+
29+
const input = '\x00'
30+
const stringify = build(schema)
31+
const output = stringify(input)
32+
33+
t.equal(output, '"\\u0000"')
34+
t.equal(JSON.parse(output), input)
35+
})
36+
37+
test('serialize long string', (t) => {
38+
t.plan(2)
39+
40+
const schema = {
41+
type: 'string'
42+
}
43+
44+
const input = new Array(2e4).fill('\x00').join('')
45+
const stringify = build(schema)
46+
const output = stringify(input)
47+
48+
t.equal(output, `"${new Array(2e4).fill('\\u0000').join('')}"`)
49+
t.equal(JSON.parse(output), input)
50+
})

0 commit comments

Comments
 (0)