mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 18:39:52 +00:00
b4de3ead08
Throwing strings is bad because it doesn't include a lot of important information that an error object does, such as a stack trace or where the error was actually thrown from. ve.Error inherits directly from Error. In the future we may create more specific subclasses and/or do custom stuff. Some interesting reading on the subject: * http://www.devthought.com/2011/12/22/a-string-is-not-an-error/ Change-Id: Ib7c568a1dcb98abac44c6c146e84dde5315b2826
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
/**
|
|
* VisualEditor Factory tests.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
QUnit.module( 've.Factory' );
|
|
|
|
/* Stubs */
|
|
|
|
ve.FactoryObjectStub = function ( a, b ) {
|
|
this.a = a;
|
|
this.b = b;
|
|
};
|
|
|
|
/* Tests */
|
|
|
|
QUnit.test( 'register', 1, function ( assert ) {
|
|
var factory = new ve.Factory();
|
|
assert.throws(
|
|
function () {
|
|
factory.register( 'factory-object-stub', 'not-a-function' );
|
|
},
|
|
ve.Error,
|
|
'throws an exception when trying to register a non-function value as a constructor'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( 'create', 2, function ( assert ) {
|
|
var factory = new ve.Factory();
|
|
assert.throws(
|
|
function () {
|
|
factory.create( 'factory-object-stub', 23, { 'bar': 'baz' } );
|
|
},
|
|
ve.Error,
|
|
'throws an exception when trying to create a object of an unregistered type'
|
|
);
|
|
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
|
|
assert.deepEqual(
|
|
factory.create( 'factory-object-stub', 16, { 'baz': 'quux' } ),
|
|
new ve.FactoryObjectStub( 16, { 'baz': 'quux' } ),
|
|
'creates objects of a registered type and passes through arguments'
|
|
);
|
|
} );
|
|
|
|
QUnit.test( 'lookup', 1, function ( assert ) {
|
|
var factory = new ve.Factory();
|
|
factory.register( 'factory-object-stub', ve.FactoryObjectStub );
|
|
assert.strictEqual( factory.lookup( 'factory-object-stub' ), ve.FactoryObjectStub );
|
|
} );
|