OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function() { | 5 (function() { |
6 | 6 |
7 'use strict'; | 7 'use strict'; |
8 | 8 |
9 QUnit.module('base.inherits'); | 9 QUnit.module('base.inherits'); |
10 | 10 |
11 /** | 11 /** |
12 * @constructor | 12 * @constructor |
13 * @extends {ChildClass} | 13 * @extends {ChildClass} |
14 */ | 14 */ |
15 var GrandChildClass = function() { | 15 var GrandChildClass = function() { |
16 base.inherits(this, ChildClass); | 16 base.inherits(this, ChildClass); |
17 this.name = 'grandChild'; | 17 this.name = 'grandChild'; |
18 } | 18 } |
19 | 19 |
20 /** @return {string} */ | 20 /** |
21 GrandChildClass.prototype.overrideMethod = function() { | 21 * @param {string} arg |
22 return 'overrideMethod - grandChild'; | 22 * @return {string} |
| 23 */ |
| 24 GrandChildClass.prototype.overrideMethod = function(arg) { |
| 25 return 'overrideMethod - grandChild - ' + arg; |
23 } | 26 } |
24 | 27 |
25 /** | 28 /** |
26 * @constructor | 29 * @constructor |
27 * @extends {ParentClass} | 30 * @extends {ParentClass} |
28 */ | 31 */ |
29 var ChildClass = function() { | 32 var ChildClass = function() { |
30 base.inherits(this, ParentClass, 'parentArg'); | 33 base.inherits(this, ParentClass, 'parentArg'); |
31 this.name = 'child'; | 34 this.name = 'child'; |
32 this.childOnly = 'childOnly'; | 35 this.childOnly = 'childOnly'; |
33 } | 36 } |
34 | 37 |
35 /** @return {string} */ | 38 /** |
36 ChildClass.prototype.overrideMethod = function() { | 39 * @param {string} arg |
37 return 'overrideMethod - child'; | 40 * @return {string} |
| 41 */ |
| 42 ChildClass.prototype.overrideMethod = function(arg) { |
| 43 return 'overrideMethod - child - ' + arg; |
38 } | 44 } |
39 | 45 |
40 /** @return {string} */ | 46 /** @return {string} */ |
41 ChildClass.prototype.childMethod = function() { | 47 ChildClass.prototype.childMethod = function() { |
42 return 'childMethod'; | 48 return 'childMethod'; |
43 } | 49 } |
44 | 50 |
45 /** | 51 /** |
| 52 * @param {string} arg |
46 * @constructor | 53 * @constructor |
47 * @param {string} arg | |
48 */ | 54 */ |
49 var ParentClass = function(arg) { | 55 var ParentClass = function(arg) { |
50 /** @type {string} */ | 56 /** @type {string} */ |
51 this.name = 'parent'; | 57 this.name = 'parent'; |
52 /** @type {string} */ | 58 /** @type {string} */ |
53 this.parentOnly = 'parentOnly'; | 59 this.parentOnly = 'parentOnly'; |
54 /** @type {string} */ | 60 /** @type {string} */ |
55 this.parentConstructorArg = arg; | 61 this.parentConstructorArg = arg; |
56 } | 62 } |
57 | 63 |
58 /** @return {string} */ | 64 /** @return {string} */ |
59 ParentClass.prototype.parentMethod = function() { | 65 ParentClass.prototype.parentMethod = function() { |
60 return 'parentMethod'; | 66 return 'parentMethod'; |
61 } | 67 } |
62 | 68 |
63 /** @return {string} */ | 69 /** |
64 ParentClass.prototype.overrideMethod = function() { | 70 * @param {string} arg |
65 return 'overrideMethod - parent'; | 71 * @return {string} |
| 72 */ |
| 73 ParentClass.prototype.overrideMethod = function(arg) { |
| 74 return 'overrideMethod - parent - ' + arg; |
66 } | 75 } |
67 | 76 |
68 QUnit.test('should invoke parent constructor with the correct arguments', | 77 QUnit.test('should invoke parent constructor with the correct arguments', |
69 function(assert) { | 78 function(assert) { |
70 var child = new ChildClass(); | 79 var child = new ChildClass(); |
71 assert.equal(child.parentConstructorArg, 'parentArg'); | 80 assert.equal(child.parentConstructorArg, 'parentArg'); |
72 }); | 81 }); |
73 | 82 |
74 QUnit.test('should preserve parent property and method', function(assert) { | 83 QUnit.test('should preserve parent property and method', function(assert) { |
75 var child = new ChildClass(); | 84 var child = new ChildClass(); |
76 assert.equal(child.parentOnly, 'parentOnly'); | 85 assert.equal(child.parentOnly, 'parentOnly'); |
77 assert.equal(child.parentMethod(), 'parentMethod'); | 86 assert.equal(child.parentMethod(), 'parentMethod'); |
78 }); | 87 }); |
79 | 88 |
80 QUnit.test('should preserve instanceof', function(assert) { | 89 QUnit.test('should preserve instanceof', function(assert) { |
81 var child = new ChildClass(); | 90 var child = new ChildClass(); |
82 var grandChild = new GrandChildClass(); | 91 var grandChild = new GrandChildClass(); |
83 assert.ok(child instanceof ParentClass); | 92 assert.ok(child instanceof ParentClass); |
84 assert.ok(child instanceof ChildClass); | 93 assert.ok(child instanceof ChildClass); |
85 assert.ok(grandChild instanceof ParentClass); | 94 assert.ok(grandChild instanceof ParentClass); |
86 assert.ok(grandChild instanceof ChildClass); | 95 assert.ok(grandChild instanceof ChildClass); |
87 assert.ok(grandChild instanceof GrandChildClass); | 96 assert.ok(grandChild instanceof GrandChildClass); |
88 }); | 97 }); |
89 | 98 |
90 QUnit.test('should override parent property and method', function(assert) { | 99 QUnit.test('should override parent property and method', function(assert) { |
91 var child = new ChildClass(); | 100 var child = new ChildClass(); |
92 assert.equal(child.name, 'child'); | 101 assert.equal(child.name, 'child'); |
93 assert.equal(child.overrideMethod(), 'overrideMethod - child'); | 102 assert.equal(child.overrideMethod('123'), 'overrideMethod - child - 123'); |
94 assert.equal(child.childOnly, 'childOnly'); | 103 assert.equal(child.childOnly, 'childOnly'); |
95 assert.equal(child.childMethod(), 'childMethod'); | 104 assert.equal(child.childMethod(), 'childMethod'); |
96 }); | 105 }); |
97 | 106 |
98 QUnit.test('should works on an inheritance chain', function(assert) { | 107 QUnit.test('should work on an inheritance chain', function(assert) { |
99 var grandChild = new GrandChildClass(); | 108 var grandChild = new GrandChildClass(); |
100 assert.equal(grandChild.name, 'grandChild'); | 109 assert.equal(grandChild.name, 'grandChild'); |
101 assert.equal(grandChild.overrideMethod(), 'overrideMethod - grandChild'); | 110 assert.equal(grandChild.overrideMethod('246'), |
| 111 'overrideMethod - grandChild - 246'); |
102 assert.equal(grandChild.childOnly, 'childOnly'); | 112 assert.equal(grandChild.childOnly, 'childOnly'); |
103 assert.equal(grandChild.childMethod(), 'childMethod'); | 113 assert.equal(grandChild.childMethod(), 'childMethod'); |
104 assert.equal(grandChild.parentOnly, 'parentOnly'); | 114 assert.equal(grandChild.parentOnly, 'parentOnly'); |
105 assert.equal(grandChild.parentMethod(), 'parentMethod'); | 115 assert.equal(grandChild.parentMethod(), 'parentMethod'); |
106 }); | 116 }); |
107 | 117 |
108 })(); | 118 QUnit.test('should be able to access parent class methods', function(assert) { |
| 119 var grandChild = new GrandChildClass(); |
| 120 |
| 121 assert.equal(grandChild.overrideMethod('789'), |
| 122 'overrideMethod - grandChild - 789'); |
| 123 |
| 124 var childMethod = ChildClass.prototype.overrideMethod.call(grandChild, '81'); |
| 125 assert.equal(childMethod, 'overrideMethod - child - 81'); |
| 126 |
| 127 var parentMethod = ParentClass.prototype.overrideMethod.call(grandChild, '4'); |
| 128 assert.equal(parentMethod, 'overrideMethod - parent - 4'); |
| 129 }); |
| 130 |
| 131 })(); |
OLD | NEW |