Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/62724
Change subject: ve.test: Import K-JS test suite for object utilities ...................................................................... ve.test: Import K-JS test suite for object utilities We've housed these utilities for a while and changed some of them quite a bit. Importing them to avoid regressions and ensure we keep them in sync with our fork of it. Removed references back through `@source`. They are sufficiently different that the reference no longer adds any value. Imported from https://github.com/Krinkle/K-js/blob/v0.1.2/test/K.test.js. Change-Id: I9e71297246b7c248c1f032ba6b6ae1123519f3c1 --- M modules/ve/test/ve.test.js M modules/ve/ve.js 2 files changed, 171 insertions(+), 7 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor refs/changes/24/62724/1 diff --git a/modules/ve/test/ve.test.js b/modules/ve/test/ve.test.js index 2f04beb..b58cd39 100644 --- a/modules/ve/test/ve.test.js +++ b/modules/ve/test/ve.test.js @@ -9,7 +9,74 @@ /* Tests */ -// ve.createObject: Tested upstream (K-js) +// ve.getObjectKeys: Untested (TODO) + +QUnit.test( 'createObject', 4, function ( assert ) { + var foo, bar, fooKeys, barKeys; + + foo = { + a: 'a of foo', + b: 'b of foo' + }; + + bar = ve.createObject(foo); + + // Add an own property, hiding the inherited one. + bar.b = 'b of bar'; + + // Add an own property, hiding an inherited property + // that will be added later + bar.c = 'c of bar'; + + // Add more properties to the origin object, + // should be visible in the inheriting object. + foo.c = 'c of foo'; + foo.d = 'd of foo'; + + // Different that only one of each has + foo.foo = true; + bar.bar = true; + + assert.deepEqual( + foo, + { + a: 'a of foo', + b: 'b of foo', + c: 'c of foo', + d: 'd of foo', + foo: true + }, + 'Foo has expected properties' + ); + + assert.deepEqual( + bar, + { + a: 'a of foo', + b: 'b of bar', + c: 'c of bar', + d: 'd of foo', + foo: true, + bar: true + }, + 'Bar has expected properties' + ); + + fooKeys = ve.getObjectKeys( foo ); + barKeys = ve.getObjectKeys( bar ); + + assert.deepEqual( + fooKeys, + ['a', 'b', 'c', 'd', 'foo'], + 'Own properties of foo' + ); + + assert.deepEqual( + barKeys, + ['b', 'c', 'bar'], + 'Own properties of bar' + ); +}); QUnit.test( 'inheritClass', 18, function ( assert ) { var foo, bar; @@ -116,7 +183,47 @@ assert.equal( bar.eFn(), 'proto of Foo', 'inheritance is live (adding a new method deeper in the chain)' ); }); -// ve.mixinClass: Tested upstream (K-js) +QUnit.test( 'mixinClass', 4, function ( assert ) { + var quux; + + function Foo() {} + Foo.prototype.aFn = function () { + return 'proto of Foo'; + }; + + function Bar() {} + // constructorInherit makes the 'constructor' + // property an own property when it restores it. + ve.inheritClass( Bar, Foo ); + Bar.prototype.bFn = function () { + return 'mixin of Bar'; + }; + + function Quux() {} + ve.mixinClass( Quux, Bar ); + + assert.strictEqual( + Quux.prototype.aFn, + undefined, + 'mixin inheritance is not copied over' + ); + + assert.strictEqual( + Quux.prototype.constructor, + Quux, + 'constructor property skipped' + ); + + assert.strictEqual( + Quux.prototype.hasOwnProperty( 'bFn' ), + true, + 'mixin properties are now own properties, not inherited' + ); + + quux = new Quux(); + + assert.equal( quux.bFn(), 'mixin of Bar', 'mixin method works as expected' ); +} ); QUnit.test( 'isMixedIn', 11, function ( assert ) { function Foo () {} @@ -143,7 +250,68 @@ assert.strictEqual( ve.isMixedIn( b, Quux ), false, 'b does not mixin Quux' ); } ); -// ve.cloneObject: Tested upstream (K-js) +QUnit.test( 'cloneObject', 4, function ( assert ) { + var myfoo, myfooClone, expected; + + function Foo( x ) { + this.x = x; + } + Foo.prototype.x = 'default'; + Foo.prototype.aFn = function () { + return 'proto of Foo'; + }; + + myfoo = new Foo( 10 ); + myfooClone = ve.cloneObject( myfoo ); + + assert.notStrictEqual( myfoo, myfooClone, 'clone is not equal when compared by reference' ); + assert.deepEqual( myfoo, myfooClone, 'clone is equal when recursively compared by value' ); + + expected = { + x: 10, + aFn: 'proto of Foo', + constructor: Foo, + instanceOf: true, + own: { + x: true, + aFn: false, + constructor: false + } + }; + + assert.deepEqual( + { + x: myfoo.x, + aFn: myfoo.aFn(), + constructor: myfoo.constructor, + instanceOf: myfoo instanceof Foo, + own: { + x: myfoo.hasOwnProperty( 'x' ), + aFn: myfoo.hasOwnProperty( 'aFn' ), + constructor: myfoo.hasOwnProperty( 'constructor' ) + } + }, + expected, + 'original looks as expected' + ); + + assert.deepEqual( + { + x: myfooClone.x, + aFn: myfooClone.aFn(), + constructor: myfooClone.constructor, + instanceOf: myfooClone instanceof Foo, + own: { + x: myfooClone.hasOwnProperty( 'x' ), + aFn: myfooClone.hasOwnProperty( 'aFn' ), + constructor: myfoo.hasOwnProperty( 'constructor' ) + } + }, + expected, + 'clone looks as expected' + ); + +} ); // ve.isPlainObject: Tested upstream (jQuery) diff --git a/modules/ve/ve.js b/modules/ve/ve.js index 74d96f0..9d33b64 100644 --- a/modules/ve/ve.js +++ b/modules/ve/ve.js @@ -30,7 +30,6 @@ * * @method * @until ES5: Object.create - * @source <https://github.com/Krinkle/K-js> * @param {Object} origin Object to inherit from * @return {Object} Empty object that inherits from origin */ @@ -73,7 +72,6 @@ * fb instanceof Foo && fb instanceof FooBar && fb instanceof FooBarQuux; * * @method - * @source <https://github.com/Krinkle/K-js> * @param {Function} targetFn * @param {Function} originFn * @throws {Error} If target already inherits from origin @@ -127,7 +125,6 @@ * ve.mixinClass( FooBar, ContextLazyLoad ); * * @method - * @source <https://github.com/Krinkle/K-js> * @param {Function} targetFn * @param {Function} originFn */ @@ -191,7 +188,6 @@ * foo.getAge(); // 22 * * @method - * @source <https://github.com/Krinkle/K-js> * @param {Object} origin * @return {Object} Clone of origin */ -- To view, visit https://gerrit.wikimedia.org/r/62724 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9e71297246b7c248c1f032ba6b6ae1123519f3c1 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Krinkle <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
