| 1 |
/*jslint nomen: true */ |
| 2 |
var helper = require('../helper'), |
| 3 |
code, |
| 4 |
verifier; |
| 5 |
|
| 6 |
module.exports = { |
| 7 |
"with a simple statement": { |
| 8 |
setUp: function (cb) { |
| 9 |
code = [ |
| 10 |
'/* istanbul ignore next */', |
| 11 |
'var x = args[0] > 5 ? args[0] : "undef";', |
| 12 |
'output = x;' |
| 13 |
]; |
| 14 |
verifier = helper.verifier(__filename, code); |
| 15 |
cb(); |
| 16 |
}, |
| 17 |
|
| 18 |
"should cover line and one branch": function (test) { |
| 19 |
verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 3: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } }); |
| 20 |
var cov = verifier.getFileCoverage(); |
| 21 |
test.equal(true, cov.statementMap[1].skip); |
| 22 |
test.equal(true, cov.branchMap[1].locations[0].skip); |
| 23 |
test.equal(true, cov.branchMap[1].locations[1].skip); |
| 24 |
test.done(); |
| 25 |
} |
| 26 |
}, |
| 27 |
"with a function declaration": { |
| 28 |
setUp: function (cb) { |
| 29 |
code = [ |
| 30 |
'/* istanbul ignore next */', |
| 31 |
'function foo(x) { return x; }', |
| 32 |
'output = args[0];' |
| 33 |
]; |
| 34 |
verifier = helper.verifier(__filename, code); |
| 35 |
cb(); |
| 36 |
}, |
| 37 |
|
| 38 |
"should not cover function call": function (test) { |
| 39 |
verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 3: 1 }, branches: {}, functions: { 1: 0 }, statements: { 1: 1, 2: 0, 3: 1 } }); |
| 40 |
var cov = verifier.getFileCoverage(); |
| 41 |
test.equal(true, cov.statementMap[1].skip); |
| 42 |
test.equal(true, cov.statementMap[2].skip); |
| 43 |
test.equal(true, cov.fnMap[1].skip); |
| 44 |
test.done(); |
| 45 |
} |
| 46 |
}, |
| 47 |
"with a function expression": { |
| 48 |
setUp: function (cb) { |
| 49 |
code = [ |
| 50 |
'/* istanbul ignore next */', |
| 51 |
'(function () { output = args[0]; })();' |
| 52 |
]; |
| 53 |
verifier = helper.verifier(__filename, code); |
| 54 |
cb(); |
| 55 |
}, |
| 56 |
|
| 57 |
"should cover function call": function (test) { |
| 58 |
verifier.verify(test, [ 10 ], 10, { lines: { 2: 1 }, branches: {}, functions: { 1: 1 }, statements: { 1: 1, 2: 1 } }); |
| 59 |
var cov = verifier.getFileCoverage(); |
| 60 |
test.equal(true, cov.statementMap[1].skip); |
| 61 |
test.equal(true, cov.statementMap[2].skip); |
| 62 |
test.equal(true, cov.fnMap[1].skip); |
| 63 |
test.done(); |
| 64 |
} |
| 65 |
}, |
| 66 |
"with a disabled switch statement": { |
| 67 |
setUp: function (cb) { |
| 68 |
code = [ |
| 69 |
'/* istanbul ignore next */', |
| 70 |
'switch (args[0]) {', |
| 71 |
'case "1": output = 2; break;', |
| 72 |
'default: output = 1;', |
| 73 |
'}' |
| 74 |
]; |
| 75 |
verifier = helper.verifier(__filename, code); |
| 76 |
cb(); |
| 77 |
}, |
| 78 |
|
| 79 |
"should ignore all branches": function (test) { |
| 80 |
verifier.verify(test, [ "1" ], 2, { lines: { 2: 1, 3: 1, 4: 1 }, branches: { 1: [ 1, 0 ]}, |
| 81 |
functions: {}, statements: { 1: 1, 2: 1, 3: 1, 4: 0 } }); |
| 82 |
var cov = verifier.getFileCoverage(); |
| 83 |
test.equal(true, cov.statementMap[1].skip); |
| 84 |
test.equal(true, cov.statementMap[2].skip); |
| 85 |
test.equal(true, cov.statementMap[3].skip); |
| 86 |
test.equal(true, cov.statementMap[4].skip); |
| 87 |
test.equal(true, cov.branchMap[1].locations[0].skip); |
| 88 |
test.equal(true, cov.branchMap[1].locations[1].skip); |
| 89 |
test.done(); |
| 90 |
} |
| 91 |
}, |
| 92 |
"with a disabled case statement": { |
| 93 |
setUp: function (cb) { |
| 94 |
code = [ |
| 95 |
'switch (args[0]) {', |
| 96 |
'/* istanbul ignore next */', |
| 97 |
'case "1": output = 2; break;', |
| 98 |
'default: output = 1;', |
| 99 |
'}' |
| 100 |
]; |
| 101 |
verifier = helper.verifier(__filename, code); |
| 102 |
cb(); |
| 103 |
}, |
| 104 |
|
| 105 |
"should ignore specific case": function (test) { |
| 106 |
verifier.verify(test, [ "2" ], 1, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { 1: [ 0, 1 ]}, |
| 107 |
functions: {}, statements: { 1: 1, 2: 0, 3: 0, 4: 1 } }); |
| 108 |
var cov = verifier.getFileCoverage(); |
| 109 |
test.equal(true, cov.branchMap[1].locations[0].skip); |
| 110 |
test.equal(true, cov.statementMap[2].skip); |
| 111 |
test.equal(true, cov.statementMap[3].skip); |
| 112 |
test.done(); |
| 113 |
} |
| 114 |
}, |
| 115 |
|
| 116 |
"with disabled conditional statement": { |
| 117 |
setUp: function (cb) { |
| 118 |
code = [ |
| 119 |
'/* istanbul ignore next */', |
| 120 |
'output = args[0] === 1 ? 1: 0;' |
| 121 |
]; |
| 122 |
verifier = helper.verifier(__filename, code); |
| 123 |
cb(); |
| 124 |
}, |
| 125 |
|
| 126 |
"should ignore conditions": function (test) { |
| 127 |
verifier.verify(test, [ 2 ], 0, { lines: { 2: 1 }, branches: { 1: [ 0, 1 ]}, |
| 128 |
functions: {}, statements: { 1: 1 } }); |
| 129 |
var cov = verifier.getFileCoverage(); |
| 130 |
test.equal(true, cov.branchMap[1].locations[0].skip); |
| 131 |
test.equal(true, cov.branchMap[1].locations[1].skip); |
| 132 |
test.equal(true, cov.statementMap[1].skip); |
| 133 |
test.done(); |
| 134 |
} |
| 135 |
|
| 136 |
}, |
| 137 |
"with disabled condition": { |
| 138 |
setUp: function (cb) { |
| 139 |
code = [ |
| 140 |
'output = args[0] === 1 ? /* istanbul ignore next */ 1 : 0;' |
| 141 |
]; |
| 142 |
verifier = helper.verifier(__filename, code); |
| 143 |
cb(); |
| 144 |
}, |
| 145 |
|
| 146 |
"should ignore conditions": function (test) { |
| 147 |
verifier.verify(test, [ 2 ], 0, { lines: { 1: 1 }, branches: { 1: [ 0, 1 ]}, |
| 148 |
functions: {}, statements: { 1: 1 } }); |
| 149 |
var cov = verifier.getFileCoverage(); |
| 150 |
test.equal(true, cov.branchMap[1].locations[0].skip); |
| 151 |
test.equal(undefined, cov.branchMap[1].locations[1].skip); |
| 152 |
test.equal(undefined, cov.statementMap[1].skip); |
| 153 |
test.done(); |
| 154 |
} |
| 155 |
}, |
| 156 |
|
| 157 |
"with a simple logical expression": { |
| 158 |
setUp: function (cb) { |
| 159 |
code = [ |
| 160 |
'if (args[0] === 1 || /* istanbul ignore next */ args[0] === 2 ) {', |
| 161 |
' output = args[0] + 10;', |
| 162 |
'} else {', |
| 163 |
' output = 20;', |
| 164 |
'}' |
| 165 |
]; |
| 166 |
verifier = helper.verifier(__filename, code); |
| 167 |
cb(); |
| 168 |
}, |
| 169 |
|
| 170 |
"should ignore conditions": function (test) { |
| 171 |
verifier.verify(test, [ 1 ], 11, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { 1: [ 1, 0 ], 2: [ 1, 0 ] }, |
| 172 |
functions: {}, statements: { 1: 1, 2: 1, 3: 0 } }); |
| 173 |
var cov = verifier.getFileCoverage(); |
| 174 |
test.equal(true, cov.branchMap[2].locations[1].skip); |
| 175 |
test.done(); |
| 176 |
} |
| 177 |
}, |
| 178 |
|
| 179 |
"with a slightly complicated logical expression": { |
| 180 |
setUp: function (cb) { |
| 181 |
code = [ |
| 182 |
'if (args[0] === 1 || /* istanbul ignore next */ (args[0] === 2 || args[0] === 3)) {', |
| 183 |
' output = args[0] + 10;', |
| 184 |
'} else {', |
| 185 |
' output = 20;', |
| 186 |
'}' |
| 187 |
]; |
| 188 |
verifier = helper.verifier(__filename, code); |
| 189 |
cb(); |
| 190 |
}, |
| 191 |
|
| 192 |
"should ignore conditions": function (test) { |
| 193 |
verifier.verify(test, [ 1 ], 11, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { 1: [ 1, 0 ], 2: [ 1, 0, 0 ] }, |
| 194 |
functions: {}, statements: { 1: 1, 2: 1, 3: 0 } }); |
| 195 |
var cov = verifier.getFileCoverage(); |
| 196 |
test.equal(undefined, cov.branchMap[2].locations[0].skip); |
| 197 |
test.equal(true, cov.branchMap[2].locations[1].skip); |
| 198 |
test.equal(true, cov.branchMap[2].locations[2].skip); |
| 199 |
test.done(); |
| 200 |
} |
| 201 |
}, |
| 202 |
|
| 203 |
"with a complicated logical expression involving implied operator precedence": { |
| 204 |
setUp: function (cb) { |
| 205 |
code = [ |
| 206 |
'if (args[0] === 1 || /* istanbul ignore next */ args[0] === 2 && args[1] === 2) {', |
| 207 |
' output = args[0] + 10;', |
| 208 |
'} else {', |
| 209 |
' output = 20;', |
| 210 |
'}' |
| 211 |
]; |
| 212 |
verifier = helper.verifier(__filename, code); |
| 213 |
cb(); |
| 214 |
}, |
| 215 |
|
| 216 |
"should ignore conditions": function (test) { |
| 217 |
verifier.verify(test, [ 1, 1 ], 11, { lines: { 1: 1, 2: 1, 4: 0 }, branches: { 1: [ 1, 0 ], 2: [ 1, 0, 0 ] }, |
| 218 |
functions: {}, statements: { 1: 1, 2: 1, 3: 0 } }); |
| 219 |
var cov = verifier.getFileCoverage(); |
| 220 |
test.equal(undefined, cov.branchMap[2].locations[0].skip); |
| 221 |
test.equal(true, cov.branchMap[2].locations[1].skip); |
| 222 |
test.equal(true, cov.branchMap[2].locations[2].skip); |
| 223 |
test.done(); |
| 224 |
} |
| 225 |
} |
| 226 |
}; |