Debian Codegraph @

test/instrumentation/test-if-with-hints.js

raw
1 /*jslint nomen: true */
2 var helper = require('../helper'),
3 code,
4 verifier;
5
6 /*jshint maxlen: 500 */
7 module.exports = {
8 "with a simple if": {
9 "as a statement": {
10 setUp: function (cb) {
11 code = [
12 'output = -1;',
13 '/* hint */',
14 'if (args[0] > args [1])',
15 ' output = args[0];'
16 ];
17 cb(null);
18 },
19 "should cover then path": function (test) {
20 code[1] = '/* istanbul ignore else */';
21 verifier = helper.verifier(__filename, code);
22 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
23 var cov = verifier.getFileCoverage();
24 test.equal(true, cov.branchMap[1].locations[1].skip);
25 test.done();
26 },
27 "should cover else path": function (test) {
28 code[1] = '/* istanbul ignore if */';
29 verifier = helper.verifier(__filename, code);
30 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
31 var cov = verifier.getFileCoverage();
32 test.equal(true, cov.branchMap[1].locations[0].skip);
33 test.equal(true, cov.statementMap[3].skip);
34 test.done();
35 }
36 },
37 "as a block": {
38 setUp: function (cb) {
39 code = [
40 'output = -1;',
41 '/* hint */',
42 'if (args[0] > args [1]) {',
43 ' output = args[0];',
44 '}'
45 ];
46 cb();
47 },
48 "should cover then path": function (test) {
49 code[1] = '/* istanbul ignore else */';
50 verifier = helper.verifier(__filename, code);
51 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
52 var cov = verifier.getFileCoverage();
53 test.equal(true, cov.branchMap[1].locations[1].skip);
54 test.done();
55 },
56 "should cover else path": function (test) {
57 code[1] = '/* istanbul ignore if */';
58 verifier = helper.verifier(__filename, code);
59 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1, 4: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
60 var cov = verifier.getFileCoverage();
61 test.equal(true, cov.branchMap[1].locations[0].skip);
62 test.equal(true, cov.statementMap[3].skip);
63 test.done();
64 }
65 },
66 "on a single line": {
67 "as statement": {
68 setUp: function (cb) {
69 code = [
70 'output = -1;',
71 '/* hint */',
72 'if (args[0] > args [1]) output = args[0];'
73 ];
74 cb();
75 },
76 "should cover then path": function (test) {
77 code[1] = '/* istanbul ignore else */';
78 verifier = helper.verifier(__filename, code);
79 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
80 var cov = verifier.getFileCoverage();
81 test.equal(true, cov.branchMap[1].locations[1].skip);
82 test.done();
83 },
84 "should cover else path": function (test) {
85 code[1] = '/* istanbul ignore if */';
86 verifier = helper.verifier(__filename, code);
87 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
88 var cov = verifier.getFileCoverage();
89 test.equal(true, cov.branchMap[1].locations[0].skip);
90 test.equal(true, cov.statementMap[3].skip);
91 test.done();
92 }
93 },
94 "as block": {
95 setUp: function (cb) {
96 code = [
97 'output = -1;',
98 '/* hint */',
99 'if (args[0] > args [1]) { output = args[0]; }'
100 ];
101 cb();
102 },
103 "should cover then path": function (test) {
104 code[1] = '/* istanbul ignore else */';
105 verifier = helper.verifier(__filename, code);
106 verifier.verify(test, [ 20, 10 ], 20, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 1 } });
107 var cov = verifier.getFileCoverage();
108 test.equal(true, cov.branchMap[1].locations[1].skip);
109 test.done();
110 },
111 "should cover else path": function (test) {
112 code[1] = '/* istanbul ignore if */';
113 verifier = helper.verifier(__filename, code);
114 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
115 var cov = verifier.getFileCoverage();
116 test.equal(true, cov.branchMap[1].locations[0].skip);
117 test.equal(true, cov.statementMap[3].skip);
118 test.done();
119 },
120 "should skip if statement completely": function (test) {
121 code[1] = '/* istanbul ignore next */';
122 verifier = helper.verifier(__filename, code);
123 verifier.verify(test, [ 10, 20 ], -1, { lines: { 1: 1, 3: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
124 var cov = verifier.getFileCoverage();
125 test.equal(true, cov.branchMap[1].locations[0].skip);
126 test.equal(true, cov.branchMap[1].locations[1].skip);
127 test.equal(true, cov.statementMap[3].skip);
128 test.done();
129 }
130 }
131 }
132 },
133 "with a simple if-else": {
134 "as a statement": {
135 setUp: function (cb) {
136 code = [
137 '// hint',
138 'if (args[0] > args [1])',
139 ' output = args[0];',
140 'else',
141 ' output = args[1];'
142 ];
143 cb(null);
144 },
145 "should cover then path": function (test) {
146 code[0] = '// istanbul ignore else';
147 verifier = helper.verifier(__filename, code);
148 verifier.verify(test, [ 20, 10 ], 20, { lines: { 2: 1, 3: 1, 5: 1 }, branches: { '1': [ 1, 0 ] }, functions: {}, statements: { '1': 1, '2': 1, '3': 0 } });
149 var cov = verifier.getFileCoverage();
150 test.equal(true, cov.branchMap[1].locations[1].skip);
151 test.equal(true, cov.statementMap[3].skip);
152 test.done();
153 },
154 "should cover else path": function (test) {
155 code[0] = '// istanbul ignore if ';
156 verifier = helper.verifier(__filename, code);
157 verifier.verify(test, [ 10, 20 ], 20, { lines: { 2: 1, 3: 1, 5: 1 }, branches: { '1': [ 0, 1 ] }, functions: {}, statements: { '1': 1, '2': 0, '3': 1 } });
158 var cov = verifier.getFileCoverage();
159 test.equal(true, cov.branchMap[1].locations[0].skip);
160 test.equal(true, cov.statementMap[2].skip);
161 test.done();
162 }
163 }
164 }
165 };
166