forked from w3c/testharness.js
-
Notifications
You must be signed in to change notification settings - Fork 7
/
idlharness.js
1842 lines (1687 loc) · 78 KB
/
idlharness.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
*/
/*
* This file automatically generates browser tests for WebIDL interfaces, using
* the testharness.js framework. To use, first include the following:
*
* <script src=/resources/testharness.js></script>
* <script src=/resources/testharnessreport.js></script>
* <script src=/resources/webidl2.js></script>
* <script src=/resources/idlharness.js></script>
*
* Then you'll need some type of IDLs. Here's some script that can be run on a
* spec written in HTML, which will grab all the elements with class="idl",
* concatenate them, and replace the body so you can copy-paste:
*
var s = "";
[].forEach.call(document.getElementsByClassName("idl"), function(idl) {
//https://www.w3.org/Bugs/Public/show_bug.cgi?id=14914
if (!idl.classList.contains("extract"))
{
s += idl.textContent + "\n\n";
}
});
document.body.innerHTML = '<pre></pre>';
document.body.firstChild.textContent = s;
*
* (TODO: write this in Python or something so that it can be done from the
* command line instead.)
*
* Once you have that, put it in your script somehow. The easiest way is to
* embed it literally in an HTML file with <script type=text/plain> or similar,
* so that you don't have to do any escaping. Another possibility is to put it
* in a separate .idl file that's fetched via XHR or similar. Sample usage:
*
* var idl_array = new IdlArray();
* idl_array.add_untested_idls("interface Node { readonly attribute DOMString nodeName; };");
* idl_array.add_idls("interface Document : Node { readonly attribute DOMString URL; };");
* idl_array.add_objects({Document: ["document"]});
* idl_array.test();
*
* This tests that window.Document exists and meets all the requirements of
* WebIDL. It also tests that window.document (the result of evaluating the
* string "document") has URL and nodeName properties that behave as they
* should, and otherwise meets WebIDL's requirements for an object whose
* primary interface is Document. It does not test that window.Node exists,
* which is what you want if the Node interface is already tested in some other
* specification's suite and your specification only extends or refers to it.
* Of course, each IDL string can define many different things, and calls to
* add_objects() can register many different objects for different interfaces:
* this is a very simple example.
*
* TODO: Write assert_writable, assert_enumerable, assert_configurable and
* their inverses, and use those instead of just checking
* getOwnPropertyDescriptor.
*
* == Public methods of IdlArray ==
*
* IdlArray objects can be obtained with new IdlArray(). Anything not
* documented in this section should be considered an implementation detail,
* and outside callers should not use it.
*
* add_idls(idl_string):
* Parses idl_string (throwing on parse error) and adds the results to the
* IdlArray. All the definitions will be tested when you run test(). If
* some of the definitions refer to other definitions, those must be present
* too. For instance, if idl_string says that Document inherits from Node,
* the Node interface must also have been provided in some call to add_idls()
* or add_untested_idls().
*
* add_untested_idls(idl_string):
* Like add_idls(), but the definitions will not be tested. If an untested
* interface is added and then extended with a tested partial interface, the
* members of the partial interface will still be tested. Also, all the
* members will still be tested for objects added with add_objects(), because
* you probably want to test that (for instance) window.document has all the
* properties from Node, not just Document, even if the Node interface itself
* is tested in a different test suite.
*
* add_objects(dict):
* dict should be an object whose keys are the names of interfaces or
* exceptions, and whose values are arrays of strings. When an interface or
* exception is tested, every string registered for it with add_objects()
* will be evaluated, and tests will be run on the result to verify that it
* correctly implements that interface or exception. This is the only way to
* test anything about [NoInterfaceObject] interfaces, and there are many
* tests that can't be run on any interface without an object to fiddle with.
*
* The interface has to be the *primary* interface of all the objects
* provided. For example, don't pass {Node: ["document"]}, but rather
* {Document: ["document"]}. Assuming the Document interface was declared to
* inherit from Node, this will automatically test that document implements
* the Node interface too.
*
* Warning: methods will be called on any provided objects, in a manner that
* WebIDL requires be safe. For instance, if a method has mandatory
* arguments, the test suite will try calling it with too few arguments to
* see if it throws an exception. If an implementation incorrectly runs the
* function instead of throwing, this might have side effects, possibly even
* preventing the test suite from running correctly.
*
* prevent_multiple_testing(name):
* This is a niche method for use in case you're testing many objects that
* implement the same interfaces, and don't want to retest the same
* interfaces every single time. For instance, HTML defines many interfaces
* that all inherit from HTMLElement, so the HTML test suite has something
* like
* .add_objects({
* HTMLHtmlElement: ['document.documentElement'],
* HTMLHeadElement: ['document.head'],
* HTMLBodyElement: ['document.body'],
* ...
* })
* and so on for dozens of element types. This would mean that it would
* retest that each and every one of those elements implements HTMLElement,
* Element, and Node, which would be thousands of basically redundant tests.
* The test suite therefore calls prevent_multiple_testing("HTMLElement").
* This means that once one object has been tested to implement HTMLElement
* and its ancestors, no other object will be. Thus in the example code
* above, the harness would test that document.documentElement correctly
* implements HTMLHtmlElement, HTMLElement, Element, and Node; but
* document.head would only be tested for HTMLHeadElement, and so on for
* further objects.
*
* test():
* Run all tests. This should be called after you've called all other
* methods to add IDLs and objects.
*/
/**
* Notes for people who want to edit this file (not just use it as a library):
*
* Most of the interesting stuff happens in the derived classes of IdlObject,
* especially IdlInterface. The entry point for all IdlObjects is .test(),
* which is called by IdlArray.test(). An IdlObject is conceptually just
* "thing we want to run tests on", and an IdlArray is an array of IdlObjects
* with some additional data thrown in.
*
* The object model is based on what WebIDLParser.js produces, which is in turn
* based on its pegjs grammar. If you want to figure out what properties an
* object will have from WebIDLParser.js, the best way is to look at the
* grammar:
*
* https://github.com/darobin/webidl.js/blob/master/lib/grammar.peg
*
* So for instance:
*
* // interface definition
* interface
* = extAttrs:extendedAttributeList? S? "interface" S name:identifier w herit:ifInheritance? w "{" w mem:ifMember* w "}" w ";" w
* { return { type: "interface", name: name, inheritance: herit, members: mem, extAttrs: extAttrs }; }
*
* This means that an "interface" object will have a .type property equal to
* the string "interface", a .name property equal to the identifier that the
* parser found, an .inheritance property equal to either null or the result of
* the "ifInheritance" production found elsewhere in the grammar, and so on.
* After each grammatical production is a JavaScript function in curly braces
* that gets called with suitable arguments and returns some JavaScript value.
*
* (Note that the version of WebIDLParser.js we use might sometimes be
* out-of-date or forked.)
*
* The members and methods of the classes defined by this file are all at least
* briefly documented, hopefully.
*/
(function(){
"use strict";
/// Helpers ///
function constValue (cnt) {
if (cnt.type === "null") return null;
if (cnt.type === "NaN") return NaN;
if (cnt.type === "Infinity") return cnt.negative ? -Infinity : Infinity;
return cnt.value;
}
/// IdlArray ///
// Entry point
window.IdlArray = function()
//@{
{
/**
* A map from strings to the corresponding named IdlObject, such as
* IdlInterface or IdlException. These are the things that test() will run
* tests on.
*/
this.members = {};
/**
* A map from strings to arrays of strings. The keys are interface or
* exception names, and are expected to also exist as keys in this.members
* (otherwise they'll be ignored). This is populated by add_objects() --
* see documentation at the start of the file. The actual tests will be
* run by calling this.members[name].test_object(obj) for each obj in
* this.objects[name]. obj is a string that will be eval'd to produce a
* JavaScript value, which is supposed to be an object implementing the
* given IdlObject (interface, exception, etc.).
*/
this.objects = {};
/**
* When adding multiple collections of IDLs one at a time, an earlier one
* might contain a partial interface or implements statement that depends
* on a later one. Save these up and handle them right before we run
* tests.
*
* .partials is simply an array of objects from WebIDLParser.js'
* "partialinterface" production. .implements maps strings to arrays of
* strings, such that
*
* A implements B;
* A implements C;
* D implements E;
*
* results in { A: ["B", "C"], D: ["E"] }.
*/
this.partials = [];
this["implements"] = {};
};
//@}
IdlArray.prototype.add_idls = function(raw_idls)
//@{
{
/** Entry point. See documentation at beginning of file. */
this.internal_add_idls(WebIDL2.parse(raw_idls));
};
//@}
IdlArray.prototype.add_untested_idls = function(raw_idls)
//@{
{
/** Entry point. See documentation at beginning of file. */
var parsed_idls = WebIDL2.parse(raw_idls);
for (var i = 0; i < parsed_idls.length; i++)
{
parsed_idls[i].untested = true;
if ("members" in parsed_idls[i])
{
for (var j = 0; j < parsed_idls[i].members.length; j++)
{
parsed_idls[i].members[j].untested = true;
}
}
}
this.internal_add_idls(parsed_idls);
};
//@}
IdlArray.prototype.internal_add_idls = function(parsed_idls)
//@{
{
/**
* Internal helper called by add_idls() and add_untested_idls().
* parsed_idls is an array of objects that come from WebIDLParser.js's
* "definitions" production. The add_untested_idls() entry point
* additionally sets an .untested property on each object (and its
* .members) so that they'll be skipped by test() -- they'll only be
* used for base interfaces of tested interfaces, return types, etc.
*/
parsed_idls.forEach(function(parsed_idl)
{
if (parsed_idl.type == "interface" && parsed_idl.partial)
{
this.partials.push(parsed_idl);
return;
}
if (parsed_idl.type == "implements")
{
if (!(parsed_idl.target in this["implements"]))
{
this["implements"][parsed_idl.target] = [];
}
this["implements"][parsed_idl.target].push(parsed_idl["implements"]);
return;
}
parsed_idl.array = this;
if (parsed_idl.name in this.members)
{
throw "Duplicate identifier " + parsed_idl.name;
}
switch(parsed_idl.type)
{
case "interface":
this.members[parsed_idl.name] = new IdlInterface(parsed_idl);
break;
case "exception":
this.members[parsed_idl.name] = new IdlException(parsed_idl);
break;
case "dictionary":
// Nothing to test, but we need the dictionary info around for type
// checks
this.members[parsed_idl.name] = new IdlDictionary(parsed_idl);
break;
case "typedef":
// TODO
console.log("typedef not yet supported");
break;
case "callback":
// TODO
console.log("callback not yet supported");
break;
case "enum":
this.members[parsed_idl.name] = new IdlEnum(parsed_idl);
break;
case "callback":
// TODO
break;
default:
throw parsed_idl.name + ": " + parsed_idl.type + " not yet supported";
}
}.bind(this));
};
//@}
IdlArray.prototype.add_objects = function(dict)
//@{
{
/** Entry point. See documentation at beginning of file. */
for (var k in dict)
{
if (k in this.objects)
{
this.objects[k] = this.objects[k].concat(dict[k]);
}
else
{
this.objects[k] = dict[k];
}
}
};
//@}
IdlArray.prototype.prevent_multiple_testing = function(name)
//@{
{
/** Entry point. See documentation at beginning of file. */
this.members[name].prevent_multiple_testing = true;
};
//@}
IdlArray.prototype.recursively_get_implements = function(interface_name)
//@{
{
/**
* Helper function for test(). Returns an array of things that implement
* interface_name, so if the IDL contains
*
* A implements B;
* B implements C;
* B implements D;
*
* then recursively_get_implements("A") should return ["B", "C", "D"].
*/
var ret = this["implements"][interface_name];
if (ret === undefined)
{
return [];
}
for (var i = 0; i < this["implements"][interface_name].length; i++)
{
ret = ret.concat(this.recursively_get_implements(ret[i]));
if (ret.indexOf(ret[i]) != ret.lastIndexOf(ret[i]))
{
throw "Circular implements statements involving " + ret[i];
}
}
return ret;
};
//@}
IdlArray.prototype.test = function()
//@{
{
/** Entry point. See documentation at beginning of file. */
// First merge in all the partial interfaces and implements statements we
// encountered.
this.partials.forEach(function(parsed_idl)
{
if (!(parsed_idl.name in this.members)
|| !(this.members[parsed_idl.name] instanceof IdlInterface))
{
throw "Partial interface " + parsed_idl.name + " with no original interface";
}
if (parsed_idl.extAttrs)
{
parsed_idl.extAttrs.forEach(function(extAttr)
{
this.members[parsed_idl.name].extAttrs.push(extAttr);
}.bind(this));
}
parsed_idl.members.forEach(function(member)
{
this.members[parsed_idl.name].members.push(new IdlInterfaceMember(member));
}.bind(this));
}.bind(this));
this.partials = [];
for (var lhs in this["implements"])
{
this.recursively_get_implements(lhs).forEach(function(rhs)
{
var errStr = lhs + " implements " + rhs + ", but ";
if (!(lhs in this.members)) throw errStr + lhs + " is undefined.";
if (!(this.members[lhs] instanceof IdlInterface)) throw errStr + lhs + " is not an interface.";
if (!(rhs in this.members)) throw errStr + rhs + " is undefined.";
if (!(this.members[rhs] instanceof IdlInterface)) throw errStr + rhs + " is not an interface.";
this.members[rhs].members.forEach(function(member)
{
this.members[lhs].members.push(new IdlInterfaceMember(member));
}.bind(this));
}.bind(this));
}
this["implements"] = {};
// Now run test() on every member, and test_object() for every object.
for (var name in this.members)
{
this.members[name].test();
if (name in this.objects)
{
this.objects[name].forEach(function(str)
{
this.members[name].test_object(str);
}.bind(this));
}
}
};
//@}
IdlArray.prototype.assert_type_is = function(value, type)
//@{
{
/**
* Helper function that tests that value is an instance of type according
* to the rules of WebIDL. value is any JavaScript value, and type is an
* object produced by WebIDLParser.js' "type" production. That production
* is fairly elaborate due to the complexity of WebIDL's types, so it's
* best to look at the grammar to figure out what properties it might have.
*/
if (type.idlType == "any")
{
// No assertions to make
return;
}
if (type.nullable && value === null)
{
// This is fine
return;
}
if (type.array)
{
// TODO: not supported yet
return;
}
if (type.sequence)
{
assert_true(Array.isArray(value), "is not array");
if (!value.length)
{
// Nothing we can do.
return;
}
this.assert_type_is(value[0], type.idlType.idlType);
return;
}
type = type.idlType;
switch(type)
{
case "void":
assert_equals(value, undefined);
return;
case "boolean":
assert_equals(typeof value, "boolean");
return;
case "byte":
assert_equals(typeof value, "number");
assert_equals(value, Math.floor(value), "not an integer");
assert_true(-128 <= value && value <= 127, "byte " + value + " not in range [-128, 127]");
return;
case "octet":
assert_equals(typeof value, "number");
assert_equals(value, Math.floor(value), "not an integer");
assert_true(0 <= value && value <= 255, "octet " + value + " not in range [0, 255]");
return;
case "short":
assert_equals(typeof value, "number");
assert_equals(value, Math.floor(value), "not an integer");
assert_true(-32768 <= value && value <= 32767, "short " + value + " not in range [-32768, 32767]");
return;
case "unsigned short":
assert_equals(typeof value, "number");
assert_equals(value, Math.floor(value), "not an integer");
assert_true(0 <= value && value <= 65535, "unsigned short " + value + " not in range [0, 65535]");
return;
case "long":
assert_equals(typeof value, "number");
assert_equals(value, Math.floor(value), "not an integer");
assert_true(-2147483648 <= value && value <= 2147483647, "long " + value + " not in range [-2147483648, 2147483647]");
return;
case "unsigned long":
assert_equals(typeof value, "number");
assert_equals(value, Math.floor(value), "not an integer");
assert_true(0 <= value && value <= 4294967295, "unsigned long " + value + " not in range [0, 4294967295]");
return;
case "long long":
assert_equals(typeof value, "number");
return;
case "unsigned long long":
assert_equals(typeof value, "number");
assert_true(0 <= value, "unsigned long long is negative");
return;
case "float":
case "double":
// TODO: distinguish these cases
assert_equals(typeof value, "number");
return;
case "DOMString":
assert_equals(typeof value, "string");
return;
case "object":
assert_true(typeof value == "object" || typeof value == "function", "wrong type: not object or function");
return;
}
if (!(type in this.members))
{
throw "Unrecognized type " + type;
}
if (this.members[type] instanceof IdlInterface)
{
// We don't want to run the full
// IdlInterface.prototype.test_instance_of, because that could result
// in an infinite loop. TODO: This means we don't have tests for
// NoInterfaceObject interfaces, and we also can't test objects that
// come from another window.
assert_true(typeof value == "object" || typeof value == "function", "wrong type: not object or function");
if (value instanceof Object
&& !this.members[type].has_extended_attribute("NoInterfaceObject")
&& type in window)
{
assert_true(value instanceof window[type], "not instanceof " + type);
}
}
else if (this.members[type] instanceof IdlEnum)
{
assert_equals(typeof value, "string");
}
else if (this.members[type] instanceof IdlDictionary)
{
// TODO: Test when we actually have something to test this on
}
else
{
throw "Type " + type + " isn't an interface or dictionary";
}
};
//@}
/// IdlObject ///
function IdlObject() {}
IdlObject.prototype.test = function()
//@{
{
/**
* By default, this does nothing, so no actual tests are run for IdlObjects
* that don't define any (e.g., IdlDictionary at the time of this writing).
*/
};
//@}
IdlObject.prototype.has_extended_attribute = function(name)
//@{
{
/**
* This is only meaningful for things that support extended attributes,
* such as interfaces, exceptions, and members.
*/
return this.extAttrs.some(function(o)
{
return o.name == name;
});
};
//@}
/// IdlDictionary ///
// Used for IdlArray.prototype.assert_type_is
function IdlDictionary(obj)
//@{
{
/**
* obj is an object produced by the WebIDLParser.js "dictionary"
* production.
*/
/** Self-explanatory. */
this.name = obj.name;
/** An array of objects produced by the "dictionaryMember" production. */
this.members = obj.members;
/**
* The name (as a string) of the dictionary type we inherit from, or null
* if there is none.
*/
this.base = obj.inheritance;
}
//@}
IdlDictionary.prototype = Object.create(IdlObject.prototype);
/// IdlExceptionOrInterface ///
// Code sharing!
function IdlExceptionOrInterface(obj)
//@{
{
/**
* obj is an object produced by the WebIDLParser.js "exception" or
* "interface" production, as appropriate.
*/
/** Self-explanatory. */
this.name = obj.name;
/** A back-reference to our IdlArray. */
this.array = obj.array;
/**
* An indicator of whether we should run tests on the (exception) interface
* object and (exception) interface prototype object. Tests on members are
* controlled by .untested on each member, not this.
*/
this.untested = obj.untested;
/** An array of objects produced by the "ExtAttr" production. */
this.extAttrs = obj.extAttrs;
/** An array of IdlInterfaceMembers. */
this.members = obj.members.map(function(m){return new IdlInterfaceMember(m); });
/**
* The name (as a string) of the type we inherit from, or null if there is
* none.
*/
this.base = obj.inheritance;
}
//@}
IdlExceptionOrInterface.prototype = Object.create(IdlObject.prototype);
IdlExceptionOrInterface.prototype.test = function()
//@{
{
if (this.has_extended_attribute("NoInterfaceObject"))
{
// No tests to do without an instance. TODO: We should still be able
// to run tests on the prototype object, if we obtain one through some
// other means.
return;
}
if (!this.untested)
{
// First test things to do with the exception/interface object and
// exception/interface prototype object.
this.test_self();
}
// Then test things to do with its members (constants, fields, attributes,
// operations, . . .). These are run even if .untested is true, because
// members might themselves be marked as .untested. This might happen to
// interfaces if the interface itself is untested but a partial interface
// that extends it is tested -- then the interface itself and its initial
// members will be marked as untested, but the members added by the partial
// interface are still tested.
this.test_members();
};
//@}
/// IdlException ///
function IdlException(obj) { IdlExceptionOrInterface.call(this, obj); }
IdlException.prototype = Object.create(IdlExceptionOrInterface.prototype);
IdlException.prototype.test_self = function()
//@{
{
test(function()
{
// "For every exception that is not declared with the
// [NoInterfaceObject] extended attribute, a corresponding property
// must exist on the exception’s relevant namespace object. The name of
// the property is the identifier of the exception, and its value is an
// object called the exception interface object, which provides access
// to any constants that have been associated with the exception. The
// property has the attributes { [[Writable]]: true, [[Enumerable]]:
// false, [[Configurable]]: true }."
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
var desc = Object.getOwnPropertyDescriptor(window, this.name);
assert_false("get" in desc, "window's property " + format_value(this.name) + " has getter");
assert_false("set" in desc, "window's property " + format_value(this.name) + " has setter");
assert_true(desc.writable, "window's property " + format_value(this.name) + " is not writable");
assert_false(desc.enumerable, "window's property " + format_value(this.name) + " is enumerable");
assert_true(desc.configurable, "window's property " + format_value(this.name) + " is not configurable");
// "The exception interface object for a given exception must be a
// function object."
// "If an object is defined to be a function object, then it has
// characteristics as follows:"
// "Its [[Prototype]] internal property is the Function prototype
// object."
// Note: This doesn't match browsers as of December 2011, see
// http://www.w3.org/Bugs/Public/show_bug.cgi?id=14813
assert_equals(Object.getPrototypeOf(window[this.name]), Function.prototype,
"prototype of window's property " + format_value(this.name) + " is not Function.prototype");
// "Its [[Get]] internal property is set as described in ECMA-262
// section 15.3.5.4."
// Not much to test for this.
// "Its [[Construct]] internal property is set as described in ECMA-262
// section 13.2.2."
// Tested below.
// "Its [[HasInstance]] internal property is set as described in
// ECMA-262 section 15.3.5.3, unless otherwise specified."
// TODO
// "Its [[Class]] internal property is “Function”."
// String() returns something implementation-dependent, because it
// calls Function#toString.
assert_class_string(window[this.name], "Function",
"class string of " + this.name);
// TODO: Test 4.9.1.1. Exception interface object [[Call]] method
// (which does not match browsers:
// http://www.w3.org/Bugs/Public/show_bug.cgi?id=14885)
}.bind(this), this.name + " exception: existence and properties of exception interface object");
test(function()
{
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
// "The exception interface object must also have a property named
// “prototype” with attributes { [[Writable]]: false, [[Enumerable]]:
// false, [[Configurable]]: false } whose value is an object called the
// exception interface prototype object. This object also provides
// access to the constants that are declared on the exception."
assert_own_property(window[this.name], "prototype",
'exception "' + this.name + '" does not have own property "prototype"');
var desc = Object.getOwnPropertyDescriptor(window[this.name], "prototype");
assert_false("get" in desc, this.name + ".prototype has getter");
assert_false("set" in desc, this.name + ".prototype has setter");
assert_false(desc.writable, this.name + ".prototype is writable");
assert_false(desc.enumerable, this.name + ".prototype is enumerable");
assert_false(desc.configurable, this.name + ".prototype is configurable");
// "The exception interface prototype object for a given exception must
// have an internal [[Prototype]] property whose value is as follows:
//
// "If the exception is declared to inherit from another exception,
// then the value of the internal [[Prototype]] property is the
// exception interface prototype object for the inherited exception.
// "Otherwise, the exception is not declared to inherit from another
// exception. The value of the internal [[Prototype]] property is the
// Error prototype object ([ECMA-262], section 15.11.3.1)."
//
// Note: This doesn't match browsers as of December 2011, see
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=14887.
var inherit_exception = this.base ? this.base : "Error";
assert_own_property(window, inherit_exception,
'should inherit from ' + inherit_exception + ', but window has no such property');
assert_own_property(window[inherit_exception], "prototype",
'should inherit from ' + inherit_exception + ', but that object has no "prototype" property');
assert_equals(Object.getPrototypeOf(window[this.name].prototype),
window[inherit_exception].prototype,
'prototype of ' + this.name + '.prototype is not ' + inherit_exception + '.prototype');
// "The class string of an exception interface prototype object is the
// concatenation of the exception’s identifier and the string
// “Prototype”."
assert_class_string(window[this.name].prototype, this.name + "Prototype",
"class string of " + this.name + ".prototype");
// TODO: Test String(), based on ES definition of
// Error.prototype.toString?
}.bind(this), this.name + " exception: existence and properties of exception interface prototype object");
test(function()
{
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
assert_own_property(window[this.name], "prototype",
'interface "' + this.name + '" does not have own property "prototype"');
// "There must be a property named “name” on the exception interface
// prototype object with attributes { [[Writable]]: true,
// [[Enumerable]]: false, [[Configurable]]: true } and whose value is
// the identifier of the exception."
assert_own_property(window[this.name].prototype, "name",
'prototype object does not have own property "name"');
var desc = Object.getOwnPropertyDescriptor(window[this.name].prototype, "name");
assert_false("get" in desc, this.name + ".prototype.name has getter");
assert_false("set" in desc, this.name + ".prototype.name has setter");
assert_true(desc.writable, this.name + ".prototype.name is not writable");
assert_false(desc.enumerable, this.name + ".prototype.name is enumerable");
assert_true(desc.configurable, this.name + ".prototype.name is not configurable");
assert_equals(desc.value, this.name, this.name + ".prototype.name has incorrect value");
}.bind(this), this.name + " exception: existence and properties of exception interface prototype object's \"name\" property");
test(function()
{
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
assert_own_property(window[this.name], "prototype",
'interface "' + this.name + '" does not have own property "prototype"');
// "If the [NoInterfaceObject] extended attribute was not specified on
// the exception, then there must also be a property named
// “constructor” on the exception interface prototype object with
// attributes { [[Writable]]: true, [[Enumerable]]: false,
// [[Configurable]]: true } and whose value is a reference to the
// exception interface object for the exception."
assert_own_property(window[this.name].prototype, "constructor",
this.name + '.prototype does not have own property "constructor"');
var desc = Object.getOwnPropertyDescriptor(window[this.name].prototype, "constructor");
assert_false("get" in desc, this.name + ".prototype.constructor has getter");
assert_false("set" in desc, this.name + ".prototype.constructor has setter");
assert_true(desc.writable, this.name + ".prototype.constructor is not writable");
assert_false(desc.enumerable, this.name + ".prototype.constructor is enumerable");
assert_true(desc.configurable, this.name + ".prototype.constructor in not configurable");
assert_equals(window[this.name].prototype.constructor, window[this.name],
this.name + '.prototype.constructor is not the same object as ' + this.name);
}.bind(this), this.name + " exception: existence and properties of exception interface prototype object's \"constructor\" property");
};
//@}
IdlException.prototype.test_members = function()
//@{
{
for (var i = 0; i < this.members.length; i++)
{
var member = this.members[i];
if (member.untested)
{
continue;
}
if (member.type == "const" && member.name != "prototype")
{
test(function()
{
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
// "For each constant defined on the exception, there must be a
// corresponding property on the exception interface object, if
// it exists, if the identifier of the constant is not
// “prototype”."
assert_own_property(window[this.name], member.name);
// "The value of the property is the ECMAScript value that is
// equivalent to the constant’s IDL value, according to the
// rules in section 4.2 above."
assert_equals(window[this.name][member.name], constValue(member.value),
"property has wrong value");
// "The property has attributes { [[Writable]]: false,
// [[Enumerable]]: true, [[Configurable]]: false }."
var desc = Object.getOwnPropertyDescriptor(window[this.name], member.name);
assert_false("get" in desc, "property has getter");
assert_false("set" in desc, "property has setter");
assert_false(desc.writable, "property is writable");
assert_true(desc.enumerable, "property is not enumerable");
assert_false(desc.configurable, "property is configurable");
}.bind(this), this.name + " exception: constant " + member.name + " on exception interface object");
// "In addition, a property with the same characteristics must
// exist on the exception interface prototype object."
test(function()
{
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
assert_own_property(window[this.name], "prototype",
'exception "' + this.name + '" does not have own property "prototype"');
assert_own_property(window[this.name].prototype, member.name);
assert_equals(window[this.name].prototype[member.name], constValue(member.value),
"property has wrong value");
var desc = Object.getOwnPropertyDescriptor(window[this.name].prototype, member.name);
assert_false("get" in desc, "property has getter");
assert_false("set" in desc, "property has setter");
assert_false(desc.writable, "property is writable");
assert_true(desc.enumerable, "property is not enumerable");
assert_false(desc.configurable, "property is configurable");
}.bind(this), this.name + " exception: constant " + member.name + " on exception interface prototype object");
}
else if (member.type == "field")
{
test(function()
{
assert_own_property(window, this.name,
"window does not have own property " + format_value(this.name));
assert_own_property(window[this.name], "prototype",
'exception "' + this.name + '" does not have own property "prototype"');
// "For each exception field, there must be a corresponding
// property on the exception interface prototype object, whose
// characteristics are as follows:
// "The name of the property is the identifier of the exception
// field."
assert_own_property(window[this.name].prototype, member.name);
// "The property has attributes { [[Get]]: G, [[Enumerable]]:
// true, [[Configurable]]: true }, where G is the exception
// field getter, defined below."
var desc = Object.getOwnPropertyDescriptor(window[this.name].prototype, member.name);
assert_false("value" in desc, "property descriptor has value but is supposed to be accessor");
assert_false("writable" in desc, 'property descriptor has "writable" field but is supposed to be accessor');
// TODO: ES5 doesn't seem to say whether desc should have a
// .set property.
assert_true(desc.enumerable, "property is not enumerable");
assert_true(desc.configurable, "property is not configurable");
// "The exception field getter is a Function object whose
// behavior when invoked is as follows:"
assert_equals(typeof desc.get, "function", "typeof getter");
// "The value of the Function object’s “length” property is the
// Number value 0."
// This test is before the TypeError tests so that it's easiest
// to see that Firefox 11a1 only fails one assert in this test.
assert_equals(desc.get.length, 0, "getter length");
// "Let O be the result of calling ToObject on the this value.
// "If O is not a platform object representing an exception for
// the exception on which the exception field was declared,
// then throw a TypeError."
// TODO: Test on a platform object representing an exception.
assert_throws(new TypeError(), function()
{
window[this.name].prototype[member.name];
}.bind(this), "getting property on prototype object must throw TypeError");
assert_throws(new TypeError(), function()
{
desc.get.call({});
}.bind(this), "calling getter on wrong object type must throw TypeError");
}.bind(this), this.name + " exception: field " + member.name + " on exception interface prototype object");
}
}
};
//@}
IdlException.prototype.test_object = function(desc)
//@{
{
var obj, exception = null;
try
{
obj = eval(desc);
}
catch(e)
{
exception = e;
}
test(function()
{
assert_equals(exception, null, "Unexpected exception when evaluating object");
assert_equals(typeof obj, "object", "wrong typeof object");
// We can't easily test that its prototype is correct if there's no
// interface object, or the object is from a different global
// environment (not instanceof Object). TODO: test in this case that
// its prototype at least looks correct, even if we can't test that
// it's actually correct.
if (!this.has_extended_attribute("NoInterfaceObject")
&& (typeof obj != "object" || obj instanceof Object))
{