js/src/tests/ecma_2/extensions/instanceof-002.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_2/extensions/instanceof-002.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          instanceof-002.js
    1.12 +   Section:
    1.13 +   Description:        Determining Instance Relationships
    1.14 +
    1.15 +   This test is the same as js1_3/inherit/proto-002, except that it uses
    1.16 +   the builtin instanceof operator rather than a user-defined function
    1.17 +   called InstanceOf.
    1.18 +
    1.19 +   This tests Object Hierarchy and Inheritance, as described in the document
    1.20 +   Object Hierarchy and Inheritance in JavaScript, last modified on 12/18/97
    1.21 +   15:19:34 on http://devedge.netscape.com/.  Current URL:
    1.22 +   http://devedge.netscape.com/docs/manuals/communicator/jsobj/contents.htm
    1.23 +
    1.24 +   This tests the syntax ObjectName.prototype = new PrototypeObject using the
    1.25 +   Employee example in the document referenced above.
    1.26 +
    1.27 +   Author:             christine@netscape.com
    1.28 +   Date:               12 november 1997
    1.29 +*/
    1.30 +//    onerror = err;
    1.31 +
    1.32 +var SECTION = "instanceof-002";
    1.33 +var VERSION = "ECMA_2";
    1.34 +var TITLE   = "Determining Instance Relationships";
    1.35 +
    1.36 +startTest();
    1.37 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.38 +
    1.39 +function InstanceOf( object, constructor ) {
    1.40 +  while ( object != null ) {
    1.41 +    if ( object == constructor.prototype ) {
    1.42 +      return true;
    1.43 +    }
    1.44 +    object = object.__proto__;
    1.45 +  }
    1.46 +  return false;
    1.47 +}
    1.48 +
    1.49 +function Employee ( name, dept ) {
    1.50 +  this.name = name || "";
    1.51 +  this.dept = dept || "general";
    1.52 +}
    1.53 +
    1.54 +function Manager () {
    1.55 +  this.reports = [];
    1.56 +}
    1.57 +Manager.prototype = new Employee();
    1.58 +
    1.59 +function WorkerBee ( name, dept, projs ) {
    1.60 +  this.base = Employee;
    1.61 +  this.base( name, dept)
    1.62 +    this.projects = projs || new Array();
    1.63 +}
    1.64 +WorkerBee.prototype = new Employee();
    1.65 +
    1.66 +function SalesPerson () {
    1.67 +  this.dept = "sales";
    1.68 +  this.quota = 100;
    1.69 +}
    1.70 +SalesPerson.prototype = new WorkerBee();
    1.71 +
    1.72 +function Engineer ( name, projs, machine ) {
    1.73 +  this.base = WorkerBee;
    1.74 +  this.base( name, "engineering", projs )
    1.75 +    this.machine = machine || "";
    1.76 +}
    1.77 +Engineer.prototype = new WorkerBee();
    1.78 +
    1.79 +var pat = new Engineer();
    1.80 +
    1.81 +new TestCase( SECTION,
    1.82 +	      "pat.__proto__ == Engineer.prototype",
    1.83 +	      true,
    1.84 +	      pat.__proto__ == Engineer.prototype );
    1.85 +
    1.86 +new TestCase( SECTION,
    1.87 +	      "pat.__proto__.__proto__ == WorkerBee.prototype",
    1.88 +	      true,
    1.89 +	      pat.__proto__.__proto__ == WorkerBee.prototype );
    1.90 +
    1.91 +new TestCase( SECTION,
    1.92 +	      "pat.__proto__.__proto__.__proto__ == Employee.prototype",
    1.93 +	      true,
    1.94 +	      pat.__proto__.__proto__.__proto__ == Employee.prototype );
    1.95 +
    1.96 +new TestCase( SECTION,
    1.97 +	      "pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype",
    1.98 +	      true,
    1.99 +	      pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype );
   1.100 +
   1.101 +new TestCase( SECTION,
   1.102 +	      "pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null",
   1.103 +	      true,
   1.104 +	      pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null );
   1.105 +
   1.106 +new TestCase( SECTION,
   1.107 +	      "pat instanceof Engineer",
   1.108 +	      true,
   1.109 +	      pat instanceof Engineer );
   1.110 +
   1.111 +new TestCase( SECTION,
   1.112 +	      "pat instanceof WorkerBee )",
   1.113 +	      true,
   1.114 +	      pat instanceof WorkerBee );
   1.115 +
   1.116 +new TestCase( SECTION,
   1.117 +	      "pat instanceof Employee )",
   1.118 +	      true,
   1.119 +	      pat instanceof Employee );
   1.120 +
   1.121 +new TestCase( SECTION,
   1.122 +	      "pat instanceof Object )",
   1.123 +	      true,
   1.124 +	      pat instanceof Object );
   1.125 +
   1.126 +new TestCase( SECTION,
   1.127 +	      "pat instanceof SalesPerson )",
   1.128 +	      false,
   1.129 +	      pat instanceof SalesPerson );
   1.130 +test();

mercurial