Skip to content

Commit ff813e9

Browse files
committed
edit tests: use modify
1 parent 5faa24d commit ff813e9

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

src/test/edit.test.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
'use strict';
66

77
import * as assert from 'assert';
8-
import { FormattingOptions, Edit, ModificationOptions } from '../main';
9-
import { setProperty, removeProperty } from '../impl/edit';
8+
import { FormattingOptions, Edit, ModificationOptions, modify } from '../main';
109

1110
suite('JSON - edits', () => {
1211

@@ -36,190 +35,190 @@ suite('JSON - edits', () => {
3635

3736
test('set property', () => {
3837
let content = '{\n "x": "y"\n}';
39-
let edits = setProperty(content, ['x'], 'bar', options);
38+
let edits = modify(content, ['x'], 'bar', options);
4039
assertEdit(content, edits, '{\n "x": "bar"\n}');
4140

4241
content = 'true';
43-
edits = setProperty(content, [], 'bar', options);
42+
edits = modify(content, [], 'bar', options);
4443
assertEdit(content, edits, '"bar"');
4544

4645
content = '{\n "x": "y"\n}';
47-
edits = setProperty(content, ['x'], { key: true }, options);
46+
edits = modify(content, ['x'], { key: true }, options);
4847
assertEdit(content, edits, '{\n "x": {\n "key": true\n }\n}');
4948

5049
content = '{\n "a": "b", "x": "y"\n}';
51-
edits = setProperty(content, ['a'], null, options);
50+
edits = modify(content, ['a'], null, options);
5251
assertEdit(content, edits, '{\n "a": null, "x": "y"\n}');
5352
});
5453

5554
test('insert property', () => {
5655
let content = '{}';
57-
let edits = setProperty(content, ['foo'], 'bar', options);
56+
let edits = modify(content, ['foo'], 'bar', options);
5857
assertEdit(content, edits, '{\n "foo": "bar"\n}');
5958

60-
edits = setProperty(content, ['foo', 'foo2'], 'bar', options);
59+
edits = modify(content, ['foo', 'foo2'], 'bar', options);
6160
assertEdit(content, edits, '{\n "foo": {\n "foo2": "bar"\n }\n}');
6261

6362
content = '{\n}';
64-
edits = setProperty(content, ['foo'], 'bar', options);
63+
edits = modify(content, ['foo'], 'bar', options);
6564
assertEdit(content, edits, '{\n "foo": "bar"\n}');
6665

6766
content = ' {\n }';
68-
edits = setProperty(content, ['foo'], 'bar', options);
67+
edits = modify(content, ['foo'], 'bar', options);
6968
assertEdit(content, edits, ' {\n "foo": "bar"\n }');
7069

7170
content = '{\n "x": "y"\n}';
72-
edits = setProperty(content, ['foo'], 'bar', options);
71+
edits = modify(content, ['foo'], 'bar', options);
7372
assertEdit(content, edits, '{\n "x": "y",\n "foo": "bar"\n}');
7473

7574
content = '{\n "x": "y"\n}';
76-
edits = setProperty(content, ['e'], 'null', options);
75+
edits = modify(content, ['e'], 'null', options);
7776
assertEdit(content, edits, '{\n "x": "y",\n "e": "null"\n}');
7877

79-
edits = setProperty(content, ['x'], 'bar', options);
78+
edits = modify(content, ['x'], 'bar', options);
8079
assertEdit(content, edits, '{\n "x": "bar"\n}');
8180

8281
content = '{\n "x": {\n "a": 1,\n "b": true\n }\n}\n';
83-
edits = setProperty(content, ['x'], 'bar', options);
82+
edits = modify(content, ['x'], 'bar', options);
8483
assertEdit(content, edits, '{\n "x": "bar"\n}\n');
8584

86-
edits = setProperty(content, ['x', 'b'], 'bar', options);
85+
edits = modify(content, ['x', 'b'], 'bar', options);
8786
assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "b": "bar"\n }\n}\n');
8887

89-
edits = setProperty(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 0 });
88+
edits = modify(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 0 });
9089
assertEdit(content, edits, '{\n "x": {\n "c": "bar",\n "a": 1,\n "b": true\n }\n}\n');
9190

92-
edits = setProperty(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 1 });
91+
edits = modify(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 1 });
9392
assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "c": "bar",\n "b": true\n }\n}\n');
9493

