mediawiki-extensions-Visual.../modules/ve/test/ve.test.js
Timo Tijhof ab7d6bf082 Documentation & clean up
* Commands for Sublime:

  Find*: "(\* @[a-z]+) ([^{].*) \{(.*)\}"
  Replace: "$1 {$3} $2"

  Save all && Close all

  Find: " function("
  Replace: " function ("

  Save all && Close all

  Find: "Intialization"
  Replace: "Initialization"

  Save all && Close all

* Consistent use of types (documented in CODING.rm):
  - Merged {Integer} into {Number}.
  - Merged {DOM Node} into {DOMElement}.

* Remove work-around /*jshint newcap: false */ from ve.js
  Calling Object() as a function to to use the internal
  toObject no longer throws a newcap warning in JSHint.
  It only does that normal functions now .

  (e.g. var a = Cap(); or var a = new uncap();)

* Add missing annotations (@static, @method, ..).

* Remove unused variables

* Remove null-assignments to variables that should just be
  undefined. There's a few variables explicitly set to null
  whereas they are set a few lines under and not used otherwise
  (e.g. 'tx' in ve.ce.Surface.prototype.onPaste)

Change-Id: I0721a08f8ecd93c25595aedaa1aadb0e08b83799
2012-09-17 16:02:52 +02:00

267 lines
5.4 KiB
JavaScript

