2 Distributed under both the W3C Test Suite License [1] and the W3C
3 3-clause BSD License [2]. To contribute to a W3C Test Suite, see the
4 policies and contribution forms [3].
6 [1] http://www.w3.org/Consortium/Legal/2008/04-testsuite-license
7 [2] http://www.w3.org/Consortium/Legal/2008/03-bsd-license
8 [3] http://www.w3.org/2004/10/27-testcases
11 /* For user documentation see docs/idlharness.md */
14 * Notes for people who want to edit this file (not just use it as a library):
16 * Most of the interesting stuff happens in the derived classes of IdlObject,
17 * especially IdlInterface. The entry point for all IdlObjects is .test(),
18 * which is called by IdlArray.test(). An IdlObject is conceptually just
19 * "thing we want to run tests on", and an IdlArray is an array of IdlObjects
20 * with some additional data thrown in.
22 * The object model is based on what WebIDLParser.js produces, which is in turn
23 * based on its pegjs grammar. If you want to figure out what properties an
24 * object will have from WebIDLParser.js, the best way is to look at the
27 * https://github.com/darobin/webidl.js/blob/master/lib/grammar.peg
31 * // interface definition
33 * = extAttrs:extendedAttributeList? S? "interface" S name:identifier w herit:ifInheritance? w "{" w mem:ifMember* w "}" w ";" w
34 * { return { type: "interface", name: name, inheritance: herit, members: mem, extAttrs: extAttrs }; }
36 * This means that an "interface" object will have a .type property equal to
37 * the string "interface", a .name property equal to the identifier that the
38 * parser found, an .inheritance property equal to either null or the result of
39 * the "ifInheritance" production found elsewhere in the grammar, and so on.
40 * After each grammatical production is a JavaScript function in curly braces
41 * that gets called with suitable arguments and returns some JavaScript value.
43 * (Note that the version of WebIDLParser.js we use might sometimes be
44 * out-of-date or forked.)
46 * The members and methods of the classes defined by this file are all at least
47 * briefly documented, hopefully.
52 function constValue (cnt) {
53 if (cnt.type === "null") return null;
54 if (cnt.type === "NaN") return NaN;
55 if (cnt.type === "Infinity") return cnt.negative ? -Infinity : Infinity;
59 function minOverloadLength(overloads) {
60 if (!overloads.length) {
64 return overloads.map(function(attr) {
65 return attr.arguments ? attr.arguments.filter(function(arg) {
66 return !arg.optional && !arg.variadic;
69 .reduce(function(m, n) { return Math.min(m, n); });
74 self.IdlArray = function()
78 * A map from strings to the corresponding named IdlObject, such as
79 * IdlInterface or IdlException. These are the things that test() will run
85 * A map from strings to arrays of strings. The keys are interface or
86 * exception names, and are expected to also exist as keys in this.members
87 * (otherwise they'll be ignored). This is populated by add_objects() --
88 * see documentation at the start of the file. The actual tests will be
89 * run by calling this.members[name].test_object(obj) for each obj in
90 * this.objects[name]. obj is a string that will be eval'd to produce a
91 * JavaScript value, which is supposed to be an object implementing the
92 * given IdlObject (interface, exception, etc.).
97 * When adding multiple collections of IDLs one at a time, an earlier one
98 * might contain a partial interface or implements statement that depends
99 * on a later one. Save these up and handle them right before we run
102 * .partials is simply an array of objects from WebIDLParser.js'
103 * "partialinterface" production. .implements maps strings to arrays of
110 * results in { A: ["B", "C"], D: ["E"] }.
113 this["implements"] = {};
117 IdlArray.prototype.add_idls = function(raw_idls)
120 /** Entry point. See documentation at beginning of file. */
121 this.internal_add_idls(WebIDL2.parse(raw_idls));
125 IdlArray.prototype.add_untested_idls = function(raw_idls)
128 /** Entry point. See documentation at beginning of file. */
129 var parsed_idls = WebIDL2.parse(raw_idls);
130 for (var i = 0; i < parsed_idls.length; i++)
132 parsed_idls[i].untested = true;
133 if ("members" in parsed_idls[i])
135 for (var j = 0; j < parsed_idls[i].members.length; j++)
137 parsed_idls[i].members[j].untested = true;
141 this.internal_add_idls(parsed_idls);
145 IdlArray.prototype.internal_add_idls = function(parsed_idls)
149 * Internal helper called by add_idls() and add_untested_idls().
150 * parsed_idls is an array of objects that come from WebIDLParser.js's
151 * "definitions" production. The add_untested_idls() entry point
152 * additionally sets an .untested property on each object (and its
153 * .members) so that they'll be skipped by test() -- they'll only be
154 * used for base interfaces of tested interfaces, return types, etc.
156 parsed_idls.forEach(parsed_idl => {
157 if (parsed_idl.type == "interface" && parsed_idl.partial)
159 this.partials.push(parsed_idl);
163 if (parsed_idl.type == "implements")
165 if (!(parsed_idl.target in this["implements"]))
167 this["implements"][parsed_idl.target] = [];
169 this["implements"][parsed_idl.target].push(parsed_idl["implements"]);
173 parsed_idl.array = this;
174 if (parsed_idl.name in this.members)
176 throw "Duplicate identifier " + parsed_idl.name;
178 switch(parsed_idl.type)
181 this.members[parsed_idl.name] =
182 new IdlInterface(parsed_idl, /* is_callback = */ false);
186 // Nothing to test, but we need the dictionary info around for type
188 this.members[parsed_idl.name] = new IdlDictionary(parsed_idl);
192 this.members[parsed_idl.name] = new IdlTypedef(parsed_idl);
197 console.log("callback not yet supported");
201 this.members[parsed_idl.name] = new IdlEnum(parsed_idl);
204 case "callback interface":
205 this.members[parsed_idl.name] =
206 new IdlInterface(parsed_idl, /* is_callback = */ true);
210 throw parsed_idl.name + ": " + parsed_idl.type + " not yet supported";
216 IdlArray.prototype.add_objects = function(dict)
219 /** Entry point. See documentation at beginning of file. */
222 if (k in this.objects)
224 this.objects[k] = this.objects[k].concat(dict[k]);
228 this.objects[k] = dict[k];
234 IdlArray.prototype.prevent_multiple_testing = function(name)
237 /** Entry point. See documentation at beginning of file. */
238 this.members[name].prevent_multiple_testing = true;
242 IdlArray.prototype.recursively_get_implements = function(interface_name)
246 * Helper function for test(). Returns an array of things that implement
247 * interface_name, so if the IDL contains
253 * then recursively_get_implements("A") should return ["B", "C", "D"].
255 var ret = this["implements"][interface_name];
256 if (ret === undefined)
260 for (var i = 0; i < this["implements"][interface_name].length; i++)
262 ret = ret.concat(this.recursively_get_implements(ret[i]));
263 if (ret.indexOf(ret[i]) != ret.lastIndexOf(ret[i]))
265 throw "Circular implements statements involving " + ret[i];
272 IdlArray.prototype.test = function()
275 /** Entry point. See documentation at beginning of file. */
277 // First merge in all the partial interfaces and implements statements we
279 this.partials.forEach(parsed_idl => {
280 if (!(parsed_idl.name in this.members)
281 || !(this.members[parsed_idl.name] instanceof IdlInterface))
283 throw "Partial interface " + parsed_idl.name + " with no original interface";
285 if (parsed_idl.extAttrs)
287 parsed_idl.extAttrs.forEach(extAttr => {
288 this.members[parsed_idl.name].extAttrs.push(extAttr);
291 parsed_idl.members.forEach(member => {
292 this.members[parsed_idl.name].members.push(new IdlInterfaceMember(member));
297 for (var lhs in this["implements"])
299 this.recursively_get_implements(lhs).forEach(rhs => {
300 var errStr = lhs + " implements " + rhs + ", but ";
301 if (!(lhs in this.members)) throw errStr + lhs + " is undefined.";
302 if (!(this.members[lhs] instanceof IdlInterface)) throw errStr + lhs + " is not an interface.";
303 if (!(rhs in this.members)) throw errStr + rhs + " is undefined.";
304 if (!(this.members[rhs] instanceof IdlInterface)) throw errStr + rhs + " is not an interface.";
305 this.members[rhs].members.forEach(member => {
306 this.members[lhs].members.push(new IdlInterfaceMember(member));
310 this["implements"] = {};
312 // Now run test() on every member, and test_object() for every object.
313 for (var name in this.members)
315 this.members[name].test();
316 if (name in this.objects)
318 this.objects[name].forEach(str => {
319 this.members[name].test_object(str);
326 IdlArray.prototype.assert_type_is = function(value, type)
330 * Helper function that tests that value is an instance of type according
331 * to the rules of WebIDL. value is any JavaScript value, and type is an
332 * object produced by WebIDLParser.js' "type" production. That production
333 * is fairly elaborate due to the complexity of WebIDL's types, so it's
334 * best to look at the grammar to figure out what properties it might have.
336 if (type.idlType == "any")
338 // No assertions to make
342 if (type.nullable && value === null)
350 // TODO: not supported yet
356 assert_true(Array.isArray(value), "is not array");
359 // Nothing we can do.
362 this.assert_type_is(value[0], type.idlType.idlType);
371 assert_equals(value, undefined);
375 assert_equals(typeof value, "boolean");
379 assert_equals(typeof value, "number");
380 assert_equals(value, Math.floor(value), "not an integer");
381 assert_true(-128 <= value && value <= 127, "byte " + value + " not in range [-128, 127]");
385 assert_equals(typeof value, "number");
386 assert_equals(value, Math.floor(value), "not an integer");
387 assert_true(0 <= value && value <= 255, "octet " + value + " not in range [0, 255]");
391 assert_equals(typeof value, "number");
392 assert_equals(value, Math.floor(value), "not an integer");
393 assert_true(-32768 <= value && value <= 32767, "short " + value + " not in range [-32768, 32767]");
396 case "unsigned short":
397 assert_equals(typeof value, "number");
398 assert_equals(value, Math.floor(value), "not an integer");
399 assert_true(0 <= value && value <= 65535, "unsigned short " + value + " not in range [0, 65535]");
403 assert_equals(typeof value, "number");
404 assert_equals(value, Math.floor(value), "not an integer");
405 assert_true(-2147483648 <= value && value <= 2147483647, "long " + value + " not in range [-2147483648, 2147483647]");
408 case "unsigned long":
409 assert_equals(typeof value, "number");
410 assert_equals(value, Math.floor(value), "not an integer");
411 assert_true(0 <= value && value <= 4294967295, "unsigned long " + value + " not in range [0, 4294967295]");
415 assert_equals(typeof value, "number");
418 case "unsigned long long":
419 case "EpochTimeStamp":
421 assert_equals(typeof value, "number");
422 assert_true(0 <= value, "unsigned long long is negative");
427 case "DOMHighResTimeStamp":
428 case "unrestricted float":
429 case "unrestricted double":
430 // TODO: distinguish these cases
431 assert_equals(typeof value, "number");
437 // TODO: https://github.com/w3c/testharness.js/issues/92
438 assert_equals(typeof value, "string");
442 assert_true(typeof value == "object" || typeof value == "function", "wrong type: not object or function");
446 if (!(type in this.members))
448 throw "Unrecognized type " + type;
451 if (this.members[type] instanceof IdlInterface)
453 // We don't want to run the full
454 // IdlInterface.prototype.test_instance_of, because that could result
455 // in an infinite loop. TODO: This means we don't have tests for
456 // NoInterfaceObject interfaces, and we also can't test objects that
457 // come from another self.
458 assert_true(typeof value == "object" || typeof value == "function", "wrong type: not object or function");
459 if (value instanceof Object
460 && !this.members[type].has_extended_attribute("NoInterfaceObject")
463 assert_true(value instanceof self[type], "not instanceof " + type);
466 else if (this.members[type] instanceof IdlEnum)
468 assert_equals(typeof value, "string");
470 else if (this.members[type] instanceof IdlDictionary)
472 // TODO: Test when we actually have something to test this on
474 else if (this.members[type] instanceof IdlTypedef)
476 // TODO: Test when we actually have something to test this on
480 throw "Type " + type + " isn't an interface or dictionary";
486 function IdlObject() {}
487 IdlObject.prototype.test = function()
491 * By default, this does nothing, so no actual tests are run for IdlObjects
492 * that don't define any (e.g., IdlDictionary at the time of this writing).
497 IdlObject.prototype.has_extended_attribute = function(name)
501 * This is only meaningful for things that support extended attributes,
502 * such as interfaces, exceptions, and members.
504 return this.extAttrs.some(function(o)
506 return o.name == name;
512 /// IdlDictionary ///
513 // Used for IdlArray.prototype.assert_type_is
514 function IdlDictionary(obj)
518 * obj is an object produced by the WebIDLParser.js "dictionary"
522 /** Self-explanatory. */
523 this.name = obj.name;
525 /** An array of objects produced by the "dictionaryMember" production. */
526 this.members = obj.members;
529 * The name (as a string) of the dictionary type we inherit from, or null
532 this.base = obj.inheritance;
536 IdlDictionary.prototype = Object.create(IdlObject.prototype);
539 function IdlInterface(obj, is_callback) {
541 * obj is an object produced by the WebIDLParser.js "exception" or
542 * "interface" production, as appropriate.
545 /** Self-explanatory. */
546 this.name = obj.name;
548 /** A back-reference to our IdlArray. */
549 this.array = obj.array;
552 * An indicator of whether we should run tests on the (exception) interface
553 * object and (exception) interface prototype object. Tests on members are
554 * controlled by .untested on each member, not this.
556 this.untested = obj.untested;
558 /** An array of objects produced by the "ExtAttr" production. */
559 this.extAttrs = obj.extAttrs;
561 /** An array of IdlInterfaceMembers. */
562 this.members = obj.members.map(function(m){return new IdlInterfaceMember(m); });
563 if (this.has_extended_attribute("Unforgeable")) {
565 .filter(function(m) { return !m["static"] && (m.type == "attribute" || m.type == "operation"); })
566 .forEach(function(m) { return m.isUnforgeable = true; });
570 * The name (as a string) of the type we inherit from, or null if there is
573 this.base = obj.inheritance;
575 this._is_callback = is_callback;
577 IdlInterface.prototype = Object.create(IdlObject.prototype);
578 IdlInterface.prototype.is_callback = function()
581 return this._is_callback;
585 IdlInterface.prototype.has_constants = function()
588 return this.members.some(function(member) {
589 return member.type === "const";
594 IdlInterface.prototype.is_global = function()
597 return this.extAttrs.some(function(attribute) {
598 return attribute.name === "Global" ||
599 attribute.name === "PrimaryGlobal";
604 IdlInterface.prototype.test = function()
607 if (this.has_extended_attribute("NoInterfaceObject"))
609 // No tests to do without an instance. TODO: We should still be able
610 // to run tests on the prototype object, if we obtain one through some
617 // First test things to do with the exception/interface object and
618 // exception/interface prototype object.
621 // Then test things to do with its members (constants, fields, attributes,
622 // operations, . . .). These are run even if .untested is true, because
623 // members might themselves be marked as .untested. This might happen to
624 // interfaces if the interface itself is untested but a partial interface
625 // that extends it is tested -- then the interface itself and its initial
626 // members will be marked as untested, but the members added by the partial
627 // interface are still tested.
632 IdlInterface.prototype.test_self = function()
636 // This function tests WebIDL as of 2015-01-13.
637 // TODO: Consider [Exposed].
639 // "For every interface that is exposed in a given ECMAScript global
641 // * is a callback interface that has constants declared on it, or
642 // * is a non-callback interface that is not declared with the
643 // [NoInterfaceObject] extended attribute,
644 // a corresponding property MUST exist on the ECMAScript global object.
645 // The name of the property is the identifier of the interface, and its
646 // value is an object called the interface object.
647 // The property has the attributes { [[Writable]]: true,
648 // [[Enumerable]]: false, [[Configurable]]: true }."
649 if (this.is_callback() && !this.has_constants()) {
653 // TODO: Should we test here that the property is actually writable
654 // etc., or trust getOwnPropertyDescriptor?
655 assert_own_property(self, this.name,
656 "self does not have own property " + format_value(this.name));
657 var desc = Object.getOwnPropertyDescriptor(self, this.name);
658 assert_false("get" in desc, "self's property " + format_value(this.name) + " has getter");
659 assert_false("set" in desc, "self's property " + format_value(this.name) + " has setter");
660 assert_true(desc.writable, "self's property " + format_value(this.name) + " is not writable");
661 assert_false(desc.enumerable, "self's property " + format_value(this.name) + " is enumerable");
662 assert_true(desc.configurable, "self's property " + format_value(this.name) + " is not configurable");
664 if (this.is_callback()) {
665 // "The internal [[Prototype]] property of an interface object for
666 // a callback interface MUST be the Object.prototype object."
667 assert_equals(Object.getPrototypeOf(self[this.name]), Object.prototype,
668 "prototype of self's property " + format_value(this.name) + " is not Object.prototype");
673 // "The interface object for a given non-callback interface is a
675 // "If an object is defined to be a function object, then it has
676 // characteristics as follows:"
678 // Its [[Prototype]] internal property is otherwise specified (see
681 // "* Its [[Get]] internal property is set as described in ECMA-262
683 // Not much to test for this.
685 // "* Its [[Construct]] internal property is set as described in
686 // ECMA-262 section 19.2.2.3."
687 // Tested below if no constructor is defined. TODO: test constructors
690 // "* Its @@hasInstance property is set as described in ECMA-262
691 // section 19.2.3.8, unless otherwise specified."
694 // ES6 (rev 30) 19.1.3.6:
695 // "Else, if O has a [[Call]] internal method, then let builtinTag be
697 assert_class_string(self[this.name], "Function", "class string of " + this.name);
699 // "The [[Prototype]] internal property of an interface object for a
700 // non-callback interface is determined as follows:"
701 var prototype = Object.getPrototypeOf(self[this.name]);
703 // "* If the interface inherits from some other interface, the
704 // value of [[Prototype]] is the interface object for that other
706 var has_interface_object =
709 .has_extended_attribute("NoInterfaceObject");
710 if (has_interface_object) {
711 assert_own_property(self, this.base,
712 'should inherit from ' + this.base +
713 ', but self has no such property');
714 assert_equals(prototype, self[this.base],
715 'prototype of ' + this.name + ' is not ' +
719 // "If the interface doesn't inherit from any other interface, the
720 // value of [[Prototype]] is %FunctionPrototype% ([ECMA-262],
721 // section 6.1.7.4)."
722 assert_equals(prototype, Function.prototype,
723 "prototype of self's property " + format_value(this.name) + " is not Function.prototype");
726 if (!this.has_extended_attribute("Constructor")) {
727 // "The internal [[Call]] method of the interface object behaves as
730 // "If I was not declared with a [Constructor] extended attribute,
731 // then throw a TypeError."
732 assert_throws(new TypeError(), () => {
734 }, "interface object didn't throw TypeError when called as a function");
735 assert_throws(new TypeError(), () => {
736 new self[this.name]();
737 }, "interface object didn't throw TypeError when called as a constructor");
739 }, this.name + " interface: existence and properties of interface object");
741 if (!this.is_callback()) {
743 // This function tests WebIDL as of 2014-10-25.
744 // https://heycam.github.io/webidl/#es-interface-call
746 assert_own_property(self, this.name,
747 "self does not have own property " + format_value(this.name));
749 // "Interface objects for non-callback interfaces MUST have a
750 // property named “length” with attributes { [[Writable]]: false,
751 // [[Enumerable]]: false, [[Configurable]]: true } whose value is
753 assert_own_property(self[this.name], "length");
754 var desc = Object.getOwnPropertyDescriptor(self[this.name], "length");
755 assert_false("get" in desc, this.name + ".length has getter");
756 assert_false("set" in desc, this.name + ".length has setter");
757 assert_false(desc.writable, this.name + ".length is writable");
758 assert_false(desc.enumerable, this.name + ".length is enumerable");
759 assert_true(desc.configurable, this.name + ".length is not configurable");
761 var constructors = this.extAttrs
762 .filter(function(attr) { return attr.name == "Constructor"; });
763 var expected_length = minOverloadLength(constructors);
764 assert_equals(self[this.name].length, expected_length, "wrong value for " + this.name + ".length");
765 }, this.name + " interface object length");
768 // TODO: Test named constructors if I find any interfaces that have them.
771 // This function tests WebIDL as of 2015-01-21.
772 // https://heycam.github.io/webidl/#interface-object
774 if (this.is_callback() && !this.has_constants()) {
778 assert_own_property(self, this.name,
779 "self does not have own property " + format_value(this.name));
781 if (this.is_callback()) {
782 assert_false("prototype" in self[this.name],
783 this.name + ' should not have a "prototype" property');
787 // "An interface object for a non-callback interface must have a
788 // property named “prototype” with attributes { [[Writable]]: false,
789 // [[Enumerable]]: false, [[Configurable]]: false } whose value is an
790 // object called the interface prototype object. This object has
791 // properties that correspond to the regular attributes and regular
792 // operations defined on the interface, and is described in more detail
793 // in section 4.5.4 below."
794 assert_own_property(self[this.name], "prototype",
795 'interface "' + this.name + '" does not have own property "prototype"');
796 var desc = Object.getOwnPropertyDescriptor(self[this.name], "prototype");
797 assert_false("get" in desc, this.name + ".prototype has getter");
798 assert_false("set" in desc, this.name + ".prototype has setter");
799 assert_false(desc.writable, this.name + ".prototype is writable");
800 assert_false(desc.enumerable, this.name + ".prototype is enumerable");
801 assert_false(desc.configurable, this.name + ".prototype is configurable");
803 // Next, test that the [[Prototype]] of the interface prototype object
804 // is correct. (This is made somewhat difficult by the existence of
805 // [NoInterfaceObject].)
806 // TODO: Aryeh thinks there's at least other place in this file where
807 // we try to figure out if an interface prototype object is
808 // correct. Consolidate that code.
810 // "The interface prototype object for a given interface A must have an
811 // internal [[Prototype]] property whose value is returned from the
813 // "If A is declared with the [Global] or [PrimaryGlobal] extended
814 // attribute, and A supports named properties, then return the named
815 // properties object for A, as defined in section 4.5.5 below.
816 // "Otherwise, if A is declared to inherit from another interface, then
817 // return the interface prototype object for the inherited interface.
818 // "Otherwise, if A is declared with the [ArrayClass] extended
819 // attribute, then return %ArrayPrototype% ([ECMA-262], section
821 // "Otherwise, return %ObjectPrototype% ([ECMA-262], section 6.1.7.4).
822 // ([ECMA-262], section 15.2.4).
823 if (this.name === "Window") {
824 assert_class_string(Object.getPrototypeOf(self[this.name].prototype),
826 'Class name for prototype of Window' +
827 '.prototype is not "WindowProperties"');
829 var inherit_interface, inherit_interface_has_interface_object;
831 inherit_interface = this.base;
832 inherit_interface_has_interface_object =
834 .members[inherit_interface]
835 .has_extended_attribute("NoInterfaceObject");
836 } else if (this.has_extended_attribute('ArrayClass')) {
837 inherit_interface = 'Array';
838 inherit_interface_has_interface_object = true;
840 inherit_interface = 'Object';
841 inherit_interface_has_interface_object = true;
843 if (inherit_interface_has_interface_object) {
844 assert_own_property(self, inherit_interface,
845 'should inherit from ' + inherit_interface + ', but self has no such property');
846 assert_own_property(self[inherit_interface], 'prototype',
847 'should inherit from ' + inherit_interface + ', but that object has no "prototype" property');
848 assert_equals(Object.getPrototypeOf(self[this.name].prototype),
849 self[inherit_interface].prototype,
850 'prototype of ' + this.name + '.prototype is not ' + inherit_interface + '.prototype');
852 // We can't test that we get the correct object, because this is the
853 // only way to get our hands on it. We only test that its class
854 // string, at least, is correct.
855 assert_class_string(Object.getPrototypeOf(self[this.name].prototype),
856 inherit_interface + 'Prototype',
857 'Class name for prototype of ' + this.name +
858 '.prototype is not "' + inherit_interface + 'Prototype"');
862 // "The class string of an interface prototype object is the
863 // concatenation of the interface’s identifier and the string
865 assert_class_string(self[this.name].prototype, this.name + "Prototype",
866 "class string of " + this.name + ".prototype");
867 // String() should end up calling {}.toString if nothing defines a
869 if (!this.has_stringifier()) {
870 assert_equals(String(self[this.name].prototype), "[object " + this.name + "Prototype]",
871 "String(" + this.name + ".prototype)");
873 }, this.name + " interface: existence and properties of interface prototype object");
876 if (this.is_callback() && !this.has_constants()) {
880 assert_own_property(self, this.name,
881 "self does not have own property " + format_value(this.name));
883 if (this.is_callback()) {
884 assert_false("prototype" in self[this.name],
885 this.name + ' should not have a "prototype" property');
889 assert_own_property(self[this.name], "prototype",
890 'interface "' + this.name + '" does not have own property "prototype"');
892 // "If the [NoInterfaceObject] extended attribute was not specified on
893 // the interface, then the interface prototype object must also have a
894 // property named “constructor” with attributes { [[Writable]]: true,
895 // [[Enumerable]]: false, [[Configurable]]: true } whose value is a
896 // reference to the interface object for the interface."
897 assert_own_property(self[this.name].prototype, "constructor",
898 this.name + '.prototype does not have own property "constructor"');
899 var desc = Object.getOwnPropertyDescriptor(self[this.name].prototype, "constructor");
900 assert_false("get" in desc, this.name + ".prototype.constructor has getter");
901 assert_false("set" in desc, this.name + ".prototype.constructor has setter");
902 assert_true(desc.writable, this.name + ".prototype.constructor is not writable");
903 assert_false(desc.enumerable, this.name + ".prototype.constructor is enumerable");
904 assert_true(desc.configurable, this.name + ".prototype.constructor in not configurable");
905 assert_equals(self[this.name].prototype.constructor, self[this.name],
906 this.name + '.prototype.constructor is not the same object as ' + this.name);
907 }, this.name + ' interface: existence and properties of interface prototype object\'s "constructor" property');
911 IdlInterface.prototype.test_member_const = function(member)
915 if (this.is_callback() && !this.has_constants()) {
919 assert_own_property(self, this.name,
920 "self does not have own property " + format_value(this.name));
922 // "For each constant defined on an interface A, there must be
923 // a corresponding property on the interface object, if it
925 assert_own_property(self[this.name], member.name);
926 // "The value of the property is that which is obtained by
927 // converting the constant’s IDL value to an ECMAScript
929 assert_equals(self[this.name][member.name], constValue(member.value),
930 "property has wrong value");
931 // "The property has attributes { [[Writable]]: false,
932 // [[Enumerable]]: true, [[Configurable]]: false }."
933 var desc = Object.getOwnPropertyDescriptor(self[this.name], member.name);
934 assert_false("get" in desc, "property has getter");
935 assert_false("set" in desc, "property has setter");
936 assert_false(desc.writable, "property is writable");
937 assert_true(desc.enumerable, "property is not enumerable");
938 assert_false(desc.configurable, "property is configurable");
939 }, this.name + " interface: constant " + member.name + " on interface object");
940 // "In addition, a property with the same characteristics must
941 // exist on the interface prototype object."
943 if (this.is_callback() && !this.has_constants()) {
947 assert_own_property(self, this.name,
948 "self does not have own property " + format_value(this.name));
950 if (this.is_callback()) {
951 assert_false("prototype" in self[this.name],
952 this.name + ' should not have a "prototype" property');
956 assert_own_property(self[this.name], "prototype",
957 'interface "' + this.name + '" does not have own property "prototype"');
959 assert_own_property(self[this.name].prototype, member.name);
960 assert_equals(self[this.name].prototype[member.name], constValue(member.value),
961 "property has wrong value");
962 var desc = Object.getOwnPropertyDescriptor(self[this.name], member.name);
963 assert_false("get" in desc, "property has getter");
964 assert_false("set" in desc, "property has setter");
965 assert_false(desc.writable, "property is writable");
966 assert_true(desc.enumerable, "property is not enumerable");
967 assert_false(desc.configurable, "property is configurable");
968 }, this.name + " interface: constant " + member.name + " on interface prototype object");
973 IdlInterface.prototype.test_member_attribute = function(member)
977 if (this.is_callback() && !this.has_constants()) {
981 assert_own_property(self, this.name,
982 "self does not have own property " + format_value(this.name));
983 assert_own_property(self[this.name], "prototype",
984 'interface "' + this.name + '" does not have own property "prototype"');
986 if (member["static"]) {
987 assert_own_property(self[this.name], member.name,
988 "The interface object must have a property " +
989 format_value(member.name));
990 } else if (this.is_global()) {
991 assert_own_property(self, member.name,
992 "The global object must have a property " +
993 format_value(member.name));
994 assert_false(member.name in self[this.name].prototype,
995 "The prototype object must not have a property " +
996 format_value(member.name));
998 // Try/catch around the get here, since it can legitimately throw.
999 // If it does, we obviously can't check for equality with direct
1000 // invocation of the getter.
1004 propVal = self[member.name];
1010 var getter = Object.getOwnPropertyDescriptor(self, member.name).get;
1011 assert_equals(typeof(getter), "function",
1012 format_value(member.name) + " must have a getter");
1013 assert_equals(propVal, getter.call(undefined),
1014 "Gets on a global should not require an explicit this");
1016 this.do_interface_attribute_asserts(self, member);
1018 assert_true(member.name in self[this.name].prototype,
1019 "The prototype object must have a property " +
1020 format_value(member.name));
1022 if (!member.has_extended_attribute("LenientThis")) {
1023 assert_throws(new TypeError(), () => {
1024 self[this.name].prototype[member.name];
1025 }, "getting property on prototype object must throw TypeError");
1027 assert_equals(self[this.name].prototype[member.name], undefined,
1028 "getting property on prototype object must return undefined");
1030 this.do_interface_attribute_asserts(self[this.name].prototype, member);
1032 }, this.name + " interface: attribute " + member.name);
1036 IdlInterface.prototype.test_member_operation = function(member)
1040 if (this.is_callback() && !this.has_constants()) {
1044 assert_own_property(self, this.name,
1045 "self does not have own property " + format_value(this.name));
1047 if (this.is_callback()) {
1048 assert_false("prototype" in self[this.name],
1049 this.name + ' should not have a "prototype" property');
1053 assert_own_property(self[this.name], "prototype",
1054 'interface "' + this.name + '" does not have own property "prototype"');
1056 // "For each unique identifier of an operation defined on the
1057 // interface, there must be a corresponding property on the
1058 // interface prototype object (if it is a regular operation) or
1059 // the interface object (if it is a static operation), unless
1060 // the effective overload set for that identifier and operation
1061 // and with an argument count of 0 (for the ECMAScript language
1062 // binding) has no entries."
1064 var memberHolderObject;
1065 if (member["static"]) {
1066 assert_own_property(self[this.name], member.name,
1067 "interface object missing static operation");
1068 memberHolderObject = self[this.name];
1069 } else if (this.is_global()) {
1070 assert_own_property(self, member.name,
1071 "global object missing non-static operation");
1072 memberHolderObject = self;
1074 assert_own_property(self[this.name].prototype, member.name,
1075 "interface prototype object missing non-static operation");
1076 memberHolderObject = self[this.name].prototype;
1079 this.do_member_operation_asserts(memberHolderObject, member);
1080 }, this.name + " interface: operation " + member.name +
1081 "(" + member.arguments.map(function(m) { return m.idlType.idlType; }) +
1086 IdlInterface.prototype.do_member_operation_asserts = function(memberHolderObject, member)
1089 var operationUnforgeable = member.isUnforgeable;
1090 var desc = Object.getOwnPropertyDescriptor(memberHolderObject, member.name);
1091 // "The property has attributes { [[Writable]]: B,
1092 // [[Enumerable]]: true, [[Configurable]]: B }, where B is false if the
1093 // operation is unforgeable on the interface, and true otherwise".
1094 assert_false("get" in desc, "property has getter");
1095 assert_false("set" in desc, "property has setter");
1096 assert_equals(desc.writable, !operationUnforgeable,
1097 "property should be writable if and only if not unforgeable");
1098 assert_true(desc.enumerable, "property is not enumerable");
1099 assert_equals(desc.configurable, !operationUnforgeable,
1100 "property should be configurable if and only if not unforgeable");
1101 // "The value of the property is a Function object whose
1102 // behavior is as follows . . ."
1103 assert_equals(typeof memberHolderObject[member.name], "function",
1104 "property must be a function");
1105 // "The value of the Function object’s “length” property is
1106 // a Number determined as follows:
1108 // "Return the length of the shortest argument list of the
1110 assert_equals(memberHolderObject[member.name].length,
1111 minOverloadLength(this.members.filter(function(m) {
1112 return m.type == "operation" && m.name == member.name;
1114 "property has wrong .length");
1116 // Make some suitable arguments
1117 var args = member.arguments.map(function(arg) {
1118 return create_suitable_object(arg.idlType);
1121 // "Let O be a value determined as follows:
1123 // "Otherwise, throw a TypeError."
1124 // This should be hit if the operation is not static, there is
1125 // no [ImplicitThis] attribute, and the this value is null.
1127 // TODO: We currently ignore the [ImplicitThis] case. Except we manually
1128 // check for globals, since otherwise we'll invoke window.close(). And we
1129 // have to skip this test for anything that on the proto chain of "self",
1130 // since that does in fact have implicit-this behavior.
1131 if (!member["static"]) {
1132 if (!this.is_global() &&
1133 memberHolderObject[member.name] != self[member.name])
1135 assert_throws(new TypeError(), function() {
1136 memberHolderObject[member.name].apply(null, args);
1137 }, "calling operation with this = null didn't throw TypeError");
1140 // ". . . If O is not null and is also not a platform object
1141 // that implements interface I, throw a TypeError."
1143 // TODO: Test a platform object that implements some other
1144 // interface. (Have to be sure to get inheritance right.)
1145 assert_throws(new TypeError(), function() {
1146 memberHolderObject[member.name].apply({}, args);
1147 }, "calling operation with this = {} didn't throw TypeError");
1152 IdlInterface.prototype.test_member_stringifier = function(member)
1156 if (this.is_callback() && !this.has_constants()) {
1160 assert_own_property(self, this.name,
1161 "self does not have own property " + format_value(this.name));
1163 if (this.is_callback()) {
1164 assert_false("prototype" in self[this.name],
1165 this.name + ' should not have a "prototype" property');
1169 assert_own_property(self[this.name], "prototype",
1170 'interface "' + this.name + '" does not have own property "prototype"');
1172 // ". . . the property exists on the interface prototype object."
1173 var interfacePrototypeObject = self[this.name].prototype;
1174 assert_own_property(self[this.name].prototype, "toString",
1175 "interface prototype object missing non-static operation");
1177 var stringifierUnforgeable = member.isUnforgeable;
1178 var desc = Object.getOwnPropertyDescriptor(interfacePrototypeObject, "toString");
1179 // "The property has attributes { [[Writable]]: B,
1180 // [[Enumerable]]: true, [[Configurable]]: B }, where B is false if the
1181 // stringifier is unforgeable on the interface, and true otherwise."
1182 assert_false("get" in desc, "property has getter");
1183 assert_false("set" in desc, "property has setter");
1184 assert_equals(desc.writable, !stringifierUnforgeable,
1185 "property should be writable if and only if not unforgeable");
1186 assert_true(desc.enumerable, "property is not enumerable");
1187 assert_equals(desc.configurable, !stringifierUnforgeable,
1188 "property should be configurable if and only if not unforgeable");
1189 // "The value of the property is a Function object, which behaves as
1191 assert_equals(typeof interfacePrototypeObject.toString, "function",
1192 "property must be a function");
1193 // "The value of the Function object’s “length” property is the Number
1195 assert_equals(interfacePrototypeObject.toString.length, 0,
1196 "property has wrong .length");
1198 // "Let O be the result of calling ToObject on the this value."
1199 assert_throws(new TypeError(), function() {
1200 self[this.name].prototype.toString.apply(null, []);
1201 }, "calling stringifier with this = null didn't throw TypeError");
1203 // "If O is not an object that implements the interface on which the
1204 // stringifier was declared, then throw a TypeError."
1206 // TODO: Test a platform object that implements some other
1207 // interface. (Have to be sure to get inheritance right.)
1208 assert_throws(new TypeError(), function() {
1209 self[this.name].prototype.toString.apply({}, []);
1210 }, "calling stringifier with this = {} didn't throw TypeError");
1211 }, this.name + " interface: stringifier");
1215 IdlInterface.prototype.test_members = function()
1218 for (var i = 0; i < this.members.length; i++)
1220 var member = this.members[i];
1221 if (member.untested) {
1225 switch (member.type) {
1227 this.test_member_const(member);
1231 // For unforgeable attributes, we do the checks in
1232 // test_interface_of instead.
1233 if (!member.isUnforgeable)
1235 this.test_member_attribute(member);
1240 // TODO: Need to correctly handle multiple operations with the same
1242 // For unforgeable operations, we do the checks in
1243 // test_interface_of instead.
1245 if (!member.isUnforgeable)
1247 this.test_member_operation(member);
1249 } else if (member.stringifier) {
1250 this.test_member_stringifier(member);
1255 // TODO: check more member types.
1262 IdlInterface.prototype.test_object = function(desc)
1265 var obj, exception = null;
1275 // TODO: WebIDLParser doesn't currently support named legacycallers, so I'm
1276 // not sure what those would look like in the AST
1277 var expected_typeof = this.members.some(function(member)
1279 return member.legacycaller
1280 || ("idlType" in member && member.idlType.legacycaller)
1281 || ("idlType" in member && typeof member.idlType == "object"
1282 && "idlType" in member.idlType && member.idlType.idlType == "legacycaller");
1283 }) ? "function" : "object";
1285 this.test_primary_interface_of(desc, obj, exception, expected_typeof);
1286 var current_interface = this;
1287 while (current_interface)
1289 if (!(current_interface.name in this.array.members))
1291 throw "Interface " + current_interface.name + " not found (inherited by " + this.name + ")";
1293 if (current_interface.prevent_multiple_testing && current_interface.already_tested)
1297 current_interface.test_interface_of(desc, obj, exception, expected_typeof);
1298 current_interface = this.array.members[current_interface.base];
1303 IdlInterface.prototype.test_primary_interface_of = function(desc, obj, exception, expected_typeof)
1306 // We can't easily test that its prototype is correct if there's no
1307 // interface object, or the object is from a different global environment
1308 // (not instanceof Object). TODO: test in this case that its prototype at
1309 // least looks correct, even if we can't test that it's actually correct.
1310 if (!this.has_extended_attribute("NoInterfaceObject")
1311 && (typeof obj != expected_typeof || obj instanceof Object))
1314 assert_equals(exception, null, "Unexpected exception when evaluating object");
1315 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1316 assert_own_property(self, this.name,
1317 "self does not have own property " + format_value(this.name));
1318 assert_own_property(self[this.name], "prototype",
1319 'interface "' + this.name + '" does not have own property "prototype"');
1321 // "The value of the internal [[Prototype]] property of the
1322 // platform object is the interface prototype object of the primary
1323 // interface from the platform object’s associated global
1325 assert_equals(Object.getPrototypeOf(obj),
1326 self[this.name].prototype,
1327 desc + "'s prototype is not " + this.name + ".prototype");
1328 }, this.name + " must be primary interface of " + desc);
1331 // "The class string of a platform object that implements one or more
1332 // interfaces must be the identifier of the primary interface of the
1333 // platform object."
1335 assert_equals(exception, null, "Unexpected exception when evaluating object");
1336 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1337 assert_class_string(obj, this.name, "class string of " + desc);
1338 if (!this.has_stringifier())
1340 assert_equals(String(obj), "[object " + this.name + "]", "String(" + desc + ")");
1342 }, "Stringification of " + desc);
1346 IdlInterface.prototype.test_interface_of = function(desc, obj, exception, expected_typeof)
1349 // TODO: Indexed and named properties, more checks on interface members
1350 this.already_tested = true;
1352 for (var i = 0; i < this.members.length; i++)
1354 var member = this.members[i];
1355 if (member.type == "attribute" && member.isUnforgeable)
1358 assert_equals(exception, null, "Unexpected exception when evaluating object");
1359 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1360 this.do_interface_attribute_asserts(obj, member);
1361 }, this.name + " interface: " + desc + ' must have own property "' + member.name + '"');
1363 else if (member.type == "operation" &&
1365 member.isUnforgeable)
1368 assert_equals(exception, null, "Unexpected exception when evaluating object");
1369 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1370 assert_own_property(obj, member.name,
1371 "Doesn't have the unforgeable operation property");
1372 this.do_member_operation_asserts(obj, member);
1373 }, this.name + " interface: " + desc + ' must have own property "' + member.name + '"');
1375 else if ((member.type == "const"
1376 || member.type == "attribute"
1377 || member.type == "operation")
1381 assert_equals(exception, null, "Unexpected exception when evaluating object");
1382 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1383 if (!member["static"]) {
1384 if (!this.is_global()) {
1385 assert_inherits(obj, member.name);
1387 assert_own_property(obj, member.name);
1390 if (member.type == "const")
1392 assert_equals(obj[member.name], constValue(member.value));
1394 if (member.type == "attribute")
1396 // Attributes are accessor properties, so they might
1397 // legitimately throw an exception rather than returning
1399 var property, thrown = false;
1402 property = obj[member.name];
1410 this.array.assert_type_is(property, member.idlType);
1413 if (member.type == "operation")
1415 assert_equals(typeof obj[member.name], "function");
1418 }, this.name + " interface: " + desc + ' must inherit property "' + member.name + '" with the proper type (' + i + ')');
1420 // TODO: This is wrong if there are multiple operations with the same
1422 // TODO: Test passing arguments of the wrong type.
1423 if (member.type == "operation" && member.name && member.arguments.length)
1426 assert_equals(exception, null, "Unexpected exception when evaluating object");
1427 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1428 if (!member["static"]) {
1429 if (!this.is_global() && !member.isUnforgeable) {
1430 assert_inherits(obj, member.name);
1432 assert_own_property(obj, member.name);
1437 assert_false(member.name in obj);
1440 var minLength = minOverloadLength(this.members.filter(function(m) {
1441 return m.type == "operation" && m.name == member.name;
1444 for (var i = 0; i < minLength; i++) {
1445 assert_throws(new TypeError(), () => {
1446 obj[member.name].apply(obj, args);
1447 }, "Called with " + i + " arguments");
1449 args.push(create_suitable_object(member.arguments[i].idlType));
1451 }, this.name + " interface: calling " + member.name +
1452 "(" + member.arguments.map(function(m) { return m.idlType.idlType; }) +
1453 ") on " + desc + " with too few arguments must throw TypeError");
1459 IdlInterface.prototype.has_stringifier = function()
1462 if (this.members.some(function(member) { return member.stringifier; })) {
1466 this.array.members[this.base].has_stringifier()) {
1473 IdlInterface.prototype.do_interface_attribute_asserts = function(obj, member)
1476 // This function tests WebIDL as of 2015-01-27.
1477 // TODO: Consider [Exposed].
1479 // This is called by test_member_attribute() with the prototype as obj if
1480 // it is not a global, and the global otherwise, and by test_interface_of()
1481 // with the object as obj.
1483 // "For each exposed attribute of the interface, whether it was declared on
1484 // the interface itself or one of its consequential interfaces, there MUST
1485 // exist a corresponding property. The characteristics of this property are
1488 // "The name of the property is the identifier of the attribute."
1489 assert_own_property(obj, member.name);
1491 // "The property has attributes { [[Get]]: G, [[Set]]: S, [[Enumerable]]:
1492 // true, [[Configurable]]: configurable }, where:
1493 // "configurable is false if the attribute was declared with the
1494 // [Unforgeable] extended attribute and true otherwise;
1495 // "G is the attribute getter, defined below; and
1496 // "S is the attribute setter, also defined below."
1497 var desc = Object.getOwnPropertyDescriptor(obj, member.name);
1498 assert_false("value" in desc, 'property descriptor has value but is supposed to be accessor');
1499 assert_false("writable" in desc, 'property descriptor has "writable" field but is supposed to be accessor');
1500 assert_true(desc.enumerable, "property is not enumerable");
1501 if (member.isUnforgeable)
1503 assert_false(desc.configurable, "[Unforgeable] property must not be configurable");
1507 assert_true(desc.configurable, "property must be configurable");
1511 // "The attribute getter is a Function object whose behavior when invoked
1513 assert_equals(typeof desc.get, "function", "getter must be Function");
1515 // "If the attribute is a regular attribute, then:"
1516 if (!member["static"]) {
1517 // "If O is not a platform object that implements I, then:
1518 // "If the attribute was specified with the [LenientThis] extended
1519 // attribute, then return undefined.
1520 // "Otherwise, throw a TypeError."
1521 if (!member.has_extended_attribute("LenientThis")) {
1522 assert_throws(new TypeError(), () => {
1524 }, "calling getter on wrong object type must throw TypeError");
1526 assert_equals(desc.get.call({}), undefined,
1527 "calling getter on wrong object type must return undefined");
1531 // "The value of the Function object’s “length” property is the Number
1533 assert_equals(desc.get.length, 0, "getter length must be 0");
1536 // TODO: Test calling setter on the interface prototype (should throw
1537 // TypeError in most cases).
1539 && !member.has_extended_attribute("PutForwards")
1540 && !member.has_extended_attribute("Replaceable"))
1542 // "The attribute setter is undefined if the attribute is declared
1543 // readonly and has neither a [PutForwards] nor a [Replaceable]
1544 // extended attribute declared on it."
1545 assert_equals(desc.set, undefined, "setter must be undefined for readonly attributes");
1549 // "Otherwise, it is a Function object whose behavior when
1550 // invoked is as follows:"
1551 assert_equals(typeof desc.set, "function", "setter must be function for PutForwards, Replaceable, or non-readonly attributes");
1553 // "If the attribute is a regular attribute, then:"
1554 if (!member["static"]) {
1555 // "If /validThis/ is false and the attribute was not specified
1556 // with the [LenientThis] extended attribute, then throw a
1558 // "If the attribute is declared with a [Replaceable] extended
1559 // attribute, then: ..."
1560 // "If validThis is false, then return."
1561 if (!member.has_extended_attribute("LenientThis")) {
1562 assert_throws(new TypeError(), () => {
1564 }, "calling setter on wrong object type must throw TypeError");
1566 assert_equals(desc.set.call({}), undefined,
1567 "calling setter on wrong object type must return undefined");
1571 // "The value of the Function object’s “length” property is the Number
1573 assert_equals(desc.set.length, 1, "setter length must be 1");
1578 /// IdlInterfaceMember ///
1579 function IdlInterfaceMember(obj)
1583 * obj is an object produced by the WebIDLParser.js "ifMember" production.
1584 * We just forward all properties to this object without modification,
1585 * except for special extAttrs handling.
1591 if (!("extAttrs" in this))
1596 this.isUnforgeable = this.has_extended_attribute("Unforgeable");
1600 IdlInterfaceMember.prototype = Object.create(IdlObject.prototype);
1602 /// Internal helper functions ///
1603 function create_suitable_object(type)
1607 * type is an object produced by the WebIDLParser.js "type" production. We
1608 * return a JavaScript value that matches the type, if we can figure out
1615 switch (type.idlType)
1621 case "byte": case "octet": case "short": case "unsigned short":
1622 case "long": case "unsigned long": case "long long":
1623 case "unsigned long long": case "float": case "double":
1624 case "unrestricted float": case "unrestricted double":
1636 return document.createTextNode("abc");
1643 // Used for IdlArray.prototype.assert_type_is
1644 function IdlEnum(obj)
1648 * obj is an object produced by the WebIDLParser.js "dictionary"
1652 /** Self-explanatory. */
1653 this.name = obj.name;
1655 /** An array of values produced by the "enum" production. */
1656 this.values = obj.values;
1661 IdlEnum.prototype = Object.create(IdlObject.prototype);
1664 // Used for IdlArray.prototype.assert_type_is
1665 function IdlTypedef(obj)
1669 * obj is an object produced by the WebIDLParser.js "typedef"
1673 /** Self-explanatory. */
1674 this.name = obj.name;
1676 /** An array of values produced by the "typedef" production. */
1677 this.values = obj.values;
1682 IdlTypedef.prototype = Object.create(IdlObject.prototype);
1685 // vim: set expandtab shiftwidth=4 tabstop=4 foldmarker=@{,@} foldmethod=marker: