/
TestRunner.js
1013 lines (864 loc) · 40.9 KB
/
TestRunner.js
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace YUITest
* @class TestRunner
* @static
*/
YUITest.TestRunner = function(){
/*(intentionally not documented)
* Determines if any of the array of test groups appears
* in the given TestRunner filter.
* @param {Array} testGroups The array of test groups to
* search for.
* @param {String} filter The TestRunner groups filter.
*/
function inGroups(testGroups, filter){
if (!filter.length){
return true;
} else {
if (testGroups){
for (var i=0, len=testGroups.length; i < len; i++){
if (filter.indexOf("," + testGroups[i] + ",") > -1){
return true;
}
}
}
return false;
}
}
/**
* A node in the test tree structure. May represent a TestSuite, TestCase, or
* test function.
* @param {Variant} testObject A TestSuite, TestCase, or the name of a test function.
* @class TestNode
* @constructor
* @private
*/
function TestNode(testObject){
/**
* The TestSuite, TestCase, or test function represented by this node.
* @type Variant
* @property testObject
*/
this.testObject = testObject;
/**
* Pointer to this node's first child.
* @type TestNode
* @property firstChild
*/
this.firstChild = null;
/**
* Pointer to this node's last child.
* @type TestNode
* @property lastChild
*/
this.lastChild = null;
/**
* Pointer to this node's parent.
* @type TestNode
* @property parent
*/
this.parent = null;
/**
* Pointer to this node's next sibling.
* @type TestNode
* @property next
*/
this.next = null;
/**
* Test results for this test object.
* @type object
* @property results
*/
this.results = new YUITest.Results();
//initialize results
if (testObject instanceof YUITest.TestSuite){
this.results.type = "testsuite";
this.results.name = testObject.name;
} else if (testObject instanceof YUITest.TestCase){
this.results.type = "testcase";
this.results.name = testObject.name;
}
}
TestNode.prototype = {
/**
* Appends a new test object (TestSuite, TestCase, or test function name) as a child
* of this node.
* @param {Variant} testObject A TestSuite, TestCase, or the name of a test function.
* @return {Void}
*/
appendChild : function (testObject){
var node = new TestNode(testObject);
if (this.firstChild === null){
this.firstChild = this.lastChild = node;
} else {
this.lastChild.next = node;
this.lastChild = node;
}
node.parent = this;
return node;
}
};
/**
* Runs test suites and test cases, providing events to allowing for the
* interpretation of test results.
* @namespace Test
* @class Runner
* @static
*/
function TestRunner(){
//inherit from EventTarget
YUITest.EventTarget.call(this);
/**
* Suite on which to attach all TestSuites and TestCases to be run.
* @type YUITest.TestSuite
* @property masterSuite
* @static
* @private
*/
this.masterSuite = new YUITest.TestSuite("yuitests" + (new Date()).getTime());
/**
* Pointer to the current node in the test tree.
* @type TestNode
* @private
* @property _cur
* @static
*/
this._cur = null;
/**
* Pointer to the root node in the test tree.
* @type TestNode
* @private
* @property _root
* @static
*/
this._root = null;
/**
* Indicates if the TestRunner will log events or not.
* @type Boolean
* @property _log
* @private
* @static
*/
this._log = true;
/**
* Indicates if the TestRunner is waiting as a result of
* wait() being called.
* @type Boolean
* @property _waiting
* @private
* @static
*/
this._waiting = false;
/**
* Indicates if the TestRunner is currently running tests.
* @type Boolean
* @private
* @property _running
* @static
*/
this._running = false;
/**
* Holds copy of the results object generated when all tests are
* complete.
* @type Object
* @private
* @property _lastResults
* @static
*/
this._lastResults = null;
/**
* Data object that is passed around from method to method.
* @type Object
* @private
* @property _data
* @static
*/
this._context = null;
/**
* The list of test groups to run. The list is represented
* by a comma delimited string with commas at the start and
* end.
* @type String
* @private
* @property _groups
* @static
*/
this._groups = "";
}
TestRunner.prototype = YUITest.Util.mix(new YUITest.EventTarget(), {
/**
* If true, YUITest will not fire an error for tests with no Asserts.
* @prop _ignoreEmpty
* @private
* @type Boolean
* @static
*/
_ignoreEmpty: false,
//restore prototype
constructor: YUITest.TestRunner,
//-------------------------------------------------------------------------
// Constants
//-------------------------------------------------------------------------
/**
* Fires when a test case is opened but before the first
* test is executed.
* @event testcasebegin
* @static
*/
TEST_CASE_BEGIN_EVENT : "testcasebegin",
/**
* Fires when all tests in a test case have been executed.
* @event testcasecomplete
* @static
*/
TEST_CASE_COMPLETE_EVENT : "testcasecomplete",
/**
* Fires when a test suite is opened but before the first
* test is executed.
* @event testsuitebegin
* @static
*/
TEST_SUITE_BEGIN_EVENT : "testsuitebegin",
/**
* Fires when all test cases in a test suite have been
* completed.
* @event testsuitecomplete
* @static
*/
TEST_SUITE_COMPLETE_EVENT : "testsuitecomplete",
/**
* Fires when a test has passed.
* @event pass
* @static
*/
TEST_PASS_EVENT : "pass",
/**
* Fires when a test has failed.
* @event fail
* @static
*/
TEST_FAIL_EVENT : "fail",
/**
* Fires when a non-test method has an error.
* @event error
* @static
*/
ERROR_EVENT : "error",
/**
* Fires when a test has been ignored.
* @event ignore
* @static
*/
TEST_IGNORE_EVENT : "ignore",
/**
* Fires when all test suites and test cases have been completed.
* @event complete
* @static
*/
COMPLETE_EVENT : "complete",
/**
* Fires when the run() method is called.
* @event begin
* @static
*/
BEGIN_EVENT : "begin",
//-------------------------------------------------------------------------
// Test Tree-Related Methods
//-------------------------------------------------------------------------
/**
* Adds a test case to the test tree as a child of the specified node.
* @param {TestNode} parentNode The node to add the test case to as a child.
* @param {YUITest.TestCase} testCase The test case to add.
* @return {Void}
* @static
* @private
* @method _addTestCaseToTestTree
*/
_addTestCaseToTestTree : function (parentNode, testCase){
//add the test suite
var node = parentNode.appendChild(testCase),
prop,
testName;
//iterate over the items in the test case
for (prop in testCase){
if ((prop.indexOf("test") === 0 || prop.indexOf(" ") > -1) && typeof testCase[prop] == "function"){
node.appendChild(prop);
}
}
},
/**
* Adds a test suite to the test tree as a child of the specified node.
* @param {TestNode} parentNode The node to add the test suite to as a child.
* @param {YUITest.TestSuite} testSuite The test suite to add.
* @return {Void}
* @static
* @private
* @method _addTestSuiteToTestTree
*/
_addTestSuiteToTestTree : function (parentNode, testSuite) {
//add the test suite
var node = parentNode.appendChild(testSuite);
//iterate over the items in the master suite
for (var i=0; i < testSuite.items.length; i++){
if (testSuite.items[i] instanceof YUITest.TestSuite) {
this._addTestSuiteToTestTree(node, testSuite.items[i]);
} else if (testSuite.items[i] instanceof YUITest.TestCase) {
this._addTestCaseToTestTree(node, testSuite.items[i]);
}
}
},
/**
* Builds the test tree based on items in the master suite. The tree is a hierarchical
* representation of the test suites, test cases, and test functions. The resulting tree
* is stored in _root and the pointer _cur is set to the root initially.
* @return {Void}
* @static
* @private
* @method _buildTestTree
*/
_buildTestTree : function () {
this._root = new TestNode(this.masterSuite);
//this._cur = this._root;
//iterate over the items in the master suite
for (var i=0; i < this.masterSuite.items.length; i++){
if (this.masterSuite.items[i] instanceof YUITest.TestSuite) {
this._addTestSuiteToTestTree(this._root, this.masterSuite.items[i]);
} else if (this.masterSuite.items[i] instanceof YUITest.TestCase) {
this._addTestCaseToTestTree(this._root, this.masterSuite.items[i]);
}
}
},
//-------------------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------------------
/**
* Handles the completion of a test object's tests. Tallies test results
* from one level up to the next.
* @param {TestNode} node The TestNode representing the test object.
* @return {Void}
* @method _handleTestObjectComplete
* @private
*/
_handleTestObjectComplete : function (node) {
var parentNode;
if (node && (typeof node.testObject == "object")) {
parentNode = node.parent;
if (parentNode){
parentNode.results.include(node.results);
parentNode.results[node.testObject.name] = node.results;
}
if (node.testObject instanceof YUITest.TestSuite){
this._execNonTestMethod(node, "tearDown", false);
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_SUITE_COMPLETE_EVENT, testSuite: node.testObject, results: node.results});
} else if (node.testObject instanceof YUITest.TestCase){
this._execNonTestMethod(node, "destroy", false);
node.results.duration = (new Date()) - node._start;
this.fire({ type: this.TEST_CASE_COMPLETE_EVENT, testCase: node.testObject, results: node.results});
}
}
},
//-------------------------------------------------------------------------
// Navigation Methods
//-------------------------------------------------------------------------
/**
* Retrieves the next node in the test tree.
* @return {TestNode} The next node in the test tree or null if the end is reached.
* @private
* @static
* @method _next
*/
_next : function () {
if (this._cur === null){
this._cur = this._root;
} else if (this._cur.firstChild) {
this._cur = this._cur.firstChild;
} else if (this._cur.next) {
this._cur = this._cur.next;
} else {
while (this._cur && !this._cur.next && this._cur !== this._root){
this._handleTestObjectComplete(this._cur);
this._cur = this._cur.parent;
}
this._handleTestObjectComplete(this._cur);
if (this._cur == this._root){
this._cur.results.type = "report";
this._cur.results.timestamp = (new Date()).toLocaleString();
this._cur.results.duration = (new Date()) - this._cur._start;
this._lastResults = this._cur.results;
this._running = false;
this.fire({ type: this.COMPLETE_EVENT, results: this._lastResults});
this._cur = null;
} else if (this._cur) {
this._cur = this._cur.next;
}
}
return this._cur;
},
/**
* Executes a non-test method (init, setUp, tearDown, destroy)
* and traps an errors. If an error occurs, an error event is
* fired.
* @param {Object} node The test node in the testing tree.
* @param {String} methodName The name of the method to execute.
* @param {Boolean} allowAsync Determines if the method can be called asynchronously.
* @return {Boolean} True if an async method was called, false if not.
* @method _execNonTestMethod
* @private
*/
_execNonTestMethod: function(node, methodName, allowAsync){
var testObject = node.testObject,
event = { type: this.ERROR_EVENT };
try {
if (allowAsync && testObject["async:" + methodName]){
testObject["async:" + methodName](this._context);
return true;
} else {
testObject[methodName](this._context);
}
} catch (ex){
node.results.errors++;
event.error = ex;
event.methodName = methodName;
if (testObject instanceof YUITest.TestCase){
event.testCase = testObject;
} else {
event.testSuite = testSuite;
}
this.fire(event);
}
return false;
},
/**
* Runs a test case or test suite, returning the results.
* @param {YUITest.TestCase|YUITest.TestSuite} testObject The test case or test suite to run.
* @return {Object} Results of the execution with properties passed, failed, and total.
* @private
* @method _run
* @static
*/
_run : function () {
//flag to indicate if the TestRunner should wait before continuing
var shouldWait = false;
//get the next test node
var node = this._next();
if (node !== null) {
//set flag to say the testrunner is running
this._running = true;
//eliminate last results
this._lastResult = null;
var testObject = node.testObject;
//figure out what to do
if (typeof testObject == "object" && testObject !== null){
if (testObject instanceof YUITest.TestSuite){
this.fire({ type: this.TEST_SUITE_BEGIN_EVENT, testSuite: testObject });
node._start = new Date();
this._execNonTestMethod(node, "setUp" ,false);
} else if (testObject instanceof YUITest.TestCase){
this.fire({ type: this.TEST_CASE_BEGIN_EVENT, testCase: testObject });
node._start = new Date();
//regular or async init
/*try {
if (testObject["async:init"]){
testObject["async:init"](this._context);
return;
} else {
testObject.init(this._context);
}
} catch (ex){
node.results.errors++;
this.fire({ type: this.ERROR_EVENT, error: ex, testCase: testObject, methodName: "init" });
}*/
if(this._execNonTestMethod(node, "init", true)){
return;
}
}
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
setTimeout(function(){
YUITest.TestRunner._run();
}, 0);
} else {
this._run();
}
} else {
this._runTest(node);
}
}
},
_resumeTest : function (segment) {
//get relevant information
var node = this._cur;
//we know there's no more waiting now
this._waiting = false;
//if there's no node, it probably means a wait() was called after resume()
if (!node){
//TODO: Handle in some way?
//console.log("wait() called after resume()");
//this.fire("error", { testCase: "(unknown)", test: "(unknown)", error: new Error("wait() called after resume()")} );
return;
}
var testName = node.testObject;
var testCase = node.parent.testObject;
//cancel other waits if available
if (testCase.__yui_wait){
clearTimeout(testCase.__yui_wait);
delete testCase.__yui_wait;
}
//get the "should" test cases
var shouldFail = testName.indexOf("fail:") === 0 ||
(testCase._should.fail || {})[testName];
var shouldError = (testCase._should.error || {})[testName];
//variable to hold whether or not the test failed
var failed = false;
var error = null;
//try the test
try {
//run the test
segment.call(testCase, this._context);
//if the test hasn't already failed and doesn't have any asserts...
if(YUITest.Assert._getCount() == 0 && !this._ignoreEmpty){
throw new YUITest.AssertionError("Test has no asserts.");
}
//if it should fail, and it got here, then it's a fail because it didn't
else if (shouldFail){
error = new YUITest.ShouldFail();
failed = true;
} else if (shouldError){
error = new YUITest.ShouldError();
failed = true;
}
} catch (thrown){
//cancel any pending waits, the test already failed
if (testCase.__yui_wait){
clearTimeout(testCase.__yui_wait);
delete testCase.__yui_wait;
}
//figure out what type of error it was
if (thrown instanceof YUITest.AssertionError) {
if (!shouldFail){
error = thrown;
failed = true;
}
} else if (thrown instanceof YUITest.Wait){
if (typeof thrown.segment == "function"){
if (typeof thrown.delay == "number"){
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
testCase.__yui_wait = setTimeout(function(){
YUITest.TestRunner._resumeTest(thrown.segment);
}, thrown.delay);
this._waiting = true;
} else {
throw new Error("Asynchronous tests not supported in this environment.");
}
}
}
return;
} else {
//first check to see if it should error
if (!shouldError) {
error = new YUITest.UnexpectedError(thrown);
failed = true;
} else {
//check to see what type of data we have
if (typeof shouldError == "string"){
//if it's a string, check the error message
if (thrown.message != shouldError){
error = new YUITest.UnexpectedError(thrown);
failed = true;
}
} else if (typeof shouldError == "function"){
//if it's a function, see if the error is an instance of it
if (!(thrown instanceof shouldError)){
error = new YUITest.UnexpectedError(thrown);
failed = true;
}
} else if (typeof shouldError == "object" && shouldError !== null){
//if it's an object, check the instance and message
if (!(thrown instanceof shouldError.constructor) ||
thrown.message != shouldError.message){
error = new YUITest.UnexpectedError(thrown);
failed = true;
}
}
}
}
}
//fire appropriate event
if (failed) {
this.fire({ type: this.TEST_FAIL_EVENT, testCase: testCase, testName: testName, error: error });
} else {
this.fire({ type: this.TEST_PASS_EVENT, testCase: testCase, testName: testName });
}
//run the tear down
this._execNonTestMethod(node.parent, "tearDown", false);
//reset the assert count
YUITest.Assert._reset();
//calculate duration
var duration = (new Date()) - node._start;
//update results
node.parent.results[testName] = {
result: failed ? "fail" : "pass",
message: error ? error.getMessage() : "Test passed",
type: "test",
name: testName,
duration: duration
};
if (failed){
node.parent.results.failed++;
} else {
node.parent.results.passed++;
}
node.parent.results.total++;
//set timeout not supported in all environments
if (typeof setTimeout != "undefined"){
setTimeout(function(){
YUITest.TestRunner._run();
}, 0);
} else {
this._run();
}
},
/**
* Handles an error as if it occurred within the currently executing
* test. This is for mock methods that may be called asynchronously
* and therefore out of the scope of the TestRunner. Previously, this
* error would bubble up to the browser. Now, this method is used
* to tell TestRunner about the error. This should never be called
* by anyplace other than the Mock object.
* @param {Error} error The error object.
* @return {Void}
* @method _handleError
* @private
* @static
*/
_handleError: function(error){
if (this._waiting){
this._resumeTest(function(){
throw error;
});
} else {
throw error;
}
},
/**
* Runs a single test based on the data provided in the node.
* @param {TestNode} node The TestNode representing the test to run.
* @return {Void}
* @static
* @private
* @name _runTest
*/
_runTest : function (node) {
//get relevant information
var testName = node.testObject,
testCase = node.parent.testObject,
test = testCase[testName],
//get the "should" test cases
shouldIgnore = testName.indexOf("ignore:") === 0 ||
!inGroups(testCase.groups, this._groups) ||
(testCase._should.ignore || {})[testName]; //deprecated
//figure out if the test should be ignored or not
if (shouldIgnore){
//update results
node.parent.results[testName] = {
result: "ignore",
message: "Test ignored",
type: "test",
name: testName.indexOf("ignore:") === 0 ? testName.substring(7) : testName
};
node.parent.results.ignored++;
node.parent.results.total++;
this.fire({ type: this.TEST_IGNORE_EVENT, testCase: testCase, testName: testName });
//some environments don't support setTimeout
if (typeof setTimeout != "undefined"){
setTimeout(function(){
YUITest.TestRunner._run();
}, 0);
} else {
this._run();
}
} else {
//mark the start time
node._start = new Date();
//run the setup
this._execNonTestMethod(node.parent, "setUp", false);
//now call the body of the test
this._resumeTest(test);
}
},
//-------------------------------------------------------------------------
// Misc Methods
//-------------------------------------------------------------------------
/**
* Retrieves the name of the current result set.
* @return {String} The name of the result set.
* @method getName
*/
getName: function(){
return this.masterSuite.name;
},
/**
* The name assigned to the master suite of the TestRunner. This is the name
* that is output as the root's name when results are retrieved.
* @param {String} name The name of the result set.
* @return {Void}
* @method setName
*/
setName: function(name){
this.masterSuite.name = name;
},
//-------------------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------------------
/**
* Adds a test suite or test case to the list of test objects to run.
* @param testObject Either a TestCase or a TestSuite that should be run.
* @return {Void}
* @method add
* @static
*/
add : function (testObject) {
this.masterSuite.add(testObject);
return this;
},
/**
* Removes all test objects from the runner.
* @return {Void}
* @method clear
* @static
*/
clear : function () {
this.masterSuite = new YUITest.TestSuite("yuitests" + (new Date()).getTime());
},
/**
* Indicates if the TestRunner is waiting for a test to resume
* @return {Boolean} True if the TestRunner is waiting, false if not.
* @method isWaiting
* @static
*/
isWaiting: function() {
return this._waiting;
},
/**
* Indicates that the TestRunner is busy running tests and therefore can't
* be stopped and results cannot be gathered.
* @return {Boolean} True if the TestRunner is running, false if not.
* @method isRunning
*/
isRunning: function(){
return this._running;
},
/**
* Returns the last complete results set from the TestRunner. Null is returned
* if the TestRunner is running or no tests have been run.
* @param {Function} format (Optional) A test format to return the results in.
* @return {Object|String} Either the results object or, if a test format is
* passed as the argument, a string representing the results in a specific
* format.
* @method getResults
*/
getResults: function(format){
if (!this._running && this._lastResults){
if (typeof format == "function"){
return format(this._lastResults);
} else {
return this._lastResults;
}
} else {
return null;
}
},
/**
* Returns the coverage report for the files that have been executed.
* This returns only coverage information for files that have been
* instrumented using YUI Test Coverage and only those that were run
* in the same pass.
* @param {Function} format (Optional) A coverage format to return results in.
* @return {Object|String} Either the coverage object or, if a coverage
* format is specified, a string representing the results in that format.
* @method getCoverage
*/
getCoverage: function(format){
if (!this._running && typeof _yuitest_coverage == "object"){
if (typeof format == "function"){
return format(_yuitest_coverage);
} else {
return _yuitest_coverage;
}
} else {
return null;
}
},
/**
* Used to continue processing when a method marked with
* "async:" is executed. This should not be used in test
* methods, only in init(). Each argument is a string, and
* when the returned function is executed, the arguments
* are assigned to the context data object using the string
* as the key name (value is the argument itself).
* @private
* @return {Function} A callback function.
*/
callback: function(){
var names = arguments,
data = this._context,
that = this;
return function(){
for (var i=0; i < arguments.length; i++){
data[names[i]] = arguments[i];
}
that._run();
};
},
/**
* Resumes the TestRunner after wait() was called.
* @param {Function} segment The function to run as the rest
* of the haulted test.
* @return {Void}
* @method resume
* @static
*/
resume : function (segment) {
if (this._waiting){
this._resumeTest(segment || function(){});
} else {
throw new Error("resume() called without wait().");
}
},
/**
* Runs the test suite.
* @param {Object|Boolean} options (Optional) Options for the runner:
* <code>oldMode</code> indicates the TestRunner should work in the YUI <= 2.8 way
* of internally managing test suites. <code>groups</code> is an array
* of test groups indicating which tests to run.
* @return {Void}
* @method run
* @static
*/
run : function (options) {
options = options || {};
//pointer to runner to avoid scope issues
var runner = YUITest.TestRunner,
oldMode = options.oldMode;
//if there's only one suite on the masterSuite, move it up
if (!oldMode && this.masterSuite.items.length == 1 && this.masterSuite.items[0] instanceof YUITest.TestSuite){
this.masterSuite = this.masterSuite.items[0];
}
//determine if there are any groups to filter on
runner._groups = (options.groups instanceof Array) ? "," + options.groups.join(",") + "," : "";
//initialize the runner
runner._buildTestTree();
runner._context = {};