-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathjson2typescript.unit.ts
310 lines (253 loc) · 13.9 KB
/
json2typescript.unit.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { JsonConvert } from "../src/json2typescript/json-convert";
import { OperationMode, PropertyMatchingRule, ValueCheckingMode } from "../src/json2typescript/json-convert-enums";
import { Any } from "../src/json2typescript/any";
import { MappingOptions, Settings } from "../src/json2typescript/json-convert-options";
import { Human } from "./model/typescript/human";
import { Cat } from "./model/typescript/cat";
import { Dog } from "./model/typescript/dog";
import { IHuman } from "./model/json/i-human";
import { ICat } from "./model/json/i-cat";
import { IDog } from "./model/json/i-dog";
import { DuplicateCat } from "./model/typescript/duplicate-cat";
import { DateConverter } from "./model/typescript/date-converter";
describe('Unit tests', () => {
const jsonConvert = new JsonConvert();
describe('JsonConvert', () => {
// JSON DATA
let human1JsonObject: IHuman = {
givenName: "Andreas",
lastName: "Muster"
};
let human2JsonObject: IHuman = {
givenName: "Michael",
name: "Meier"
};
let cat1JsonObject: ICat = {
catName: "Meowy",
district: 100,
owner: human1JsonObject,
talky: true,
other: "cute",
friends: []
};
let cat2JsonObject: ICat = {
catName: "Links",
district: 50,
talky: true,
other: "sweet",
birthdate: "2014-09-01",
friends: null
};
let dog1JsonObject: IDog = {
name: "Barky",
barking: true,
other: 1.1,
};
// TYPESCRIPT INSTANCES
let human1 = new Human();
human1.firstname = "Andreas";
human1.lastname = "Muster";
let human2 = new Human();
human2.firstname = "Michael";
human2.lastname = "Meier";
let cat1 = new Cat();
cat1.name = "Meowy";
cat1.district = 100;
cat1.owner = human1;
cat1.talky = true;
cat1.other = "cute";
cat1.friends = [];
let cat2 = new Cat();
cat2.name = "Links";
cat2.district = 50;
cat2.other = "sweet";
cat2.birthdate = new Date("2014-09-01");
cat2.friends = null;
cat2.talky = true;
let dog1 = new Dog();
dog1.name = "Barky";
dog1.isBarking = true;
dog1.owner = null;
dog1.other = 1.1;
let duplicateCat1 = new DuplicateCat();
duplicateCat1.name = "Duplicate";
duplicateCat1.district = new Date("2014-10-01");
duplicateCat1.talky = new Date("2015-02-03");
// SETUP CHECKS
describe('setup checks', () => {
it('JsonConvert instance', () => {
let jsonConvertTest: JsonConvert;
jsonConvertTest = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.ALLOW_OBJECT_NULL, false);
expect(jsonConvertTest.operationMode).toEqual(OperationMode.ENABLE);
expect(jsonConvertTest.valueCheckingMode).toEqual(ValueCheckingMode.ALLOW_OBJECT_NULL);
expect(jsonConvertTest.ignorePrimitiveChecks).toEqual(false);
jsonConvertTest = new JsonConvert(OperationMode.DISABLE, ValueCheckingMode.ALLOW_NULL, true);
expect(jsonConvertTest.operationMode).toEqual(OperationMode.DISABLE);
expect(jsonConvertTest.valueCheckingMode).toEqual(ValueCheckingMode.ALLOW_NULL);
expect(jsonConvertTest.ignorePrimitiveChecks).toEqual(true);
jsonConvertTest = new JsonConvert(OperationMode.LOGGING, ValueCheckingMode.DISALLOW_NULL, false);
expect(jsonConvertTest.operationMode).toEqual(OperationMode.LOGGING);
expect(jsonConvertTest.valueCheckingMode).toEqual(ValueCheckingMode.DISALLOW_NULL);
expect(jsonConvertTest.ignorePrimitiveChecks).toEqual(false);
jsonConvertTest = new JsonConvert();
expect(jsonConvertTest.operationMode).toEqual(OperationMode.ENABLE);
expect(jsonConvertTest.valueCheckingMode).toEqual(ValueCheckingMode.ALLOW_OBJECT_NULL);
expect(jsonConvertTest.ignorePrimitiveChecks).toEqual(false);
});
it('JsonObject decorator', () => {
expect((<any>human1)[Settings.CLASS_IDENTIFIER]).toEqual("Human");
expect((<any>cat1)[Settings.CLASS_IDENTIFIER]).toEqual("Kitty");
expect((<any>dog1)[Settings.CLASS_IDENTIFIER]).toEqual("Dog");
});
});
// NULL/UNDEFINED CHECKS
describe('null/undefined checks', () => {
it('serialize and deserialize null', () => {
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
let t_cat = (<any>jsonConvert).deserialize(null, Cat);
expect(t_cat).toEqual(null);
let t_catJsonObject = (<any>jsonConvert).serialize(null);
expect(t_catJsonObject).toEqual(null);
jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;
expect(() => (<any>jsonConvert).deserialize(null, Cat)).toThrow();
expect(() => (<any>jsonConvert).serialize(null)).toThrow();
});
it('deserialize and serialize undefined', () => {
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
expect(() => (<any>jsonConvert).deserialize(undefined, Cat)).toThrowError();
expect(() => (<any>jsonConvert).serialize(undefined)).toThrowError();
});
});
// BASIC CHECKS
describe('basic checks', () => {
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
it('serialize and deserialize same data', () => {
let t_catJsonObject = (<any>jsonConvert).serialize(cat1);
expect(t_catJsonObject).toEqual(cat1JsonObject);
let t_cat = (<any>jsonConvert).deserialize(t_catJsonObject, Cat);
expect(t_cat).toEqual(cat1);
});
it('deserialize and serialize same data', () => {
let t_cat = (<any>jsonConvert).deserialize(cat1JsonObject, Cat);
expect(t_cat).toEqual(cat1);
let t_catJsonObject = (<any>jsonConvert).serialize(t_cat);
expect(t_catJsonObject).toEqual(cat1JsonObject);
});
});
// PRIVATE METHODS
describe('private methods', () => {
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
it('serializeObject_loopProperty()', () => {
let t_cat = {};
(<any>jsonConvert).serializeObject_loopProperty(cat1, "name", t_cat);
expect((<any>t_cat)["catName"]).toBe(cat1.name);
(<any>jsonConvert).serializeObject_loopProperty(cat1, "district", t_cat);
expect((<any>t_cat)["district"]).toBe(100);
(<any>jsonConvert).serializeObject_loopProperty(cat1, "owner", t_cat);
expect((<any>t_cat)["owner"]["givenName"]).toBe("Andreas");
});
it('deserializeObject_loopProperty()', () => {
let t_cat = new Cat();
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "name", {"catName": "Meowy"});
expect(t_cat.name).toEqual("Meowy");
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "district", {"district": 100});
expect(t_cat.district).toEqual(100);
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "owner", {
"owner": {
givenName: "Andreas",
lastName: "Muster"
}
});
expect(t_cat.owner!.lastname).toEqual("Muster");
let t_dog = new Dog();
(<any>jsonConvert).deserializeObject_loopProperty(t_dog, "name", {"name": "Barky"});
expect(t_dog.name).toEqual("Barky");
jsonConvert.propertyMatchingRule = PropertyMatchingRule.CASE_INSENSITIVE;
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "name", {"catName": "Meowy"});
expect(t_cat.name).toEqual("Meowy");
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "name", {"catNAME": "Meowy"});
expect(t_cat.name).toEqual("Meowy");
expect(() => (<any>jsonConvert).deserializeObject_loopProperty(t_cat, "name", {"catNames": "Meowy"})).toThrow();
jsonConvert.propertyMatchingRule = PropertyMatchingRule.CASE_STRICT;
});
jsonConvert.valueCheckingMode = ValueCheckingMode.DISALLOW_NULL;
it('serializeObject_loopProperty()', () => {
let t_cat = {};
(<any>jsonConvert).serializeObject_loopProperty(cat2, "owner", t_cat);
expect((<any>t_cat)["owner"]).toBe(undefined);
});
it('deserializeObject_loopProperty()', () => {
let t_cat = new Cat();
(<any>jsonConvert).deserializeObject_loopProperty(t_cat, "owner", { "owner": null });
expect(t_cat.owner).toBe(null);
});
});
// HELPER METHODS
describe('helper methods', () => {
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
it('getClassPropertyMappingOptions()', () => {
/* The actual JSON types used are not strings, even though MappingOption.expectedJsonType is typed as string,
* so obscure this by assigning the expected JSON type to an "any" variable. */
let jsonType: any = String;
const catNameMapping = new MappingOptions();
catNameMapping.classPropertyName = "name";
catNameMapping.jsonPropertyName = "catName";
catNameMapping.expectedJsonType = jsonType;
catNameMapping.isOptional = false;
catNameMapping.customConverter = null;
expect((<any>jsonConvert).getClassPropertyMappingOptions(cat1, "name")).toEqual(catNameMapping);
const dogNameMapping = new MappingOptions();
dogNameMapping.classPropertyName = "name";
dogNameMapping.jsonPropertyName = "name";
dogNameMapping.expectedJsonType = jsonType;
dogNameMapping.isOptional = false;
dogNameMapping.customConverter = null;
expect((<any>jsonConvert).getClassPropertyMappingOptions(dog1, "name")).toEqual(dogNameMapping);
// Check that mapped property on "sibling" DuplicateCat class with same name as Cat property
// but different type is handled correctly
const duplicateCatTalkyMapping = new MappingOptions();
duplicateCatTalkyMapping.classPropertyName = "talky";
duplicateCatTalkyMapping.jsonPropertyName = "talky";
duplicateCatTalkyMapping.expectedJsonType = undefined;
duplicateCatTalkyMapping.isOptional = false;
duplicateCatTalkyMapping.customConverter = new DateConverter();
expect((<any>jsonConvert).getClassPropertyMappingOptions(duplicateCat1, "talky")).toEqual(duplicateCatTalkyMapping);
expect((<any>jsonConvert).getClassPropertyMappingOptions(human1, "name")).toBeNull();
// Unmapped property should not return mapping, even though property is the same name as a mapped property on another class
expect((<any>jsonConvert).getClassPropertyMappingOptions(duplicateCat1, "district")).toBeNull();
});
it('verifyProperty()', () => {
expect((<any>jsonConvert).verifyProperty(String, "Andreas", false)).toBe("Andreas");
expect((<any>jsonConvert).verifyProperty([String, [Boolean, Number]], ["Andreas", [true, 2.2]], false)).toEqual(["Andreas", [true, 2.2]]);
expect(() => (<any>jsonConvert).verifyProperty(Number, "Andreas", false)).toThrow();
});
it('getObjectValue()', () => {
expect((<any>jsonConvert).getObjectValue({ "name": "Andreas" }, "name")).toBe("Andreas");
expect(() => (<any>jsonConvert).getObjectValue({ "nAmE": "Andreas" }, "NaMe")).toThrow();
jsonConvert.propertyMatchingRule = PropertyMatchingRule.CASE_INSENSITIVE;
expect((<any>jsonConvert).getObjectValue({ "nAmE": "Andreas" }, "NaMe")).toBe("Andreas");
jsonConvert.propertyMatchingRule = PropertyMatchingRule.CASE_STRICT;
});
});
// JSON2TYPESCRIPT TYPES
describe('json2typescript types', () => {
jsonConvert.valueCheckingMode = ValueCheckingMode.ALLOW_NULL;
it('getExpectedType()', () => {
expect((<any>jsonConvert).getExpectedType(JsonConvert)).toBe("JsonConvert");
expect((<any>jsonConvert).getExpectedType([String, [Boolean, Number]])).toBe("[string,[boolean,number]]");
expect((<any>jsonConvert).getExpectedType([[null, Any], Object])).toBe("[[any,any],any]");
expect((<any>jsonConvert).getExpectedType(undefined)).toBe("undefined");
expect((<any>jsonConvert).getExpectedType("?")).toBe("?????");
});
it('getJsonType()', () => {
expect((<any>jsonConvert).getJsonType({name: "Andreas"})).toBe("object");
expect((<any>jsonConvert).getJsonType(["a", 0, [true, null]])).toBe("[string,number,[boolean,null]]");
});
it('getTrueType()', () => {
expect((<any>jsonConvert).getTrueType(new JsonConvert())).toBe("object");
expect((<any>jsonConvert).getTrueType({name: "Andreas"})).toBe("object");
expect((<any>jsonConvert).getTrueType("Andreas")).toBe("string");
});
});
});
});