95-
edits = setProperty(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 2 });
94+
edits = modify(content, ['x', 'c'], 'bar', { formattingOptions, getInsertionIndex: () => 2 });
9695
assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "b": true,\n "c": "bar"\n }\n}\n');
9796

98-
edits = setProperty(content, ['c'], 'bar', options);
97+
edits = modify(content, ['c'], 'bar', options);
9998
assertEdit(content, edits, '{\n "x": {\n "a": 1,\n "b": true\n },\n "c": "bar"\n}\n');
10099

101100
content = '{\n "a": [\n {\n } \n ] \n}';
102-
edits = setProperty(content, ['foo'], 'bar', options);
101+
edits = modify(content, ['foo'], 'bar', options);
103102
assertEdit(content, edits, '{\n "a": [\n {\n } \n ],\n "foo": "bar"\n}');
104103

105104
content = '';
106-
edits = setProperty(content, ['foo', 0], 'bar', options);
105+
edits = modify(content, ['foo', 0], 'bar', options);
107106
assertEdit(content, edits, '{\n "foo": [\n "bar"\n ]\n}');
108107

109108
content = '//comment';
110-
edits = setProperty(content, ['foo', 0], 'bar', options);
109+
edits = modify(content, ['foo', 0], 'bar', options);
111110
assertEdit(content, edits, '{\n "foo": [\n "bar"\n ]\n} //comment');
112111
});
113112

114113
test('remove property', () => {
115114
let content = '{\n "x": "y"\n}';
116-
let edits = removeProperty(content, ['x'], options);
115+
let edits = modify(content, ['x'], undefined, options);
117116
assertEdit(content, edits, '{\n}');
118117

119118
content = '{\n "x": "y", "a": []\n}';
120-
edits = removeProperty(content, ['x'], options);
119+
edits = modify(content, ['x'], undefined, options);
121120
assertEdit(content, edits, '{\n "a": []\n}');
122121

123122
content = '{\n "x": "y", "a": []\n}';
124-
edits = removeProperty(content, ['a'], options);
123+
edits = modify(content, ['a'], undefined, options);
125124
assertEdit(content, edits, '{\n "x": "y"\n}');
126125
});
127126

128127
test('set item', () => {
129128
let content = '{\n "x": [1, 2, 3],\n "y": 0\n}';
130129

131-
let edits = setProperty(content, ['x', 0], 6, options);
130+
let edits = modify(content, ['x', 0], 6, options);
132131
assertEdit(content, edits, '{\n "x": [6, 2, 3],\n "y": 0\n}');
133132

134-
edits = setProperty(content, ['x', 1], 5, options);
133+
edits = modify(content, ['x', 1], 5, options);
135134
assertEdit(content, edits, '{\n "x": [1, 5, 3],\n "y": 0\n}');
136135

137-
edits = setProperty(content, ['x', 2], 4, options);
136+
edits = modify(content, ['x', 2], 4, options);
138137
assertEdit(content, edits, '{\n "x": [1, 2, 4],\n "y": 0\n}');
139138

140-
edits = setProperty(content, ['x', 3], 3, options);
139+
edits = modify(content, ['x', 3], 3, options);
141140
assertEdit(content, edits, '{\n "x": [\n 1,\n 2,\n 3,\n 3\n ],\n "y": 0\n}');
142141
});
143142

144143
test('insert item at 0; isArrayInsertion = true', () => {
145144
let content = '[\n 2,\n 3\n]';
146-
let edits = setProperty(content, [0], 1, { formattingOptions, isArrayInsertion: true });
145+
let edits = modify(content, [0], 1, { formattingOptions, isArrayInsertion: true });
147146
assertEdit(content, edits, '[\n 1,\n 2,\n 3\n]');
148147
});
149148

150149
test('insert item at 0 in empty array', () => {
151150
let content = '[\n]';
152-
let edits = setProperty(content, [0], 1, options);
151+
let edits = modify(content, [0], 1, options);
153152
assertEdit(content, edits, '[\n 1\n]');
154153
});
155154

