Debian Codegraph @

test/instrumentation/test-statement.js

raw
1 /*jslint nomen: true */
2 var helper = require('../helper'),
3 Instrumenter = require('../../lib/instrumenter'),
4 code,
5 verifier;
6
7 module.exports = {
8 "with a simple statement": {
9 setUp: function (cb) {
10 code = [
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: { 1: 1, 2: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
20 test.done();
21 },
22 "should cover line and other branch": function (test) {
23 verifier.verify(test, [ 1 ], "undef", { lines: { 1: 1, 2: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
24 test.done();
25 }
26 },
27 "with a metaproperty": {
28 setUp: function (cb) {
29 code = [
30 'class FooClass {',
31 ' constructor() {',
32 ' if (new.target === FooClass) {',
33 ' throw new Error(\'Cannot instanciate directly.\');',
34 ' }',
35 ' }',
36 '}'
37 ];
38 verifier = helper.verifier(__filename, code);
39 cb();
40 },
41 "should be able to parse": function (test) {
42 // Do we need to test coverage here really? We're just checking
43 // that a new type of statement is parsable, the if-else logic
44 // is already covered in statement-if.
45 test.done();
46 }
47 },
48 "with no filename": {
49 setUp: function (cb) {
50 code = [
51 'output = args[0];'
52 ];
53 verifier = helper.verifier(null, code, { debug: true, walkDebug: true });
54 cb();
55 },
56 "should not barf in setup": function (test) {
57 verifier.verify(test, [ 1 ], 1, { lines: { 1: 1 }, branches: {}, functions: {}, statements: { 1: 1 } });
58 test.done();
59 }
60 },
61 "with a windows style file path": {
62 setUp: function (cb) {
63 code = [
64 'var x = args[0] > 5 ? args[0] : "undef";',
65 'output = x;'
66 ];
67 verifier = helper.verifier("c:\\a\\b\\c\\d\\e.js", code);
68 cb();
69 },
70 "should have correct key in coverage variable": function (test) {
71 verifier.verify(test, [ 1 ], "undef", { lines: { 1: 1, 2: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
72 var coverage = verifier.getCoverage(),
73 key = Object.keys(coverage)[0];
74 test.equals("c:\\a\\b\\c\\d\\e.js", key);
75 test.done();
76 }
77
78 },
79 "with junk code": {
80 setUp: function (cb) {
81 code = [
82 'output = args[0] : 1 : 2;'
83 ];
84 verifier = helper.verifier(null, code, { debug: true, walkDebug: true });
85 cb();
86 },
87 "should have verification errors": function (test) {
88 verifier.verifyError(test);
89 test.done();
90 }
91 },
92 "with code that is not a string": {
93 "should have verification errors": function (test) {
94 test.throws(function () {
95 var instrumenter = new Instrumenter();
96 instrumenter.instrumentSync({}, 'foo.js');
97 }, Error, 'Code must be a string');
98 test.done();
99 }
100 },
101 "with shebang code": {
102 setUp: function (cb) {
103 code = [
104 '#!/usr/bin/env node',
105 'var x = args[0] > 5 ? args[0] : "undef";',
106 'output = x;'
107 ];
108 verifier = helper.verifier(__filename, code);
109 cb();
110 },
111
112 "should cover line and one branch": function (test) {
113 verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 3: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
114 test.done();
115 },
116 "should cover line and other branch": function (test) {
117 verifier.verify(test, [ 1 ], "undef", { lines: { 2: 1, 3: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
118 test.done();
119 }
120 },
121 "with source code packed in": {
122 setUp: function (cb) {
123 code = [
124 'var x = args[0] > 5 ? args[0] : "undef";',
125 'output = x;'
126 ];
127 verifier = helper.verifier(__filename, code, { embedSource: true, coverageVariable: null });
128 cb();
129 },
130 "coverage should have code packed in": function (test) {
131 verifier.verifyNoError(test);
132 verifier.verify(test, [ 10 ], 10, { lines: { 1: 1, 2: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
133 var cov = verifier.getCoverage(),
134 fileCov = cov[Object.keys(cov)[0]];
135 test.ok(fileCov.code.length > 1);
136 test.ok(fileCov.code[1] === 'output = x;');
137 test.done();
138 },
139 "code packed in does not have \\r characters": function (test) {
140 code = [
141 'var x = args[0] > 5 ? args[0] : "undef";\r',
142 'output = x;\r'
143 ];
144 verifier = helper.verifier(__filename + '2', code, { embedSource: true, coverageVariable: null });
145 verifier.verifyNoError(test);
146 verifier.verify(test, [ 10 ], 10, { lines: { 1: 1, 2: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
147 var cov = verifier.getCoverage(),
148 fileCov = cov[Object.keys(cov)[0]];
149 test.ok(fileCov.code.length > 1);
150 test.ok(fileCov.code[1] === 'output = x;');
151 test.done();
152 }
153 },
154 "with code having a return statement on mainline": {
155 setUp: function (cb) {
156 code = [
157 'return 10;'
158 ];
159 verifier = helper.verifier(__filename, code);
160 cb();
161 },
162
163 "should pass coverage": function (test) {
164 verifier.verifyNoError(test);
165 test.done();
166 }
167 },
168 "with code having a return statement on mainline and no autowrap": {
169 setUp: function (cb) {
170 code = [
171 'return 10;'
172 ];
173 verifier = helper.verifier(__filename, code, { noAutoWrap: true });
174 cb();
175 },
176
177 "should fail coverage": function (test) {
178 verifier.verifyError(test);
179 test.done();
180 }
181 },
182 "with no mainline returns and no autowrap": {
183 setUp: function (cb) {
184 code = [
185 '#!/usr/bin/env node',
186 'var x = args[0] > 5 ? args[0] : "undef";',
187 'output = x;'
188 ];
189 verifier = helper.verifier(__filename, code, { noAutoWrap: true });
190 cb();
191 },
192
193 "should cover line and one branch": function (test) {
194 verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 3: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
195 test.done();
196 },
197 "should cover line and other branch": function (test) {
198 verifier.verify(test, [ 1 ], "undef", { lines: { 2: 1, 3: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
199 test.done();
200 }
201 },
202 "with code having comments": {
203 setUp: function (cb) {
204 code = [
205 '#!/usr/bin/env node',
206 'var x = args[0] > 5 ? args[0] : "undef";',
207 '/* set the output */',
208 'output = x;'
209 ];
210 verifier = helper.verifier(__filename, code, { noAutoWrap: true, preserveComments: true });
211 cb();
212 },
213
214 "should preserve comments in generated code": function (test) {
215 test.ok(verifier.generatedCode.match(/\/\* set the output \*\//));
216 test.done();
217 },
218
219 "should cover line and one branch": function (test) {
220 verifier.verify(test, [ 10 ], 10, { lines: { 2: 1, 4: 1 }, branches: { 1: [1, 0 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
221 test.done();
222 },
223 "should cover line and other branch": function (test) {
224 verifier.verify(test, [ 1 ], "undef", { lines: { 2: 1, 4: 1 }, branches: { 1: [ 0, 1 ]}, functions: {}, statements: { 1: 1, 2: 1 } });
225 test.done();
226 }
227 }
228 };
229