mediawiki-extensions-Visual.../tests/ve2/dm/ve.dm.NodeFactory.test.js
Trevor Parscal 1798a478e7 Massive refactor of node trees, generalizing lots of previously dm only stuff
Change-Id: I9dbb7094f86338ea2e725ad29f66e9e4ca2b52ed
2012-05-09 21:11:09 -07:00

87 lines
2.3 KiB
JavaScript

module( 've.dm.NodeFactory' );
/* Stubs */
ve.dm.NodeFactoryNodeStub = function( a, b ) {
this.a = a;
this.b = b;
};
ve.dm.NodeFactoryNodeStub.rules = {
'canHaveChildren': false,
'canHaveGrandchildren': false,
'isWrapped': true,
'childNodeTypes': ['a', 'b'],
'parentNodeTypes': ['c', 'd']
};
/* Tests */
test( 'getChildNodeTypes', 2, function() {
var factory = new ve.dm.NodeFactory();
raises( function() {
factory.create( 'stub', 23, { 'bar': 'baz' } );
},
/^Unknown node type: stub$/,
'throws an exception when getting allowed child nodes of a node of an unregistered type'
);
factory.register( 'stub', ve.dm.NodeFactoryNodeStub );
deepEqual(
factory.getChildNodeTypes( 'stub' ),
['a', 'b'],
'gets child type rules for registered nodes'
);
} );
test( 'getParentNodeTypes', 2, function() {
var factory = new ve.dm.NodeFactory();
raises( function() {
factory.create( 'stub', 23, { 'bar': 'baz' } );
},
/^Unknown node type: stub$/,
'throws an exception when getting allowed parent nodes of a node of an unregistered type'
);
factory.register( 'stub', ve.dm.NodeFactoryNodeStub );
deepEqual(
factory.getParentNodeTypes( 'stub' ),
['c', 'd'],
'gets parent type rules for registered nodes'
);
} );
test( 'canNodeHaveChildren', 2, function() {
var factory = new ve.dm.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.dm.NodeFactoryNodeStub );
strictEqual(
factory.canNodeHaveChildren( 'stub' ),
false,
'gets child rules for registered nodes'
);
} );
test( 'canNodeHaveGrandchildren', 2, function() {
var factory = new ve.dm.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.dm.NodeFactoryNodeStub );
strictEqual(
factory.canNodeHaveGrandchildren( 'stub' ),
false,
'gets grandchild rules for registered nodes'
);
} );
test( 'initialization', 1, function() {
ok( ve.dm.factory instanceof ve.dm.NodeFactory, 'factory is initialized at ve.dm.factory' );
} );