156155
test('insert item at an index; isArrayInsertion = true', () => {
157156
let content = '[\n 1,\n 3\n]';
158-
let edits = setProperty(content, [1], 2, { formattingOptions, isArrayInsertion: true });
157+
let edits = modify(content, [1], 2, { formattingOptions, isArrayInsertion: true });
159158
assertEdit(content, edits, '[\n 1,\n 2,\n 3\n]');
160159
});
161160

162161
test('insert item at an index in empty array', () => {
163162
let content = '[\n]';
164-
let edits = setProperty(content, [1], 1, options);
163+
let edits = modify(content, [1], 1, options);
165164
assertEdit(content, edits, '[\n 1\n]');
166165
});
167166

168167
test('insert item at end index', () => {
169168
let content = '[\n 1,\n 2\n]';
170-
let edits = setProperty(content, [2], 3, options);
169+
let edits = modify(content, [2], 3, options);
171170
assertEdit(content, edits, '[\n 1,\n 2,\n 3\n]');
172171
});
173172

174173
test('insert item at end to empty array', () => {
175174
let content = '[\n]';
176-
let edits = setProperty(content, [-1], 'bar', options);
175+
let edits = modify(content, [-1], 'bar', options);
177176
assertEdit(content, edits, '[\n "bar"\n]');
178177
});
179178

180179
test('insert item at end', () => {
181180
let content = '[\n 1,\n 2\n]';
182-
let edits = setProperty(content, [-1], 'bar', options);
181+
let edits = modify(content, [-1], 'bar', options);
183182
assertEdit(content, edits, '[\n 1,\n 2,\n "bar"\n]');
184183
});
185184

186185
test('remove item in array with one item', () => {
187186
let content = '[\n 1\n]';
188-
let edits = setProperty(content, [0], void 0, options);
187+
let edits = modify(content, [0], void 0, options);
189188
assertEdit(content, edits, '[]');
190189
});
191190

192191
test('remove item in the middle of the array', () => {
193192
let content = '[\n 1,\n 2,\n 3\n]';
194-
let edits = setProperty(content, [1], void 0, options);
193+
let edits = modify(content, [1], void 0, options);
195194
assertEdit(content, edits, '[\n 1,\n 3\n]');
196195
});
197196

198197
test('remove last item in the array', () => {
199198
let content = '[\n 1,\n 2,\n "bar"\n]';
200-
let edits = setProperty(content, [2], void 0, options);
199+
let edits = modify(content, [2], void 0, options);
201200
assertEdit(content, edits, '[\n 1,\n 2\n]');
202201
});
203202

204203
test('remove last item in the array if ends with comma', () => {
205204
let content = '[\n 1,\n "foo",\n "bar",\n]';
206-
let edits = setProperty(content, [2], void 0, options);
205+
let edits = modify(content, [2], void 0, options);
207206
assertEdit(content, edits, '[\n 1,\n "foo"\n]');
208207
});
209208

210209
test('remove last item in the array if there is a comment in the beginning', () => {
211210
let content = '// This is a comment\n[\n 1,\n "foo",\n "bar"\n]';
212-
let edits = setProperty(content, [2], void 0, options);
211+
let edits = modify(content, [2], void 0, options);
213212
assertEdit(content, edits, '// This is a comment\n[\n 1,\n "foo"\n]');
214213
});
215214

216215
test('set property without formatting', () => {
217216
let content = '{\n "x": [1, 2, 3],\n "y": 0\n}';
218217

219-
let edits = setProperty(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions });
218+
let edits = modify(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions });
220219
assertEdit(content, edits, '{\n "x": [{\n "a": 1,\n "b": 2\n }, 2, 3],\n "y": 0\n}');
221220

222-
edits = setProperty(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions: undefined });
221+
edits = modify(content, ['x', 0], { a: 1, b: 2 }, { formattingOptions: undefined });
223222
assertEdit(content, edits, '{\n "x": [{"a":1,"b":2}, 2, 3],\n "y": 0\n}');
224223
});
225224
});

0 commit comments

Comments
 (0)