2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor Factory tests.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
module( 've.Factory' );
|
|
|
|
|
|
|
|
/* Stubs */
|
|
|
|
|
|
|
|
ve.FactoryObjectStub = function( a, b ) {
|
|
|
|
this.a = a;
|
|
|
|
this.b = b;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'register', 1, function( assert ) {
|
2012-05-31 23:50:16 +00:00
|
|
|
var factory = new ve.Factory();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.throws(
|
2012-05-31 23:50:16 +00:00
|
|
|
function() {
|
|
|
|
factory.register( 'factory-object-stub', 'not-a-function' );
|
|
|
|
},
|
|
|
|
/^Constructor must be a function, cannot be a string$/,
|
|
|
|
'throws an exception when trying to register a non-function value as a constructor'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'create', 2, function( assert ) {
|
2012-05-31 23:50:16 +00:00
|
|
|
var factory = new ve.Factory();
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.throws(
|
2012-05-31 23:50:16 +00:00
|
|
|
function() {
|
|
|
|
factory.create( 'factory-object-stub', 23, { 'bar': 'baz' } );
|
|
|
|
},
|
|
|
|
/^Unknown object type: factory-object-stub$/,
|
|
|
|
'throws an exception when trying to create a object of an unregistered type'
|
|
|
|
);
|
|
|
|
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.deepEqual(
|
2012-05-31 23:50:16 +00:00
|
|
|
factory.create( 'factory-object-stub', 16, { 'baz': 'quux' } ),
|
|
|
|
new ve.FactoryObjectStub( 16, { 'baz': 'quux' } ),
|
|
|
|
'creates objects of a registered type and passes through arguments'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2012-07-10 19:46:08 +00:00
|
|
|
test( 'lookup', 1, function( assert ) {
|
2012-05-31 23:50:16 +00:00
|
|
|
var factory = new ve.Factory();
|
|
|
|
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
|
2012-07-10 19:46:08 +00:00
|
|
|
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
|
2012-05-31 23:50:16 +00:00
|
|
|
} );
|