2019-08-28 19:43:06 +08:00
|
|
|
const assert = require('assert')
|
|
|
|
const stringifyPartial = require('../src/js/jsonUtils').stringifyPartial
|
|
|
|
const containsArray = require('../src/js/jsonUtils').containsArray
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
describe('jsonUtils', () => {
|
|
|
|
it('should stringify a small object', () => {
|
|
|
|
const json = {
|
2019-06-27 03:57:22 +08:00
|
|
|
a: 2,
|
|
|
|
b: 'foo',
|
|
|
|
c: null,
|
|
|
|
d: false,
|
2019-08-28 19:21:14 +08:00
|
|
|
e: [1, 2, 3],
|
2019-06-27 03:57:22 +08:00
|
|
|
f: { g: 'h' }
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(stringifyPartial(json), '{"a":2,"b":"foo","c":null,"d":false,"e":[1,2,3],"f":{"g":"h"}}')
|
|
|
|
})
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should stringify a small object with formatting', () => {
|
|
|
|
const json = {
|
2019-06-27 03:57:22 +08:00
|
|
|
a: 2,
|
|
|
|
b: 'foo',
|
|
|
|
c: null,
|
|
|
|
d: false,
|
2019-08-28 19:21:14 +08:00
|
|
|
e: [1, 2, 3],
|
2019-06-27 03:57:22 +08:00
|
|
|
f: { g: 'h' }
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
2019-06-27 03:57:22 +08:00
|
|
|
|
|
|
|
assert.strictEqual(stringifyPartial(json, 2),
|
2019-08-28 19:21:14 +08:00
|
|
|
'{\n' +
|
2019-06-27 03:57:22 +08:00
|
|
|
' "a": 2,\n' +
|
|
|
|
' "b": "foo",\n' +
|
|
|
|
' "c": null,\n' +
|
|
|
|
' "d": false,\n' +
|
|
|
|
' "e": [\n' +
|
|
|
|
' 1,\n' +
|
|
|
|
' 2,\n' +
|
|
|
|
' 3\n' +
|
|
|
|
' ],\n' +
|
|
|
|
' "f": {\n' +
|
|
|
|
' "g": "h"\n' +
|
|
|
|
' }\n' +
|
2019-08-28 19:21:14 +08:00
|
|
|
'}')
|
2019-06-27 03:57:22 +08:00
|
|
|
|
|
|
|
assert.strictEqual(stringifyPartial(json, ' '), '{\n' +
|
|
|
|
' "a": 2,\n' +
|
|
|
|
' "b": "foo",\n' +
|
|
|
|
' "c": null,\n' +
|
|
|
|
' "d": false,\n' +
|
|
|
|
' "e": [\n' +
|
|
|
|
' 1,\n' +
|
|
|
|
' 2,\n' +
|
|
|
|
' 3\n' +
|
|
|
|
' ],\n' +
|
|
|
|
' "f": {\n' +
|
|
|
|
' "g": "h"\n' +
|
|
|
|
' }\n' +
|
2019-08-28 19:21:14 +08:00
|
|
|
'}')
|
|
|
|
})
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should limit stringified output', () => {
|
|
|
|
const json = {
|
2019-06-27 03:57:22 +08:00
|
|
|
a: 2,
|
|
|
|
b: 'foo',
|
|
|
|
c: null,
|
|
|
|
d: false,
|
2019-08-28 19:21:14 +08:00
|
|
|
e: [1, 2, 3],
|
2019-06-27 03:57:22 +08:00
|
|
|
f: { g: 'h' }
|
2019-08-28 19:21:14 +08:00
|
|
|
}
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
const all = '{"a":2,"b":"foo","c":null,"d":false,"e":[1,2,3],"f":{"g":"h"}}'
|
|
|
|
const limit = 20
|
2019-06-27 03:57:22 +08:00
|
|
|
|
|
|
|
assert.strictEqual(stringifyPartial(json, undefined, limit),
|
2019-08-28 19:21:14 +08:00
|
|
|
all.slice(0, limit) + '...')
|
2019-07-03 17:54:00 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(stringifyPartial(json, undefined, all.length), all)
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(stringifyPartial([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], undefined, 10),
|
|
|
|
'[1,2,3,4,5...')
|
2019-07-03 17:54:00 +08:00
|
|
|
|
2019-08-28 19:21:14 +08:00
|
|
|
assert.strictEqual(stringifyPartial(12345678, undefined, 4), '1234...')
|
|
|
|
})
|
2019-06-27 03:57:22 +08:00
|
|
|
|
2019-08-28 19:43:06 +08:00
|
|
|
it('should count array items', () => {
|
2019-07-03 19:10:00 +08:00
|
|
|
// assert.strictEqual(countArrayItems('[1,2,3]'), 3)
|
|
|
|
assert.strictEqual(containsArray('[]'), true)
|
|
|
|
assert.strictEqual(containsArray(' []'), true)
|
|
|
|
assert.strictEqual(containsArray(' \t []'), true)
|
|
|
|
assert.strictEqual(containsArray(' \t\n []'), true)
|
|
|
|
assert.strictEqual(containsArray('"["'), false)
|
|
|
|
assert.strictEqual(containsArray('2'), false)
|
|
|
|
assert.strictEqual(containsArray('null'), false)
|
|
|
|
assert.strictEqual(containsArray('{}'), false)
|
2019-08-28 19:21:14 +08:00
|
|
|
})
|
|
|
|
})
|