/**
* VisualEditor Base method tests.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
QUnit.module( 've' );
/* Tests */
QUnit.test( 'getHash: Basic usage', 5, function ( assert ) {
var tmp, hash, objects;
objects = {};
objects['a-z literal'] = {
a: 1,
b: 1,
c: 1
};
objects['z-a literal'] = {
c: 1,
b: 1,
a: 1
};
tmp = {};
objects['a-z augmented'] = tmp;
tmp.a = 1;
tmp.b = 1;
tmp.c = 1;
tmp = {};
objects['z-a augmented'] = tmp;
tmp.c = 1;
tmp.b = 1;
tmp.a = 1;
hash = '{"a":1,"b":1,"c":1}';
$.each( objects, function ( key, val ) {
assert.equal(
ve.getHash( val ),
hash,
'Similar enough objects have the same hash, regardless of "property order"'
);
});
// .. and that something completely different is in face different
// (just incase getHash is broken and always returns the same)
assert.notEqual(
ve.getHash( { a: 2, b: 2 } ),
hash,
'A different object has a different hash'
);
} );
QUnit.test( 'getHash: Complex usage', 4, function ( assert ) {
var obj, hash, frame;
obj = {
a: 1,
b: 1,
c: 1,
// Nested array
d: ['x', 'y', 'z'],
e: {
a: 2,
b: 2,
c: 2
}
};
assert.equal(
ve.getHash( obj ),
'{"a":1,"b":1,"c":1,"d":["x","y","z"],"e":{"a":2,"b":2,"c":2}}',
'Object with nested array and circular reference'
);
// Include a circular reference
obj.f = obj;
assert.throws( function () {
ve.getHash( obj );
}, 'Throw exceptions for objects with cirular refences ' );
function Foo() {
this.a = 1;
this.c = 3;
this.b = 2;
}
hash = '{"a":1,"b":2,"c":3}';
assert.equal(
ve.getHash( new Foo() ),
hash,
// This was previously broken when we used .constructor === Object
// ve.getHash.keySortReplacer, because although instances of Foo
// do inherit from Object (( new Foo() ) instanceof Object === true),
// direct comparison would return false.
'Treat objects constructed by a function as well'
);
frame = document.createElement( 'frame' );
frame.src = 'about:blank';
$( '#qunit-fixture' ).append( frame );
obj = new frame.contentWindow.Object();
obj.c = 3;
obj.b = 2;
obj.a = 1;
assert.equal(
ve.getHash( obj ),
hash,
// This was previously broken when we used comparison with "Object" in
// ve.getHash.keySortReplacer, because they are an instance of the other
// window's "Object".
'Treat objects constructed by a another window as well'
);
} );
QUnit.test( 'getObjectValues', 6, function ( assert ) {
var tmp;
assert.deepEqual(
ve.getObjectValues( { a: 1, b: 2, c: 3, foo: 'bar' } ),
[ 1, 2, 3, 'bar' ],
'Simple object with numbers and strings as values'
);
assert.deepEqual(
ve.getObjectValues( [ 1, 2, 3, 'bar' ] ),
[ 1, 2, 3, 'bar' ],
'Simple array with numbers and strings as values'
);
tmp = function () {
this.isTest = true;
return this;
};
tmp.a = 'foo';
tmp.b = 'bar';
assert.deepEqual(
ve.getObjectValues( tmp ),
['foo', 'bar'],
'Function with static members'
);
assert.throws(
function () {
ve.getObjectValues( 'hello' );
},
TypeError,
'Throw exception for non-object (string)'
);
assert.throws(
function () {
ve.getObjectValues( 123 );
},
TypeError,
'Throw exception for non-object (number)'
);
assert.throws(
function () {
ve.getObjectValues( null );
},
TypeError,
'Throw exception for non-object (null)'
);
} );
QUnit.test( 'copyArray', 6, function ( assert ) {
var simpleArray = [ 'foo', 3 ],
withObj = [ { 'bar': 'baz', 'quux': 3 }, 5 ],
nestedArray = [ [ 'a', 'b' ], [ 1, 3, 4 ] ],
sparseArray = [ 'a', undefined, undefined, 'b' ],
withSparseArray = [ [ 'a', undefined, undefined, 'b' ] ],
Cloneable = function ( p ) {
this.p = p;
};
Cloneable.prototype.clone = function () {
return new Cloneable( this.p + '-clone' );
};
assert.deepEqual(
ve.copyArray( simpleArray ),
simpleArray,
'Simple array'
);
assert.deepEqual(
ve.copyArray( withObj ),
withObj,
'Array containing object'
);
assert.deepEqual(
ve.copyArray( [ new Cloneable( 'bar' ) ] ),
[ new Cloneable( 'bar-clone' ) ],
'Use the .clone() method if available'
);
assert.deepEqual(
ve.copyArray( nestedArray ),
nestedArray,
'Nested array'
);
assert.deepEqual(
ve.copyArray( sparseArray ),
sparseArray,
'Sparse array'
);
assert.deepEqual(
ve.copyArray( withSparseArray ),
withSparseArray,
'Nested sparse array'
);
} );
QUnit.test( 'copyObject', 6, function ( assert ) {
var simpleObj = { 'foo': 'bar', 'baz': 3 },
nestedObj = { 'foo': { 'bar': 'baz', 'quux': 3 }, 'whee': 5 },
withArray = { 'foo': [ 'a', 'b' ], 'bar': [ 1, 3, 4 ] },
withSparseArray = { 'foo': [ 'a', undefined, undefined, 'b' ] },
Cloneable = function ( p ) {
this.p = p;
};
Cloneable.prototype.clone = function () { return new Cloneable( this.p + '-clone' ); };
assert.deepEqual(
ve.copyObject( simpleObj ),
simpleObj,
'Simple object'
);
assert.deepEqual(
ve.copyObject( nestedObj ),
nestedObj,
'Nested object'
);
assert.deepEqual(
ve.copyObject( new Cloneable( 'foo' ) ),
new Cloneable( 'foo-clone' ),
'Cloneable object'
);
assert.deepEqual(
ve.copyObject( { 'foo': new Cloneable( 'bar' ) } ),
{ 'foo': new Cloneable( 'bar-clone' ) },
'Object containing object'
);
assert.deepEqual(
ve.copyObject( withArray ),
withArray,
'Object with array'
);
assert.deepEqual(
ve.copyObject( withSparseArray ),
withSparseArray,
'Object with sparse array'
);
} );