Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/w3c/resources/idlharness.js

Issue 1898703003: Remove testharness.js and friends in LayoutTests/http/tests/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
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].
5
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
9 */
10
11 /* For user documentation see docs/idlharness.md */
12
13 /**
14 * Notes for people who want to edit this file (not just use it as a library):
15 *
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.
21 *
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
25 * grammar:
26 *
27 * https://github.com/darobin/webidl.js/blob/master/lib/grammar.peg
28 *
29 * So for instance:
30 *
31 * // interface definition
32 * interface
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, membe rs: mem, extAttrs: extAttrs }; }
35 *
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.
42 *
43 * (Note that the version of WebIDLParser.js we use might sometimes be
44 * out-of-date or forked.)
45 *
46 * The members and methods of the classes defined by this file are all at least
47 * briefly documented, hopefully.
48 */
49 (function(){
50 "use strict";
51 /// Helpers ///
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;
56 return cnt.value;
57 }
58
59 function minOverloadLength(overloads) {
60 if (!overloads.length) {
61 return 0;
62 }
63
64 return overloads.map(function(attr) {
65 return attr.arguments ? attr.arguments.filter(function(arg) {
66 return !arg.optional && !arg.variadic;
67 }).length : 0;
68 })
69 .reduce(function(m, n) { return Math.min(m, n); });
70 }
71
72 /// IdlArray ///
73 // Entry point
74 self.IdlArray = function()
75 //@{
76 {
77 /**
78 * A map from strings to the corresponding named IdlObject, such as
79 * IdlInterface or IdlException. These are the things that test() will run
80 * tests on.
81 */
82 this.members = {};
83
84 /**
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.).
93 */
94 this.objects = {};
95
96 /**
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
100 * tests.
101 *
102 * .partials is simply an array of objects from WebIDLParser.js'
103 * "partialinterface" production. .implements maps strings to arrays of
104 * strings, such that
105 *
106 * A implements B;
107 * A implements C;
108 * D implements E;
109 *
110 * results in { A: ["B", "C"], D: ["E"] }.
111 */
112 this.partials = [];
113 this["implements"] = {};
114 };
115
116 //@}
117 IdlArray.prototype.add_idls = function(raw_idls)
118 //@{
119 {
120 /** Entry point. See documentation at beginning of file. */
121 this.internal_add_idls(WebIDL2.parse(raw_idls));
122 };
123
124 //@}
125 IdlArray.prototype.add_untested_idls = function(raw_idls)
126 //@{
127 {
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++)
131 {
132 parsed_idls[i].untested = true;
133 if ("members" in parsed_idls[i])
134 {
135 for (var j = 0; j < parsed_idls[i].members.length; j++)
136 {
137 parsed_idls[i].members[j].untested = true;
138 }
139 }
140 }
141 this.internal_add_idls(parsed_idls);
142 };
143
144 //@}
145 IdlArray.prototype.internal_add_idls = function(parsed_idls)
146 //@{
147 {
148 /**
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.
155 */
156 parsed_idls.forEach(function(parsed_idl)
157 {
158 if (parsed_idl.type == "interface" && parsed_idl.partial)
159 {
160 this.partials.push(parsed_idl);
161 return;
162 }
163
164 if (parsed_idl.type == "implements")
165 {
166 if (!(parsed_idl.target in this["implements"]))
167 {
168 this["implements"][parsed_idl.target] = [];
169 }
170 this["implements"][parsed_idl.target].push(parsed_idl["implements"]) ;
171 return;
172 }
173
174 parsed_idl.array = this;
175 if (parsed_idl.name in this.members)
176 {
177 throw "Duplicate identifier " + parsed_idl.name;
178 }
179 switch(parsed_idl.type)
180 {
181 case "interface":
182 this.members[parsed_idl.name] =
183 new IdlInterface(parsed_idl, /* is_callback = */ false);
184 break;
185
186 case "dictionary":
187 // Nothing to test, but we need the dictionary info around for type
188 // checks
189 this.members[parsed_idl.name] = new IdlDictionary(parsed_idl);
190 break;
191
192 case "typedef":
193 this.members[parsed_idl.name] = new IdlTypedef(parsed_idl);
194 break;
195
196 case "callback":
197 // TODO
198 console.log("callback not yet supported");
199 break;
200
201 case "enum":
202 this.members[parsed_idl.name] = new IdlEnum(parsed_idl);
203 break;
204
205 case "callback interface":
206 this.members[parsed_idl.name] =
207 new IdlInterface(parsed_idl, /* is_callback = */ true);
208 break;
209
210 default:
211 throw parsed_idl.name + ": " + parsed_idl.type + " not yet supported ";
212 }
213 }.bind(this));
214 };
215
216 //@}
217 IdlArray.prototype.add_objects = function(dict)
218 //@{
219 {
220 /** Entry point. See documentation at beginning of file. */
221 for (var k in dict)
222 {
223 if (k in this.objects)
224 {
225 this.objects[k] = this.objects[k].concat(dict[k]);
226 }
227 else
228 {
229 this.objects[k] = dict[k];
230 }
231 }
232 };
233
234 //@}
235 IdlArray.prototype.prevent_multiple_testing = function(name)
236 //@{
237 {
238 /** Entry point. See documentation at beginning of file. */
239 this.members[name].prevent_multiple_testing = true;
240 };
241
242 //@}
243 IdlArray.prototype.recursively_get_implements = function(interface_name)
244 //@{
245 {
246 /**
247 * Helper function for test(). Returns an array of things that implement
248 * interface_name, so if the IDL contains
249 *
250 * A implements B;
251 * B implements C;
252 * B implements D;
253 *
254 * then recursively_get_implements("A") should return ["B", "C", "D"].
255 */
256 var ret = this["implements"][interface_name];
257 if (ret === undefined)
258 {
259 return [];
260 }
261 for (var i = 0; i < this["implements"][interface_name].length; i++)
262 {
263 ret = ret.concat(this.recursively_get_implements(ret[i]));
264 if (ret.indexOf(ret[i]) != ret.lastIndexOf(ret[i]))
265 {
266 throw "Circular implements statements involving " + ret[i];
267 }
268 }
269 return ret;
270 };
271
272 //@}
273 IdlArray.prototype.test = function()
274 //@{
275 {
276 /** Entry point. See documentation at beginning of file. */
277
278 // First merge in all the partial interfaces and implements statements we
279 // encountered.
280 this.partials.forEach(function(parsed_idl)
281 {
282 if (!(parsed_idl.name in this.members)
283 || !(this.members[parsed_idl.name] instanceof IdlInterface))
284 {
285 throw "Partial interface " + parsed_idl.name + " with no original in terface";
286 }
287 if (parsed_idl.extAttrs)
288 {
289 parsed_idl.extAttrs.forEach(function(extAttr)
290 {
291 this.members[parsed_idl.name].extAttrs.push(extAttr);
292 }.bind(this));
293 }
294 parsed_idl.members.forEach(function(member)
295 {
296 this.members[parsed_idl.name].members.push(new IdlInterfaceMember(me mber));
297 }.bind(this));
298 }.bind(this));
299 this.partials = [];
300
301 for (var lhs in this["implements"])
302 {
303 this.recursively_get_implements(lhs).forEach(function(rhs)
304 {
305 var errStr = lhs + " implements " + rhs + ", but ";
306 if (!(lhs in this.members)) throw errStr + lhs + " is undefined.";
307 if (!(this.members[lhs] instanceof IdlInterface)) throw errStr + lhs + " is not an interface.";
308 if (!(rhs in this.members)) throw errStr + rhs + " is undefined.";
309 if (!(this.members[rhs] instanceof IdlInterface)) throw errStr + rhs + " is not an interface.";
310 this.members[rhs].members.forEach(function(member)
311 {
312 this.members[lhs].members.push(new IdlInterfaceMember(member));
313 }.bind(this));
314 }.bind(this));
315 }
316 this["implements"] = {};
317
318 // Now run test() on every member, and test_object() for every object.
319 for (var name in this.members)
320 {
321 this.members[name].test();
322 if (name in this.objects)
323 {
324 this.objects[name].forEach(function(str)
325 {
326 this.members[name].test_object(str);
327 }.bind(this));
328 }
329 }
330 };
331
332 //@}
333 IdlArray.prototype.assert_type_is = function(value, type)
334 //@{
335 {
336 /**
337 * Helper function that tests that value is an instance of type according
338 * to the rules of WebIDL. value is any JavaScript value, and type is an
339 * object produced by WebIDLParser.js' "type" production. That production
340 * is fairly elaborate due to the complexity of WebIDL's types, so it's
341 * best to look at the grammar to figure out what properties it might have.
342 */
343 if (type.idlType == "any")
344 {
345 // No assertions to make
346 return;
347 }
348
349 if (type.nullable && value === null)
350 {
351 // This is fine
352 return;
353 }
354
355 if (type.array)
356 {
357 // TODO: not supported yet
358 return;
359 }
360
361 if (type.sequence)
362 {
363 assert_true(Array.isArray(value), "is not array");
364 if (!value.length)
365 {
366 // Nothing we can do.
367 return;
368 }
369 this.assert_type_is(value[0], type.idlType.idlType);
370 return;
371 }
372
373 type = type.idlType;
374
375 switch(type)
376 {
377 case "void":
378 assert_equals(value, undefined);
379 return;
380
381 case "boolean":
382 assert_equals(typeof value, "boolean");
383 return;
384
385 case "byte":
386 assert_equals(typeof value, "number");
387 assert_equals(value, Math.floor(value), "not an integer");
388 assert_true(-128 <= value && value <= 127, "byte " + value + " not i n range [-128, 127]");
389 return;
390
391 case "octet":
392 assert_equals(typeof value, "number");
393 assert_equals(value, Math.floor(value), "not an integer");
394 assert_true(0 <= value && value <= 255, "octet " + value + " not in range [0, 255]");
395 return;
396
397 case "short":
398 assert_equals(typeof value, "number");
399 assert_equals(value, Math.floor(value), "not an integer");
400 assert_true(-32768 <= value && value <= 32767, "short " + value + " not in range [-32768, 32767]");
401 return;
402
403 case "unsigned short":
404 assert_equals(typeof value, "number");
405 assert_equals(value, Math.floor(value), "not an integer");
406 assert_true(0 <= value && value <= 65535, "unsigned short " + value + " not in range [0, 65535]");
407 return;
408
409 case "long":
410 assert_equals(typeof value, "number");
411 assert_equals(value, Math.floor(value), "not an integer");
412 assert_true(-2147483648 <= value && value <= 2147483647, "long " + v alue + " not in range [-2147483648, 2147483647]");
413 return;
414
415 case "unsigned long":
416 assert_equals(typeof value, "number");
417 assert_equals(value, Math.floor(value), "not an integer");
418 assert_true(0 <= value && value <= 4294967295, "unsigned long " + va lue + " not in range [0, 4294967295]");
419 return;
420
421 case "long long":
422 assert_equals(typeof value, "number");
423 return;
424
425 case "unsigned long long":
426 case "DOMTimeStamp":
427 assert_equals(typeof value, "number");
428 assert_true(0 <= value, "unsigned long long is negative");
429 return;
430
431 case "float":
432 case "double":
433 case "DOMHighResTimeStamp":
434 case "unrestricted float":
435 case "unrestricted double":
436 // TODO: distinguish these cases
437 assert_equals(typeof value, "number");
438 return;
439
440 case "DOMString":
441 case "ByteString":
442 case "USVString":
443 // TODO: https://github.com/w3c/testharness.js/issues/92
444 assert_equals(typeof value, "string");
445 return;
446
447 case "object":
448 assert_true(typeof value == "object" || typeof value == "function", "wrong type: not object or function");
449 return;
450 }
451
452 if (!(type in this.members))
453 {
454 throw "Unrecognized type " + type;
455 }
456
457 if (this.members[type] instanceof IdlInterface)
458 {
459 // We don't want to run the full
460 // IdlInterface.prototype.test_instance_of, because that could result
461 // in an infinite loop. TODO: This means we don't have tests for
462 // NoInterfaceObject interfaces, and we also can't test objects that
463 // come from another self.
464 assert_true(typeof value == "object" || typeof value == "function", "wro ng type: not object or function");
465 if (value instanceof Object
466 && !this.members[type].has_extended_attribute("NoInterfaceObject")
467 && type in self)
468 {
469 assert_true(value instanceof self[type], "not instanceof " + type);
470 }
471 }
472 else if (this.members[type] instanceof IdlEnum)
473 {
474 assert_equals(typeof value, "string");
475 }
476 else if (this.members[type] instanceof IdlDictionary)
477 {
478 // TODO: Test when we actually have something to test this on
479 }
480 else if (this.members[type] instanceof IdlTypedef)
481 {
482 // TODO: Test when we actually have something to test this on
483 }
484 else
485 {
486 throw "Type " + type + " isn't an interface or dictionary";
487 }
488 };
489 //@}
490
491 /// IdlObject ///
492 function IdlObject() {}
493 IdlObject.prototype.test = function()
494 //@{
495 {
496 /**
497 * By default, this does nothing, so no actual tests are run for IdlObjects
498 * that don't define any (e.g., IdlDictionary at the time of this writing).
499 */
500 };
501
502 //@}
503 IdlObject.prototype.has_extended_attribute = function(name)
504 //@{
505 {
506 /**
507 * This is only meaningful for things that support extended attributes,
508 * such as interfaces, exceptions, and members.
509 */
510 return this.extAttrs.some(function(o)
511 {
512 return o.name == name;
513 });
514 };
515
516 //@}
517
518 /// IdlDictionary ///
519 // Used for IdlArray.prototype.assert_type_is
520 function IdlDictionary(obj)
521 //@{
522 {
523 /**
524 * obj is an object produced by the WebIDLParser.js "dictionary"
525 * production.
526 */
527
528 /** Self-explanatory. */
529 this.name = obj.name;
530
531 /** An array of objects produced by the "dictionaryMember" production. */
532 this.members = obj.members;
533
534 /**
535 * The name (as a string) of the dictionary type we inherit from, or null
536 * if there is none.
537 */
538 this.base = obj.inheritance;
539 }
540
541 //@}
542 IdlDictionary.prototype = Object.create(IdlObject.prototype);
543
544 /// IdlInterface ///
545 function IdlInterface(obj, is_callback) {
546 /**
547 * obj is an object produced by the WebIDLParser.js "exception" or
548 * "interface" production, as appropriate.
549 */
550
551 /** Self-explanatory. */
552 this.name = obj.name;
553
554 /** A back-reference to our IdlArray. */
555 this.array = obj.array;
556
557 /**
558 * An indicator of whether we should run tests on the (exception) interface
559 * object and (exception) interface prototype object. Tests on members are
560 * controlled by .untested on each member, not this.
561 */
562 this.untested = obj.untested;
563
564 /** An array of objects produced by the "ExtAttr" production. */
565 this.extAttrs = obj.extAttrs;
566
567 /** An array of IdlInterfaceMembers. */
568 this.members = obj.members.map(function(m){return new IdlInterfaceMember(m); });
569 if (this.has_extended_attribute("Unforgeable")) {
570 this.members
571 .filter(function(m) { return !m["static"] && (m.type == "attribute" || m.type == "operation"); })
572 .forEach(function(m) { return m.isUnforgeable = true; });
573 }
574
575 /**
576 * The name (as a string) of the type we inherit from, or null if there is
577 * none.
578 */
579 this.base = obj.inheritance;
580
581 this._is_callback = is_callback;
582 }
583 IdlInterface.prototype = Object.create(IdlObject.prototype);
584 IdlInterface.prototype.is_callback = function()
585 //@{
586 {
587 return this._is_callback;
588 };
589 //@}
590
591 IdlInterface.prototype.has_constants = function()
592 //@{
593 {
594 return this.members.some(function(member) {
595 return member.type === "const";
596 });
597 };
598 //@}
599
600 IdlInterface.prototype.is_global = function()
601 //@{
602 {
603 return this.extAttrs.some(function(attribute) {
604 return attribute.name === "Global" ||
605 attribute.name === "PrimaryGlobal";
606 });
607 };
608 //@}
609
610 IdlInterface.prototype.test = function()
611 //@{
612 {
613 if (this.has_extended_attribute("NoInterfaceObject"))
614 {
615 // No tests to do without an instance. TODO: We should still be able
616 // to run tests on the prototype object, if we obtain one through some
617 // other means.
618 return;
619 }
620
621 if (!this.untested)
622 {
623 // First test things to do with the exception/interface object and
624 // exception/interface prototype object.
625 this.test_self();
626 }
627 // Then test things to do with its members (constants, fields, attributes,
628 // operations, . . .). These are run even if .untested is true, because
629 // members might themselves be marked as .untested. This might happen to
630 // interfaces if the interface itself is untested but a partial interface
631 // that extends it is tested -- then the interface itself and its initial
632 // members will be marked as untested, but the members added by the partial
633 // interface are still tested.
634 this.test_members();
635 };
636 //@}
637
638 IdlInterface.prototype.test_self = function()
639 //@{
640 {
641 test(function()
642 {
643 // This function tests WebIDL as of 2015-01-13.
644 // TODO: Consider [Exposed].
645
646 // "For every interface that is exposed in a given ECMAScript global
647 // environment and:
648 // * is a callback interface that has constants declared on it, or
649 // * is a non-callback interface that is not declared with the
650 // [NoInterfaceObject] extended attribute,
651 // a corresponding property MUST exist on the ECMAScript global object.
652 // The name of the property is the identifier of the interface, and its
653 // value is an object called the interface object.
654 // The property has the attributes { [[Writable]]: true,
655 // [[Enumerable]]: false, [[Configurable]]: true }."
656 if (this.is_callback() && !this.has_constants()) {
657 return;
658 }
659
660 // TODO: Should we test here that the property is actually writable
661 // etc., or trust getOwnPropertyDescriptor?
662 assert_own_property(self, this.name,
663 "self does not have own property " + format_value(th is.name));
664 var desc = Object.getOwnPropertyDescriptor(self, this.name);
665 assert_false("get" in desc, "self's property " + format_value(this.name) + " has getter");
666 assert_false("set" in desc, "self's property " + format_value(this.name) + " has setter");
667 assert_true(desc.writable, "self's property " + format_value(this.name) + " is not writable");
668 assert_false(desc.enumerable, "self's property " + format_value(this.nam e) + " is enumerable");
669 assert_true(desc.configurable, "self's property " + format_value(this.na me) + " is not configurable");
670
671 if (this.is_callback()) {
672 // "The internal [[Prototype]] property of an interface object for
673 // a callback interface MUST be the Object.prototype object."
674 assert_equals(Object.getPrototypeOf(self[this.name]), Object.prototy pe,
675 "prototype of self's property " + format_value(this.na me) + " is not Object.prototype");
676
677 return;
678 }
679
680 // "The interface object for a given non-callback interface is a
681 // function object."
682 // "If an object is defined to be a function object, then it has
683 // characteristics as follows:"
684
685 // Its [[Prototype]] internal property is otherwise specified (see
686 // below).
687
688 // "* Its [[Get]] internal property is set as described in ECMA-262
689 // section 9.1.8."
690 // Not much to test for this.
691
692 // "* Its [[Construct]] internal property is set as described in
693 // ECMA-262 section 19.2.2.3."
694 // Tested below if no constructor is defined. TODO: test constructors
695 // if defined.
696
697 // "* Its @@hasInstance property is set as described in ECMA-262
698 // section 19.2.3.8, unless otherwise specified."
699 // TODO
700
701 // ES6 (rev 30) 19.1.3.6:
702 // "Else, if O has a [[Call]] internal method, then let builtinTag be
703 // "Function"."
704 assert_class_string(self[this.name], "Function", "class string of " + th is.name);
705
706 // "The [[Prototype]] internal property of an interface object for a
707 // non-callback interface is determined as follows:"
708 var prototype = Object.getPrototypeOf(self[this.name]);
709 if (this.base) {
710 // "* If the interface inherits from some other interface, the
711 // value of [[Prototype]] is the interface object for that other
712 // interface."
713 var has_interface_object =
714 !this.array
715 .members[this.base]
716 .has_extended_attribute("NoInterfaceObject");
717 if (has_interface_object) {
718 assert_own_property(self, this.base,
719 'should inherit from ' + this.base +
720 ', but self has no such property');
721 assert_equals(prototype, self[this.base],
722 'prototype of ' + this.name + ' is not ' +
723 this.base);
724 }
725 } else {
726 // "If the interface doesn't inherit from any other interface, the
727 // value of [[Prototype]] is %FunctionPrototype% ([ECMA-262],
728 // section 6.1.7.4)."
729 assert_equals(prototype, Function.prototype,
730 "prototype of self's property " + format_value(this.na me) + " is not Function.prototype");
731 }
732
733 if (!this.has_extended_attribute("Constructor")) {
734 // "The internal [[Call]] method of the interface object behaves as
735 // follows . . .
736 //
737 // "If I was not declared with a [Constructor] extended attribute,
738 // then throw a TypeError."
739 assert_throws(new TypeError(), function() {
740 self[this.name]();
741 }.bind(this), "interface object didn't throw TypeError when called a s a function");
742 assert_throws(new TypeError(), function() {
743 new self[this.name]();
744 }.bind(this), "interface object didn't throw TypeError when called a s a constructor");
745 }
746 }.bind(this), this.name + " interface: existence and properties of interface object");
747
748 if (!this.is_callback()) {
749 test(function() {
750 // This function tests WebIDL as of 2014-10-25.
751 // https://heycam.github.io/webidl/#es-interface-call
752
753 assert_own_property(self, this.name,
754 "self does not have own property " + format_valu e(this.name));
755
756 // "Interface objects for non-callback interfaces MUST have a
757 // property named “length” with attributes { [[Writable]]: false,
758 // [[Enumerable]]: false, [[Configurable]]: true } whose value is
759 // a Number."
760 assert_own_property(self[this.name], "length");
761 var desc = Object.getOwnPropertyDescriptor(self[this.name], "length" );
762 assert_false("get" in desc, this.name + ".length has getter");
763 assert_false("set" in desc, this.name + ".length has setter");
764 assert_false(desc.writable, this.name + ".length is writable");
765 assert_false(desc.enumerable, this.name + ".length is enumerable");
766 assert_true(desc.configurable, this.name + ".length is not configura ble");
767
768 var constructors = this.extAttrs
769 .filter(function(attr) { return attr.name == "Constructor"; });
770 var expected_length = minOverloadLength(constructors);
771 assert_equals(self[this.name].length, expected_length, "wrong value for " + this.name + ".length");
772 }.bind(this), this.name + " interface object length");
773 }
774
775 if (!this.is_callback() || this.has_constants()) {
776 test(function() {
777 // This function tests WebIDL as of 2015-11-17.
778 // https://heycam.github.io/webidl/#interface-object
779
780 assert_own_property(self, this.name,
781 "self does not have own property " + format_valu e(this.name));
782
783 // "All interface objects must have a property named “name” with
784 // attributes { [[Writable]]: false, [[Enumerable]]: false,
785 // [[Configurable]]: true } whose value is the identifier of the
786 // corresponding interface."
787
788 assert_own_property(self[this.name], "name");
789 var desc = Object.getOwnPropertyDescriptor(self[this.name], "name");
790 assert_false("get" in desc, this.name + ".name has getter");
791 assert_false("set" in desc, this.name + ".name has setter");
792 assert_false(desc.writable, this.name + ".name is writable");
793 assert_false(desc.enumerable, this.name + ".name is enumerable");
794 assert_true(desc.configurable, this.name + ".name is not configurabl e");
795 assert_equals(self[this.name].name, this.name, "wrong value for " + this.name + ".name");
796 }.bind(this), this.name + " interface object name");
797 }
798
799 // TODO: Test named constructors if I find any interfaces that have them.
800
801 test(function()
802 {
803 // This function tests WebIDL as of 2015-01-21.
804 // https://heycam.github.io/webidl/#interface-object
805
806 if (this.is_callback() && !this.has_constants()) {
807 return;
808 }
809
810 assert_own_property(self, this.name,
811 "self does not have own property " + format_value(th is.name));
812
813 if (this.is_callback()) {
814 assert_false("prototype" in self[this.name],
815 this.name + ' should not have a "prototype" property');
816 return;
817 }
818
819 // "An interface object for a non-callback interface must have a
820 // property named “prototype” with attributes { [[Writable]]: false,
821 // [[Enumerable]]: false, [[Configurable]]: false } whose value is an
822 // object called the interface prototype object. This object has
823 // properties that correspond to the regular attributes and regular
824 // operations defined on the interface, and is described in more detail
825 // in section 4.5.4 below."
826 assert_own_property(self[this.name], "prototype",
827 'interface "' + this.name + '" does not have own pro perty "prototype"');
828 var desc = Object.getOwnPropertyDescriptor(self[this.name], "prototype") ;
829 assert_false("get" in desc, this.name + ".prototype has getter");
830 assert_false("set" in desc, this.name + ".prototype has setter");
831 assert_false(desc.writable, this.name + ".prototype is writable");
832 assert_false(desc.enumerable, this.name + ".prototype is enumerable");
833 assert_false(desc.configurable, this.name + ".prototype is configurable" );
834
835 // Next, test that the [[Prototype]] of the interface prototype object
836 // is correct. (This is made somewhat difficult by the existence of
837 // [NoInterfaceObject].)
838 // TODO: Aryeh thinks there's at least other place in this file where
839 // we try to figure out if an interface prototype object is
840 // correct. Consolidate that code.
841
842 // "The interface prototype object for a given interface A must have an
843 // internal [[Prototype]] property whose value is returned from the
844 // following steps:
845 // "If A is declared with the [Global] or [PrimaryGlobal] extended
846 // attribute, and A supports named properties, then return the named
847 // properties object for A, as defined in section 4.5.5 below.
848 // "Otherwise, if A is declared to inherit from another interface, then
849 // return the interface prototype object for the inherited interface.
850 // "Otherwise, if A is declared with the [ArrayClass] extended
851 // attribute, then return %ArrayPrototype% ([ECMA-262], section
852 // 6.1.7.4).
853 // "Otherwise, return %ObjectPrototype% ([ECMA-262], section 6.1.7.4).
854 // ([ECMA-262], section 15.2.4).
855 if (this.name === "Window") {
856 assert_class_string(Object.getPrototypeOf(self[this.name].prototype) ,
857 'WindowProperties',
858 'Class name for prototype of Window' +
859 '.prototype is not "WindowProperties"');
860 } else {
861 var inherit_interface, inherit_interface_has_interface_object;
862 if (this.base) {
863 inherit_interface = this.base;
864 inherit_interface_has_interface_object =
865 !this.array
866 .members[inherit_interface]
867 .has_extended_attribute("NoInterfaceObject");
868 } else if (this.has_extended_attribute('ArrayClass')) {
869 inherit_interface = 'Array';
870 inherit_interface_has_interface_object = true;
871 } else {
872 inherit_interface = 'Object';
873 inherit_interface_has_interface_object = true;
874 }
875 if (inherit_interface_has_interface_object) {
876 assert_own_property(self, inherit_interface,
877 'should inherit from ' + inherit_interface + ', but self has no such property');
878 assert_own_property(self[inherit_interface], 'prototype',
879 'should inherit from ' + inherit_interface + ', but that object has no "prototype" property');
880 assert_equals(Object.getPrototypeOf(self[this.name].prototype),
881 self[inherit_interface].prototype,
882 'prototype of ' + this.name + '.prototype is not ' + inherit_interface + '.prototype');
883 } else {
884 // We can't test that we get the correct object, because this is the
885 // only way to get our hands on it. We only test that its class
886 // string, at least, is correct.
887 assert_class_string(Object.getPrototypeOf(self[this.name].protot ype),
888 inherit_interface + 'Prototype',
889 'Class name for prototype of ' + this.name +
890 '.prototype is not "' + inherit_interface + 'Prototype"');
891 }
892 }
893
894 // "The class string of an interface prototype object is the
895 // concatenation of the interface’s identifier and the string
896 // “Prototype”."
897 assert_class_string(self[this.name].prototype, this.name + "Prototype",
898 "class string of " + this.name + ".prototype");
899 // String() should end up calling {}.toString if nothing defines a
900 // stringifier.
901 if (!this.has_stringifier()) {
902 assert_equals(String(self[this.name].prototype), "[object " + this.n ame + "Prototype]",
903 "String(" + this.name + ".prototype)");
904 }
905 }.bind(this), this.name + " interface: existence and properties of interface prototype object");
906
907 test(function()
908 {
909 if (this.is_callback() && !this.has_constants()) {
910 return;
911 }
912
913 assert_own_property(self, this.name,
914 "self does not have own property " + format_value(th is.name));
915
916 if (this.is_callback()) {
917 assert_false("prototype" in self[this.name],
918 this.name + ' should not have a "prototype" property');
919 return;
920 }
921
922 assert_own_property(self[this.name], "prototype",
923 'interface "' + this.name + '" does not have own pro perty "prototype"');
924
925 // "If the [NoInterfaceObject] extended attribute was not specified on
926 // the interface, then the interface prototype object must also have a
927 // property named “constructor” with attributes { [[Writable]]: true,
928 // [[Enumerable]]: false, [[Configurable]]: true } whose value is a
929 // reference to the interface object for the interface."
930 assert_own_property(self[this.name].prototype, "constructor",
931 this.name + '.prototype does not have own property " constructor"');
932 var desc = Object.getOwnPropertyDescriptor(self[this.name].prototype, "c onstructor");
933 assert_false("get" in desc, this.name + ".prototype.constructor has gett er");
934 assert_false("set" in desc, this.name + ".prototype.constructor has sett er");
935 assert_true(desc.writable, this.name + ".prototype.constructor is not wr itable");
936 assert_false(desc.enumerable, this.name + ".prototype.constructor is enu merable");
937 assert_true(desc.configurable, this.name + ".prototype.constructor in no t configurable");
938 assert_equals(self[this.name].prototype.constructor, self[this.name],
939 this.name + '.prototype.constructor is not the same object as ' + this.name);
940 }.bind(this), this.name + ' interface: existence and properties of interface prototype object\'s "constructor" property');
941 };
942
943 //@}
944 IdlInterface.prototype.test_member_const = function(member)
945 //@{
946 {
947 test(function()
948 {
949 if (this.is_callback() && !this.has_constants()) {
950 return;
951 }
952
953 assert_own_property(self, this.name,
954 "self does not have own property " + format_value(th is.name));
955
956 // "For each constant defined on an interface A, there must be
957 // a corresponding property on the interface object, if it
958 // exists."
959 assert_own_property(self[this.name], member.name);
960 // "The value of the property is that which is obtained by
961 // converting the constant’s IDL value to an ECMAScript
962 // value."
963 assert_equals(self[this.name][member.name], constValue(member.value),
964 "property has wrong value");
965 // "The property has attributes { [[Writable]]: false,
966 // [[Enumerable]]: true, [[Configurable]]: false }."
967 var desc = Object.getOwnPropertyDescriptor(self[this.name], member.name) ;
968 assert_false("get" in desc, "property has getter");
969 assert_false("set" in desc, "property has setter");
970 assert_false(desc.writable, "property is writable");
971 assert_true(desc.enumerable, "property is not enumerable");
972 assert_false(desc.configurable, "property is configurable");
973 }.bind(this), this.name + " interface: constant " + member.name + " on inter face object");
974 // "In addition, a property with the same characteristics must
975 // exist on the interface prototype object."
976 test(function()
977 {
978 if (this.is_callback() && !this.has_constants()) {
979 return;
980 }
981
982 assert_own_property(self, this.name,
983 "self does not have own property " + format_value(th is.name));
984
985 if (this.is_callback()) {
986 assert_false("prototype" in self[this.name],
987 this.name + ' should not have a "prototype" property');
988 return;
989 }
990
991 assert_own_property(self[this.name], "prototype",
992 'interface "' + this.name + '" does not have own pro perty "prototype"');
993
994 assert_own_property(self[this.name].prototype, member.name);
995 assert_equals(self[this.name].prototype[member.name], constValue(member. value),
996 "property has wrong value");
997 var desc = Object.getOwnPropertyDescriptor(self[this.name], member.name) ;
998 assert_false("get" in desc, "property has getter");
999 assert_false("set" in desc, "property has setter");
1000 assert_false(desc.writable, "property is writable");
1001 assert_true(desc.enumerable, "property is not enumerable");
1002 assert_false(desc.configurable, "property is configurable");
1003 }.bind(this), this.name + " interface: constant " + member.name + " on inter face prototype object");
1004 };
1005
1006
1007 //@}
1008 IdlInterface.prototype.test_member_attribute = function(member)
1009 //@{
1010 {
1011 test(function()
1012 {
1013 if (this.is_callback() && !this.has_constants()) {
1014 return;
1015 }
1016
1017 assert_own_property(self, this.name,
1018 "self does not have own property " + format_value(th is.name));
1019 assert_own_property(self[this.name], "prototype",
1020 'interface "' + this.name + '" does not have own pro perty "prototype"');
1021
1022 if (member["static"]) {
1023 assert_own_property(self[this.name], member.name,
1024 "The interface object must have a property " +
1025 format_value(member.name));
1026 } else if (this.is_global()) {
1027 assert_own_property(self, member.name,
1028 "The global object must have a property " +
1029 format_value(member.name));
1030 assert_false(member.name in self[this.name].prototype,
1031 "The prototype object must not have a property " +
1032 format_value(member.name));
1033
1034 // Try/catch around the get here, since it can legitimately throw.
1035 // If it does, we obviously can't check for equality with direct
1036 // invocation of the getter.
1037 var gotValue;
1038 var propVal;
1039 try {
1040 propVal = self[member.name];
1041 gotValue = true;
1042 } catch (e) {
1043 gotValue = false;
1044 }
1045 if (gotValue) {
1046 var getter = Object.getOwnPropertyDescriptor(self, member.name). get;
1047 assert_equals(typeof(getter), "function",
1048 format_value(member.name) + " must have a getter") ;
1049 assert_equals(propVal, getter.call(undefined),
1050 "Gets on a global should not require an explicit t his");
1051 }
1052 this.do_interface_attribute_asserts(self, member);
1053 } else {
1054 assert_true(member.name in self[this.name].prototype,
1055 "The prototype object must have a property " +
1056 format_value(member.name));
1057
1058 if (!member.has_extended_attribute("LenientThis")) {
1059 assert_throws(new TypeError(), function() {
1060 self[this.name].prototype[member.name];
1061 }.bind(this), "getting property on prototype object must throw T ypeError");
1062 } else {
1063 assert_equals(self[this.name].prototype[member.name], undefined,
1064 "getting property on prototype object must return undefined");
1065 }
1066 this.do_interface_attribute_asserts(self[this.name].prototype, membe r);
1067 }
1068 }.bind(this), this.name + " interface: attribute " + member.name);
1069 };
1070
1071 //@}
1072 IdlInterface.prototype.test_member_operation = function(member)
1073 //@{
1074 {
1075 test(function()
1076 {
1077 if (this.is_callback() && !this.has_constants()) {
1078 return;
1079 }
1080
1081 assert_own_property(self, this.name,
1082 "self does not have own property " + format_value(th is.name));
1083
1084 if (this.is_callback()) {
1085 assert_false("prototype" in self[this.name],
1086 this.name + ' should not have a "prototype" property');
1087 return;
1088 }
1089
1090 assert_own_property(self[this.name], "prototype",
1091 'interface "' + this.name + '" does not have own pro perty "prototype"');
1092
1093 // "For each unique identifier of an operation defined on the
1094 // interface, there must be a corresponding property on the
1095 // interface prototype object (if it is a regular operation) or
1096 // the interface object (if it is a static operation), unless
1097 // the effective overload set for that identifier and operation
1098 // and with an argument count of 0 (for the ECMAScript language
1099 // binding) has no entries."
1100 //
1101 var memberHolderObject;
1102 if (member["static"]) {
1103 assert_own_property(self[this.name], member.name,
1104 "interface object missing static operation");
1105 memberHolderObject = self[this.name];
1106 } else if (this.is_global()) {
1107 assert_own_property(self, member.name,
1108 "global object missing non-static operation");
1109 memberHolderObject = self;
1110 } else {
1111 assert_own_property(self[this.name].prototype, member.name,
1112 "interface prototype object missing non-static operation");
1113 memberHolderObject = self[this.name].prototype;
1114 }
1115
1116 this.do_member_operation_asserts(memberHolderObject, member);
1117 }.bind(this), this.name + " interface: operation " + member.name +
1118 "(" + member.arguments.map(function(m) { return m.idlType.idlType; }) +
1119 ")");
1120 };
1121
1122 //@}
1123 IdlInterface.prototype.do_member_operation_asserts = function(memberHolderObject , member)
1124 //@{
1125 {
1126 var operationUnforgeable = member.isUnforgeable;
1127 var desc = Object.getOwnPropertyDescriptor(memberHolderObject, member.name);
1128 // "The property has attributes { [[Writable]]: B,
1129 // [[Enumerable]]: true, [[Configurable]]: B }, where B is false if the
1130 // operation is unforgeable on the interface, and true otherwise".
1131 assert_false("get" in desc, "property has getter");
1132 assert_false("set" in desc, "property has setter");
1133 assert_equals(desc.writable, !operationUnforgeable,
1134 "property should be writable if and only if not unforgeable");
1135 assert_true(desc.enumerable, "property is not enumerable");
1136 assert_equals(desc.configurable, !operationUnforgeable,
1137 "property should be configurable if and only if not unforgeabl e");
1138 // "The value of the property is a Function object whose
1139 // behavior is as follows . . ."
1140 assert_equals(typeof memberHolderObject[member.name], "function",
1141 "property must be a function");
1142 // "The value of the Function object’s “length” property is
1143 // a Number determined as follows:
1144 // ". . .
1145 // "Return the length of the shortest argument list of the
1146 // entries in S."
1147 assert_equals(memberHolderObject[member.name].length,
1148 minOverloadLength(this.members.filter(function(m) {
1149 return m.type == "operation" && m.name == member.name;
1150 })),
1151 "property has wrong .length");
1152
1153 // Make some suitable arguments
1154 var args = member.arguments.map(function(arg) {
1155 return create_suitable_object(arg.idlType);
1156 });
1157
1158 // "Let O be a value determined as follows:
1159 // ". . .
1160 // "Otherwise, throw a TypeError."
1161 // This should be hit if the operation is not static, there is
1162 // no [ImplicitThis] attribute, and the this value is null.
1163 //
1164 // TODO: We currently ignore the [ImplicitThis] case. Except we manually
1165 // check for globals, since otherwise we'll invoke window.close(). And we
1166 // have to skip this test for anything that on the proto chain of "self",
1167 // since that does in fact have implicit-this behavior.
1168 if (!member["static"]) {
1169 if (!this.is_global() &&
1170 memberHolderObject[member.name] != self[member.name])
1171 {
1172 assert_throws(new TypeError(), function() {
1173 memberHolderObject[member.name].apply(null, args);
1174 }, "calling operation with this = null didn't throw TypeError");
1175 }
1176
1177 // ". . . If O is not null and is also not a platform object
1178 // that implements interface I, throw a TypeError."
1179 //
1180 // TODO: Test a platform object that implements some other
1181 // interface. (Have to be sure to get inheritance right.)
1182 assert_throws(new TypeError(), function() {
1183 memberHolderObject[member.name].apply({}, args);
1184 }, "calling operation with this = {} didn't throw TypeError");
1185 }
1186 }
1187
1188 //@}
1189 IdlInterface.prototype.test_member_stringifier = function(member)
1190 //@{
1191 {
1192 test(function()
1193 {
1194 if (this.is_callback() && !this.has_constants()) {
1195 return;
1196 }
1197
1198 assert_own_property(self, this.name,
1199 "self does not have own property " + format_value(th is.name));
1200
1201 if (this.is_callback()) {
1202 assert_false("prototype" in self[this.name],
1203 this.name + ' should not have a "prototype" property');
1204 return;
1205 }
1206
1207 assert_own_property(self[this.name], "prototype",
1208 'interface "' + this.name + '" does not have own pro perty "prototype"');
1209
1210 // ". . . the property exists on the interface prototype object."
1211 var interfacePrototypeObject = self[this.name].prototype;
1212 assert_own_property(self[this.name].prototype, "toString",
1213 "interface prototype object missing non-static operation");
1214
1215 var stringifierUnforgeable = member.isUnforgeable;
1216 var desc = Object.getOwnPropertyDescriptor(interfacePrototypeObject, "to String");
1217 // "The property has attributes { [[Writable]]: B,
1218 // [[Enumerable]]: true, [[Configurable]]: B }, where B is false if the
1219 // stringifier is unforgeable on the interface, and true otherwise."
1220 assert_false("get" in desc, "property has getter");
1221 assert_false("set" in desc, "property has setter");
1222 assert_equals(desc.writable, !stringifierUnforgeable,
1223 "property should be writable if and only if not unforgeabl e");
1224 assert_true(desc.enumerable, "property is not enumerable");
1225 assert_equals(desc.configurable, !stringifierUnforgeable,
1226 "property should be configurable if and only if not unforg eable");
1227 // "The value of the property is a Function object, which behaves as
1228 // follows . . ."
1229 assert_equals(typeof interfacePrototypeObject.toString, "function",
1230 "property must be a function");
1231 // "The value of the Function object’s “length” property is the Number
1232 // value 0."
1233 assert_equals(interfacePrototypeObject.toString.length, 0,
1234 "property has wrong .length");
1235
1236 // "Let O be the result of calling ToObject on the this value."
1237 assert_throws(new TypeError(), function() {
1238 self[this.name].prototype.toString.apply(null, []);
1239 }, "calling stringifier with this = null didn't throw TypeError");
1240
1241 // "If O is not an object that implements the interface on which the
1242 // stringifier was declared, then throw a TypeError."
1243 //
1244 // TODO: Test a platform object that implements some other
1245 // interface. (Have to be sure to get inheritance right.)
1246 assert_throws(new TypeError(), function() {
1247 self[this.name].prototype.toString.apply({}, []);
1248 }, "calling stringifier with this = {} didn't throw TypeError");
1249 }.bind(this), this.name + " interface: stringifier");
1250 };
1251
1252 //@}
1253 IdlInterface.prototype.test_members = function()
1254 //@{
1255 {
1256 for (var i = 0; i < this.members.length; i++)
1257 {
1258 var member = this.members[i];
1259 if (member.untested) {
1260 continue;
1261 }
1262
1263 switch (member.type) {
1264 case "const":
1265 this.test_member_const(member);
1266 break;
1267
1268 case "attribute":
1269 // For unforgeable attributes, we do the checks in
1270 // test_interface_of instead.
1271 if (!member.isUnforgeable)
1272 {
1273 this.test_member_attribute(member);
1274 }
1275 break;
1276
1277 case "operation":
1278 // TODO: Need to correctly handle multiple operations with the same
1279 // identifier.
1280 // For unforgeable operations, we do the checks in
1281 // test_interface_of instead.
1282 if (member.name) {
1283 if (!member.isUnforgeable)
1284 {
1285 this.test_member_operation(member);
1286 }
1287 } else if (member.stringifier) {
1288 this.test_member_stringifier(member);
1289 }
1290 break;
1291
1292 default:
1293 // TODO: check more member types.
1294 break;
1295 }
1296 }
1297 };
1298
1299 //@}
1300 IdlInterface.prototype.test_object = function(desc)
1301 //@{
1302 {
1303 var obj, exception = null;
1304 try
1305 {
1306 obj = eval(desc);
1307 }
1308 catch(e)
1309 {
1310 exception = e;
1311 }
1312
1313 var expected_typeof =
1314 this.members.some(function(member) { return member.legacycaller; })
1315 ? "function"
1316 : "object";
1317
1318 this.test_primary_interface_of(desc, obj, exception, expected_typeof);
1319 var current_interface = this;
1320 while (current_interface)
1321 {
1322 if (!(current_interface.name in this.array.members))
1323 {
1324 throw "Interface " + current_interface.name + " not found (inherited by " + this.name + ")";
1325 }
1326 if (current_interface.prevent_multiple_testing && current_interface.alre ady_tested)
1327 {
1328 return;
1329 }
1330 current_interface.test_interface_of(desc, obj, exception, expected_typeo f);
1331 current_interface = this.array.members[current_interface.base];
1332 }
1333 };
1334
1335 //@}
1336 IdlInterface.prototype.test_primary_interface_of = function(desc, obj, exception , expected_typeof)
1337 //@{
1338 {
1339 // We can't easily test that its prototype is correct if there's no
1340 // interface object, or the object is from a different global environment
1341 // (not instanceof Object). TODO: test in this case that its prototype at
1342 // least looks correct, even if we can't test that it's actually correct.
1343 if (!this.has_extended_attribute("NoInterfaceObject")
1344 && (typeof obj != expected_typeof || obj instanceof Object))
1345 {
1346 test(function()
1347 {
1348 assert_equals(exception, null, "Unexpected exception when evaluating object");
1349 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1350 assert_own_property(self, this.name,
1351 "self does not have own property " + format_valu e(this.name));
1352 assert_own_property(self[this.name], "prototype",
1353 'interface "' + this.name + '" does not have own property "prototype"');
1354
1355 // "The value of the internal [[Prototype]] property of the
1356 // platform object is the interface prototype object of the primary
1357 // interface from the platform object’s associated global
1358 // environment."
1359 assert_equals(Object.getPrototypeOf(obj),
1360 self[this.name].prototype,
1361 desc + "'s prototype is not " + this.name + ".prototyp e");
1362 }.bind(this), this.name + " must be primary interface of " + desc);
1363 }
1364
1365 // "The class string of a platform object that implements one or more
1366 // interfaces must be the identifier of the primary interface of the
1367 // platform object."
1368 test(function()
1369 {
1370 assert_equals(exception, null, "Unexpected exception when evaluating obj ect");
1371 assert_equals(typeof obj, expected_typeof, "wrong typeof object");
1372 assert_class_string(obj, this.name, "class string of " + desc);
1373 if (!this.has_stringifier())
1374 {
1375 assert_equals(String(obj), "[object " + this.name + "]", "String(" + desc + ")");
1376 }
1377 }.bind(this), "Stringification of " + desc);
1378 };
1379
1380 //@}
1381 IdlInterface.prototype.test_interface_of = function(desc, obj, exception, expect ed_typeof)
1382 //@{
1383 {
1384 // TODO: Indexed and named properties, more checks on interface members
1385 this.already_tested = true;
1386
1387 for (var i = 0; i < this.members.length; i++)
1388 {
1389 var member = this.members[i];
1390 if (member.type == "attribute" && member.isUnforgeable)
1391 {
1392 test(function()
1393 {
1394 assert_equals(exception, null, "Unexpected exception when evalua ting object");
1395 assert_equals(typeof obj, expected_typeof, "wrong typeof object" );
1396 this.do_interface_attribute_asserts(obj, member);
1397 }.bind(this), this.name + " interface: " + desc + ' must have own pr operty "' + member.name + '"');
1398 }
1399 else if (member.type == "operation" &&
1400 member.name &&
1401 member.isUnforgeable)
1402 {
1403 test(function()
1404 {
1405 assert_equals(exception, null, "Unexpected exception when evalua ting object");
1406 assert_equals(typeof obj, expected_typeof, "wrong typeof object" );
1407 assert_own_property(obj, member.name,
1408 "Doesn't have the unforgeable operation prop erty");
1409 this.do_member_operation_asserts(obj, member);
1410 }.bind(this), this.name + " interface: " + desc + ' must have own pr operty "' + member.name + '"');
1411 }
1412 else if ((member.type == "const"
1413 || member.type == "attribute"
1414 || member.type == "operation")
1415 && member.name)
1416 {
1417 test(function()
1418 {
1419 assert_equals(exception, null, "Unexpected exception when evalua ting object");
1420 assert_equals(typeof obj, expected_typeof, "wrong typeof object" );
1421 if (!member["static"]) {
1422 if (!this.is_global()) {
1423 assert_inherits(obj, member.name);
1424 } else {
1425 assert_own_property(obj, member.name);
1426 }
1427
1428 if (member.type == "const")
1429 {
1430 assert_equals(obj[member.name], constValue(member.value) );
1431 }
1432 if (member.type == "attribute")
1433 {
1434 // Attributes are accessor properties, so they might
1435 // legitimately throw an exception rather than returning
1436 // anything.
1437 var property, thrown = false;
1438 try
1439 {
1440 property = obj[member.name];
1441 }
1442 catch (e)
1443 {
1444 thrown = true;
1445 }
1446 if (!thrown)
1447 {
1448 this.array.assert_type_is(property, member.idlType);
1449 }
1450 }
1451 if (member.type == "operation")
1452 {
1453 assert_equals(typeof obj[member.name], "function");
1454 }
1455 }
1456 }.bind(this), this.name + " interface: " + desc + ' must inherit pro perty "' + member.name + '" with the proper type (' + i + ')');
1457 }
1458 // TODO: This is wrong if there are multiple operations with the same
1459 // identifier.
1460 // TODO: Test passing arguments of the wrong type.
1461 if (member.type == "operation" && member.name && member.arguments.length )
1462 {
1463 test(function()
1464 {
1465 assert_equals(exception, null, "Unexpected exception when evalua ting object");
1466 assert_equals(typeof obj, expected_typeof, "wrong typeof object" );
1467 if (!member["static"]) {
1468 if (!this.is_global() && !member.isUnforgeable) {
1469 assert_inherits(obj, member.name);
1470 } else {
1471 assert_own_property(obj, member.name);
1472 }
1473 }
1474 else
1475 {
1476 assert_false(member.name in obj);
1477 }
1478
1479 var minLength = minOverloadLength(this.members.filter(function(m ) {
1480 return m.type == "operation" && m.name == member.name;
1481 }));
1482 var args = [];
1483 for (var i = 0; i < minLength; i++) {
1484 assert_throws(new TypeError(), function()
1485 {
1486 obj[member.name].apply(obj, args);
1487 }.bind(this), "Called with " + i + " arguments");
1488
1489 args.push(create_suitable_object(member.arguments[i].idlType ));
1490 }
1491 }.bind(this), this.name + " interface: calling " + member.name +
1492 "(" + member.arguments.map(function(m) { return m.idlType.idlType; } ) +
1493 ") on " + desc + " with too few arguments must throw TypeError");
1494 }
1495 }
1496 };
1497
1498 //@}
1499 IdlInterface.prototype.has_stringifier = function()
1500 //@{
1501 {
1502 if (this.members.some(function(member) { return member.stringifier; })) {
1503 return true;
1504 }
1505 if (this.base &&
1506 this.array.members[this.base].has_stringifier()) {
1507 return true;
1508 }
1509 return false;
1510 };
1511
1512 //@}
1513 IdlInterface.prototype.do_interface_attribute_asserts = function(obj, member)
1514 //@{
1515 {
1516 // This function tests WebIDL as of 2015-01-27.
1517 // TODO: Consider [Exposed].
1518
1519 // This is called by test_member_attribute() with the prototype as obj if
1520 // it is not a global, and the global otherwise, and by test_interface_of()
1521 // with the object as obj.
1522
1523 // "For each exposed attribute of the interface, whether it was declared on
1524 // the interface itself or one of its consequential interfaces, there MUST
1525 // exist a corresponding property. The characteristics of this property are
1526 // as follows:"
1527
1528 // "The name of the property is the identifier of the attribute."
1529 assert_own_property(obj, member.name);
1530
1531 // "The property has attributes { [[Get]]: G, [[Set]]: S, [[Enumerable]]:
1532 // true, [[Configurable]]: configurable }, where:
1533 // "configurable is false if the attribute was declared with the
1534 // [Unforgeable] extended attribute and true otherwise;
1535 // "G is the attribute getter, defined below; and
1536 // "S is the attribute setter, also defined below."
1537 var desc = Object.getOwnPropertyDescriptor(obj, member.name);
1538 assert_false("value" in desc, 'property descriptor has value but is supposed to be accessor');
1539 assert_false("writable" in desc, 'property descriptor has "writable" field b ut is supposed to be accessor');
1540 assert_true(desc.enumerable, "property is not enumerable");
1541 if (member.isUnforgeable)
1542 {
1543 assert_false(desc.configurable, "[Unforgeable] property must not be conf igurable");
1544 }
1545 else
1546 {
1547 assert_true(desc.configurable, "property must be configurable");
1548 }
1549
1550
1551 // "The attribute getter is a Function object whose behavior when invoked
1552 // is as follows:"
1553 assert_equals(typeof desc.get, "function", "getter must be Function");
1554
1555 // "If the attribute is a regular attribute, then:"
1556 if (!member["static"]) {
1557 // "If O is not a platform object that implements I, then:
1558 // "If the attribute was specified with the [LenientThis] extended
1559 // attribute, then return undefined.
1560 // "Otherwise, throw a TypeError."
1561 if (!member.has_extended_attribute("LenientThis")) {
1562 assert_throws(new TypeError(), function() {
1563 desc.get.call({});
1564 }.bind(this), "calling getter on wrong object type must throw TypeEr ror");
1565 } else {
1566 assert_equals(desc.get.call({}), undefined,
1567 "calling getter on wrong object type must return undef ined");
1568 }
1569 }
1570
1571 // "The value of the Function object’s “length” property is the Number
1572 // value 0."
1573 assert_equals(desc.get.length, 0, "getter length must be 0");
1574
1575
1576 // TODO: Test calling setter on the interface prototype (should throw
1577 // TypeError in most cases).
1578 if (member.readonly
1579 && !member.has_extended_attribute("PutForwards")
1580 && !member.has_extended_attribute("Replaceable"))
1581 {
1582 // "The attribute setter is undefined if the attribute is declared
1583 // readonly and has neither a [PutForwards] nor a [Replaceable]
1584 // extended attribute declared on it."
1585 assert_equals(desc.set, undefined, "setter must be undefined for readonl y attributes");
1586 }
1587 else
1588 {
1589 // "Otherwise, it is a Function object whose behavior when
1590 // invoked is as follows:"
1591 assert_equals(typeof desc.set, "function", "setter must be function for PutForwards, Replaceable, or non-readonly attributes");
1592
1593 // "If the attribute is a regular attribute, then:"
1594 if (!member["static"]) {
1595 // "If /validThis/ is false and the attribute was not specified
1596 // with the [LenientThis] extended attribute, then throw a
1597 // TypeError."
1598 // "If the attribute is declared with a [Replaceable] extended
1599 // attribute, then: ..."
1600 // "If validThis is false, then return."
1601 if (!member.has_extended_attribute("LenientThis")) {
1602 assert_throws(new TypeError(), function() {
1603 desc.set.call({});
1604 }.bind(this), "calling setter on wrong object type must throw Ty peError");
1605 } else {
1606 assert_equals(desc.set.call({}), undefined,
1607 "calling setter on wrong object type must return u ndefined");
1608 }
1609 }
1610
1611 // "The value of the Function object’s “length” property is the Number
1612 // value 1."
1613 assert_equals(desc.set.length, 1, "setter length must be 1");
1614 }
1615 }
1616 //@}
1617
1618 /// IdlInterfaceMember ///
1619 function IdlInterfaceMember(obj)
1620 //@{
1621 {
1622 /**
1623 * obj is an object produced by the WebIDLParser.js "ifMember" production.
1624 * We just forward all properties to this object without modification,
1625 * except for special extAttrs handling.
1626 */
1627 for (var k in obj)
1628 {
1629 this[k] = obj[k];
1630 }
1631 if (!("extAttrs" in this))
1632 {
1633 this.extAttrs = [];
1634 }
1635
1636 this.isUnforgeable = this.has_extended_attribute("Unforgeable");
1637 }
1638
1639 //@}
1640 IdlInterfaceMember.prototype = Object.create(IdlObject.prototype);
1641
1642 /// Internal helper functions ///
1643 function create_suitable_object(type)
1644 //@{
1645 {
1646 /**
1647 * type is an object produced by the WebIDLParser.js "type" production. We
1648 * return a JavaScript value that matches the type, if we can figure out
1649 * how.
1650 */
1651 if (type.nullable)
1652 {
1653 return null;
1654 }
1655 switch (type.idlType)
1656 {
1657 case "any":
1658 case "boolean":
1659 return true;
1660
1661 case "byte": case "octet": case "short": case "unsigned short":
1662 case "long": case "unsigned long": case "long long":
1663 case "unsigned long long": case "float": case "double":
1664 case "unrestricted float": case "unrestricted double":
1665 return 7;
1666
1667 case "DOMString":
1668 case "ByteString":
1669 case "USVString":
1670 return "foo";
1671
1672 case "object":
1673 return {a: "b"};
1674
1675 case "Node":
1676 return document.createTextNode("abc");
1677 }
1678 return null;
1679 }
1680 //@}
1681
1682 /// IdlEnum ///
1683 // Used for IdlArray.prototype.assert_type_is
1684 function IdlEnum(obj)
1685 //@{
1686 {
1687 /**
1688 * obj is an object produced by the WebIDLParser.js "dictionary"
1689 * production.
1690 */
1691
1692 /** Self-explanatory. */
1693 this.name = obj.name;
1694
1695 /** An array of values produced by the "enum" production. */
1696 this.values = obj.values;
1697
1698 }
1699 //@}
1700
1701 IdlEnum.prototype = Object.create(IdlObject.prototype);
1702
1703 /// IdlTypedef ///
1704 // Used for IdlArray.prototype.assert_type_is
1705 function IdlTypedef(obj)
1706 //@{
1707 {
1708 /**
1709 * obj is an object produced by the WebIDLParser.js "typedef"
1710 * production.
1711 */
1712
1713 /** Self-explanatory. */
1714 this.name = obj.name;
1715
1716 /** An array of values produced by the "typedef" production. */
1717 this.values = obj.values;
1718
1719 }
1720 //@}
1721
1722 IdlTypedef.prototype = Object.create(IdlObject.prototype);
1723
1724 }());
1725 // vim: set expandtab shiftwidth=4 tabstop=4 foldmarker=@{,@} foldmethod=marker:
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698