|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -import { parseJsonAst } from '@angular-devkit/core'; |
9 | | -import { HostTree } from '@angular-devkit/schematics'; |
| 8 | +import { JsonAstArray, JsonAstObject, JsonValue, parseJsonAst } from '@angular-devkit/core'; |
| 9 | +import { HostTree, UpdateRecorder } from '@angular-devkit/schematics'; |
10 | 10 | import { UnitTestTree } from '@angular-devkit/schematics/testing'; |
11 | | -import { insertPropertyInAstObjectInOrder } from './json-utils'; |
12 | | - |
13 | | -type Pojso = { |
14 | | - [key: string]: string; |
15 | | -}; |
| 11 | +import { |
| 12 | + appendPropertyInAstObject, |
| 13 | + appendValueInAstArray, |
| 14 | + insertPropertyInAstObjectInOrder, |
| 15 | +} from './json-utils'; |
16 | 16 |
|
17 | 17 | describe('json-utils', () => { |
| 18 | + const a = 'a'; |
| 19 | + const b = 'b'; |
| 20 | + const m = 'm'; |
| 21 | + const z = 'z'; |
18 | 22 | const filePath = '/temp'; |
19 | 23 | let tree: UnitTestTree; |
| 24 | + |
| 25 | + function runTest(testFn: Function, obj: JsonValue, indent: number): string { |
| 26 | + const content = JSON.stringify(obj, null, indent); |
| 27 | + tree.create(filePath, content); |
| 28 | + const ast = parseJsonAst(content); |
| 29 | + const rec = tree.beginUpdate(filePath); |
| 30 | + testFn(rec, ast); |
| 31 | + tree.commitUpdate(rec); |
| 32 | + |
| 33 | + const result = tree.readContent(filePath); |
| 34 | + // Clean up the tree by deleting the file. |
| 35 | + tree.delete(filePath); |
| 36 | + |
| 37 | + return result; |
| 38 | + } |
| 39 | + |
20 | 40 | beforeEach(() => { |
21 | 41 | tree = new UnitTestTree(new HostTree()); |
22 | 42 | }); |
23 | 43 |
|
24 | | - describe('insertPropertyInAstObjectInOrder', () => { |
25 | | - function runTest(obj: Pojso, prop: string, val: string): Pojso { |
26 | | - const content = JSON.stringify(obj, null, 2); |
27 | | - tree.create(filePath, content); |
28 | | - const ast = parseJsonAst(content); |
29 | | - const rec = tree.beginUpdate(filePath); |
30 | | - if (ast.kind === 'object') { |
31 | | - insertPropertyInAstObjectInOrder(rec, ast, prop, val, 2); |
| 44 | + describe('appendPropertyInAstObject', () => { |
| 45 | + it('should insert multiple props with different indentations', () => { |
| 46 | + const cases: Array<[{}, string, {}, number]> = [ |
| 47 | + // initial | prop | want | indent |
| 48 | + [{}, z, {z}, 0], |
| 49 | + [{z}, m, {z, m}, 0], |
| 50 | + [{m, z}, a, {m, z, a}, 0], |
| 51 | + [{a, m, z}, b, {a, m, z, b}, 0], |
| 52 | + [{}, z, {z}, 2], |
| 53 | + [{z}, m, {z, m}, 2], |
| 54 | + [{m, z}, a, {m, z, a}, 2], |
| 55 | + [{a, m, z}, b, {a, m, z, b}, 2], |
| 56 | + [{}, z, {z}, 4], |
| 57 | + [{z}, m, {z, m}, 4], |
| 58 | + [{m, z}, a, {m, z, a}, 4], |
| 59 | + [{a, m, z}, b, {a, m, z, b}, 4], |
| 60 | + ]; |
| 61 | + for (const c of cases) { |
| 62 | + const [initial, prop, want, indent] = c; |
| 63 | + const got = runTest((rec: UpdateRecorder, ast: JsonAstObject) => { |
| 64 | + expect(ast.kind).toBe('object'); |
| 65 | + appendPropertyInAstObject(rec, ast, prop, prop, indent); |
| 66 | + }, initial, indent); |
| 67 | + expect(got).toBe(JSON.stringify(want, null, indent)); |
| 68 | + expect(JSON.parse(got)).toEqual(want); |
32 | 69 | } |
33 | | - tree.commitUpdate(rec); |
34 | | - |
35 | | - const result = JSON.parse(tree.readContent(filePath)); |
36 | | - // Clean up the tree by deleting the file. |
37 | | - tree.delete(filePath); |
38 | | - |
39 | | - return result; |
40 | | - } |
| 70 | + }); |
| 71 | + }); |
41 | 72 |
|
| 73 | + describe('insertPropertyInAstObjectInOrder', () => { |
42 | 74 | it('should insert a first prop', () => { |
43 | | - const obj = { |
44 | | - m: 'm', |
45 | | - z: 'z', |
46 | | - }; |
47 | | - const result = runTest(obj, 'a', 'val'); |
48 | | - expect(Object.keys(result)).toEqual(['a', 'm', 'z']); |
| 75 | + const indent = 2; |
| 76 | + const result = runTest((rec: UpdateRecorder, ast: JsonAstObject) => { |
| 77 | + expect(ast.kind).toBe('object'); |
| 78 | + insertPropertyInAstObjectInOrder(rec, ast, a, a, indent); |
| 79 | + }, {m, z}, indent); |
| 80 | + expect(result).toBe(JSON.stringify({a, m, z}, null, indent)); |
| 81 | + expect(JSON.parse(result)).toEqual({a, m, z}); |
49 | 82 | }); |
50 | 83 |
|
51 | 84 | it('should insert a middle prop', () => { |
52 | | - const obj = { |
53 | | - a: 'a', |
54 | | - z: 'z', |
55 | | - }; |
56 | | - const result = runTest(obj, 'm', 'val'); |
57 | | - expect(Object.keys(result)).toEqual(['a', 'm', 'z']); |
| 85 | + const indent = 2; |
| 86 | + const result = runTest((rec: UpdateRecorder, ast: JsonAstObject) => { |
| 87 | + expect(ast.kind).toBe('object'); |
| 88 | + insertPropertyInAstObjectInOrder(rec, ast, m, m, indent); |
| 89 | + }, {a, z}, indent); |
| 90 | + expect(result).toBe(JSON.stringify({a, m, z}, null, indent)); |
| 91 | + expect(JSON.parse(result)).toEqual({a, m, z}); |
58 | 92 | }); |
59 | 93 |
|
60 | 94 | it('should insert a last prop', () => { |
61 | | - const obj = { |
62 | | - a: 'a', |
63 | | - m: 'm', |
64 | | - }; |
65 | | - const result = runTest(obj, 'z', 'val'); |
66 | | - expect(Object.keys(result)).toEqual(['a', 'm', 'z']); |
| 95 | + const indent = 2; |
| 96 | + const result = runTest((rec: UpdateRecorder, ast: JsonAstObject) => { |
| 97 | + expect(ast.kind).toBe('object'); |
| 98 | + insertPropertyInAstObjectInOrder(rec, ast, z, z, indent); |
| 99 | + }, {a, m}, indent); |
| 100 | + expect(result).toBe(JSON.stringify({a, m, z}, null, indent)); |
| 101 | + expect(JSON.parse(result)).toEqual({a, m, z}); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should insert multiple props with different indentations', () => { |
| 105 | + const cases: Array<[{}, string, {}, number]> = [ |
| 106 | + // initial | prop | want | indent |
| 107 | + [{}, z, {z}, 0], |
| 108 | + [{z}, m, {m, z}, 0], |
| 109 | + [{m, z}, a, {a, m, z}, 0], |
| 110 | + [{a, m, z}, b, {a, b, m, z}, 0], |
| 111 | + [{}, z, {z}, 2], |
| 112 | + [{z}, m, {m, z}, 2], |
| 113 | + [{m, z}, a, {a, m, z}, 2], |
| 114 | + [{a, m, z}, b, {a, b, m, z}, 2], |
| 115 | + [{}, z, {z}, 4], |
| 116 | + [{z}, m, {m, z}, 4], |
| 117 | + [{m, z}, a, {a, m, z}, 4], |
| 118 | + [{a, m, z}, b, {a, b, m, z}, 4], |
| 119 | + ]; |
| 120 | + for (const c of cases) { |
| 121 | + const [initial, prop, want, indent] = c; |
| 122 | + const got = runTest((rec: UpdateRecorder, ast: JsonAstObject) => { |
| 123 | + expect(ast.kind).toBe('object'); |
| 124 | + insertPropertyInAstObjectInOrder(rec, ast, prop, prop, indent); |
| 125 | + }, initial, indent); |
| 126 | + expect(got).toBe(JSON.stringify(want, null, indent)); |
| 127 | + expect(JSON.parse(got)).toEqual(want); |
| 128 | + } |
67 | 129 | }); |
| 130 | + }); |
68 | 131 |
|
69 | | - it('should insert multiple props', () => { |
70 | | - let obj = {}; |
71 | | - obj = runTest(obj, 'z', 'val'); |
72 | | - expect(Object.keys(obj)).toEqual(['z']); |
73 | | - obj = runTest(obj, 'm', 'val'); |
74 | | - expect(Object.keys(obj)).toEqual(['m', 'z']); |
75 | | - obj = runTest(obj, 'a', 'val'); |
76 | | - expect(Object.keys(obj)).toEqual(['a', 'm', 'z']); |
77 | | - obj = runTest(obj, 'b', 'val'); |
78 | | - expect(Object.keys(obj)).toEqual(['a', 'b', 'm', 'z']); |
| 132 | + describe('appendValueInAstArray', () => { |
| 133 | + it('should insert multiple props with different indentations', () => { |
| 134 | + const cases: Array<[string[], string, {}, number]> = [ |
| 135 | + // initial | value | want | indent |
| 136 | + [[], z, [z], 0], |
| 137 | + [[z], m, [z, m], 0], |
| 138 | + [[m, z], a, [m, z, a], 0], |
| 139 | + [[a, m, z], b, [a, m, z, b], 0], |
| 140 | + [[], z, [z], 2], |
| 141 | + [[z], m, [z, m], 2], |
| 142 | + [[m, z], a, [m, z, a], 2], |
| 143 | + [[a, m, z], b, [a, m, z, b], 2], |
| 144 | + [[], z, [z], 4], |
| 145 | + [[z], m, [z, m], 4], |
| 146 | + [[m, z], a, [m, z, a], 4], |
| 147 | + [[a, m, z], b, [a, m, z, b], 4], |
| 148 | + ]; |
| 149 | + for (const c of cases) { |
| 150 | + const [initial, value, want, indent] = c; |
| 151 | + const got = runTest((rec: UpdateRecorder, ast: JsonAstArray) => { |
| 152 | + expect(ast.kind).toBe('array'); |
| 153 | + appendValueInAstArray(rec, ast, value, indent); |
| 154 | + }, initial, indent); |
| 155 | + expect(got).toBe(JSON.stringify(want, null, indent)); |
| 156 | + expect(JSON.parse(got)).toEqual(want); |
| 157 | + } |
79 | 158 | }); |
80 | 159 | }); |
| 160 | + |
81 | 161 | }); |
0 commit comments