forked from reporter123/gsunit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCode.js
767 lines (673 loc) · 25.8 KB
/
Code.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
/**
* --- GsUnit ---
*
* Copyright (c) 2012 James Ferreira
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Thank you Edward Hieatt, http://www.JsUnit.net
*
* Forked by Sunny Patel (https://github.com/laughdonor/GSUnit)
*/
var GsUnit = {};
/**
* For convenience, a variable that equals "undefined"
*/
var GsUnit_UNDEFINED_VALUE;
/**
* Whether or not the current test page has been (completely) loaded yet
*/
var isTestPageLoaded = false;
/**
* Predicate used for testing JavaScript == (i.e. equality excluding type)
*/
GsUnit.DOUBLE_EQUALITY_PREDICATE = function(var1, var2) {return var1 == var2;};
/**
* Predicate used for testing JavaScript === (i.e. equality including type)
*/
GsUnit.TRIPLE_EQUALITY_PREDICATE = function(var1, var2) {return var1 === var2;};
/**
* Predicate used for testing whether two objects' valueOf are equal
*/
GsUnit.TO_VALUE_EQUALITY_PREDICATE = function(var1, var2) {return var1.valueOf() === var2.valueOf();};
/**
* Predicate used for testing whether two objects' toStrings are equal
*/
GsUnit.TO_STRING_EQUALITY_PREDICATE = function(var1, var2) {return var1.toString() === var2.toString();};
/**
* Hash of predicates for testing equality by primitive type
*/
GsUnit.PRIMITIVE_EQUALITY_PREDICATES = {
'String': GsUnit.DOUBLE_EQUALITY_PREDICATE,
'Number': GsUnit.DOUBLE_EQUALITY_PREDICATE,
'Boolean': GsUnit.DOUBLE_EQUALITY_PREDICATE,
'Date': GsUnit.TO_VALUE_EQUALITY_PREDICATE,
'RegExp': GsUnit.TO_STRING_EQUALITY_PREDICATE,
'Function': GsUnit.TO_STRING_EQUALITY_PREDICATE
}
/**
* @param Any object
* @return String - the type of the given object
* @private
*/
GsUnit.trueTypeOf = function(something) {
var result = typeof something;
try {
switch (result) {
case 'string':
break;
case 'boolean':
break;
case 'number':
break;
case 'object':
case 'function':
switch (something.constructor) {
case new String().constructor:
result = 'String';
break;
case new Boolean().constructor:
result = 'Boolean';
break;
case new Number().constructor:
result = 'Number';
break;
case new Array().constructor:
result = 'Array';
break;
case new RegExp().constructor:
result = 'RegExp';
break;
case new Date().constructor:
result = 'Date';
break;
case Function:
result = 'Function';
break;
default:
var m = something.constructor.toString().match(/function\s*([^( ]+)\(/);
if (m)
result = m[1];
else
break;
}
break;
}
}
finally {
result = result.substr(0, 1).toUpperCase() + result.substr(1);
return result;
}
}
/**
* @private
*/
GsUnit.displayStringForValue = function(aVar) {
var result = '<' + aVar + '>';
if (!(aVar === null || aVar === GsUnit_UNDEFINED_VALUE)) {
result += ' (' + GsUnit.trueTypeOf(aVar) + ')';
}
return result;
}
/**
* @private
*/
GsUnit.validateArguments = function(expectedNumberOfNonCommentArgs, args) {
if (!( args.length == expectedNumberOfNonCommentArgs ||
(args.length == expectedNumberOfNonCommentArgs + 1 && (typeof(args[0]) == 'string') || args[0] == null)))
throw 'Incorrect arguments passed to assert function';
}
/**
* @private
*/
GsUnit.nonCommentArg = function(desiredNonCommentArgIndex, expectedNumberOfNonCommentArgs, args) {
return GsUnit.argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) ?
args[desiredNonCommentArgIndex] :
args[desiredNonCommentArgIndex - 1];
}
/**
* @private
*/
GsUnit.argumentsIncludeComments = function(expectedNumberOfNonCommentArgs, args) {
return args.length == expectedNumberOfNonCommentArgs + 1;
}
/**
* @private
*/
GsUnit.commentArg = function(expectedNumberOfNonCommentArgs, args) {
if (GsUnit.argumentsIncludeComments(expectedNumberOfNonCommentArgs, args))
return args[0];
return null;
}
/**
* @private
*/
GsUnit.checkEquals = function(var1, var2) {
return var1 === var2;
}
/**
* @private
*/
GsUnit.checkNotUndefined = function(aVar) {
return aVar !== GsUnit_UNDEFINED_VALUE;
}
/**
* @private
*/
GsUnit.checkNotNull = function(aVar) {
return aVar !== null;
}
/**
* All assertions ultimately go through this method.
* @private
*/
GsUnit.assert = function(comment, booleanValue, failureMessage) {
if (!booleanValue)
throw new GsUnit.Failure(comment, failureMessage);
}
/**
* Checks that two values are equal (using ===)
* @param {String} comment optional, displayed in the case of failure
* @param {Value} expected the expected value
* @param {Value} actual the actual value
* @throws GsUnit.Failure if the values are not equal
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertEquals() {
GsUnit.validateArguments(2, arguments);
var var1 = GsUnit.nonCommentArg(1, 2, arguments);
var var2 = GsUnit.nonCommentArg(2, 2, arguments);
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkEquals(var1, var2), 'Expected ' + GsUnit.displayStringForValue(var1) + ' but was ' + GsUnit.displayStringForValue(var2));
}
/**
* Checks that the given boolean value is true.
* @param {String} comment optional, displayed in the case of failure
* @param {Boolean} value that is expected to be true
* @throws GsUnit.Failure if the given value is not true
* @throws GsUnitInvalidAssertionArgument if the given value is not a boolean or if an incorrect number of arguments is passed
*/
function assert() {
GsUnit.validateArguments(1, arguments);
var booleanValue = GsUnit.nonCommentArg(1, 1, arguments);
if (typeof(booleanValue) != 'boolean')
throw new GsUnit.AssertionArgumentError('Bad argument to assert(boolean)');
GsUnit.assert(GsUnit.commentArg(1, arguments), booleanValue === true, 'Call to assert(boolean) with false');
}
/**
* Synonym for assertTrue
* @see #assert
*/
function assertTrue() {
GsUnit.validateArguments(1, arguments);
assert(GsUnit.commentArg(1, arguments), GsUnit.nonCommentArg(1, 1, arguments));
}
/**
* Checks that a boolean value is false.
* @param {String} comment optional, displayed in the case of failure
* @param {Boolean} value that is expected to be false
* @throws GsUnit.Failure if value is not false
* @throws GsUnitInvalidAssertionArgument if the given value is not a boolean or if an incorrect number of arguments is passed
*/
function assertFalse() {
GsUnit.validateArguments(1, arguments);
var booleanValue = GsUnit.nonCommentArg(1, 1, arguments);
if (typeof(booleanValue) != 'boolean')
throw new GsUnit.AssertionArgumentError('Bad argument to assertFalse(boolean)');
GsUnit.assert(GsUnit.commentArg(1, arguments), booleanValue === false, 'Call to assertFalse(boolean) with true');
}
/**
* Checks that two values are not equal (using !==)
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value1 a value
* @param {Value} value2 another value
* @throws GsUnit.Failure if the values are equal
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertNotEquals() {
GsUnit.validateArguments(2, arguments);
var var1 = GsUnit.nonCommentArg(1, 2, arguments);
var var2 = GsUnit.nonCommentArg(2, 2, arguments);
GsUnit.assert(GsUnit.commentArg(2, arguments), var1 !== var2, 'Expected not to be ' + GsUnit.displayStringForValue(var2));
}
/**
* Checks that a value is null
* @param {String} comment optional, displayed in the case of failure
* @param {Null} value the value
* @throws GsUnit.Failure if the value is not null
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertNull() {
GsUnit.validateArguments(1, arguments);
var aVar = GsUnit.nonCommentArg(1, 1, arguments);
GsUnit.assert(GsUnit.commentArg(1, arguments), aVar === null, 'Expected ' + GsUnit.displayStringForValue(null) + ' but was ' + GsUnit.displayStringForValue(aVar));
}
/**
* Checks that a value is not null
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value the value
* @throws GsUnit.Failure if the value is null
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertNotNull() {
GsUnit.validateArguments(1, arguments);
var aVar = GsUnit.nonCommentArg(1, 1, arguments);
GsUnit.assert(GsUnit.commentArg(1, arguments), GsUnit.checkNotNull(aVar), 'Expected not to be ' + GsUnit.displayStringForValue(null));
}
/**
* Checks that a value is undefined
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value the value
* @throws GsUnit.Failure if the value is not undefined
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertUndefined() {
GsUnit.validateArguments(1, arguments);
var aVar = GsUnit.nonCommentArg(1, 1, arguments);
GsUnit.assert(GsUnit.commentArg(1, arguments), aVar === GsUnit_UNDEFINED_VALUE, 'Expected ' + GsUnit.displayStringForValue(GsUnit_UNDEFINED_VALUE) + ' but was ' + GsUnit.displayStringForValue(aVar));
}
/**
* Checks that a value is not undefined
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value the value
* @throws GsUnit.Failure if the value is undefined
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertNotUndefined() {
GsUnit.validateArguments(1, arguments);
var aVar = GsUnit.nonCommentArg(1, 1, arguments);
GsUnit.assert(GsUnit.commentArg(1, arguments), GsUnit.checkNotUndefined(aVar), 'Expected not to be ' + GsUnit.displayStringForValue(GsUnit_UNDEFINED_VALUE));
}
/**
* Checks that a value is NaN (Not a Number)
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value the value
* @throws GsUnit.Failure if the value is a number
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertNaN() {
GsUnit.validateArguments(1, arguments);
var aVar = GsUnit.nonCommentArg(1, 1, arguments);
GsUnit.assert(GsUnit.commentArg(1, arguments), isNaN(aVar), 'Expected NaN');
}
/**
* Checks that a value is not NaN (i.e. is a number)
* @param {String} comment optional, displayed in the case of failure
* @param {Number} value the value
* @throws GsUnit.Failure if the value is not a number
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertNotNaN() {
GsUnit.validateArguments(1, arguments);
var aVar = GsUnit.nonCommentArg(1, 1, arguments);
GsUnit.assert(GsUnit.commentArg(1, arguments), !isNaN(aVar), 'Expected not NaN');
}
/**
* Checks that an object is equal to another using === for primitives and their object counterparts but also desceding
* into collections and calling assertObjectEquals for each element
* @param {String} comment optional, displayed in the case of failure
* @param {Object} value the expected value
* @param {Object} value the actual value
* @throws GsUnit.Failure if the actual value does not equal the expected value
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertObjectEquals() {
GsUnit.validateArguments(2, arguments);
var var1 = GsUnit.nonCommentArg(1, 2, arguments);
var var2 = GsUnit.nonCommentArg(2, 2, arguments);
var failureMessage = GsUnit.commentArg(2, arguments) ? GsUnit.commentArg(2, arguments) : '';
if (var1 === var2)
return;
var isEqual = false;
var typeOfVar1 = GsUnit.trueTypeOf(var1);
var typeOfVar2 = GsUnit.trueTypeOf(var2);
if (typeOfVar1 == typeOfVar2) {
var primitiveEqualityPredicate = GsUnit.PRIMITIVE_EQUALITY_PREDICATES[typeOfVar1];
if (primitiveEqualityPredicate) {
isEqual = primitiveEqualityPredicate(var1, var2);
} else {
var expectedKeys = GsUnit.Util.getKeys(var1).sort().join(", ");
var actualKeys = GsUnit.Util.getKeys(var2).sort().join(", ");
if (expectedKeys != actualKeys) {
GsUnit.assert(failureMessage, false, 'Expected keys "' + expectedKeys + '" but found "' + actualKeys + '"');
}
for (var i in var1) {
assertObjectEquals(failureMessage + ' found nested ' + typeOfVar1 + '@' + i + '\n', var1[i], var2[i]);
}
isEqual = true;
}
}
GsUnit.assert(failureMessage, isEqual, 'Expected ' + GsUnit.displayStringForValue(var1) + ' but was ' + GsUnit.displayStringForValue(var2));
}
/**
* Checks that an array is equal to another by checking that both are arrays and then comparing their elements using assertObjectEquals
* @param {String} comment optional, displayed in the case of failure
* @param {Array} value the expected array
* @param {Array} value the actual array
* @throws GsUnit.Failure if the actual value does not equal the expected value
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertArrayEquals() {
GsUnit.validateArguments(2, arguments);
var array1 = GsUnit.nonCommentArg(1, 2, arguments);
var array2 = GsUnit.nonCommentArg(2, 2, arguments);
if (GsUnit.trueTypeOf(array1) != 'Array' || GsUnit.trueTypeOf(array2) != 'Array') {
throw new GsUnit.AssertionArgumentError('Non-array passed to assertArrayEquals');
}
assertObjectEquals(GsUnit.commentArg(2, arguments), GsUnit.nonCommentArg(1, 2, arguments), GsUnit.nonCommentArg(2, 2, arguments));
}
/**
* Checks that a value evaluates to true in the sense that value == true
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value the value
* @throws GsUnit.Failure if the actual value does not evaluate to true
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertEvaluatesToTrue() {
GsUnit.validateArguments(1, arguments);
var value = GsUnit.nonCommentArg(1, 1, arguments);
if (!value)
fail(GsUnit.commentArg(1, arguments));
}
/**
* Checks that a value evaluates to false in the sense that value == false
* @param {String} comment optional, displayed in the case of failure
* @param {Value} value the value
* @throws GsUnit.Failure if the actual value does not evaluate to true
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertEvaluatesToFalse() {
GsUnit.validateArguments(1, arguments);
var value = GsUnit.nonCommentArg(1, 1, arguments);
if (value)
fail(GsUnit.commentArg(1, arguments));
}
/**
* Checks that a hash is has the same contents as another by iterating over the expected hash and checking that each
* key's value is present in the actual hash and calling assertEquals on the two values, and then checking that there is
* no key in the actual hash that isn't present in the expected hash.
* @param {String} comment optional, displayed in the case of failure
* @param {Object} value the expected hash
* @param {Object} value the actual hash
* @throws GsUnit.Failure if the actual hash does not evaluate to true
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertHashEquals() {
GsUnit.validateArguments(2, arguments);
var var1 = GsUnit.nonCommentArg(1, 2, arguments);
var var2 = GsUnit.nonCommentArg(2, 2, arguments);
for (var key in var1) {
assertNotUndefined("Expected hash had key " + key + " that was not found", var2[key]);
assertEquals(
"Value for key " + key + " mismatch - expected = " + var1[key] + ", actual = " + var2[key],
var1[key], var2[key]
);
}
for (var key in var2) {
assertNotUndefined("Actual hash had key " + key + " that was not expected", var1[key]);
}
}
/**
* Checks that two value are within a tolerance of one another
* @param {String} comment optional, displayed in the case of failure
* @param {Number} value1 a value
* @param {Number} value1 another value
* @param {Number} tolerance the tolerance
* @throws GsUnit.Failure if the two values are not within tolerance of each other
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments is passed
*/
function assertRoughlyEquals() {
GsUnit.validateArguments(3, arguments);
var expected = GsUnit.nonCommentArg(1, 3, arguments);
var actual = GsUnit.nonCommentArg(2, 3, arguments);
var tolerance = GsUnit.nonCommentArg(3, 3, arguments);
assertTrue(
"Expected " + expected + ", but got " + actual + " which was more than " + tolerance + " away",
Math.abs(expected - actual) < tolerance
);
}
/**
* Checks that a collection contains a value by checking that collection.indexOf(value) is not -1
* @param {String} comment optional, displayed in the case of failure
* @param {Value} collection the collection
* @param {Value} value the value
* @throws GsUnit.Failure if the collection does not contain the value
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments are passed
*/
function assertContains() {
GsUnit.validateArguments(2, arguments);
var value = GsUnit.nonCommentArg(1, 2, arguments);
var collection = GsUnit.nonCommentArg(2, 2, arguments);
assertTrue(
"Expected '" + collection + "' to contain '" + value + "'",
collection.indexOf(value) != -1
);
}
/**
* Checks that two arrays have the same contents, ignoring the order of the contents
* @param {String} comment optional, displayed in the case of failure
* @param {Array} array1 first array
* @param {Array} array2 second array
* @throws GsUnit.Failure if the two arrays contain different contents
* @throws GsUnitInvalidAssertionArgument if an incorrect number of arguments are passed
*/
function assertArrayEqualsIgnoringOrder() {
GsUnit.validateArguments(2, arguments);
var var1 = GsUnit.nonCommentArg(1, 2, arguments);
var var2 = GsUnit.nonCommentArg(2, 2, arguments);
var notEqualsMessage = "Expected arrays " + GsUnit.displayStringForValue(var1) + " and " + GsUnit.displayStringForValue(var2) + " to be equal (ignoring order)";
var notArraysMessage = "Expected arguments " + GsUnit.displayStringForValue(var1) + " and " + GsUnit.displayStringForValue(var2) + " to be arrays";
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkNotNull(var1), notEqualsMessage);
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkNotNull(var2), notEqualsMessage);
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkNotUndefined(var1.length), notArraysMessage);
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkNotUndefined(var1.join), notArraysMessage);
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkNotUndefined(var2.length), notArraysMessage);
GsUnit.assert(GsUnit.commentArg(2, arguments), GsUnit.checkNotUndefined(var2.join), notArraysMessage);
GsUnit.assert(GsUnit.commentArg(1, arguments), GsUnit.checkEquals(var1.length, var2.length), notEqualsMessage);
for (var i = 0; i < var1.length; i++) {
var found = false;
for (var j = 0; j < var2.length; j++) {
try {
assertObjectEquals(notEqualsMessage, var1[i], var2[j]);
found = true;
} catch (ignored) {
}
}
GsUnit.assert(GsUnit.commentArg(2, arguments), found, notEqualsMessage);
}
}
/**
* Causes a failure
* @param failureMessage the message for the failure
*/
function fail(failureMessage) {
throw new GsUnit.Failure("Call to fail()", failureMessage);
}
/**
* @class
* A GsUnit.Failure represents an assertion failure (or a call to fail()) during the execution of a Test Function
* @param comment an optional comment about the failure
* @param message the reason for the failure
*/
GsUnit.Failure = function(comment, message) {
/**
* Declaration that this is a GsUnit.Failure
* @ignore
*/
this.isGsUnitFailure = true;
/**
* An optional comment about the failure
*/
this.comment = comment;
/**
* The reason for the failure
*/
this.GsUnitMessage = message;
/**
* The stack trace at the point at which the failure was encountered
*/
// this.stackTrace = GsUnit.Util.getStackTrace();
var failComment = '';
if (comment != null) failComment = 'Comment: '+comment;
throw failComment +' -- Failure: '+ message;
}
/**
* @class
* A GsUnitAssertionArgumentError represents an invalid call to an assertion function - either an invalid argument type
* or an incorrect number of arguments
* @param description a description of the argument error
*/
GsUnit.AssertionArgumentError = function(description) {
/**
* A description of the argument error
*/
this.description = description;
throw 'Argument error: '+ description;
}
/**
* @class
* @constructor
* Contains utility functions for the GsUnit framework
*/
GsUnit.Util = {};
/**
* Standardizes an HTML string by temporarily creating a DIV, setting its innerHTML to the string, and the asking for
* the innerHTML back
* @param html
*/
GsUnit.Util.standardizeHTML = function(html) {
var translator = document.createElement("DIV");
translator.innerHTML = html;
return GsUnit.Util.trim(translator.innerHTML);
}
/**
* Returns whether the given string is blank after being trimmed of whitespace
* @param string
*/
GsUnit.Util.isBlank = function(string) {
return GsUnit.Util.trim(string) == '';
}
/**
* Implemented here because the JavaScript Array.push(anObject) and Array.pop() functions are not available in IE 5.0
* @param anArray the array onto which to push
* @param anObject the object to push onto the array
*/
GsUnit.Util.push = function(anArray, anObject) {
anArray[anArray.length] = anObject;
}
/**
* Implemented here because the JavaScript Array.push(anObject) and Array.pop() functions are not available in IE 5.0
* @param anArray the array from which to pop
*/
GsUnit.Util.pop = function pop(anArray) {
if (anArray.length >= 1) {
delete anArray[anArray.length - 1];
anArray.length--;
}
}
/**
* Returns the name of the given function, or 'anonymous' if it has no name
* @param aFunction
*/
GsUnit.Util.getFunctionName = function(aFunction) {
var regexpResult = aFunction.toString().match(/function(\s*)(\w*)/);
if (regexpResult && regexpResult.length >= 2 && regexpResult[2]) {
return regexpResult[2];
}
return 'anonymous';
}
/**
* Returns the current stack trace
*/
GsUnit.Util.getStackTrace = function() {
var result = '';
if (typeof(arguments.caller) != 'undefined') { // IE, not ECMA
for (var a = arguments.caller; a != null; a = a.caller) {
result += '> ' + GsUnit.Util.getFunctionName(a.callee) + '\n';
if (a.caller == a) {
result += '*';
break;
}
}
}
else { // Mozilla, not ECMA
// fake an exception so we can get Mozilla's error stack
try
{
foo.bar;
}
catch(exception)
{
var stack = GsUnit.Util.parseErrorStack(exception);
for (var i = 1; i < stack.length; i++)
{
result += '> ' + stack[i] + '\n';
}
}
}
return result;
}
/**
* Returns an array of stack trace elements from the given exception
* @param exception
*/
GsUnit.Util.parseErrorStack = function(exception) {
var stack = [];
var name;
if (!exception || !exception.stack) {
return stack;
}
var stacklist = exception.stack.split('\n');
for (var i = 0; i < stacklist.length - 1; i++) {
var framedata = stacklist[i];
name = framedata.match(/^(\w*)/)[1];
if (!name) {
name = 'anonymous';
}
stack[stack.length] = name;
}
// remove top level anonymous functions to match IE
while (stack.length && stack[stack.length - 1] == 'anonymous') {
stack.length = stack.length - 1;
}
return stack;
}
/**
* Strips whitespace from either end of the given string
* @param string
*/
GsUnit.Util.trim = function(string) {
if (string == null)
return null;
var startingIndex = 0;
var endingIndex = string.length - 1;
var singleWhitespaceRegex = /\s/;
while (string.substring(startingIndex, startingIndex + 1).match(singleWhitespaceRegex))
startingIndex++;
while (string.substring(endingIndex, endingIndex + 1).match(singleWhitespaceRegex))
endingIndex--;
if (endingIndex < startingIndex)
return '';
return string.substring(startingIndex, endingIndex + 1);
}
GsUnit.Util.getKeys = function(obj) {
var keys = [];
for (var key in obj) {
GsUnit.Util.push(keys, key);
}
return keys;
}
GsUnit.Util.inherit = function(superclass, subclass) {
var x = function() {};
x.prototype = superclass.prototype;
subclass.prototype = new x();
}