| 1 |
// Copyright 2017 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.module('goog.delegate.DelegateRegistryTest'); |
| 16 |
goog.setTestOnly(); |
| 17 |
|
| 18 |
const DelegateRegistry = goog.require('goog.delegate.DelegateRegistry'); |
| 19 |
const testSuite = goog.require('goog.testing.testSuite'); |
| 20 |
const {zip} = goog.require('goog.array'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
/** Always throws. */ |
| 24 |
const THROW = () => { |
| 25 |
throw new Error('Unexpected call'); |
| 26 |
}; |
| 27 |
|
| 28 |
class Foo { |
| 29 |
constructor(arg = undefined) { |
| 30 |
this.arg = arg; |
| 31 |
} |
| 32 |
toString() { |
| 33 |
return `Foo(${this.arg !== undefined ? this.arg : ''})`; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
class Bar { |
| 38 |
constructor(arg = undefined) { |
| 39 |
this.arg = arg; |
| 40 |
} |
| 41 |
toString() { |
| 42 |
return `Bar(${this.arg !== undefined ? this.arg : ''})`; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
testSuite({ |
| 47 |
shouldRunTests() { |
| 48 |
return typeof Array.prototype.map == 'function'; |
| 49 |
}, |
| 50 |
|
| 51 |
testBasic_registerNothing() { |
| 52 |
/** @type {!DelegateRegistry<!Foo>} */ |
| 53 |
const registry = new DelegateRegistry(); |
| 54 |
|
| 55 |
assertArrayEquals([], registry.delegates(THROW)); |
| 56 |
assertUndefined(registry.delegate(THROW)); |
| 57 |
}, |
| 58 |
|
| 59 |
testBasic_registerClass() { |
| 60 |
/** @type {!DelegateRegistry<!Foo>} */ |
| 61 |
const registry = new DelegateRegistry().expectAtMostOneDelegate(); |
| 62 |
|
| 63 |
registry.registerClass(Foo); |
| 64 |
|
| 65 |
const delegates = registry.delegates(); |
| 66 |
assertEquals('Foo()', delegates.join(' ')); |
| 67 |
// Make sure we get a new instance. |
| 68 |
assertNotEquals(delegates[0], registry.delegates()[0]); |
| 69 |
|
| 70 |
assertEquals('Foo(42)', registry.delegates(ctor => new ctor(42)).join(' ')); |
| 71 |
|
| 72 |
const delegate = registry.delegate(); |
| 73 |
assertEquals('Foo()', delegate.toString()); |
| 74 |
assertNotEquals(delegates[0], delegate); |
| 75 |
|
| 76 |
assertEquals('Foo(42)', registry.delegate(ctor => new ctor(42)).toString()); |
| 77 |
}, |
| 78 |
|
| 79 |
testBasic_registerInstance() { |
| 80 |
/** @type {!DelegateRegistry<!Foo>} */ |
| 81 |
const registry = new DelegateRegistry(); |
| 82 |
const delegate = new Foo(23); |
| 83 |
|
| 84 |
registry.registerInstance(delegate); |
| 85 |
assertArrayEquals([delegate], registry.delegates(THROW)); |
| 86 |
assertEquals(delegate, registry.delegate(THROW)); |
| 87 |
}, |
| 88 |
|
| 89 |
testBasic_registerMultiple() { |
| 90 |
/** @type {!DelegateRegistry<!Foo|!Bar>} */ |
| 91 |
const registry = new DelegateRegistry(); |
| 92 |
|
| 93 |
registry.registerClass(Bar); |
| 94 |
registry.registerInstance(new Foo(23)); |
| 95 |
registry.registerClass(Foo); |
| 96 |
|
| 97 |
const delegates = registry.delegates(); |
| 98 |
assertEquals('Bar() Foo(23) Foo()', delegates.join(' ')); |
| 99 |
const delegates2 = registry.delegates(); |
| 100 |
assertArrayEquals( |
| 101 |
[false, true, false], |
| 102 |
zip(delegates, delegates2).map(([a, b]) => a == b)); |
| 103 |
|
| 104 |
assertEquals( |
| 105 |
'Bar(42) Foo(23) Foo(42)', |
| 106 |
registry.delegates(ctor => new ctor(42)).join(' ')); |
| 107 |
|
| 108 |
assertEquals('Bar()', registry.delegate().toString()); |
| 109 |
assertNotEquals(delegates[0], registry.delegate()); |
| 110 |
}, |
| 111 |
|
| 112 |
testPrioritized_registerClass() { |
| 113 |
/** @type {!DelegateRegistry.Prioritized<string>} */ |
| 114 |
const registry = new DelegateRegistry.Prioritized(); |
| 115 |
registry.registerClass(Foo, 0); |
| 116 |
registry.registerClass(Bar, 10); |
| 117 |
assertEquals( |
| 118 |
'Bar(2) Foo(2)', registry.delegates(ctor => new ctor(2)).join(' ')); |
| 119 |
}, |
| 120 |
|
| 121 |
testPrioritized_registerInstance() { |
| 122 |
/** @type {!DelegateRegistry.Prioritized<string>} */ |
| 123 |
const registry = new DelegateRegistry.Prioritized(); |
| 124 |
registry.registerInstance('o', 10); |
| 125 |
registry.registerInstance('l', 20); |
| 126 |
registry.registerInstance('r', -5); |
| 127 |
registry.registerInstance('s', 5); |
| 128 |
registry.registerInstance('c', 25); |
| 129 |
registry.registerInstance('e', -10); |
| 130 |
registry.registerInstance('u', 0); |
| 131 |
assertEquals('closure', registry.delegates(THROW).join('')); |
| 132 |
assertEquals('c', registry.delegate(THROW)); |
| 133 |
|
| 134 |
// Duplicate priority |
| 135 |
assertThrows(() => registry.registerInstance('x', 5)); |
| 136 |
}, |
| 137 |
|
| 138 |
testAllowLateRegistration() { |
| 139 |
/** @type {!DelegateRegistry<!Foo|!Bar>} */ |
| 140 |
const registry = new DelegateRegistry(); |
| 141 |
assertUndefined(registry.delegate(THROW)); |
| 142 |
|
| 143 |
assertThrows(() => registry.registerClass(Bar)); |
| 144 |
assertThrows(() => registry.registerInstance(new Bar())); |
| 145 |
|
| 146 |
assertEquals(registry, registry.allowLateRegistration()); |
| 147 |
|
| 148 |
registry.registerClass(Foo); |
| 149 |
registry.registerInstance(new Foo(23)); |
| 150 |
|
| 151 |
assertEquals('Foo() Foo(23)', registry.delegates().join(' ')); |
| 152 |
}, |
| 153 |
|
| 154 |
testCacheInstantiation() { |
| 155 |
/** @type {!DelegateRegistry<!Foo>} */ |
| 156 |
const registry = new DelegateRegistry(); |
| 157 |
registry.registerClass(Foo); |
| 158 |
registry.registerInstance(new Foo(5)); |
| 159 |
const d1 = registry.delegates(ctor => new ctor(42)); |
| 160 |
assertEquals('Foo(42) Foo(5)', d1.join(' ')); |
| 161 |
const d2 = registry.delegates(ctor => new ctor(23)); |
| 162 |
assertEquals('Foo(23) Foo(5)', d2.join(' ')); |
| 163 |
assertEquals(d1[1], d2[1]); |
| 164 |
|
| 165 |
assertEquals(registry, registry.cacheInstantiation()); |
| 166 |
const d3 = registry.delegates(ctor => new ctor(56)); |
| 167 |
assertEquals('Foo(56) Foo(5)', d3.join(' ')); |
| 168 |
assertArrayEquals(d3, registry.delegates(THROW)); |
| 169 |
}, |
| 170 |
|
| 171 |
testCacheInstantiation_withLateRegistration() { |
| 172 |
/** @type {!DelegateRegistry<!Foo>} */ |
| 173 |
const registry = |
| 174 |
new DelegateRegistry().cacheInstantiation().allowLateRegistration(); |
| 175 |
registry.registerClass(Foo); |
| 176 |
registry.registerClass(Foo); |
| 177 |
assertEquals('Foo(42)', registry.delegate(ctor => new ctor(42)).toString()); |
| 178 |
assertEquals( |
| 179 |
'Foo(42) Foo(3)', registry.delegates(ctor => new ctor(3)).join(' ')); |
| 180 |
registry.registerClass(Foo); |
| 181 |
assertEquals( |
| 182 |
'Foo(42) Foo(3) Foo(99)', |
| 183 |
registry.delegates(ctor => new ctor(99)).join(' ')); |
| 184 |
}, |
| 185 |
|
| 186 |
testExpectAtMostOneDelegate() { |
| 187 |
/** @type {!DelegateRegistry<!Foo>} */ |
| 188 |
const registry = new DelegateRegistry(); |
| 189 |
assertEquals(registry, registry.expectAtMostOneDelegate()); |
| 190 |
registry.registerClass(Foo); |
| 191 |
assertThrows(() => registry.registerClass(Foo)); |
| 192 |
}, |
| 193 |
}); |