Spaces:
Runtime error
Runtime error
File size: 1,573 Bytes
23ac194 |
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 |
'use strict'
const { test } = require('node:test')
const build = require('..')
test('serialize short string', (t) => {
t.plan(2)
const schema = {
type: 'string'
}
const input = 'abcd'
const stringify = build(schema)
const output = stringify(input)
t.assert.equal(output, '"abcd"')
t.assert.equal(JSON.parse(output), input)
})
test('serialize short string', (t) => {
t.plan(2)
const schema = {
type: 'string'
}
const input = '\x00'
const stringify = build(schema)
const output = stringify(input)
t.assert.equal(output, '"\\u0000"')
t.assert.equal(JSON.parse(output), input)
})
test('serialize long string', (t) => {
t.plan(2)
const schema = {
type: 'string'
}
const input = new Array(2e4).fill('\x00').join('')
const stringify = build(schema)
const output = stringify(input)
t.assert.equal(output, `"${new Array(2e4).fill('\\u0000').join('')}"`)
t.assert.equal(JSON.parse(output), input)
})
test('unsafe string', (t) => {
t.plan(2)
const schema = {
type: 'string',
format: 'unsafe'
}
const input = 'abcd'
const stringify = build(schema)
const output = stringify(input)
t.assert.equal(output, `"${input}"`)
t.assert.equal(JSON.parse(output), input)
})
test('unsafe unescaped string', (t) => {
t.plan(2)
const schema = {
type: 'string',
format: 'unsafe'
}
const input = 'abcd "abcd"'
const stringify = build(schema)
const output = stringify(input)
t.assert.equal(output, `"${input}"`)
t.assert.throws(function () {
JSON.parse(output)
})
})
|