Debian Codegraph @

test/instrumentation/test-for.js

raw
1 /*jslint nomen: true */
2 var helper = require('../helper'),
3 code,
4 verifier;
5
6 module.exports = {
7 "with a simple for": {
8 setUp: function (cb) {
9 code = [
10 'var x = args[0], i, j = -10;',
11 'for (i =0; i < x; i++) j = i;',
12 'output = j;'
13 ];
14 verifier = helper.verifier(__filename, code);
15 cb();
16 },
17
18 "should cover loop exactly once": function (test) {
19 verifier.verify(test, [ 10 ], 9, {
20 lines: { 1: 1, 2: 10, 3: 1 },
21 branches: { },
22 functions: {},
23 statements: { '1': 1, '2': 1, '3': 10, '4': 1 }
24 });
25 test.done();
26 }
27 },
28 "with a simple for - declaring the loop variable in initializer": {
29 setUp: function (cb) {
30 code = [
31 'var x = args[0], j = -10;',
32 'for (var i =0; i < x; i++) j = i;',
33 'output = j;'
34 ];
35 verifier = helper.verifier(__filename, code);
36 cb();
37 },
38
39 "should cover loop exactly once": function (test) {
40 verifier.verify(test, [ 10 ], 9, {
41 lines: { 1: 1, 2: 10, 3: 1 },
42 branches: { },
43 functions: {},
44 statements: { '1': 1, '2': 1, '3': 10, '4': 1 }
45 });
46 test.done();
47 }
48 },
49 "with a simple for - not having an initializer": {
50 setUp: function (cb) {
51 code = [
52 'var x = args[0], j = -10, i=0;',
53 'for (; i < x; i++) j = i;',
54 'output = j;'
55 ];
56 verifier = helper.verifier(__filename, code);
57 cb();
58 },
59
60 "should cover loop exactly once": function (test) {
61 verifier.verify(test, [ 10 ], 9, {
62 lines: { 1: 1, 2: 10, 3: 1 },
63 branches: { },
64 functions: {},
65 statements: { '1': 1, '2': 1, '3': 10, '4': 1 }
66 });
67 test.done();
68 }
69 },
70 "with a simple for - statement on a different line": {
71 setUp: function (cb) {
72 code = [
73 'var x = args[0], i, j = -10;',
74 'for (i =0; i < x; i++)',
75 ' j = i;',
76 'output = j;'
77 ];
78 verifier = helper.verifier(__filename, code);
79 cb();
80 },
81
82 "should cover loop one time": function (test) {
83 verifier.verify(test, [ 10 ], 9, {
84 lines: { 1: 1, 2: 1, 3: 10, 4: 1 },
85 branches: {},
86 functions: {},
87 statements: { '1': 1, '2': 1, '3': 10, '4': 1 }
88 });
89 test.done();
90 },
91
92 "should not cover loop at all": function (test) {
93 verifier.verify(test, [ -1 ], -10, {
94 lines: { 1: 1, 2: 1, 3: 0, 4: 1 },
95 branches: {},
96 functions: {},
97 statements: { '1': 1, '2': 1, '3': 0, '4': 1 }
98 });
99 test.done();
100 }
101 },
102 "with a simple for in block": {
103 setUp: function (cb) {
104 code = [
105 'var x = args[0], i, j = -10;',
106 'for (i =0; i < x; i++) { j = i; }',
107 'output = j;'
108 ];
109 verifier = helper.verifier(__filename, code);
110 cb();
111 },
112
113 "should cover multi-loop exactly once": function (test) {
114 verifier.verify(test, [ 10 ], 9, {
115 lines: { 1: 1, 2: 10, 3: 1 },
116 branches: { },
117 functions: {},
118 statements: { '1': 1, '2': 1, '3': 10, '4': 1 }
119 });
120 test.done();
121 },
122
123 "should not cover loop at all": function (test) {
124 verifier.verify(test, [ -1 ], -10, {
125 lines: { 1: 1, 2: 1, 3: 1 },
126 branches: {},
127 functions: {},
128 statements: { '1': 1, '2': 1, '3': 0, '4': 1 }
129 });
130 test.done();
131 }
132 },
133 "with a labeled for": {
134 setUp: function (cb) {
135 code = [
136 'var x = args[0], i, j = -10;',
137 'outer:for (i =0; i < x; i++) { j = i; }',
138 'output = j;'
139 ];
140 verifier = helper.verifier(__filename, code);
141 cb();
142 },
143
144 "should cover multi-loop exactly once": function (test) {
145 verifier.verify(test, [ 10 ], 9, {
146 lines: { 1: 1, 2: 10, 3: 1 },
147 branches: {},
148 functions: {},
149 statements: { '1': 1, '2': 1, '3': 1, '4': 10, '5': 1 }
150 });
151 test.done();
152 }
153 },
154 "with a nested labeled for": {
155 setUp: function (cb) {
156 code = [
157 'var x = args[0], i, j, k = 0;',
158 'outer:for (i = 0; i < x; i++)',
159 ' for (j=0; j < i ; j++) {',
160 ' if (j === 2) continue outer;',
161 ' k++;',
162 ' }',
163 'output = k;'
164 ];
165 verifier = helper.verifier(__filename, code);
166 cb();
167 },
168
169 "should cover multi-loop exactly once": function (test) {
170 verifier.verify(test, [ 10 ], 17, {
171 lines: { '1': 1, '2': 1, '3': 10, '4': 24, '5': 17, '7': 1 },
172 branches: { '1': [ 7, 17 ] },
173 functions: {},
174 statements: { '1': 1, '2': 1, '3': 1, '4': 10, '5': 24, '6': 7, '7': 17, '8': 1 }
175 });
176 test.done();
177 }
178 },
179 "with a nested labeled for (label on different line)": {
180 setUp: function (cb) {
181 code = [
182 'var x = args[0], i, j, k = 0;',
183 'outer:',
184 'for (i = 0; i < x; i++)',
185 ' for (j=0; j < i ; j++) {',
186 ' if (j === 2) continue outer;',
187 ' k++;',
188 ' }',
189 'output = k;'
190 ];
191 verifier = helper.verifier(__filename, code);
192 cb();
193 },
194
195 "should cover multi-loop exactly once": function (test) {
196 verifier.verify(test, [ 10 ], 17, {
197 lines: { '1': 1, '2': 1, '3': 1, '4': 10, '5': 24, '6': 17, '8': 1 },
198 branches: { '1': [ 7, 17 ] },
199 functions: {},
200 statements: { '1': 1, '2': 1, '3': 1, '4': 10, '5': 24, '6': 7, '7': 17, '8': 1 }
201 });
202 test.done();
203 }
204 }
205 };
206