-
Notifications
You must be signed in to change notification settings - Fork 9
/
testharness.js
2233 lines (2054 loc) · 74.7 KB
/
testharness.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
/*
Distributed under both the W3C Test Suite License [1] and the W3C
3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
policies and contribution forms [3].
[1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
[2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
[3] http://www.w3.org/2004/10/27-testcases
*/
/*
* == Introduction ==
*
* This file provides a framework for writing testcases. It is intended to
* provide a convenient API for making common assertions, and to work both
* for testing synchronous and asynchronous DOM features in a way that
* promotes clear, robust, tests.
*
* == Basic Usage ==
*
* To use this file, import the script and the testharnessreport script into
* the test document:
* <script src="/resources/testharness.js"></script>
* <script src="/resources/testharnessreport.js"></script>
*
* Within each file one may define one or more tests. Each test is atomic
* in the sense that a single test has a single result (pass/fail/timeout).
* Within each test one may have a number of asserts. The test fails at the
* first failing assert, and the remainder of the test is (typically) not run.
*
* If the file containing the tests is a HTML file with an element of id "log"
* this will be populated with a table containing the test results after all
* the tests have run.
*
* NOTE: By default tests must be created before the load event fires. For ways
* to create tests after the load event, see "Determining when all tests
* are complete", below
*
* == Synchronous Tests ==
*
* To create a synchronous test use the test() function:
*
* test(test_function, name, properties)
*
* test_function is a function that contains the code to test. For example a
* trivial passing test would be:
*
* test(function() {assert_true(true)}, "assert_true with true")
*
* The function passed in is run in the test() call.
*
* properties is an object that overrides default test properties. The
* recognised properties are:
* timeout - the test timeout in ms
*
* e.g.
* test(test_function, "Sample test", {timeout:1000})
*
* would run test_function with a timeout of 1s.
*
* Additionally, test-specific metadata can be passed in the properties. These
* are used when the individual test has different metadata from that stored
* in the <head>.
* The recognized metadata properties are:
*
* help - The url of the part of the specification being tested
*
* assert - A human readable description of what the test is attempting
* to prove
*
* author - Name and contact information for the author of the test in the
* format: "Name <email_addr>" or "Name http://contact/url"
*
* == Asynchronous Tests ==
*
* Testing asynchronous features is somewhat more complex since the result of
* a test may depend on one or more events or other callbacks. The API provided
* for testing these features is indended to be rather low-level but hopefully
* applicable to many situations.
*
* To create a test, one starts by getting a Test object using async_test:
*
* async_test(name, properties)
*
* e.g.
* var t = async_test("Simple async test")
*
* Assertions can be added to the test by calling the step method of the test
* object with a function containing the test assertions:
*
* t.step(function() {assert_true(true)});
*
* When all the steps are complete, the done() method must be called:
*
* t.done();
*
* As a convenience, async_test can also takes a function as first argument.
* This function is called with the test object as both its `this` object and
* first argument. The above example can be rewritten as:
*
* async_test(function(t) {
* object.some_event = function() {
* t.step(function (){assert_true(true); t.done();});
* };
* }, "Simple async test");
*
* which avoids cluttering the global scope with references to async
* tests instances.
*
* The properties argument is identical to that for test().
*
* In many cases it is convenient to run a step in response to an event or a
* callback. A convenient method of doing this is through the step_func method
* which returns a function that, when called runs a test step. For example
*
* object.some_event = t.step_func(function(e) {assert_true(e.a)});
*
* == Making assertions ==
*
* Functions for making assertions start assert_
* The best way to get a list is to look in this file for functions names
* matching that pattern. The general signature is
*
* assert_something(actual, expected, description)
*
* although not all assertions precisely match this pattern e.g. assert_true
* only takes actual and description as arguments.
*
* The description parameter is used to present more useful error messages when
* a test fails
*
* NOTE: All asserts must be located in a test() or a step of an async_test().
* asserts outside these places won't be detected correctly by the harness
* and may cause a file to stop testing.
*
* == Setup ==
*
* Sometimes tests require non-trivial setup that may fail. For this purpose
* there is a setup() function, that may be called with one or two arguments.
* The two argument version is:
*
* setup(func, properties)
*
* The one argument versions may omit either argument.
* func is a function to be run synchronously. setup() becomes a no-op once
* any tests have returned results. Properties are global properties of the test
* harness. Currently recognised properties are:
*
* timeout - The time in ms after which the harness should stop waiting for
* tests to complete (this is different to the per-test timeout
* because async tests do not start their timer until .step is called)
*
* explicit_done - Wait for an explicit call to done() before declaring all
* tests complete (see below)
*
* output_document - The document to which results should be logged. By default
* this is the current document but could be an ancestor
* document in some cases e.g. a SVG test loaded in an HTML
* wrapper
*
* explicit_timeout - disable file timeout; only stop waiting for results
* when the timeout() function is called (typically for
* use when integrating with some existing test framework
* that has its own timeout mechanism).
*
* == Determining when all tests are complete ==
*
* By default the test harness will assume there are no more results to come
* when:
* 1) There are no Test objects that have been created but not completed
* 2) The load event on the document has fired
*
* This behaviour can be overridden by setting the explicit_done property to
* true in a call to setup(). If explicit_done is true, the test harness will
* not assume it is done until the global done() function is called. Once done()
* is called, the two conditions above apply like normal.
*
* == Generating tests ==
*
* NOTE: this functionality may be removed
*
* There are scenarios in which is is desirable to create a large number of
* (synchronous) tests that are internally similar but vary in the parameters
* used. To make this easier, the generate_tests function allows a single
* function to be called with each set of parameters in a list:
*
* generate_tests(test_function, parameter_lists, properties)
*
* For example:
*
* generate_tests(assert_equals, [
* ["Sum one and one", 1+1, 2],
* ["Sum one and zero", 1+0, 1]
* ])
*
* Is equivalent to:
*
* test(function() {assert_equals(1+1, 2)}, "Sum one and one")
* test(function() {assert_equals(1+0, 1)}, "Sum one and zero")
*
* Note that the first item in each parameter list corresponds to the name of
* the test.
*
* The properties argument is identical to that for test(). This may be a
* single object (used for all generated tests) or an array.
*
* == Callback API ==
*
* The framework provides callbacks corresponding to 3 events:
*
* start - happens when the first Test is created
* result - happens when a test result is recieved
* complete - happens when all results are recieved
*
* The page defining the tests may add callbacks for these events by calling
* the following methods:
*
* add_start_callback(callback) - callback called with no arguments
* add_result_callback(callback) - callback called with a test argument
* add_completion_callback(callback) - callback called with an array of tests
* and an status object
*
* tests have the following properties:
* status: A status code. This can be compared to the PASS, FAIL, TIMEOUT and
* NOTRUN properties on the test object
* message: A message indicating the reason for failure. In the future this
* will always be a string
*
* The status object gives the overall status of the harness. It has the
* following properties:
* status: Can be compared to the OK, ERROR and TIMEOUT properties
* message: An error message set when the status is ERROR
*
* == External API ==
*
* In order to collect the results of multiple pages containing tests, the test
* harness will, when loaded in a nested browsing context, attempt to call
* certain functions in each ancestor and opener browsing context:
*
* start - start_callback
* result - result_callback
* complete - completion_callback
*
* These are given the same arguments as the corresponding internal callbacks
* described above.
*
* == External API through cross-document messaging ==
*
* Where supported, the test harness will also send messages using
* cross-document messaging to each ancestor and opener browsing context. Since
* it uses the wildcard keyword (*), cross-origin communication is enabled and
* script on different origins can collect the results.
*
* This API follows similar conventions as those described above only slightly
* modified to accommodate message event API. Each message is sent by the harness
* is passed a single vanilla object, available as the `data` property of the
* event object. These objects are structures as follows:
*
* start - { type: "start" }
* result - { type: "result", test: Test }
* complete - { type: "complete", tests: [Test, ...], status: TestsStatus }
*
* == List of assertions ==
*
* assert_true(actual, description)
* asserts that /actual/ is strictly true
*
* assert_false(actual, description)
* asserts that /actual/ is strictly false
*
* assert_equals(actual, expected, description)
* asserts that /actual/ is the same value as /expected/
*
* assert_not_equals(actual, expected, description)
* asserts that /actual/ is a different value to /expected/. Yes, this means
* that "expected" is a misnomer
*
* assert_in_array(actual, expected, description)
* asserts that /expected/ is an Array, and /actual/ is equal to one of the
* members -- expected.indexOf(actual) != -1
*
* assert_array_equals(actual, expected, description)
* asserts that /actual/ and /expected/ have the same length and the value of
* each indexed property in /actual/ is the strictly equal to the corresponding
* property value in /expected/
*
* assert_approx_equals(actual, expected, epsilon, description)
* asserts that /actual/ is a number within +/- /epsilon/ of /expected/
*
* assert_less_than(actual, expected, description)
* asserts that /actual/ is a number less than /expected/
*
* assert_greater_than(actual, expected, description)
* asserts that /actual/ is a number greater than /expected/
*
* assert_less_than_equal(actual, expected, description)
* asserts that /actual/ is a number less than or equal to /expected/
*
* assert_greater_than_equal(actual, expected, description)
* asserts that /actual/ is a number greater than or equal to /expected/
*
* assert_regexp_match(actual, expected, description)
* asserts that /actual/ matches the regexp /expected/
*
* assert_class_string(object, class_name, description)
* asserts that the class string of /object/ as returned in
* Object.prototype.toString is equal to /class_name/.
*
* assert_own_property(object, property_name, description)
* assert that object has own property property_name
*
* assert_inherits(object, property_name, description)
* assert that object does not have an own property named property_name
* but that property_name is present in the prototype chain for object
*
* assert_idl_attribute(object, attribute_name, description)
* assert that an object that is an instance of some interface has the
* attribute attribute_name following the conditions specified by WebIDL
*
* assert_readonly(object, property_name, description)
* assert that property property_name on object is readonly
*
* assert_throws(code, func, description)
* code - the expected exception:
* o string: the thrown exception must be a DOMException with the given
* name, e.g., "TimeoutError" (for compatibility with existing
* tests, a constant is also supported, e.g., "TIMEOUT_ERR")
* o object: the thrown exception must have a property called "name" that
* matches code.name
* o null: allow any exception (in general, one of the options above
* should be used)
* func - a function that should throw
*
* assert_unreached(description)
* asserts if called. Used to ensure that some codepath is *not* taken e.g.
* an event does not fire.
*
* assert_any(assert_func, actual, expected_array, extra_arg_1, ... extra_arg_N)
* asserts that one assert_func(actual, expected_array_N, extra_arg1, ..., extra_arg_N)
* is true for some expected_array_N in expected_array. This only works for assert_func
* with signature assert_func(actual, expected, args_1, ..., args_N). Note that tests
* with multiple allowed pass conditions are bad practice unless the spec specifically
* allows multiple behaviours. Test authors should not use this method simply to hide
* UA bugs.
*
* assert_exists(object, property_name, description)
* *** deprecated ***
* asserts that object has an own property property_name
*
* assert_not_exists(object, property_name, description)
* *** deprecated ***
* assert that object does not have own property property_name
*/
(function ()
{
var debug = false;
// default timeout is 5 seconds, test can override if needed
var settings = {
output:true,
timeout:5000,
test_timeout:2000
};
var xhtml_ns = "http://www.w3.org/1999/xhtml";
// script_prefix is used by Output.prototype.show_results() to figure out
// where to get testharness.css from. It's enclosed in an extra closure to
// not pollute the library's namespace with variables like "src".
var script_prefix = null;
(function ()
{
var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++)
{
if (scripts[i].src)
{
var src = scripts[i].src;
}
else if (scripts[i].href)
{
//SVG case
var src = scripts[i].href.baseVal;
}
if (src && src.slice(src.length - "testharness.js".length) === "testharness.js")
{
script_prefix = src.slice(0, src.length - "testharness.js".length);
break;
}
}
})();
/*
* API functions
*/
var name_counter = 0;
function next_default_name()
{
//Don't use document.title to work around an Opera bug in XHTML documents
var prefix = document.getElementsByTagName("title").length > 0 ?
document.getElementsByTagName("title")[0].firstChild.data :
"Untitled";
var suffix = name_counter > 0 ? " " + name_counter : "";
name_counter++;
return prefix + suffix;
}
function test(func, name, properties)
{
var test_name = name ? name : next_default_name();
properties = properties ? properties : {};
var test_obj = new Test(test_name, properties);
test_obj.step(func);
if (test_obj.status === test_obj.NOTRUN) {
test_obj.done();
}
}
function async_test(func, name, properties)
{
if (typeof func !== "function") {
properties = name;
name = func;
func = null;
}
var test_name = name ? name : next_default_name();
properties = properties ? properties : {};
var test_obj = new Test(test_name, properties);
if (func) {
test_obj.step(func, test_obj, test_obj);
}
return test_obj;
}
function setup(func_or_properties, maybe_properties)
{
var func = null;
var properties = {};
if (arguments.length === 2) {
func = func_or_properties;
properties = maybe_properties;
} else if (func_or_properties instanceof Function){
func = func_or_properties;
} else {
properties = func_or_properties;
}
tests.setup(func, properties);
output.setup(properties);
}
function done() {
tests.end_wait();
}
function generate_tests(func, args, properties) {
forEach(args, function(x, i)
{
var name = x[0];
test(function()
{
func.apply(this, x.slice(1));
},
name,
Array.isArray(properties) ? properties[i] : properties);
});
}
function on_event(object, event, callback)
{
object.addEventListener(event, callback, false);
}
expose(test, 'test');
expose(async_test, 'async_test');
expose(generate_tests, 'generate_tests');
expose(setup, 'setup');
expose(done, 'done');
expose(on_event, 'on_event');
/*
* Return a string truncated to the given length, with ... added at the end
* if it was longer.
*/
function truncate(s, len)
{
if (s.length > len) {
return s.substring(0, len - 3) + "...";
}
return s;
}
/*
* Convert a value to a nice, human-readable string
*/
function format_value(val, seen)
{
if (!seen) {
seen = [];
}
if (typeof val === "object" && val !== null)
{
if (seen.indexOf(val) >= 0)
{
return "[...]";
}
seen.push(val);
}
if (Array.isArray(val))
{
return "[" + val.map(function(x) {return format_value(x, seen)}).join(", ") + "]";
}
switch (typeof val)
{
case "string":
val = val.replace("\\", "\\\\");
for (var i = 0; i < 32; i++)
{
var replace = "\\";
switch (i) {
case 0: replace += "0"; break;
case 1: replace += "x01"; break;
case 2: replace += "x02"; break;
case 3: replace += "x03"; break;
case 4: replace += "x04"; break;
case 5: replace += "x05"; break;
case 6: replace += "x06"; break;
case 7: replace += "x07"; break;
case 8: replace += "b"; break;
case 9: replace += "t"; break;
case 10: replace += "n"; break;
case 11: replace += "v"; break;
case 12: replace += "f"; break;
case 13: replace += "r"; break;
case 14: replace += "x0e"; break;
case 15: replace += "x0f"; break;
case 16: replace += "x10"; break;
case 17: replace += "x11"; break;
case 18: replace += "x12"; break;
case 19: replace += "x13"; break;
case 20: replace += "x14"; break;
case 21: replace += "x15"; break;
case 22: replace += "x16"; break;
case 23: replace += "x17"; break;
case 24: replace += "x18"; break;
case 25: replace += "x19"; break;
case 26: replace += "x1a"; break;
case 27: replace += "x1b"; break;
case 28: replace += "x1c"; break;
case 29: replace += "x1d"; break;
case 30: replace += "x1e"; break;
case 31: replace += "x1f"; break;
}
val = val.replace(RegExp(String.fromCharCode(i), "g"), replace);
}
return '"' + val.replace(/"/g, '\\"') + '"';
case "boolean":
case "undefined":
return String(val);
case "number":
// In JavaScript, -0 === 0 and String(-0) == "0", so we have to
// special-case.
if (val === -0 && 1/val === -Infinity)
{
return "-0";
}
return String(val);
case "object":
if (val === null)
{
return "null";
}
// Special-case Node objects, since those come up a lot in my tests. I
// ignore namespaces. I use duck-typing instead of instanceof, because
// instanceof doesn't work if the node is from another window (like an
// iframe's contentWindow):
// http://www.w3.org/Bugs/Public/show_bug.cgi?id=12295
if ("nodeType" in val
&& "nodeName" in val
&& "nodeValue" in val
&& "childNodes" in val)
{
switch (val.nodeType)
{
case Node.ELEMENT_NODE:
var ret = "<" + val.tagName.toLowerCase();
for (var i = 0; i < val.attributes.length; i++)
{
ret += " " + val.attributes[i].name + '="' + val.attributes[i].value + '"';
}
ret += ">" + val.innerHTML + "</" + val.tagName.toLowerCase() + ">";
return "Element node " + truncate(ret, 60);
case Node.TEXT_NODE:
return 'Text node "' + truncate(val.data, 60) + '"';
case Node.PROCESSING_INSTRUCTION_NODE:
return "ProcessingInstruction node with target " + format_value(truncate(val.target, 60)) + " and data " + format_value(truncate(val.data, 60));
case Node.COMMENT_NODE:
return "Comment node <!--" + truncate(val.data, 60) + "-->";
case Node.DOCUMENT_NODE:
return "Document node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
case Node.DOCUMENT_TYPE_NODE:
return "DocumentType node";
case Node.DOCUMENT_FRAGMENT_NODE:
return "DocumentFragment node with " + val.childNodes.length + (val.childNodes.length == 1 ? " child" : " children");
default:
return "Node object of unknown type";
}
}
// Fall through to default
default:
return typeof val + ' "' + truncate(String(val), 60) + '"';
}
}
expose(format_value, "format_value");
/*
* Assertions
*/
function assert_true(actual, description)
{
assert(actual === true, "assert_true", description,
"expected true got ${actual}", {actual:actual});
};
expose(assert_true, "assert_true");
function assert_false(actual, description)
{
assert(actual === false, "assert_false", description,
"expected false got ${actual}", {actual:actual});
};
expose(assert_false, "assert_false");
function same_value(x, y) {
if (y !== y)
{
//NaN case
return x !== x;
}
else if (x === 0 && y === 0) {
//Distinguish +0 and -0
return 1/x === 1/y;
}
else
{
//typical case
return x === y;
}
}
function assert_equals(actual, expected, description)
{
/*
* Test if two primitives are equal or two objects
* are the same object
*/
if (typeof actual != typeof expected)
{
assert(false, "assert_equals", description,
"expected (" + typeof expected + ") ${expected} but got (" + typeof actual + ") ${actual}",
{expected:expected, actual:actual});
return;
}
assert(same_value(actual, expected), "assert_equals", description,
"expected ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_equals, "assert_equals");
function assert_not_equals(actual, expected, description)
{
/*
* Test if two primitives are unequal or two objects
* are different objects
*/
assert(!same_value(actual, expected), "assert_not_equals", description,
"got disallowed value ${actual}",
{actual:actual});
};
expose(assert_not_equals, "assert_not_equals");
function assert_in_array(actual, expected, description)
{
assert(expected.indexOf(actual) != -1, "assert_in_array", description,
"value ${actual} not in array ${expected}",
{actual:actual, expected:expected});
}
expose(assert_in_array, "assert_in_array");
function assert_object_equals(actual, expected, description)
{
//This needs to be improved a great deal
function check_equal(actual, expected, stack)
{
stack.push(actual);
var p;
for (p in actual)
{
assert(expected.hasOwnProperty(p), "assert_object_equals", description,
"unexpected property ${p}", {p:p});
if (typeof actual[p] === "object" && actual[p] !== null)
{
if (stack.indexOf(actual[p]) === -1)
{
check_equal(actual[p], expected[p], stack);
}
}
else
{
assert(same_value(actual[p], expected[p]), "assert_object_equals", description,
"property ${p} expected ${expected} got ${actual}",
{p:p, expected:expected, actual:actual});
}
}
for (p in expected)
{
assert(actual.hasOwnProperty(p),
"assert_object_equals", description,
"expected property ${p} missing", {p:p});
}
stack.pop();
}
check_equal(actual, expected, []);
};
expose(assert_object_equals, "assert_object_equals");
function assert_array_equals(actual, expected, description)
{
assert(actual.length === expected.length,
"assert_array_equals", description,
"lengths differ, expected ${expected} got ${actual}",
{expected:expected.length, actual:actual.length});
for (var i=0; i < actual.length; i++)
{
assert(actual.hasOwnProperty(i) === expected.hasOwnProperty(i),
"assert_array_equals", description,
"property ${i}, property expected to be $expected but was $actual",
{i:i, expected:expected.hasOwnProperty(i) ? "present" : "missing",
actual:actual.hasOwnProperty(i) ? "present" : "missing"});
assert(same_value(expected[i], actual[i]),
"assert_array_equals", description,
"property ${i}, expected ${expected} but got ${actual}",
{i:i, expected:expected[i], actual:actual[i]});
}
}
expose(assert_array_equals, "assert_array_equals");
function assert_approx_equals(actual, expected, epsilon, description)
{
/*
* Test if two primitive numbers are equal withing +/- epsilon
*/
assert(typeof actual === "number",
"assert_approx_equals", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});
assert(Math.abs(actual - expected) <= epsilon,
"assert_approx_equals", description,
"expected ${expected} +/- ${epsilon} but got ${actual}",
{expected:expected, actual:actual, epsilon:epsilon});
};
expose(assert_approx_equals, "assert_approx_equals");
function assert_less_than(actual, expected, description)
{
/*
* Test if a primitive number is less than another
*/
assert(typeof actual === "number",
"assert_less_than", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});
assert(actual < expected,
"assert_less_than", description,
"expected a number less than ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_less_than, "assert_less_than");
function assert_greater_than(actual, expected, description)
{
/*
* Test if a primitive number is greater than another
*/
assert(typeof actual === "number",
"assert_greater_than", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});
assert(actual > expected,
"assert_greater_than", description,
"expected a number greater than ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_greater_than, "assert_greater_than");
function assert_less_than_equal(actual, expected, description)
{
/*
* Test if a primitive number is less than or equal to another
*/
assert(typeof actual === "number",
"assert_less_than_equal", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});
assert(actual <= expected,
"assert_less_than", description,
"expected a number less than or equal to ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_less_than_equal, "assert_less_than_equal");
function assert_greater_than_equal(actual, expected, description)
{
/*
* Test if a primitive number is greater than or equal to another
*/
assert(typeof actual === "number",
"assert_greater_than_equal", description,
"expected a number but got a ${type_actual}",
{type_actual:typeof actual});
assert(actual >= expected,
"assert_greater_than_equal", description,
"expected a number greater than or equal to ${expected} but got ${actual}",
{expected:expected, actual:actual});
};
expose(assert_greater_than_equal, "assert_greater_than_equal");
function assert_regexp_match(actual, expected, description) {
/*
* Test if a string (actual) matches a regexp (expected)
*/
assert(expected.test(actual),
"assert_regexp_match", description,
"expected ${expected} but got ${actual}",
{expected:expected, actual:actual});
}
expose(assert_regexp_match, "assert_regexp_match");
function assert_class_string(object, class_string, description) {
assert_equals({}.toString.call(object), "[object " + class_string + "]",
description);
}
expose(assert_class_string, "assert_class_string");
function _assert_own_property(name) {
return function(object, property_name, description)
{
assert(object.hasOwnProperty(property_name),
name, description,
"expected property ${p} missing", {p:property_name});
};
}
expose(_assert_own_property("assert_exists"), "assert_exists");
expose(_assert_own_property("assert_own_property"), "assert_own_property");
function assert_not_exists(object, property_name, description)
{
assert(!object.hasOwnProperty(property_name),
"assert_not_exists", description,
"unexpected property ${p} found", {p:property_name});
};
expose(assert_not_exists, "assert_not_exists");
function _assert_inherits(name) {
return function (object, property_name, description)
{
assert(typeof object === "object",
name, description,
"provided value is not an object");
assert("hasOwnProperty" in object,
name, description,
"provided value is an object but has no hasOwnProperty method");
assert(!object.hasOwnProperty(property_name),
name, description,
"property ${p} found on object expected in prototype chain",
{p:property_name});
assert(property_name in object,
name, description,
"property ${p} not found in prototype chain",
{p:property_name});
};
}
expose(_assert_inherits("assert_inherits"), "assert_inherits");
expose(_assert_inherits("assert_idl_attribute"), "assert_idl_attribute");
function assert_readonly(object, property_name, description)
{
var initial_value = object[property_name];
try {
//Note that this can have side effects in the case where
//the property has PutForwards
object[property_name] = initial_value + "a"; //XXX use some other value here?
assert(same_value(object[property_name], initial_value),
"assert_readonly", description,
"changing property ${p} succeeded",
{p:property_name});
}
finally
{
object[property_name] = initial_value;
}
};
expose(assert_readonly, "assert_readonly");
function assert_throws(code, func, description)
{
try
{
func.call(this);
assert(false, "assert_throws", description,
"${func} did not throw", {func:func});
}
catch(e)
{
if (e instanceof AssertionError) {
throw(e);
}
if (code === null)
{
return;
}
if (typeof code === "object")
{
assert(typeof e == "object" && "name" in e && e.name == code.name,
"assert_throws", description,
"${func} threw ${actual} (${actual_name}) expected ${expected} (${expected_name})",
{func:func, actual:e, actual_name:e.name,
expected:code,
expected_name:code.name});
return;
}
var code_name_map = {
INDEX_SIZE_ERR: 'IndexSizeError',
HIERARCHY_REQUEST_ERR: 'HierarchyRequestError',
WRONG_DOCUMENT_ERR: 'WrongDocumentError',
INVALID_CHARACTER_ERR: 'InvalidCharacterError',
NO_MODIFICATION_ALLOWED_ERR: 'NoModificationAllowedError',
NOT_FOUND_ERR: 'NotFoundError',
NOT_SUPPORTED_ERR: 'NotSupportedError',
INVALID_STATE_ERR: 'InvalidStateError',
SYNTAX_ERR: 'SyntaxError',
INVALID_MODIFICATION_ERR: 'InvalidModificationError',
NAMESPACE_ERR: 'NamespaceError',
INVALID_ACCESS_ERR: 'InvalidAccessError',
TYPE_MISMATCH_ERR: 'TypeMismatchError',
SECURITY_ERR: 'SecurityError',
NETWORK_ERR: 'NetworkError',
ABORT_ERR: 'AbortError',
URL_MISMATCH_ERR: 'URLMismatchError',
QUOTA_EXCEEDED_ERR: 'QuotaExceededError',
TIMEOUT_ERR: 'TimeoutError',
INVALID_NODE_TYPE_ERR: 'InvalidNodeTypeError',
DATA_CLONE_ERR: 'DataCloneError'
};
var name = code in code_name_map ? code_name_map[code] : code;
var name_code_map = {
IndexSizeError: 1,
HierarchyRequestError: 3,
WrongDocumentError: 4,
InvalidCharacterError: 5,
NoModificationAllowedError: 7,
NotFoundError: 8,
NotSupportedError: 9,
InvalidStateError: 11,
SyntaxError: 12,
InvalidModificationError: 13,
NamespaceError: 14,
InvalidAccessError: 15,
TypeMismatchError: 17,
SecurityError: 18,
NetworkError: 19,
AbortError: 20,
URLMismatchError: 21,
QuotaExceededError: 22,
TimeoutError: 23,
InvalidNodeTypeError: 24,
DataCloneError: 25,
UnknownError: 0,
ConstraintError: 0,
DataError: 0,
TransactionInactiveError: 0,