From ff813e9f741b2b35f8bea2dd7022a3a43156d04a Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Wed, 2 Dec 2020 11:42:55 +0100 Subject: [PATCH] edit tests: use modify --- src/test/edit.test.ts | 85 +++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/src/test/edit.test.ts b/src/test/edit.test.ts index 1022a01..808c906 100644 --- a/src/test/edit.test.ts +++ b/src/test/edit.test.ts @@ -5,8 +5,7 @@ 'use strict'; import * as assert from 'assert'; -import { FormattingOptions, Edit, ModificationOptions } from '../main'; -import { setProperty, removeProperty } from '../impl/edit'; +import { FormattingOptions, Edit, ModificationOptions, modify } from '../main'; suite('JSON - edits', () => { @@ -36,190 +35,190 @@ suite('JSON - edits', () => { test('set property', () => { let content = '{\n "x": "y"\n}'; - let edits = setProperty(content, ['x'], 'bar', options); + let edits = modify(content, ['x'], 'bar', options); assertEdit(content, edits, '{\n "x": "bar"\n}'); content = 'true'; - edits = setProperty(content, [], 'bar', options); + edits = modify(content, [], 'bar', options); assertEdit(content, edits, '"bar"'); content = '{\n "x": "y"\n}'; - edits = setProperty(content, ['x'], { key: true }, options); + edits = modify(content, ['x'], { key: true }, options); assertEdit(content, edits, '{\n "x": {\n "key": true\n }\n}'); content = '{\n "a": "b", "x": "y"\n}'; - edits = setProperty(content, ['a'], null, options); + edits = modify(content, ['a'], null, options); assertEdit(content, edits, '{\n "a": null, "x": "y"\n}'); }); test('insert property', () => { let content = '{}'; - let edits = setProperty(content, ['foo'], 'bar', options); + let edits = modify(content, ['foo'], 'bar', options); assertEdit(content, edits, '{\n "foo": "bar"\n}'); - edits = setProperty(content, ['foo', 'foo2'], 'bar', options); + edits = modify(content, ['foo', 'foo2'], 'bar', options); assertEdit(content, edits, '{\n "foo": {\n "foo2": "bar"\n }\n}'); content = '{\n}'; - edits = setProperty(content, ['foo'], 'bar', options); + edits = modify(content, ['foo'], 'bar', options); assertEdit(content, edits, '{\n "foo": "bar"\n}'); content = ' {\n }'; - edits = setProperty(content, ['foo'], 'bar', options); + edits = modify(content, ['foo'], 'bar', options); assertEdit(content, edits, ' {\n "foo": "bar"\n }'); content = '{\n "x": "y"\n}'; - edits = setProperty(content, ['foo'], 'bar', options); + edits = modify(content, ['foo'], 'bar', options); assertEdit(content, edits, '{\n "x": "y",\n "foo": "bar"\n}'); content = '{\n "x": "y"\n}'; - edits = setProperty(content, ['e'], 'null', options); + edits = modify(content, ['e'], 'null', options); assertEdit(content, edits, '{\n "x": "y",\n "e": "null"\n}'); - edits = setProperty(content, ['x'], 'bar', options); + edits = modify(content, ['x'], 'bar', options); assertEdit(content, edits, '{\n "x": "bar"\n}'); content = '{\n "x": {\n "a": 1,\n "b": true\n }\n}\n'; - edits = setProperty(content, ['x'], 'bar', options); + edits = modify(content, ['x'], 'bar', options); assertEdit(content, edits, '{\n "x": "bar"\n}\n'); - edits = setProperty(content, ['x', 'b'], 'bar', options); + edits = modify(content, ['x', 'b'], 'bar', options); assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "b": "bar"\n }\n}\n'); - edits = setProperty(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 0 }); + edits = modify(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 0 }); assertEdit(content, edits, '{\n "x": {\n "c": "bar",\n "a": 1,\n "b": true\n }\n}\n'); - edits = setProperty(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 1 }); + edits = modify(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 1 }); assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "c": "bar",\n "b": true\n }\n}\n'); - edits = setProperty(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 2 }); + edits = modify(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 2 }); assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "b": true,\n "c": "bar"\n }\n}\n'); - edits = setProperty(content, ['c'], 'bar', options); + edits = modify(content, ['c'], 'bar', options); assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "b": true\n },\n "c": "bar"\n}\n'); content = '{\n "a": [\n {\n } \n ] \n}'; - edits = setProperty(content, ['foo'], 'bar', options); + edits = modify(content, ['foo'], 'bar', options); assertEdit(content, edits, '{\n "a": [\n {\n } \n ],\n "foo": "bar"\n}'); content = ''; - edits = setProperty(content, ['foo', 0], 'bar', options); + edits = modify(content, ['foo', 0], 'bar', options); assertEdit(content, edits, '{\n "foo": [\n "bar"\n ]\n}'); content = '//comment'; - edits = setProperty(content, ['foo', 0], 'bar', options); + edits = modify(content, ['foo', 0], 'bar', options); assertEdit(content, edits, '{\n "foo": [\n "bar"\n ]\n} //comment'); }); test('remove property', () => { let content = '{\n "x": "y"\n}'; - let edits = removeProperty(content, ['x'], options); + let edits = modify(content, ['x'], undefined, options); assertEdit(content, edits, '{\n}'); content = '{\n "x": "y", "a": []\n}'; - edits = removeProperty(content, ['x'], options); + edits = modify(content, ['x'], undefined, options); assertEdit(content, edits, '{\n "a": []\n}'); content = '{\n "x": "y", "a": []\n}'; - edits = removeProperty(content, ['a'], options); + edits = modify(content, ['a'], undefined, options); assertEdit(content, edits, '{\n "x": "y"\n}'); }); test('set item', () => { let content = '{\n "x": [1, 2, 3],\n "y": 0\n}'; - let edits = setProperty(content, ['x', 0], 6, options); + let edits = modify(content, ['x', 0], 6, options); assertEdit(content, edits, '{\n "x": [6, 2, 3],\n "y": 0\n}'); - edits = setProperty(content, ['x', 1], 5, options); + edits = modify(content, ['x', 1], 5, options); assertEdit(content, edits, '{\n "x": [1, 5, 3],\n "y": 0\n}'); - edits = setProperty(content, ['x', 2], 4, options); + edits = modify(content, ['x', 2], 4, options); assertEdit(content, edits, '{\n "x": [1, 2, 4],\n "y": 0\n}'); - edits = setProperty(content, ['x', 3], 3, options); + edits = modify(content, ['x', 3], 3, options); assertEdit(content, edits, '{\n "x": [\n 1,\n 2,\n 3,\n 3\n ],\n "y": 0\n}'); }); test('insert item at 0; isArrayInsertion = true', () => { let content = '[\n 2,\n 3\n]'; - let edits = setProperty(content, [0], 1, { formattingOptions, isArrayInsertion: true }); + let edits = modify(content, [0], 1, { formattingOptions, isArrayInsertion: true }); assertEdit(content, edits, '[\n 1,\n 2,\n 3\n]'); }); test('insert item at 0 in empty array', () => { let content = '[\n]'; - let edits = setProperty(content, [0], 1, options); + let edits = modify(content, [0], 1, options); assertEdit(content, edits, '[\n 1\n]'); }); test('insert item at an index; isArrayInsertion = true', () => { let content = '[\n 1,\n 3\n]'; - let edits = setProperty(content, [1], 2, { formattingOptions, isArrayInsertion: true }); + let edits = modify(content, [1], 2, { formattingOptions, isArrayInsertion: true }); assertEdit(content, edits, '[\n 1,\n 2,\n 3\n]'); }); test('insert item at an index in empty array', () => { let content = '[\n]'; - let edits = setProperty(content, [1], 1, options); + let edits = modify(content, [1], 1, options); assertEdit(content, edits, '[\n 1\n]'); }); test('insert item at end index', () => { let content = '[\n 1,\n 2\n]'; - let edits = setProperty(content, [2], 3, options); + let edits = modify(content, [2], 3, options); assertEdit(content, edits, '[\n 1,\n 2,\n 3\n]'); }); test('insert item at end to empty array', () => { let content = '[\n]'; - let edits = setProperty(content, [-1], 'bar', options); + let edits = modify(content, [-1], 'bar', options); assertEdit(content, edits, '[\n "bar"\n]'); }); test('insert item at end', () => { let content = '[\n 1,\n 2\n]'; - let edits = setProperty(content, [-1], 'bar', options); + let edits = modify(content, [-1], 'bar', options); assertEdit(content, edits, '[\n 1,\n 2,\n "bar"\n]'); }); test('remove item in array with one item', () => { let content = '[\n 1\n]'; - let edits = setProperty(content, [0], void 0, options); + let edits = modify(content, [0], void 0, options); assertEdit(content, edits, '[]'); }); test('remove item in the middle of the array', () => { let content = '[\n 1,\n 2,\n 3\n]'; - let edits = setProperty(content, [1], void 0, options); + let edits = modify(content, [1], void 0, options); assertEdit(content, edits, '[\n 1,\n 3\n]'); }); test('remove last item in the array', () => { let content = '[\n 1,\n 2,\n "bar"\n]'; - let edits = setProperty(content, [2], void 0, options); + let edits = modify(content, [2], void 0, options); assertEdit(content, edits, '[\n 1,\n 2\n]'); }); test('remove last item in the array if ends with comma', () => { let content = '[\n 1,\n "foo",\n "bar",\n]'; - let edits = setProperty(content, [2], void 0, options); + let edits = modify(content, [2], void 0, options); assertEdit(content, edits, '[\n 1,\n "foo"\n]'); }); test('remove last item in the array if there is a comment in the beginning', () => { let content = '// This is a comment\n[\n 1,\n "foo",\n "bar"\n]'; - let edits = setProperty(content, [2], void 0, options); + let edits = modify(content, [2], void 0, options); assertEdit(content, edits, '// This is a comment\n[\n 1,\n "foo"\n]'); }); test('set property without formatting', () => { let content = '{\n "x": [1, 2, 3],\n "y": 0\n}'; - let edits = setProperty(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions }); + let edits = modify(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions }); assertEdit(content, edits, '{\n "x": [{\n "a": 1,\n "b": 2\n }, 2, 3],\n "y": 0\n}'); - edits = setProperty(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions: undefined }); + edits = modify(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions: undefined }); assertEdit(content, edits, '{\n "x": [{"a":1,"b":2}, 2, 3],\n "y": 0\n}'); }); }); \ No newline at end of file