-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_loader.cfc
More file actions
672 lines (651 loc) · 19.8 KB
/
test_loader.cfc
File metadata and controls
672 lines (651 loc) · 19.8 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
component extends="mxunit.framework.TestCase" {
/*
run tests:
/cfc_loader/test_loader.cfc?method=runtestremote&output=html
*/
variables.tempLoadersPath = "test_generated_" & GetTickCount();
/* this will run before every single test in this test case */
public void function setUp() {
variables.loader = new loader();
variables.loader.setLoadersPath(variables.tempLoadersPath);
}
/* this will run after every single test in this test case */
public void function tearDown() {}
/* this will run once after initialization and before setUp() */
public void function beforeTests() {
if ( !DirectoryExists(ExpandPath(variables.tempLoadersPath)) ) {
DirectoryCreate(ExpandPath(variables.tempLoadersPath));
}
}
/* this will run once after all tests have been run */
public void function afterTests() {
if ( DirectoryExists(ExpandPath(variables.tempLoadersPath)) ) {
// comment this out if you want to see generated CFCs
DirectoryDelete(ExpandPath(variables.tempLoadersPath), true);
}
}
/*
* TESTS
*/
/**
* @hint "I test getCfcLoader."
**/
public void function test_getCfcLoader() {
variables.loader.setLoadersPath("test_loaders");
var tests = [
"Bundle loader (exists: no generate) (use cfcName)": {
"args": {
"cfc": new test_cfcs.Bundle().setId(1),
"cfcName": "test_cfcs.Bundle"
},
"mocks": {
"getCfcName": "test_cfcs.Bundle",
"getCfcLoaderName": "test_loaders.test_cfcs_Bundle",
"loaderExists": true,
"generator.generate": "N/A"
},
"expect": {
"calls": {
"getCfcName": [], // arg cfcName skips getCfcName(), thus skipping GetMetaData(cfc).name
"getCfcLoaderName": [
{
"cfc": SerializeJson(new test_cfcs.Bundle().setId(1)),
"cfcName": "test_cfcs.Bundle"
}
],
"loaderExists": [
{"cfcName": "test_loaders.test_cfcs_Bundle"}
],
"generator.generate": [], // exists, so no generation
"writeLoader": [] // exists, so no generation
},
"returnType": "test_loaders.test_cfcs_Bundle"
}
},
"Bundle loader (exists: no generate)": {
"args": {
"cfc": new test_cfcs.Bundle().setId(1)
},
"mocks": {
"getCfcName": "test_cfcs.Bundle",
"getCfcLoaderName": "test_loaders.test_cfcs_Bundle",
"loaderExists": true,
"generator.generate": "N/A"
},
"expect": {
"calls": {
"getCfcName": [
{"cfc": SerializeJson(new test_cfcs.Bundle().setId(1))},
{"cfc": SerializeJson(new test_cfcs.Bundle().setId(1))}
],
"getCfcLoaderName": [
{
"cfc": SerializeJson(new test_cfcs.Bundle().setId(1)),
"cfcName": "test_cfcs.Bundle"
}
],
"loaderExists": [
{"cfcName": "test_loaders.test_cfcs_Bundle"}
],
"generator.generate": [], // exists, so no generation
"writeLoader": [] // exists, so no generation
},
"returnType": "test_loaders.test_cfcs_Bundle"
}
},
"Option loader (!exists: generate)": {
"args": {
"cfc": new test_cfcs.Option().setId(2)
},
"mocks": {
"getCfcName": "test_cfcs.Option",
"getCfcLoaderName": "test_loaders.test_cfcs_Option",
"loaderExists": false,
"generator.generate": "option loader code"
},
"expect": {
"calls": {
"getCfcName": [
{"cfc": SerializeJson(new test_cfcs.Option().setId(2))},
{"cfc": SerializeJson(new test_cfcs.Option().setId(2))}
],
"getCfcLoaderName": [
{
"cfc": SerializeJson(new test_cfcs.Option().setId(2)),
"cfcName": "test_cfcs.Option"
}
],
"loaderExists": [
{"cfcName": "test_loaders.test_cfcs_Option"}
],
"generator.generate": [
{"cfc": SerializeJson(new test_cfcs.Option().setId(2))}
],
"writeLoader": [
{"cfcName": "test_loaders.test_cfcs_Option", "code": "option loader code"}
]
},
"returnType": "test_loaders.test_cfcs_Option"
}
}
];
MakePublic(variables.loader, "getCfcName");
InjectMethod(variables.loader, this, "getCfcNameMock", "getCfcName");
MakePublic(variables.loader, "getCfcLoaderName");
InjectMethod(variables.loader, this, "getCfcLoaderNameMock", "getCfcLoaderName");
MakePublic(variables.loader, "loaderExists");
InjectMethod(variables.loader, this, "loaderExistsMock", "loaderExists");
MakePublic(variables.loader, "writeLoader");
InjectMethod(variables.loader, this, "writeLoaderMock", "writeLoader");
for ( var name in tests ) {
var test = tests[name];
// reset calls and mocks
var calls = {
"getCfcName": [],
"getCfcLoaderName": [],
"loaderExists": [],
"generator.generate": [],
"writeLoader": []
};
variables.loader["getCfcName_Mock"] = test.mocks.getCfcName;
variables.loader["getCfcName_Args"] = [];
variables.loader["getCfcLoaderName_Mock"] = test.mocks.getCfcLoaderName;
variables.loader["getCfcLoaderName_Args"] = [];
variables.loader["loaderExists_Mock"] = test.mocks.loaderExists;
variables.loader["loaderExists_Args"] = [];
variables.loader["writeLoader_Args"] = [];
var generatorMock = new test_cfcs.Blank();
generatorMock.generate = function(required component cfc) {
ArrayAppend(calls["generator.generate"], {"cfc": SerializeJson(arguments.cfc)});
var mocks = Duplicate(test.mocks);
return mocks["generator.generate"];
};
variables.loader.setGenerator(generatorMock);
// clear loader cache between tests
variables.loader.clearLoaderCache();
// call method under test (more than once to ensure caching)
var result = variables.loader.getCfcLoader(argumentCollection = test.args);
var result = variables.loader.getCfcLoader(argumentCollection = test.args);
// check assertions
calls["getCfcName"] = variables.loader["getCfcName_Args"];
calls["getCfcLoaderName"] = variables.loader["getCfcLoaderName_Args"];
calls["loaderExists"] = variables.loader["loaderExists_Args"];
calls["writeLoader"] = variables.loader["writeLoader_Args"];
AssertEquals(test.expect.calls, calls, "#name# - calls don't match expected");
AssertTrue(IsInstanceOf(result, test.expect.returnType), "#name# - response not of expected type");
}
}
/**
* @hint "I test getCfcLoaderName."
**/
public void function test_getCfcLoaderName() {
MakePublic(variables.loader, "getCfcLoaderName");
variables.loader.setLoadersPath("test_loaders");
var tests = [
"test one": {
"args": {
"cfc": new test_cfcs.Bundle().setId(1)
},
"mocks": {
"getCfcName": "foo.bar.baz",
"generator.getSignature": "signature1"
},
"expect": {
"calls": {
"getCfcName": [
{"cfc": SerializeJson(new test_cfcs.Bundle().setId(1))}
],
"generator.getSignature": [
{"cfcName": "foo.bar.baz"}
]
},
"result": "test_loaders.foo_bar_baz_signature1"
}
},
"test two": {
"args": {
"cfc": new test_cfcs.Option().setId(2)
},
"mocks": {
"getCfcName": "foo.bar_baz.qux",
"generator.getSignature": "signature2"
},
"expect": {
"calls": {
"getCfcName": [
{"cfc": SerializeJson(new test_cfcs.Option().setId(2))}
],
"generator.getSignature": [
{"cfcName": "foo.bar_baz.qux"}
]
},
"result": "test_loaders.foo_bar__baz_qux_signature2" // double underscore to disambiguate directory dots being replaced by _
}
},
"test three (use cfcName)": {
"args": {
"cfc": new test_cfcs.Option().setId(2),
"cfcName": "test_cfcs.Option"
},
"mocks": {
"getCfcName": "N/A",
"generator.getSignature": "signature2"
},
"expect": {
"calls": {
"getCfcName": [], // arg cfcName skips getCfcName(), thus skipping GetMetaData(cfc).name
"generator.getSignature": [
{"cfcName": "test_cfcs.Option"}
]
},
"result": "test_loaders.test__cfcs_option_signature2" // double underscore to disambiguate directory dots being replaced by _
}
}
];
MakePublic(variables.loader, "getCfcName");
InjectMethod(variables.loader, this, "getCfcNameMock", "getCfcName");
for ( var name in tests ) {
var test = tests[name];
// reset calls and mocks
var calls = {
"getCfcName": [],
"generator.getSignature": []
};
variables.loader["getCfcName_Mock"] = test.mocks.getCfcName;
variables.loader["getCfcName_Args"] = [];
var mockGenerator = new test_cfcs.Blank();
mockGenerator.getSignature = function(required string cfcName) {
ArrayAppend(calls["generator.getSignature"], {"cfcName": arguments.cfcName});
var mocks = Duplicate(test.mocks);
return mocks["generator.getSignature"];
};
variables.loader.setGenerator(mockGenerator);
// call method under test
var result = variables.loader.getCfcLoaderName(argumentCollection = test.args);
// check assertions
calls["getCfcName"] = variables.loader["getCfcName_Args"];
AssertEquals(test.expect.calls, calls, "#name# - calls don't match expected");
AssertEquals(test.expect.result, result, "#name# failed");
}
}
/**
* @hint "I test getCfcName."
**/
public void function test_getCfcName() {
MakePublic(variables.loader, "getCfcName");
var tests = [
"Bundle path": {
"cfc": new test_cfcs.Bundle(),
"expect": "test_cfcs.Bundle"
},
"Option path": {
"cfc": new test_cfcs.Option(),
"expect": "test_cfcs.Option"
}
];
for ( var name in tests ) {
var test = tests[name];
// call method under test
var result = variables.loader.getCfcName(test.cfc);
// check assertions
AssertEquals(test.expect, result, "#name# - result doesn't match expected");
}
}
/**
* @hint "I test load (for speed)."
**/
public void function test_load_benchmark() {
// NOTE
// This test could fail (be too slow) on different hardware/OS/etc combinations.
// I'm aware of this.
// I'll adjust this as necessary.
var data = {
"id": 1,
"name": "Benchmark Bundle",
"widgets": [
{
"id": 1,
"name": "Benchmark Widget 1",
"options": [
{"id": 1, "name": "Benchmark Option 1"},
{"id": 2, "name": "Benchmark Option 2"}
],
"isInBundles": [
{"id": 1, "Name": "Benchmark Bundle"},
{"id": 7, "Name": "Some Other Bundle"}
]
},
{
"id": 2,
"name": "Benchmark Widget 2",
"options": [
{"id": 3, "name": "Benchmark Option 3"},
{"id": 4, "name": "Benchmark Option 4"}
],
"isInBundles": [
{"id": 1, "Name": "Benchmark Bundle"},
{"id": 42, "Name": "Yet Another Bundle"}
]
}
]
};
var loopCount = 1000;
// note the time
var startTime = GetTickCount();
// make sure loader is working (also warms loader by generating and caching required sub-loaders)
AssertEquals(
DeserializeJson(SerializeJson(data)),
DeserializeJson(SerializeJson(variables.loader.load(
cfcName = "test_cfcs.Bundle",
cfc = new test_cfcs.Bundle(),
data = data
))),
"start - loader failure (bundle doesn't match source data)"
);
// load a bunch of bundles nested 3 levels deep (i.e. bundle > widget[] > bundle[])
for ( var i = 0; i < loopCount; i++ ) {
var bundle = variables.loader.load(
cfcName = "test_cfcs.Bundle",
cfc = new test_cfcs.Bundle(),
data = data
);
}
// figure out how long it took
var elapsedTime = GetTickCount() - startTime;
// make sure the CFCs were being completely loaded (sanity check)
AssertEquals(
DeserializeJson(SerializeJson(data)),
DeserializeJson(SerializeJson(bundle)),
"finished - loader failure (bundle doesn't match source data)"
);
// "baseline" stats from 2019-02-01
var baseline = [
"elapsed ms": 210,
"loop count": 1000,
"avg load() ms": 0.21
];
// stats for the current run
var stats = [
"elapsed ms": elapsedTime,
"loop count": loopCount,
"avg load() ms": elapsedTime / loopCount
];
// display stats when run in debug
debug([
"baseline": baseline,
"current": stats
]);
// have we drastically exceeded previous benchmarks? (see var baseline above)
AssertTrue(
stats["elapsed ms"] < (3 * baseline["elapsed ms"]),
"load time has more than tripled since benchmark was established"
);
}
/**
* @hint "I test load (with generation)."
**/
public void function test_load_integration() {
var tests = [
"test Widget (data is just structs/arrays)": {
"args": {
"cfc": new test_cfcs.Widget(),
"data": {
"id": 1,
"isOnSpecial": "",
"name": "structs/arrays",
"options": [
{"id": 1, "name": "Option 1"},
{"id": 2, "name": "Option 2"}
]
}
},
"expect": {
"returnType": "test_cfcs.Widget",
"serialized": {
"id": 1,
"name": "structs/arrays",
"options": [
{"id": 1, "name": "Option 1"},
{"id": 2, "name": "Option 2"}
]
}
}
},
"test Widget (data has items of item_type)": {
"args": {
"cfc": new test_cfcs.Widget(),
"data": {
"id": 2,
"name": "items of item_type",
"options": [
new test_cfcs.Option().setId(3).setName("Option 3"),
new test_cfcs.Option().setId(4).setName("Option 4")
]
}
},
"expect": {
"returnType": "test_cfcs.Widget",
"serialized": {
"id": 2,
"name": "items of item_type",
"options": [
{"id": 3, "name": "Option 3"},
{"id": 4, "name": "Option 4"}
]
}
}
}
];
for ( var dataType in ["json", "struct"] ) {
for ( var name in tests ) {
var test = tests[name];
if ( dataType == "json" ) {
test.args.data = SerializeJson(test.args.data);
}
// call method under test
var result = variables.loader.load(
cfc = test.args.cfc,
data = test.args.data
);
// check assertions
AssertEquals(test.expect.returnType, GetMetaData(result).name, "(data:#dataType#) #name# - return type doesn't match expected");
AssertEquals(
test.expect.serialized,
DeserializeJson(SerializeJson(result)),
"(data:#dataType#) #name# - result did not contain expected data"
);
}
}
}
/**
* @hint "I test load."
**/
public void function test_load() {
var tests = [
"test Option (1)": {
"args": {
"cfc": new test_cfcs.Option().setId(1),
"data": {name: "Option 1"}
},
"expect": {
"calls": {
"getCfcLoader": [{
"cfcName": "",
"cfc": SerializeJson(new test_cfcs.Option().setId(1))
}],
"cfcLoader.load": {
"cfc": SerializeJson(new test_cfcs.Option().setId(1)),
"data": {name: "Option 1"}
}
},
"returnType": "test_cfcs.Option"
}
},
"test Widget (2)": {
"args": {
"cfc": new test_cfcs.Widget().setId(2),
"data": {name: "Widget 2"}
},
"expect": {
"calls": {
"getCfcLoader": [{
"cfcName": "",
"cfc": SerializeJson(new test_cfcs.Widget().setId(2))
}],
"cfcLoader.load": {
"cfc": SerializeJson(new test_cfcs.Widget().setId(2)),
"data": {name: "Widget 2"}
}
},
"returnType": "test_cfcs.Widget"
}
},
"test Widget (with cfcName)": {
"args": {
"cfcName": "test_cfcs.Widget",
"cfc": new test_cfcs.Widget().setId(2),
"data": {name: "Widget 2"}
},
"expect": {
"calls": {
"getCfcLoader": [{
"cfcName": "test_cfcs.Widget",
"cfc": SerializeJson(new test_cfcs.Widget().setId(2))
}],
"cfcLoader.load": {
"cfc": SerializeJson(new test_cfcs.Widget().setId(2)),
"data": {name: "Widget 2"}
}
},
"returnType": "test_cfcs.Widget"
}
}
];
MakePublic(variables.loader, "getCfcLoader");
InjectMethod(variables.loader, this, "getCfcLoaderMock", "getCfcLoader");
for ( var name in tests ) {
var test = tests[name];
// reset calls and mocks
var calls = {
"getCfcLoader": [],
"cfcLoader.load": {}
};
var loaderMock = new test_cfcs.Blank();
loaderMock.load = function(
required component cfc,
required struct data,
string cfcName = ""
) {
calls["cfcLoader.load"] = {
"cfc": SerializeJson(arguments.cfc),
"data": arguments.data
};
};
variables.loader["getCfcLoader_Mock"] = loaderMock;
variables.loader["getCfcLoader_Args"] = [];
// call method under test
var result = variables.loader.load(argumentCollection = test.args);
// check assertions
calls["getCfcLoader"] = variables.loader["getCfcLoader_Args"];
AssertEquals(test.expect.calls, calls, "<br>#name# - calls don't match expected");
AssertEquals(test.expect.returnType, GetMetaData(result).name, "<br>#name# - return type doesn't match expected");
}
}
/**
* @hint "I test loaderExists."
**/
public void function test_loaderExists() {
MakePublic(variables.loader, "loaderExists");
var tests = [
"loader exists (returns true)": {
"args": {
"loaderName": "test_loaders.test_cfcs_Bundle"
},
"expect": {
"result": true
}
},
"loader doesn't exist (returns false)": {
"args": {
"loaderName": "test_loaders.does_not_exist"
},
"expect": {
"result": false
}
}
];
for ( var name in tests ) {
var test = tests[name];
// call method under test
var result = variables.loader.loaderExists(cfcName = test.args.loaderName);
// check assertions
AssertEquals(test.expect.result, result, "#name# - result doesn't match expected");
}
}
/**
* @hint "I test writeLoader."
**/
public void function test_writeLoader() {
MakePublic(variables.loader, "writeLoader");
var tests = [
"test one": {
"args": {
"cfcName": variables.tempLoadersPath & ".writeLoader_test_one",
"code": "write loader test one"
}
},
"test two": {
"args": {
"cfcName": variables.tempLoadersPath & ".writeLoader_test_two",
"code": "write loader test two"
}
}
];
for ( var name in tests ) {
var test = tests[name];
// call method under test
var result = variables.loader.writeLoader(
cfcName = test.args.cfcName,
code = test.args.code
);
// check assertions
var filePath = ExpandPath("/" & Replace(test.args.cfcName, ".", "/", "all") & ".cfc");
AssertTrue(FileExists(filePath), "#name# - file missing");
AssertEquals(test.args.code, FileRead(filePath), "#name# - file content wrong");
}
}
/* MOCKS */
private component function getCfcLoaderMock(required component cfc, string cfcName) {
ArrayAppend(
this["getCfcLoader_Args"],
{
"cfc": SerializeJson(arguments.cfc),
"cfcName": arguments.cfcName
}
);
return this["getCfcLoader_Mock"];
}
private string function getCfcLoaderNameMock(required component cfc, string cfcName) {
var args = {};
if ( StructKeyExists(arguments, "cfcName") && Len(arguments.cfcName) > 0 ) {
args["cfcName"] = arguments.cfcName;
}
args.cfc = SerializeJson(arguments.cfc);
ArrayAppend(this["getCfcLoaderName_Args"], args);
return this["getCfcLoaderName_Mock"];
}
private string function getCfcNameMock(required component cfc) {
ArrayAppend(this["getCfcName_Args"], {"cfc": SerializeJson(arguments.cfc)});
return this["getCfcName_Mock"];
}
private boolean function loaderExistsMock(required string cfcName) {
ArrayAppend(this["loaderExists_Args"], arguments);
return this["loaderExists_Mock"];
}
private void function writeLoaderMock(
required string cfcName,
required string code
) {
ArrayAppend(this["writeLoader_Args"], arguments);
}
}