michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 444787; michael@0: var summary = 'Object.getPrototypeOf'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: var i; michael@0: var type; michael@0: var instance; michael@0: var types = [ michael@0: Array, michael@0: Boolean, michael@0: Date, michael@0: Error, michael@0: Function, michael@0: Math, michael@0: Number, michael@0: Object, michael@0: RegExp, michael@0: String, michael@0: ]; michael@0: michael@0: for (i = 0; i < types.length; i++) michael@0: { michael@0: type = types[i]; michael@0: michael@0: if (typeof type.__proto__ != 'undefined') michael@0: { michael@0: expect = type.__proto__; michael@0: actual = Object.getPrototypeOf(type); michael@0: reportCompare(expect, actual, summary + ': ' + type.name); michael@0: } michael@0: michael@0: try michael@0: { michael@0: eval('instance = new ' + type.name); michael@0: expect = type.prototype; michael@0: actual = Object.getPrototypeOf(instance); michael@0: reportCompare(expect, actual, summary + ': new ' + type.name); michael@0: } michael@0: catch(ex if ex instanceof TypeError) michael@0: { michael@0: print('Ignore ' + ex); michael@0: } michael@0: catch(ex) michael@0: { michael@0: actual = ex + ''; michael@0: reportCompare(expect, actual, summary + ': new ' + type.name); michael@0: } michael@0: michael@0: } michael@0: michael@0: types = [null, undefined]; michael@0: michael@0: for (i = 0; i < types.length; i++) michael@0: { michael@0: type = types[i]; michael@0: expect = 'TypeError: Object.getPrototype is not a function'; michael@0: try michael@0: { michael@0: actual = Object.getPrototype(null); michael@0: } michael@0: catch(ex) michael@0: { michael@0: actual = ex + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ': ' + type); michael@0: } michael@0: michael@0: var objects = [ michael@0: {instance: [0], type: Array}, michael@0: {instance: (function () {}), type: Function}, michael@0: {instance: eval, type: Function}, michael@0: {instance: parseInt, type: Function}, michael@0: {instance: {a: ''}, type: Object}, michael@0: {instance: /foo/, type: RegExp} michael@0: ]; michael@0: michael@0: for (i = 0; i < objects.length; i++) michael@0: { michael@0: instance = objects[i].instance; michael@0: type = objects[i].type; michael@0: expect = type.prototype; michael@0: actual = Object.getPrototypeOf(instance); michael@0: reportCompare(expect, actual, summary + ' instance: ' + instance + ', type: ' + type.name); michael@0: } michael@0: michael@0: var non_objects = [ true, false, 1.0, Infinity, NaN, Math.PI, "bar" ]; michael@0: michael@0: for (i = 0; i < non_objects.length; i++) michael@0: { michael@0: instance = non_objects[i]; michael@0: expect = 'TypeError: instance is not an object'; michael@0: try michael@0: { michael@0: actual = Object.getPrototypeOf(instance); michael@0: } michael@0: catch(ex) michael@0: { michael@0: actual = ex + ''; michael@0: } michael@0: reportCompare(expect, actual, summary + ' non-object: ' + actual); michael@0: } michael@0: michael@0: exitFunc ('test'); michael@0: }