r103175 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103174‎ | r103175 | r103176 >
Date:14:35, 15 November 2011
Author:catrope
Status:deferred
Tags:
Comment:
Rewrite traverseLeafNodes tests using a data provider pattern
Modified paths:
  • /trunk/extensions/VisualEditor/tests/es/es.DocumentNode.test.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/es/es.DocumentNode.test.js
@@ -939,131 +939,141 @@
940940 }
941941 } );
942942
943 -test( 'es.DocumentBranchNode.traverseLeafNodes', 10, function() {
 943+test( 'es.DocumentBranchNode.traverseLeafNodes', 11, function() {
944944 var root3 = es.DocumentModel.newFromPlainObject( esTest.obj );
945945
946 - // Test 1
947 - var realLeaves = [], expectedLeaves = [
948 - root3.children[0],
949 - root3.children[1].children[0].children[0].children[0],
950 - root3.children[1].children[0].children[0].children[1].children[0].children[0],
951 - root3.children[1].children[0].children[0].children[1].children[1].children[0],
952 - root3.children[1].children[0].children[0].children[1].children[2].children[0],
953 - root3.children[2]
 946+ var tests = [
 947+ // Test 1 & 2
 948+ {
 949+ 'node': root3,
 950+ 'output': [
 951+ root3.children[0],
 952+ root3.children[1].children[0].children[0].children[0],
 953+ root3.children[1].children[0].children[0].children[1].children[0].children[0],
 954+ root3.children[1].children[0].children[0].children[1].children[1].children[0],
 955+ root3.children[1].children[0].children[0].children[1].children[2].children[0],
 956+ root3.children[2]
 957+ ],
 958+ 'reverse': true,
 959+ 'desc': 'Traversing the entire document returns all leaf nodes'
 960+ },
 961+ // Test 3 & 4
 962+ {
 963+ 'node': root3,
 964+ 'output': [
 965+ root3.children[0],
 966+ root3.children[1].children[0].children[0].children[0],
 967+ root3.children[1].children[0].children[0].children[1].children[0].children[0]
 968+ ],
 969+ 'reverse': [
 970+ root3.children[2],
 971+ root3.children[1].children[0].children[0].children[1].children[2].children[0],
 972+ root3.children[1].children[0].children[0].children[1].children[1].children[0],
 973+ root3.children[1].children[0].children[0].children[1].children[0].children[0]
 974+ ],
 975+ 'callback': function( node ) {
 976+ if ( node === root3.children[1].children[0].children[0].children[1].children[0].children[0] ) {
 977+ return false;
 978+ }
 979+ },
 980+ 'desc': 'Returning false from the callback stops the traversal'
 981+ },
 982+ // Test 5 & 6
 983+ {
 984+ 'node': root3,
 985+ 'output': [
 986+ root3.children[1].children[0].children[0].children[1].children[1].children[0],
 987+ root3.children[1].children[0].children[0].children[1].children[2].children[0],
 988+ root3.children[2]
 989+ ],
 990+ 'reverse': [
 991+ root3.children[1].children[0].children[0].children[1].children[1].children[0],
 992+ root3.children[1].children[0].children[0].children[1].children[0].children[0],
 993+ root3.children[1].children[0].children[0].children[0],
 994+ root3.children[0]
 995+ ],
 996+ 'from': root3.children[1].children[0].children[0].children[1].children[1].children[0],
 997+ 'desc': 'Starting at a leaf node returns that leaf node and everything after it',
 998+ 'reverseDesc': 'Starting at a leaf node returns that leaf node and everything before it (in reverse)'
 999+ },
 1000+ // Test 7 & 8
 1001+ {
 1002+ 'node': root3,
 1003+ 'output': [
 1004+ root3.children[1].children[0].children[0].children[0],
 1005+ root3.children[1].children[0].children[0].children[1].children[0].children[0],
 1006+ root3.children[1].children[0].children[0].children[1].children[1].children[0],
 1007+ root3.children[1].children[0].children[0].children[1].children[2].children[0],
 1008+ root3.children[2]
 1009+ ],
 1010+ 'reverse': [
 1011+ root3.children[0]
 1012+ ],
 1013+ 'from': root3.children[1],
 1014+ 'desc': 'Starting at a non-leaf node returns all leaf nodes inside and after it',
 1015+ 'reverseDesc': 'Starting at a non-leaf node returns all leaf nodes before it and none inside (in reverse)'
 1016+ },
 1017+ // Test 9 & 10
 1018+ {
 1019+ 'node': root3.children[1],
 1020+ 'output': [
 1021+ root3.children[1].children[0].children[0].children[0],
 1022+ root3.children[1].children[0].children[0].children[1].children[0].children[0],
 1023+ root3.children[1].children[0].children[0].children[1].children[1].children[0],
 1024+ root3.children[1].children[0].children[0].children[1].children[2].children[0]
 1025+ ],
 1026+ 'reverse': true,
 1027+ 'desc': 'Calling traverseLeafNodes() on a non-root node only returns leaf nodes inside that node'
 1028+ },
 1029+ // Test 11
 1030+ {
 1031+ 'node': root3.children[1],
 1032+ 'from': root3.children[2],
 1033+ 'exception': /^from parameter passed to traverseLeafNodes\(\) must be a descendant$/,
 1034+ 'desc': 'Passing a sibling for from results in an exception'
 1035+ }
9541036 ];
955 - root3.traverseLeafNodes( function( node ) { realLeaves.push( node ); } );
956 - ok(
957 - es.compareArrays( realLeaves, expectedLeaves ),
958 - 'Traversing the entire document returns all leaf nodes'
959 - );
960 - // Test 2
961 - realLeaves = [];
962 - expectedLeaves = expectedLeaves.reverse();
963 - root3.traverseLeafNodes( function( node ) { realLeaves.push( node ) }, undefined, true );
964 - ok(
965 - es.compareArrays( realLeaves, expectedLeaves ),
966 - 'Traversing the entire document returns all leaf nodes (in reverse)'
967 - );
9681037
969 - // Test 3
970 - realLeaves = [];
971 - expectedLeaves = [
972 - root3.children[1].children[0].children[0].children[1].children[1].children[0],
973 - root3.children[1].children[0].children[0].children[1].children[2].children[0],
974 - root3.children[2]
975 - ];
976 - root3.traverseLeafNodes( function( node ) { realLeaves.push( node ); }, root3.children[1].children[0].children[0].children[1].children[1].children[0] );
977 - ok(
978 - es.compareArrays( realLeaves, expectedLeaves ),
979 - 'Starting at a leaf node returns that leaf node and everything after it'
980 - );
981 - // Test 4
982 - realLeaves = [];
983 - expectedLeaves = [
984 - root3.children[0],
985 - root3.children[1].children[0].children[0].children[0],
986 - root3.children[1].children[0].children[0].children[1].children[0].children[0],
987 - root3.children[1].children[0].children[0].children[1].children[1].children[0],
988 - ].reverse();
989 - root3.traverseLeafNodes( function( node ) { realLeaves.push( node ); }, root3.children[1].children[0].children[0].children[1].children[1].children[0], true );
990 - ok(
991 - es.compareArrays( realLeaves, expectedLeaves ),
992 - 'Starting at a leaf node returns that leaf node and everything before it (in reverse)'
993 - );
994 -
995 - // Test 5
996 - realLeaves = [];
997 - expectedLeaves = [
998 - root3.children[1].children[0].children[0].children[0],
999 - root3.children[1].children[0].children[0].children[1].children[0].children[0],
1000 - root3.children[1].children[0].children[0].children[1].children[1].children[0],
1001 - root3.children[1].children[0].children[0].children[1].children[2].children[0],
1002 - root3.children[2]
1003 - ];
1004 - root3.traverseLeafNodes( function( node ) { realLeaves.push( node ); }, root3.children[1] );
1005 - ok(
1006 - es.compareArrays( realLeaves, expectedLeaves ),
1007 - 'Starting at a non-leaf node returns all leaf nodes inside and after it'
1008 - );
1009 - // Test 6
1010 - realLeaves = [];
1011 - expectedLeaves = [
1012 - root3.children[0]
1013 - ];
1014 - root3.traverseLeafNodes( function( node ) { realLeaves.push( node ); }, root3.children[1], true );
1015 - ok(
1016 - es.compareArrays( realLeaves, expectedLeaves ),
1017 - 'Starting at a non-leaf node returns all leaf nodes before it and none inside (in reverse)'
1018 - );
 1038+ for ( var i = 0; i < tests.length; i++ ) {
 1039+ executeTest( tests[i] );
 1040+ if ( tests[i].reverse !== undefined ) {
 1041+ var reversed = {
 1042+ 'node': tests[i].node,
 1043+ 'from': tests[i].from,
 1044+ 'callback': tests[i].callback,
 1045+ 'exception': tests[i].exception,
 1046+ 'isReversed': true,
 1047+ 'desc': tests[i].reverseDesc || tests[i].desc + ' (in reverse)'
 1048+ };
 1049+ if ( tests[i].output !== undefined && tests[i].reverse === true ) {
 1050+ reversed.output = tests[i].output.reverse();
 1051+ } else {
 1052+ reversed.output = tests[i].reverse;
 1053+ }
 1054+ executeTest( reversed );
 1055+ }
 1056+ }
10191057
1020 - // Test 7
1021 - realLeaves = [];
1022 - expectedLeaves = [
1023 - root3.children[1].children[0].children[0].children[0],
1024 - root3.children[1].children[0].children[0].children[1].children[0].children[0],
1025 - root3.children[1].children[0].children[0].children[1].children[1].children[0],
1026 - root3.children[1].children[0].children[0].children[1].children[2].children[0]
1027 - ];
1028 - root3.children[1].traverseLeafNodes( function( node ) { realLeaves.push( node ); } );
1029 - ok(
1030 - es.compareArrays( realLeaves, expectedLeaves ),
1031 - 'Calling traverseLeafNodes() on a non-root node only returns leaf nodes inside that node'
1032 - );
1033 - // Test 8
1034 - realLeaves = [];
1035 - expectedLeaves = expectedLeaves.reverse();
1036 - root3.children[1].traverseLeafNodes( function( node ) { realLeaves.push( node ); }, undefined, true );
1037 - ok(
1038 - es.compareArrays( realLeaves, expectedLeaves ),
1039 - 'Calling traverseLeafNodes() on a non-root node only returns leaf nodes inside that node (in reverse)'
1040 - );
1041 -
1042 - // Test 9
1043 - realLeaves = [];
1044 - raises(
1045 - function() {
1046 - root3.children[1].traverseLeafNodes( function( node ) { realLeaves.push( node ); }, root3.children[2] );
1047 - },
1048 - /^from parameter passed to traverseLeafNodes\(\) must be a descendant$/,
1049 - 'Passing a sibling for from results in an exception'
1050 - );
1051 -
1052 - // Test 10
1053 - // TODO this should be after test 1
1054 - realLeaves = [];
1055 - expectedLeaves = [
1056 - root3.children[0],
1057 - root3.children[1].children[0].children[0].children[0],
1058 - root3.children[1].children[0].children[0].children[1].children[0].children[0]
1059 - ];
1060 - root3.traverseLeafNodes( function( node ) {
1061 - realLeaves.push( node );
1062 - if ( node === root3.children[1].children[0].children[0].children[1].children[0].children[0] ) {
1063 - return false;
 1058+ function executeTest( test ) {
 1059+ var realLeaves = [],
 1060+ callback = function( node ) {
 1061+ var retval;
 1062+ realLeaves.push( node );
 1063+ if ( test.callback ) {
 1064+ retval = test.callback( node );
 1065+ if ( retval !== undefined ) {
 1066+ return retval;
 1067+ }
 1068+ }
 1069+ },
 1070+ f = function() {
 1071+ test.node.traverseLeafNodes( callback, test.from, test.isReversed );
 1072+ };
 1073+ if ( test.exception ) {
 1074+ raises( f, test.exception, test.desc );
 1075+ } else {
 1076+ f();
 1077+ ok( es.compareArrays( realLeaves, test.output ), test.desc );
10641078 }
1065 - } );
1066 - ok(
1067 - es.compareArrays( realLeaves, expectedLeaves ),
1068 - 'Returning false from the callback stops the traversal'
1069 - );
 1079+ }
10701080 } );
\ No newline at end of file

Status & tagging log