mediawiki-extensions-Visual.../tests/ve2/ve.NodeFactory.test.js
Catrope 93d6adb5f2 Add an isWrapped property and make getOuterLength use it
This gets rid of the length == outerLength-2 hack in getDataFromNode()
and will make it easier to implement similar logic in selectNodes()

Change-Id: I1294350b67ca3eefde2b7fe9fea0bc6d8b90f772
2012-05-07 12:00:07 -07:00

81 lines
2.1 KiB
JavaScript

module( 've.NodeFactory' );
/* Stubs */
ve.NodeFactoryNodeStub = function( a, b ) {
this.a = a;
this.b = b;
};
ve.NodeFactoryNodeStub.rules = {
'canHaveChildren': false,
'canHaveGrandchildren': false,
'isWrapped': true
};
/* Tests */
test( 'register', 1, function() {
var factory = new ve.NodeFactory();
raises( function() {
factory.register( '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'
);
} );
test( 'create', 2, function() {
var factory = new ve.NodeFactory();
raises( function() {
factory.create( 'stub', 23, { 'bar': 'baz' } );
},
/^Unknown node type: stub$/,
'throws an exception when trying to create a node of an unregistered type'
);
factory.register( 'stub', ve.NodeFactoryNodeStub );
deepEqual(
factory.create( 'stub', 16, { 'baz': 'quux' } ),
new ve.NodeFactoryNodeStub( 16, { 'baz': 'quux' } ),
'creates nodes of a registered type and passes through arguments'
);
} );
test( 'lookup', 1, function() {
var factory = new ve.NodeFactory();
factory.register( 'stub', ve.NodeFactoryNodeStub );
strictEqual( factory.lookup( 'stub' ), ve.NodeFactoryNodeStub );
} );
test( 'canNodeHaveChildren', 2, function() {
var factory = new ve.NodeFactory();
raises( function() {
factory.create( 'stub', 23, { 'bar': 'baz' } );
},
/^Unknown node type: stub$/,
'throws an exception when checking if a node of an unregistered type can have children'
);
factory.register( 'stub', ve.NodeFactoryNodeStub );
strictEqual(
factory.canNodeHaveChildren( 'stub' ),
false,
'gets child rules for registered nodes'
);
} );
test( 'canNodeHaveGrandchildren', 2, function() {
var factory = new ve.NodeFactory();
raises( function() {
factory.create( 'stub', 23, { 'bar': 'baz' } );
},
/^Unknown node type: stub$/,
'throws an exception when checking if a node of an unregistered type can have grandchildren'
);
factory.register( 'stub', ve.NodeFactoryNodeStub );
strictEqual(
factory.canNodeHaveGrandchildren( 'stub' ),
false,
'gets grandchild rules for registered nodes'
);
} );