addon-sdk/source/test/traits/descriptor-tests.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 var Trait = require("sdk/deprecated/light-traits").Trait;
michael@0 8 var utils = require("./utils");
michael@0 9 var Data = utils.Data;
michael@0 10 var Method = utils.Method;
michael@0 11 var Accessor = utils.Accessor;
michael@0 12 var Required = utils.Required;
michael@0 13 var Conflict = utils.Conflict;
michael@0 14
michael@0 15 function method() {}
michael@0 16
michael@0 17 exports.Assert = require("./assert").Assert
michael@0 18 exports["test simple composition"] = function(assert) {
michael@0 19 var actual = Trait.compose(
michael@0 20 Trait({ a: 0, b: 1 }),
michael@0 21 { c: { value: 2 }, d: { value: method, enumerable: true } }
michael@0 22 );
michael@0 23
michael@0 24 var expected = {
michael@0 25 a: Data(0),
michael@0 26 b: Data(1),
michael@0 27 c: Data(2, false, false, false),
michael@0 28 d: Method(method, true, false, false)
michael@0 29 };
michael@0 30
michael@0 31 assert.equalTraits(actual, expected);
michael@0 32 };
michael@0 33
michael@0 34 exports["test composition with conflict"] = function(assert) {
michael@0 35 var actual = Trait.compose(
michael@0 36 Trait({ a: 0, b: 1 }),
michael@0 37 {
michael@0 38 a: {
michael@0 39 value: 2,
michael@0 40 writable: true,
michael@0 41 configurable: true,
michael@0 42 enumerable: true
michael@0 43 },
michael@0 44 c: {
michael@0 45 value: method,
michael@0 46 configurable: true
michael@0 47 }
michael@0 48 }
michael@0 49 );
michael@0 50
michael@0 51 var expected = {
michael@0 52 a: Conflict("a"),
michael@0 53 b: Data(1),
michael@0 54 c: Method(method, false, true, false)
michael@0 55 };
michael@0 56
michael@0 57 assert.equalTraits(actual, expected);
michael@0 58 };
michael@0 59
michael@0 60 exports["test identical props does not cause conflict"] = function(assert) {
michael@0 61 var actual = Trait.compose(
michael@0 62 {
michael@0 63 a: {
michael@0 64 value: 0,
michael@0 65 writable: true,
michael@0 66 configurable: true,
michael@0 67 enumerable: true
michael@0 68 },
michael@0 69 b: {
michael@0 70 value: 1
michael@0 71 }
michael@0 72 },
michael@0 73 Trait({
michael@0 74 a: 0,
michael@0 75 c: method
michael@0 76 })
michael@0 77 );
michael@0 78
michael@0 79 var expected = {
michael@0 80 a: Data(0),
michael@0 81 b: Data(1, false, false, false),
michael@0 82 c: Method(method)
michael@0 83 }
michael@0 84
michael@0 85 assert.equalTraits(actual, expected);
michael@0 86 };
michael@0 87
michael@0 88 exports["test composition with identical required props"] = function(assert) {
michael@0 89 var actual = Trait.compose(
michael@0 90 Trait({ a: Trait.required, b: 1 }),
michael@0 91 { a: { required: true }, c: { value: method } }
michael@0 92 );
michael@0 93
michael@0 94 var expected = {
michael@0 95 a: Required(),
michael@0 96 b: Data(1),
michael@0 97 c: Method(method, false, false, false)
michael@0 98 };
michael@0 99
michael@0 100 assert.equalTraits(actual, expected);
michael@0 101 };
michael@0 102
michael@0 103 exports["test composition satisfying a required prop"] = function(assert) {
michael@0 104 var actual = Trait.compose(
michael@0 105 Trait({ a: Trait.required, b: 1 }),
michael@0 106 { a: { value: method, enumerable: true } }
michael@0 107 );
michael@0 108
michael@0 109 var expected = {
michael@0 110 a: Method(method, true, false, false),
michael@0 111 b: Data(1)
michael@0 112 };
michael@0 113
michael@0 114 assert.equalTraits(actual, expected);
michael@0 115 };
michael@0 116
michael@0 117 exports["test compose is neutral wrt conflicts"] = function(assert) {
michael@0 118 var actual = Trait.compose(
michael@0 119 Trait({ a: { value: 1 } }, Trait({ a: 2 })),
michael@0 120 { b: { value: 0, writable: true, configurable: true, enumerable: false } }
michael@0 121 );
michael@0 122
michael@0 123 var expected = { a: Conflict("a"), b: Data(0, false) };
michael@0 124
michael@0 125 assert.equalTraits(actual, expected);
michael@0 126 };
michael@0 127
michael@0 128 exports["test conflicting prop overrides Trait.required"] = function(assert) {
michael@0 129 var actual = Trait.compose(
michael@0 130 Trait.compose(
michael@0 131 Trait({ a: 1 }),
michael@0 132 { a: { value: 2 } }
michael@0 133 ),
michael@0 134 { a: { value: Trait.required } }
michael@0 135 );
michael@0 136
michael@0 137 var expected = { a: Conflict("a") };
michael@0 138
michael@0 139 assert.equalTraits(actual, expected);
michael@0 140 };
michael@0 141
michael@0 142 exports["test compose is commutative"] = function(assert) {
michael@0 143 var actual = Trait.compose(
michael@0 144 Trait({ a: 0, b: 1 }),
michael@0 145 { c: { value: 2 }, d: { value: method } }
michael@0 146 );
michael@0 147
michael@0 148 var expected = Trait.compose(
michael@0 149 { c: { value: 2 }, d: { value: method } },
michael@0 150 Trait({ a: 0, b: 1 })
michael@0 151 );
michael@0 152
michael@0 153 assert.equalTraits(actual, expected);
michael@0 154 }
michael@0 155
michael@0 156 exports["test compose is commutative, also for required/conflicting props"] = function(assert) {
michael@0 157 var actual = Trait.compose(
michael@0 158 {
michael@0 159 a: { value: 0 },
michael@0 160 b: { value: 1 },
michael@0 161 c: { value: 3 },
michael@0 162 e: { value: Trait.required }
michael@0 163 },
michael@0 164 {
michael@0 165 c: { value: 2 },
michael@0 166 d: { get: method }
michael@0 167 }
michael@0 168 );
michael@0 169
michael@0 170 var expected = Trait.compose(
michael@0 171 Trait({ c: 3 }),
michael@0 172 {
michael@0 173 c: { value: 2 },
michael@0 174 d: { get: method },
michael@0 175 a: { value: 0 },
michael@0 176 b: { value: 1 },
michael@0 177 e: { value: Trait.required },
michael@0 178 }
michael@0 179 );
michael@0 180
michael@0 181 assert.equalTraits(actual, expected);
michael@0 182 };
michael@0 183
michael@0 184 exports["test compose is associative"] = function(assert) {
michael@0 185 var actual = Trait.compose(
michael@0 186 {
michael@0 187 a: { value: 0 },
michael@0 188 b: { value: 1 },
michael@0 189 c: { value: 3 },
michael@0 190 d: { value: Trait.required }
michael@0 191 },
michael@0 192 Trait.compose(
michael@0 193 { c: { value: 3 }, d: { value: Trait.required } },
michael@0 194 { c: { value: 2 }, d: { value: method }, e: { value: "foo" } }
michael@0 195 )
michael@0 196 );
michael@0 197
michael@0 198 var expected = Trait.compose(
michael@0 199 Trait.compose(
michael@0 200 {
michael@0 201 a: { value: 0 },
michael@0 202 b: { value: 1 },
michael@0 203 c: { value: 3 },
michael@0 204 d: { value: Trait.required }
michael@0 205 },
michael@0 206 {
michael@0 207 c: { value: 3 },
michael@0 208 d: { value: Trait.required }
michael@0 209 }
michael@0 210 ),
michael@0 211 {
michael@0 212 c: { value: 2 },
michael@0 213 d: { value: method },
michael@0 214 e: { value: "foo" }
michael@0 215 }
michael@0 216 );
michael@0 217
michael@0 218 assert.equalTraits(actual, expected);
michael@0 219 };
michael@0 220
michael@0 221 exports["test diamond import of same prop do not conflict"] = function(assert) {
michael@0 222 var actual = Trait.compose(
michael@0 223 Trait.compose(
michael@0 224 { b: { value: 2 } },
michael@0 225 { a: { value: 1, enumerable: true, configurable: true, writable: true } }
michael@0 226 ),
michael@0 227 Trait.compose(
michael@0 228 { c: { value: 3 } },
michael@0 229 Trait({ a: 1 })
michael@0 230 ),
michael@0 231 Trait({ d: 4 })
michael@0 232 );
michael@0 233
michael@0 234 var expected = {
michael@0 235 a: Data(1),
michael@0 236 b: Data(2, false, false, false),
michael@0 237 c: Data(3, false, false, false),
michael@0 238 d: Data(4)
michael@0 239 };
michael@0 240
michael@0 241 assert.equalTraits(actual, expected);
michael@0 242 };
michael@0 243
michael@0 244 exports["test create simple"] = function(assert) {
michael@0 245 var o1 = Trait.compose(
michael@0 246 Trait({ a: 1 }),
michael@0 247 {
michael@0 248 b: {
michael@0 249 value: function() {
michael@0 250 return this.a;
michael@0 251 }
michael@0 252 }
michael@0 253 }
michael@0 254 ).create(Object.prototype);
michael@0 255
michael@0 256 assert.equal(Object.getPrototypeOf(o1), Object.prototype, "o1 prototype");
michael@0 257 assert.equal(1, o1.a, "o1.a");
michael@0 258 assert.equal(1, o1.b(), "o1.b()");
michael@0 259 assert.equal(Object.keys(o1).length, 1, "Object.keys(o1).length === 2");
michael@0 260 };
michael@0 261
michael@0 262 exports["test create with Array.prototype"] = function(assert) {
michael@0 263 var o2 = Trait.compose({}, {}).create(Array.prototype);
michael@0 264 assert.equal(Object.getPrototypeOf(o2), Array.prototype, "o2 prototype");
michael@0 265 };
michael@0 266
michael@0 267 exports["test exception for incomplete required properties"] = function(assert) {
michael@0 268 assert.throws(function() {
michael@0 269 Trait({ foo: Trait.required }).create(Object.prototype)
michael@0 270 }, /Missing required property: `foo`/, "required prop error");
michael@0 271 }
michael@0 272
michael@0 273 exports["test exception for unresolved conflicts"] = function(assert) {
michael@0 274 assert.throws(function() {
michael@0 275 Trait(Trait({ a: 0 }), Trait({ a: 1 })).create({})
michael@0 276 }, /Remaining conflicting property: `a`/, "conflicting prop error");
michael@0 277 }
michael@0 278
michael@0 279 exports["test conflicting properties are present"] = function(assert) {
michael@0 280 var o5 = Object.create(Object.prototype, Trait.compose(
michael@0 281 { a: { value: 0 } },
michael@0 282 { a: { value: 1 } }
michael@0 283 ));
michael@0 284
michael@0 285 assert.ok("a" in o5, "conflicting property present");
michael@0 286 assert.throws(function() {
michael@0 287 o5.a
michael@0 288 }, /Remaining conflicting property: `a`/, "conflicting prop access error");
michael@0 289 };
michael@0 290
michael@0 291 exports["test diamond with conflicts"] = function(assert) {
michael@0 292 function makeT1(x) {
michael@0 293 return {
michael@0 294 m: {
michael@0 295 value: function() {
michael@0 296 return x
michael@0 297 }
michael@0 298 }
michael@0 299 };
michael@0 300 };
michael@0 301
michael@0 302 function makeT2(x) {
michael@0 303 return Trait.compose(
michael@0 304 Trait({ t2: "foo" }),
michael@0 305 makeT1(x)
michael@0 306 );
michael@0 307 };
michael@0 308
michael@0 309 function makeT3(x) {
michael@0 310 return Trait.compose(
michael@0 311 {
michael@0 312 t3: { value: "bar" }
michael@0 313 },
michael@0 314 makeT1(x)
michael@0 315 );
michael@0 316 };
michael@0 317
michael@0 318 var T4 = Trait.compose(makeT2(5), makeT3(5));
michael@0 319
michael@0 320 assert.throws(function() {
michael@0 321 T4.create(Object.prototype);
michael@0 322 }, /Remaining conflicting property: `m`/, "diamond prop conflict");
michael@0 323 };
michael@0 324
michael@0 325 exports["test providing requirements through proto"] = function(assert) {
michael@0 326 var t = Trait.compose(
michael@0 327 {},
michael@0 328 { required: { required: true } }
michael@0 329 ).create({ required: "test" });
michael@0 330
michael@0 331 assert.equal(t.required, "test", "property from proto is inherited");
michael@0 332 };
michael@0 333
michael@0 334 if (module == require.main)
michael@0 335 require("test").run(exports);

mercurial