-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclass_decorator_test.ts
254 lines (220 loc) · 10.4 KB
/
class_decorator_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import merge from '../src/index';
import { expect } from 'chai';
import 'mocha';
describe('Merging class decorators', () => {
describe('should add the decorator from', () => {
const base = "@deca class a {}", patch = "@decb class a {}";
it('the patch.', () => {
const result: String[] = merge(base, patch, false)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.indexOf('@decb'))
.to.be.greaterThan(-1, 'decoration from patch should be present at class a');
expect(result.indexOf('@deca'))
.to.be.greaterThan(-1, 'decoration from base should be present at class a');
});
it('the patch with patchOverride.', () => {
const result: String[] = merge(base, patch, true)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.indexOf('@decb'))
.to.be.greaterThan(-1, 'decoration from patch should be present in class a');
expect(result.indexOf('@deca'))
.to.be.greaterThan(-1, 'decoration from base should be present at class a');
});
});
describe('should use the value from', () => {
const base = "@deca(true) class a {}", patch = "@deca(false) class a {}";
it('the base if decorator is present in base and patch.', () => {
const result: String[] = merge(base, patch, false)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.indexOf('@deca(true)')).to.be.greaterThan(-1, 'decoration should have value from base');
});
it('the patch if decoration is present in base and patch, and patchOverride is true.', () => {
const result: String[] = merge(base, patch, true)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.indexOf('@deca(false)')).to.be.greaterThan(-1, 'decoration should have value from patch');
});
});
describe('should merge the NgModule decorator properties', () => {
const base = `@NgModule({
providers: [a, b]
})
class a {}`,
patch = `@NgModule({
id: '1'
})
class a {}`;
it('preserving patch', () => {
const fullResult: String = merge(base, patch, false);
const result: String[] = fullResult.split("\n").map(value => value.trim()).filter(value => value != "");
const concatResult: String = result.reduce((prev, curr) => prev.toString() + curr.toString(), "");
let ngModuleRegex = /@NgModule\(\{.*/;
let ngModuleFullRegex = /@NgModule\(\{.*,*.*\}\).*/;
let providerRegex = /.*providers:\s*\[\s*a\s*,\s*b\s*\].*/;
let idRegex = /.*id:\s*'1'.*/;
expect(result.filter(value => ngModuleRegex.test(value.toString())).length).to.be.equal(1, 'There should exactly be one @NgModule decorator');
//expect(result.filter(value => providerRegex.test(value.toString())).length).to.be.equal(1, 'There should exactly be one providers property in the NgModule');
expect(providerRegex.test(concatResult.toString()), 'The "providers" property should be present').to.be.true;
expect(idRegex.test(concatResult.toString()), 'The "id" property should be present').to.be.true;
expect(ngModuleFullRegex.test(concatResult.toString()), 'The @NgModule decorator should be valid TypeScript').to.be.true;
});
it('with patchOverride', () => {
const fullResult: String = merge(base, patch, true);
const result: String[] = fullResult.split("\n").map(value => value.trim()).filter(value => value != "");
const concatResult: String = result.reduce((prev, curr) => prev.toString() + curr.toString(), "");
console.log(concatResult);
let ngModuleRegex = /@NgModule\(\{.*/;
let ngModuleFullRegex = /@NgModule\(\{.*,.*\}\)/;
let providerExistenceRegex = /.*providers\s*:.*/;
let providerRegex = /.*providers:\s*\[\s*a\s*,\s*b\s*\].*/;
let idExistenceRegex = /.*id\s*:.*/;
let idRegex = /.*id\s*:\s*'1'.*/;
expect(result.filter(value => ngModuleRegex.test(value.toString())).length).to.be.equal(1, 'There should exactly be one @NgModule decorator');
expect(result.filter(value => providerExistenceRegex.test(value.toString())).length).to.be.equal(1, 'There should exactly be one providers property in the NgModule');
expect(result.filter(value => idExistenceRegex.test(value.toString())).length).to.be.equal(1, 'There should exactly be one id property in the NgModule');
expect(ngModuleFullRegex.test(concatResult.toString()), 'The @NgModule decorator should be valid TypeScript').to.be.true;
});
});
describe('should use the value from', () => {
const base = `@NgModule({
id: '1'
})
class a {}`,
patch = `@NgModule({
id: '2'
})
class a {}`;
it('the base if NgModule property is present in base and patch.', () => {
const result: String[] = merge(base, patch, false)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.filter(value => /[^]*id: '1'[^]*/.test(value.toString())).length).to.be.equal(1, 'id should have value from base');
});
it('the patch if ngModule property is present in base and patch, and patchOverride is true.', () => {
const result: String[] = merge(base, patch, true)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.filter(value => /[^]*id: '2'[^]*/.test(value.toString())).length).to.be.equal(1, 'id should have value from patch');
});
});
describe('should add the patch decorator', () => {
const base = `class a {}`, patch = `@deca class a {}`;
it('although base has none.', () => {
const result: String[] = merge(base, patch, false)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.filter(value => /@deca.*/.test(value.toString())).length).to.be.equal(1, 'id should have value from patch');
});
it('although base base none with patchOverride.', () => {
const result: String[] = merge(base, patch, true)
.split("\n")
.map(value => value.trim())
.filter(value => value != "");
expect(result.filter(value => /@deca.*/.test(value.toString())).length).to.be.equal(1, 'id should have value from patch');
});
});
describe('should use the property value from', () => {
const base = `@decorator("a", "b") class a {}`, patch = `@decorator("c", "d") class a {}`;
it('should use the property value from the base.', () => {
const result = merge(base, patch, false)
.split("\n")
.map(value => value.trim())
.filter(value => value != "").reduce((prev, curr) => prev.toString() + curr.toString(), "");
const baseDecoratorRegex = /@decorator\(\s*["']a["']\s*,\s*["']b["']\s*\).*/;
expect(baseDecoratorRegex.test(result.toString()), 'id of the base should be present').to.be.true;
});
it('should use the property value from the patch with patchOverride.', () => {
const result = merge(base, patch, true)
.split("\n")
.map(value => value.trim())
.filter(value => value != "").reduce((prev, curr) => prev.toString() + curr.toString(), "");
const patchDecoratorRegex = /@decorator\(\s*["']c["']\s*,\s*["']d["']\s*\).*/;
expect(patchDecoratorRegex.test(result.toString()), 'id of the patch should be present').to.be.true;
});
});
describe('should not merge the decorator argument since 2 are present', () => {
const base = `@decorator([provider:a], "b") class a {}`, patch = `@decorator([id:b], "b") class a {}`;
it('preserving base.', () => {
const result = merge(base, patch, false)
.split("\n")
.map(value => value.trim())
.filter(value => value != "").reduce((prev, curr) => prev.toString() + curr.toString(), "");
const patchDecoratorRegex = /.*id.*/;
expect(patchDecoratorRegex.test(result.toString()), 'id of the base should be present').to.be.false;
});
it('should not merge the decorator argument since 2 are present with patchOverride.', () => {
const result = merge(base, patch, true)
.split("\n")
.map(value => value.trim())
.filter(value => value != "").reduce((prev, curr) => prev.toString() + curr.toString(), "");
const baseDecoratorRegex = /.*provider.*/;
expect(baseDecoratorRegex.test(result.toString()), 'id of the patch should be present').to.be.false;
});
});
});
describe('Property without initialized value', () => {
const base = `
@NgModule({
declarations: [AppComponent],
imports: [
StoreModule.forRoot(reducers, {
metaReducers,
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true,
},
}),
],
providers: [],
bootstrap: [],
})
export class AppModule {}
`,
patch = `
@NgModule({
declarations: [AppComponent],
imports: [
StoreModule.forRoot(reducers, {
blaBla,
runtimeChecks: {
strictStateImmutability: true,
strictActionImmutability: true,
},
}),
],
providers: [],
bootstrap: [],
})
export class AppModule {}
`;
const patchDecoratorRegex = /.*blaBla,.*/;
const baseDecoratorRegex = /.*metaReducers,.*/;
it('should be preserved from base.', () => {
const result: String = merge(base, patch, false)
.split('\n')
.map((value) => value.trim())
.filter((value) => value != '')
.reduce((prev, curr) => prev.toString() + curr.toString(), "");;
expect(baseDecoratorRegex.test(result.toString()), 'metaReducers of the base should be present').to.be.true;
expect(patchDecoratorRegex.test(result.toString()), 'blaBla of the patch should not be present').to.be.false;
});
it('should be overwritten by patch.', () => {
const result: String = merge(base, patch, true)
.split('\n')
.map((value) => value.trim())
.filter((value) => value != '')
.reduce((prev, curr) => prev.toString() + curr.toString(), "");;
expect(baseDecoratorRegex.test(result.toString()), 'metaReducers of the base not should be present').to.be.false;
expect(patchDecoratorRegex.test(result.toString()), 'blaBla of the patch should be present').to.be.true;
});
});