Home | History | Annotate | Download | only in inherit
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          proto_10.js
     24     Section:
     25     Description:        Determining Instance Relationships
     26 
     27     This tests Object Hierarchy and Inheritance, as described in the document
     28     Object Hierarchy and Inheritance in JavaScript, last modified on 12/18/97
     29     15:19:34 on http://devedge.netscape.com/.  Current URL:
     30     http://devedge.netscape.com/docs/manuals/communicator/jsobj/contents.htm
     31 
     32     This tests the syntax ObjectName.prototype = new PrototypeObject using the
     33     Employee example in the document referenced above.
     34 
     35     Author:             christine (at) netscape.com
     36     Date:               12 november 1997
     37 */
     38 
     39     var SECTION = "proto_10";
     40     var VERSION = "JS1_3";
     41     var TITLE   = "Determining Instance Relationships";
     42 
     43     startTest();
     44     writeHeaderToLog( SECTION + " "+ TITLE);
     45 
     46     var testcases = new Array();
     47 
     48 function InstanceOf( object, constructor ) {
     49     while ( object != null ) {
     50         if ( object == constructor.prototype ) {
     51             return true;
     52         }
     53         object = object.__proto__;
     54     }
     55     return false;
     56 }
     57 function Employee ( name, dept ) {
     58      this.name = name || "";
     59      this.dept = dept || "general";
     60 }
     61 
     62 function Manager () {
     63      this.reports = [];
     64 }
     65 Manager.prototype = new Employee();
     66 
     67 function WorkerBee ( name, dept, projs ) {
     68     this.base = Employee;
     69     this.base( name, dept)
     70     this.projects = projs || new Array();
     71 }
     72 WorkerBee.prototype = new Employee();
     73 
     74 function SalesPerson () {
     75     this.dept = "sales";
     76     this.quota = 100;
     77 }
     78 SalesPerson.prototype = new WorkerBee();
     79 
     80 function Engineer ( name, projs, machine ) {
     81     this.base = WorkerBee;
     82     this.base( name, "engineering", projs )
     83     this.machine = machine || "";
     84 }
     85 Engineer.prototype = new WorkerBee();
     86 
     87 function test() {
     88     for ( tc=0; tc < testcases.length; tc++ ) {
     89         testcases[tc].passed = writeTestCaseResult(
     90                             testcases[tc].expect,
     91                             testcases[tc].actual,
     92                             testcases[tc].description +" = "+
     93                             testcases[tc].actual );
     94 
     95         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     96     }
     97     stopTest();
     98     return ( testcases );
     99 }
    100     var pat = new Engineer()
    101 
    102     testcases[tc++] = new TestCase( SECTION,
    103                                     "pat.__proto__ == Engineer.prototype",
    104                                     true,
    105                                     pat.__proto__ == Engineer.prototype );
    106 
    107     testcases[tc++] = new TestCase( SECTION,
    108                                     "pat.__proto__.__proto__ == WorkerBee.prototype",
    109                                     true,
    110                                     pat.__proto__.__proto__ == WorkerBee.prototype );
    111 
    112     testcases[tc++] = new TestCase( SECTION,
    113                                     "pat.__proto__.__proto__.__proto__ == Employee.prototype",
    114                                     true,
    115                                     pat.__proto__.__proto__.__proto__ == Employee.prototype );
    116 
    117     testcases[tc++] = new TestCase( SECTION,
    118                                     "pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype",
    119                                     true,
    120                                     pat.__proto__.__proto__.__proto__.__proto__ == Object.prototype );
    121 
    122     testcases[tc++] = new TestCase( SECTION,
    123                                     "pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null",
    124                                     true,
    125                                     pat.__proto__.__proto__.__proto__.__proto__.__proto__ == null );
    126 
    127 
    128     testcases[tc++] = new TestCase( SECTION,
    129                                     "InstanceOf( pat, Engineer )",
    130                                     true,
    131                                     InstanceOf( pat, Engineer ) );
    132 
    133     testcases[tc++] = new TestCase( SECTION,
    134                                     "InstanceOf( pat, WorkerBee )",
    135                                     true,
    136                                     InstanceOf( pat, WorkerBee ) );
    137 
    138     testcases[tc++] = new TestCase( SECTION,
    139                                     "InstanceOf( pat, Employee )",
    140                                     true,
    141                                     InstanceOf( pat, Employee ) );
    142 
    143     testcases[tc++] = new TestCase( SECTION,
    144                                     "InstanceOf( pat, Object )",
    145                                     true,
    146                                     InstanceOf( pat, Object ) );
    147 
    148     testcases[tc++] = new TestCase( SECTION,
    149                                     "InstanceOf( pat, SalesPerson )",
    150                                     false,
    151                                     InstanceOf ( pat, SalesPerson ) );
    152     test();
    153