jsoneditor/test/eson.test.js

580 lines
18 KiB
JavaScript
Raw Normal View History

import { readFileSync } from 'fs'
import test from 'ava'
import { setIn, getIn, deleteIn } from '../src/utils/immutabilityHelpers'
2016-09-09 17:01:06 +08:00
import {
2017-12-15 21:02:42 +08:00
META,
2017-12-16 03:34:07 +08:00
esonToJson, pathExists, transform,
2017-12-15 19:59:45 +08:00
parseJSONPointer, compileJSONPointer,
jsonToEson,
2017-12-15 19:59:45 +08:00
expand, expandOne, expandPath, applyErrors, search, nextSearchResult,
previousSearchResult,
applySelection, pathsFromSelection,
2017-12-15 21:02:42 +08:00
SELECTED, SELECTED_END
2017-09-08 19:36:15 +08:00
} from '../src/eson'
2017-12-15 21:02:42 +08:00
import 'console.table'
import repeat from 'lodash/repeat'
import { assertDeepEqualEson } from './utils/assertDeepEqualEson'
const ESON2 = loadJSON('./resources/eson2.json')
2016-11-12 21:10:10 +08:00
test('jsonToEson', t => {
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, jsonToEson(1), {[META]: {id: '[ID]', path: [], type: 'value', value: 1}})
assertDeepEqualEson(t, jsonToEson("foo"), {[META]: {id: '[ID]', path: [], type: 'value', value: "foo"}})
assertDeepEqualEson(t, jsonToEson(null), {[META]: {id: '[ID]', path: [], type: 'value', value: null}})
assertDeepEqualEson(t, jsonToEson(false), {[META]: {id: '[ID]', path: [], type: 'value', value: false}})
assertDeepEqualEson(t, jsonToEson({a:1, b: 2}), {
[META]: {id: '[ID]', path: [], type: 'Object', keys: ['a', 'b']},
a: {[META]: {id: '[ID]', path: ['a'], type: 'value', value: 1}},
b: {[META]: {id: '[ID]', path: ['b'], type: 'value', value: 2}}
2017-11-30 04:52:18 +08:00
})
2017-12-15 21:02:42 +08:00
const actual = jsonToEson([1,2])
const expected = [
{[META]: {id: '[ID]', path: ['0'], type: 'value', value: 1}},
{[META]: {id: '[ID]', path: ['1'], type: 'value', value: 2}}
2017-12-15 21:02:42 +08:00
]
expected[META] = {id: '[ID]', path: [], type: 'Array'}
assertDeepEqualEson(t, actual, expected)
2017-11-30 04:52:18 +08:00
})
2017-09-08 19:36:15 +08:00
test('esonToJson', t => {
const json = {
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
}
const eson = jsonToEson(json)
t.deepEqual(esonToJson(eson), json)
})
test('expand a single path', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const path = ['obj', 'arr', 2]
const collapsed = expandOne(eson, path, false)
2017-12-15 21:02:42 +08:00
t.is(collapsed.obj.arr[2][META].expanded, false)
assertDeepEqualEson(t, deleteIn(collapsed, path.concat([META, 'expanded'])), eson)
const expanded = expandOne(eson, path, true)
2017-12-15 21:02:42 +08:00
t.is(expanded.obj.arr[2][META].expanded, true)
assertDeepEqualEson(t, deleteIn(expanded, path.concat([META, 'expanded'])), eson)
})
test('expand all objects/arrays on a path', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const path = ['obj', 'arr', 2]
const collapsed = expandPath(eson, path, false)
2017-12-15 21:02:42 +08:00
t.is(collapsed[META].expanded, false)
t.is(collapsed.obj[META].expanded, false)
t.is(collapsed.obj.arr[META].expanded, false)
t.is(collapsed.obj.arr[2][META].expanded, false)
const expanded = expandPath(eson, path, true)
2017-12-15 21:02:42 +08:00
t.is(expanded[META].expanded, true)
t.is(expanded.obj[META].expanded, true)
t.is(expanded.obj.arr[META].expanded, true)
t.is(expanded.obj.arr[2][META].expanded, true)
let orig = expanded
2017-12-15 21:02:42 +08:00
orig = deleteIn(orig, [].concat([META, 'expanded']))
orig = deleteIn(orig, ['obj'].concat([META, 'expanded']))
orig = deleteIn(orig, ['obj', 'arr'].concat([META, 'expanded']))
orig = deleteIn(orig, ['obj', 'arr', 2].concat([META, 'expanded']))
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, orig, eson)
})
test('expand a callback', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
function filterCallback (path) {
return path.length >= 1
}
const expandedValue = false
const collapsed = expand(eson, filterCallback, expandedValue)
2017-12-15 21:02:42 +08:00
t.is(collapsed[META].expanded, undefined)
t.is(collapsed.obj[META].expanded, expandedValue)
t.is(collapsed.obj.arr[META].expanded, expandedValue)
t.is(collapsed.obj.arr[2][META].expanded, expandedValue)
let orig = collapsed
2017-12-15 21:02:42 +08:00
orig = deleteIn(orig, ['obj'].concat([META, 'expanded']))
orig = deleteIn(orig, ['obj', 'arr'].concat([META, 'expanded']))
orig = deleteIn(orig, ['obj', 'arr', 2].concat([META, 'expanded']))
assertDeepEqualEson(t, orig, eson)
})
test('expand a callback should not change the object when nothing happens', t => {
const eson = jsonToEson({a: [1,2,3], b: {c: 4}})
function callback (path) {
return false
}
const expanded = false
const collapsed = expand(eson, callback, expanded)
t.is(collapsed, eson)
})
test('transform (no change)', t => {
const eson = jsonToEson({a: [1,2,3], b: {c: 4}})
const updated = transform(eson, (value, path) => value)
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, updated, eson)
t.is(updated, eson)
})
test('transform (change based on value)', t => {
const eson = jsonToEson({a: [1,2,3], b: {c: 4}})
const updated = transform(eson,
2017-12-15 21:02:42 +08:00
(value, path) => value[META].value === 2 ? jsonToEson(20, path) : value)
const expected = jsonToEson({a: [1,20,3], b: {c: 4}})
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, updated, expected)
t.is(updated.b, eson.b) // should not have replaced b
})
test('transform (change based on path)', t => {
const eson = jsonToEson({a: [1,2,3], b: {c: 4}})
const updated = transform(eson,
(value, path) => path.join('.') === 'a.1' ? jsonToEson(20, path) : value)
const expected = jsonToEson({a: [1,20,3], b: {c: 4}})
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, updated, expected)
t.is(updated.b, eson.b) // should not have replaced b
})
2016-09-09 17:01:06 +08:00
test('pathExists', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
t.is(pathExists(eson, ['obj', 'arr', 2, 'first']), true)
t.is(pathExists(eson, ['obj', 'foo']), false)
t.is(pathExists(eson, ['obj', 'foo', 'bar']), false)
t.is(pathExists(eson, []), true)
2016-09-09 17:01:06 +08:00
})
test('parseJSONPointer', t => {
t.deepEqual(parseJSONPointer('/obj/a'), ['obj', 'a'])
t.deepEqual(parseJSONPointer('/arr/-'), ['arr', '-'])
t.deepEqual(parseJSONPointer('/foo/~1~0 ~0~1'), ['foo', '/~ ~/'])
t.deepEqual(parseJSONPointer('/obj'), ['obj'])
2016-10-28 17:04:06 +08:00
t.deepEqual(parseJSONPointer('/'), [''])
t.deepEqual(parseJSONPointer(''), [])
2016-09-09 17:01:06 +08:00
})
test('compileJSONPointer', t => {
t.deepEqual(compileJSONPointer(['foo', 'bar']), '/foo/bar')
t.deepEqual(compileJSONPointer(['foo', '/~ ~/']), '/foo/~1~0 ~0~1')
2016-10-28 17:04:06 +08:00
t.deepEqual(compileJSONPointer(['']), '/')
t.deepEqual(compileJSONPointer([]), '')
2016-09-09 17:01:06 +08:00
})
2016-11-12 21:10:10 +08:00
test('add and remove errors', t => {
2017-12-14 21:30:02 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const jsonSchemaErrors = [
{dataPath: '/obj/arr/2/last', message: 'String expected'},
{dataPath: '/nill', message: 'Null expected'}
]
2017-12-14 23:40:55 +08:00
const actual1 = applyErrors(eson, jsonSchemaErrors)
2017-12-14 21:30:02 +08:00
let expected = eson
2017-12-15 21:02:42 +08:00
expected = setIn(expected, ['obj', 'arr', '2', 'last', META, 'error'], jsonSchemaErrors[0])
expected = setIn(expected, ['nill', META, 'error'], jsonSchemaErrors[1])
assertDeepEqualEson(t, actual1, expected)
2017-12-14 21:30:02 +08:00
// re-applying the same errors should not change eson
2017-12-14 23:40:55 +08:00
const actual2 = applyErrors(actual1, jsonSchemaErrors)
2017-12-14 21:30:02 +08:00
t.is(actual2, actual1)
2017-12-14 21:30:02 +08:00
// clear errors
2017-12-14 23:40:55 +08:00
const actual3 = applyErrors(actual2, [])
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, actual3, eson)
2017-12-14 21:30:02 +08:00
t.is(actual3.str, eson.str) // shouldn't have touched values not affected by the errors
2016-11-20 03:44:58 +08:00
})
2016-11-20 04:21:09 +08:00
test('search', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const searchResult = search(eson, 'L')
const esonWithSearch = searchResult.eson
const matches = searchResult.matches
const active = searchResult.active
t.deepEqual(matches, [
{path: ['obj', 'arr', '2', 'last'], area: 'property'},
2017-11-03 21:23:04 +08:00
{path: ['str'], area: 'value'},
{path: ['nill'], area: 'property'},
{path: ['nill'], area: 'value'},
{path: ['bool'], area: 'property'},
{path: ['bool'], area: 'value'}
])
t.deepEqual(active, {path: ['obj', 'arr', '2', 'last'], area: 'property'})
let expected = esonWithSearch
2017-12-15 21:02:42 +08:00
expected = setIn(expected, ['obj', 'arr', '2', 'last', META, 'searchProperty'], 'active')
expected = setIn(expected, ['str', META, 'searchValue'], 'normal')
expected = setIn(expected, ['nill', META, 'searchProperty'], 'normal')
expected = setIn(expected, ['nill', META, 'searchValue'], 'normal')
expected = setIn(expected, ['bool', META, 'searchProperty'], 'normal')
expected = setIn(expected, ['bool', META, 'searchValue'], 'normal')
assertDeepEqualEson(t, esonWithSearch, expected)
2016-11-12 21:10:10 +08:00
})
test('nextSearchResult', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const searchResult = search(eson, 'A')
t.deepEqual(searchResult.matches, [
{path: ['obj', 'arr'], area: 'property'},
{path: ['obj', 'arr', '2', 'last'], area: 'property'},
{path: ['bool'], area: 'value'}
])
t.deepEqual(searchResult.active, {path: ['obj', 'arr'], area: 'property'})
2017-12-15 21:02:42 +08:00
t.is(getIn(searchResult.eson, ['obj', 'arr', META, 'searchProperty']), 'active')
t.is(getIn(searchResult.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'normal')
2017-12-15 21:02:42 +08:00
t.is(getIn(searchResult.eson, ['bool', META, 'searchValue']), 'normal')
const second = nextSearchResult(searchResult.eson, searchResult.matches, searchResult.active)
t.deepEqual(second.active, {path: ['obj', 'arr', '2', 'last'], area: 'property'})
2017-12-15 21:02:42 +08:00
t.is(getIn(second.eson, ['obj', 'arr', META, 'searchProperty']), 'normal')
t.is(getIn(second.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'active')
2017-12-15 21:02:42 +08:00
t.is(getIn(second.eson, ['bool', META, 'searchValue']), 'normal')
const third = nextSearchResult(second.eson, second.matches, second.active)
t.deepEqual(third.active, {path: ['bool'], area: 'value'})
2017-12-15 21:02:42 +08:00
t.is(getIn(third.eson, ['obj', 'arr', META, 'searchProperty']), 'normal')
t.is(getIn(third.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'normal')
2017-12-15 21:02:42 +08:00
t.is(getIn(third.eson, ['bool', META, 'searchValue']), 'active')
const wrappedAround = nextSearchResult(third.eson, third.matches, third.active)
t.deepEqual(wrappedAround.active, {path: ['obj', 'arr'], area: 'property'})
2017-12-15 21:02:42 +08:00
t.is(getIn(wrappedAround.eson, ['obj', 'arr', META, 'searchProperty']), 'active')
t.is(getIn(wrappedAround.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'normal')
2017-12-15 21:02:42 +08:00
t.is(getIn(wrappedAround.eson, ['bool', META, 'searchValue']), 'normal')
})
test('previousSearchResult', t => {
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const searchResult = search(eson, 'A')
t.deepEqual(searchResult.matches, [
{path: ['obj', 'arr'], area: 'property'},
{path: ['obj', 'arr', '2', 'last'], area: 'property'},
{path: ['bool'], area: 'value'}
])
t.deepEqual(searchResult.active, {path: ['obj', 'arr'], area: 'property'})
2017-12-15 21:02:42 +08:00
t.is(getIn(searchResult.eson, ['obj', 'arr', META, 'searchProperty']), 'active')
t.is(getIn(searchResult.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'normal')
2017-12-15 21:02:42 +08:00
t.is(getIn(searchResult.eson, ['bool', META, 'searchValue']), 'normal')
const third = previousSearchResult(searchResult.eson, searchResult.matches, searchResult.active)
t.deepEqual(third.active, {path: ['bool'], area: 'value'})
2017-12-15 21:02:42 +08:00
t.is(getIn(third.eson, ['obj', 'arr', META, 'searchProperty']), 'normal')
t.is(getIn(third.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'normal')
2017-12-15 21:02:42 +08:00
t.is(getIn(third.eson, ['bool', META, 'searchValue']), 'active')
const second = previousSearchResult(third.eson, third.matches, third.active)
t.deepEqual(second.active, {path: ['obj', 'arr', '2', 'last'], area: 'property'})
2017-12-15 21:02:42 +08:00
t.is(getIn(second.eson, ['obj', 'arr', META, 'searchProperty']), 'normal')
t.is(getIn(second.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'active')
2017-12-15 21:02:42 +08:00
t.is(getIn(second.eson, ['bool', META, 'searchValue']), 'normal')
const first = previousSearchResult(second.eson, second.matches, second.active)
t.deepEqual(first.active, {path: ['obj', 'arr'], area: 'property'})
2017-12-15 21:02:42 +08:00
t.is(getIn(first.eson, ['obj', 'arr', META, 'searchProperty']), 'active')
t.is(getIn(first.eson, ['obj', 'arr', '2', 'last', META, 'searchProperty']), 'normal')
2017-12-15 21:02:42 +08:00
t.is(getIn(first.eson, ['bool', META, 'searchValue']), 'normal')
})
test('selection (object)', t => {
2017-12-14 23:40:55 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr', '2', 'last'],
end: ['nill']
}
2017-12-14 23:40:55 +08:00
const actual = applySelection(eson, selection)
2017-12-14 23:40:55 +08:00
let expected = eson
2017-12-15 21:02:42 +08:00
expected = setIn(expected, ['obj', META, 'selected'], SELECTED)
expected = setIn(expected, ['str', META, 'selected'], SELECTED)
expected = setIn(expected, ['nill', META, 'selected'], SELECTED_END)
assertDeepEqualEson(t, actual, expected)
2017-12-14 23:40:55 +08:00
// test whether old selection results are cleaned up
const selection2 = {
start: ['nill'],
end: ['bool']
}
const actual2 = applySelection(actual, selection2)
let expected2 = eson
2017-12-15 21:02:42 +08:00
expected2 = setIn(expected2, ['nill', META, 'selected'], SELECTED)
expected2 = setIn(expected2, ['bool', META, 'selected'], SELECTED_END)
assertDeepEqualEson(t, actual2, expected2)
})
test('selection (array)', t => {
2017-12-14 23:40:55 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr', '1'],
end: ['obj', 'arr', '0'] // note the "wrong" order of start and end
}
2017-12-14 23:40:55 +08:00
const actual = applySelection(eson, selection)
2017-12-14 23:40:55 +08:00
let expected = eson
2017-12-15 21:02:42 +08:00
expected = setIn(expected, ['obj', 'arr', '0', META, 'selected'], SELECTED_END)
expected = setIn(expected, ['obj', 'arr', '1', META, 'selected'], SELECTED)
2017-12-15 21:02:42 +08:00
assertDeepEqualEson(t, actual, expected)
})
test('selection (value)', t => {
2017-12-14 23:40:55 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr', '2', 'first'],
end: ['obj', 'arr', '2', 'first']
}
2017-12-14 23:40:55 +08:00
const actual = applySelection(eson, selection)
2017-12-15 21:02:42 +08:00
const expected = setIn(eson, ['obj', 'arr', '2', 'first', META, 'selected'], SELECTED_END)
assertDeepEqualEson(t, actual, expected)
})
2017-09-29 17:08:44 +08:00
test('selection (node)', t => {
2017-12-14 23:40:55 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr'],
end: ['obj', 'arr']
}
2017-12-14 23:40:55 +08:00
const actual = applySelection(eson, selection)
2017-12-15 21:02:42 +08:00
const expected = setIn(eson, ['obj', 'arr', META, 'selected'], SELECTED_END)
assertDeepEqualEson(t, actual, expected)
})
test('pathsFromSelection (object)', t => {
2017-12-16 03:34:07 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr', '2', 'last'],
end: ['nill']
}
2017-12-16 03:34:07 +08:00
t.deepEqual(pathsFromSelection(eson, selection), [
['obj'],
['str'],
['nill']
])
})
test('pathsFromSelection (array)', t => {
2017-12-16 03:34:07 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr', '1'],
end: ['obj', 'arr', '0'] // note the "wrong" order of start and end
}
2017-12-16 03:34:07 +08:00
t.deepEqual(pathsFromSelection(eson, selection), [
['obj', 'arr', '0'],
['obj', 'arr', '1']
])
})
test('pathsFromSelection (value)', t => {
2017-12-16 03:34:07 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
const selection = {
2017-11-22 17:12:59 +08:00
start: ['obj', 'arr', '2', 'first'],
end: ['obj', 'arr', '2', 'first']
}
2017-12-16 03:34:07 +08:00
t.deepEqual(pathsFromSelection(eson, selection), [
['obj', 'arr', '2', 'first'],
])
})
2017-11-22 17:12:59 +08:00
test('pathsFromSelection (before)', t => {
2017-12-16 03:34:07 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
2017-11-22 17:12:59 +08:00
const selection = {
before: ['obj', 'arr', '2', 'first']
}
2017-12-16 03:34:07 +08:00
t.deepEqual(pathsFromSelection(eson, selection), [])
2017-11-22 17:12:59 +08:00
})
test('pathsFromSelection (after)', t => {
2017-12-16 03:34:07 +08:00
const eson = jsonToEson({
"obj": {
"arr": [1,2, {"first":3,"last":4}]
},
"str": "hello world",
"nill": null,
"bool": false
})
2017-11-22 17:12:59 +08:00
const selection = {
after: ['obj', 'arr', '2', 'first']
}
2017-12-16 03:34:07 +08:00
t.deepEqual(pathsFromSelection(eson, selection), [])
2017-11-22 17:12:59 +08:00
})
// helper function to print JSON in the console
function printJSON (json, message = null) {
if (message) {
console.log(message)
}
console.log(JSON.stringify(json, null, 2))
}
2017-12-15 21:02:42 +08:00
function printESON (eson, message = null) {
if (message) {
console.log(message)
}
let data = []
transform(eson, function (value, path) {
// const strPath = padEnd(, 20)
// console.log(`${strPath} ${'value' in value[META] ? value[META].value : ''} ${JSON.stringify(value[META])}`)
data.push({
path: '[' + path.join(', ') + ']',
value: repeat(' ', path.length) + (value[META].type === 'Object'
? '{...}'
: value[META].type === 'Array'
? '[...]'
: JSON.stringify(value[META].value)),
meta: JSON.stringify(value[META])
})
return value
})
console.table(data)
}
// helper function to load a JSON file
function loadJSON (filename) {
return JSON.parse(readFileSync(__dirname + '/' + filename, 'utf-8'))
}