Debian Codegraph @

test/other/test-object-utils.js

raw
1 /*jslint nomen: true */
2 var utils = require('../../lib/object-utils'),
3 it,
4 it2,
5 it3;
6
7 module.exports = {
8 "with some coverage info": {
9 setUp: function (cb) {
10 it = {
11 foo: {
12 s: {
13 1: 1,
14 2: 2,
15 3: 3,
16 4: 4
17 },
18 statementMap: {
19 1: { start: { line: 1, column: 1}, end: { line: 1, column: 10 }},
20 2: { start: { line: 2, column: 1}, end: { line: 1, column: 9 }},
21 3: { start: { line: 2, column: 10 }, end: { line: 2, column: 20 }},
22 4: { start: { line: 7, column: 0 }, end: { line: 40, column: 10 }}
23 }
24 }
25 };
26 cb();
27 },
28 "should add/ remove derived coverage correctly": function (test) {
29 utils.addDerivedInfo(it);
30 var l = it.foo.l;
31 test.ok(l);
32 test.equals(3, Object.keys(l).length);
33 test.equals(1, l[1]);
34 test.equals(3, l[2]);
35 test.equals(4, l[7]);
36 l[7] = 0;
37 utils.addDerivedInfo(it);
38 test.equals(0, it.foo.l[7]); //does not change
39 utils.removeDerivedInfo(it);
40 test.ok(!it.foo.l);
41 test.done();
42 }
43 },
44 "with full coverage info": {
45 setUp: function (cb) {
46 it = {
47 foo: {
48 s: {
49 1: 1,
50 2: 2,
51 3: 3,
52 4: 4,
53 5: 0
54 },
55 statementMap: {
56 1: { start: { line: 1, column: 1}, end: { line: 1, column: 10 }},
57 2: { start: { line: 2, column: 1}, end: { line: 1, column: 9 }},
58 3: { start: { line: 2, column: 10 }, end: { line: 2, column: 20 }},
59 4: { start: { line: 7, column: 0 }, end: { line: 40, column: 10 }},
60 5: { start: { line: 41, column: 0 }, end: { line: 42, column: 10 }}
61 },
62 f: {
63 1: 10,
64 2: 0
65 },
66 fnMap: {
67 1: { name: 'foo', line: 1 },
68 2: { name: 'anonymous_1', line: 7 }
69 },
70 b: {
71 1: [ 10, 0, 2],
72 2: [ 0, 0]
73 },
74 branchMap: {
75 1: { line: 2, type: 'switch', locations: [ ] },
76 2: { line: 3, type: 'if', locations: [ ] }
77 }
78 }
79 };
80 it2 = it;
81 it3 = {
82 foo: {
83 s: {
84 1: 2,
85 2: 1,
86 3: 78,
87 4: 99,
88 5: 0
89 },
90 statementMap: {
91 1: { start: { line: 1, column: 1}, end: { line: 1, column: 10 }},
92 2: { start: { line: 2, column: 1}, end: { line: 1, column: 9 }},
93 3: { start: { line: 2, column: 10 }, end: { line: 2, column: 20 }},
94 4: { start: { line: 7, column: 0 }, end: { line: 40, column: 10 }},
95 5: { start: { line: 41, column: 0 }, end: { line: 42, column: 10 }}
96 },
97 f: {
98 1: 9,
99 2: 1
100 },
101 fnMap: {
102 1: { name: 'foo', line: 1 },
103 2: { name: 'anonymous_1', line: 7 }
104 },
105 b: {
106 1: [ 0, 1, 1],
107 2: [ 3, 0]
108 },
109 branchMap: {
110 1: { line: 2, type: 'switch', locations: [ ] },
111 2: { line: 3, type: 'if', locations: [ ] }
112 }
113 }
114 };
115 cb();
116 },
117 "should calculate correct summary": function (test) {
118 var ret = utils.summarizeFileCoverage(it.foo);
119 test.deepEqual({ total: 4, covered: 3, pct: 75, skipped: 0 }, ret.lines);
120 test.deepEqual({ total: 5, covered: 4, pct: 80, skipped: 0 }, ret.statements);
121 test.deepEqual({ total: 2, covered: 1, pct: 50, skipped: 0 }, ret.functions);
122 test.deepEqual({ total: 5, covered: 2, pct: 40, skipped: 0 }, ret.branches);
123 test.done();
124 },
125 "should return a pct of 100 when nothing is available": function (test) {
126 it.foo.b = {};
127 var ret = utils.summarizeFileCoverage(it.foo);
128 test.deepEqual({ total: 4, covered: 3, pct: 75, skipped: 0 }, ret.lines);
129 test.deepEqual({ total: 5, covered: 4, pct: 80, skipped: 0 }, ret.statements);
130 test.deepEqual({ total: 2, covered: 1, pct: 50, skipped: 0 }, ret.functions);
131 test.deepEqual({ total: 0, covered: 0, pct: 100, skipped: 0 }, ret.branches);
132 test.done();
133 },
134 "should merge summary correctly": function (test) {
135 var s1 = utils.summarizeFileCoverage(it.foo),
136 s2 = utils.summarizeFileCoverage(it2.foo),
137 ret = utils.mergeSummaryObjects(s1, s2);
138 test.deepEqual({ total: 8, covered: 6, pct: 75, skipped: 0 }, ret.lines);
139 test.deepEqual({ total: 10, covered: 8, pct: 80, skipped: 0 }, ret.statements);
140 test.deepEqual({ total: 4, covered: 2, pct: 50, skipped: 0 }, ret.functions);
141 test.deepEqual({ total: 10, covered: 4, pct: 40, skipped: 0 }, ret.branches);
142 test.done();
143 },
144 "should merge summary correctly in one call": function (test) {
145 var coverage = { foo: it.foo, 'bar': it2.foo },
146 ret = utils.summarizeCoverage(coverage);
147 test.deepEqual({ total: 8, covered: 6, pct: 75, skipped: 0 }, ret.lines);
148 test.deepEqual({ total: 10, covered: 8, pct: 80, skipped: 0 }, ret.statements);
149 test.deepEqual({ total: 4, covered: 2, pct: 50, skipped: 0 }, ret.functions);
150 test.deepEqual({ total: 10, covered: 4, pct: 40, skipped: 0 }, ret.branches);
151 test.done();
152 },
153 "can merge with a blank object in first position": function (test) {
154 var s1 = null,
155 s2 = utils.summarizeFileCoverage(it2.foo),
156 ret = utils.mergeSummaryObjects(s1, s2);
157 test.deepEqual({ total: 4, covered: 3, pct: 75, skipped: 0 }, ret.lines);
158 test.deepEqual({ total: 5, covered: 4, pct: 80, skipped: 0 }, ret.statements);
159 test.deepEqual({ total: 2, covered: 1, pct: 50, skipped: 0 }, ret.functions);
160 test.deepEqual({ total: 5, covered: 2, pct: 40, skipped: 0 }, ret.branches);
161 test.done();
162 },
163 "can merge with a blank object in second position": function (test) {
164 var s1 = utils.summarizeFileCoverage(it2.foo),
165 s2 = null,
166 ret = utils.mergeSummaryObjects(s1, s2);
167 test.deepEqual({ total: 4, covered: 3, pct: 75, skipped: 0 }, ret.lines);
168 test.deepEqual({ total: 5, covered: 4, pct: 80, skipped: 0 }, ret.statements);
169 test.deepEqual({ total: 2, covered: 1, pct: 50, skipped: 0 }, ret.functions);
170 test.deepEqual({ total: 5, covered: 2, pct: 40, skipped: 0 }, ret.branches);
171 test.done();
172 },
173 "can turn it into a YUI coverage object": function (test) {
174 var ret = utils.toYUICoverage(it);
175 test.deepEqual({ '1': 1, '2': 3, '7': 4, '41': 0 }, ret.foo.lines);
176 test.deepEqual({ 'foo:1': 10, 'anonymous_1:7': 0 }, ret.foo.functions);
177 test.equal(3, ret.foo.calledLines);
178 test.equal(4, ret.foo.coveredLines);
179 test.equal(1, ret.foo.calledFunctions);
180 test.equal(2, ret.foo.coveredFunctions);
181 test.done();
182 },
183 "merge two like coverage objects for the same file correctly": function (test) {
184 var base = JSON.parse(JSON.stringify(it.foo)),
185 ret = utils.mergeFileCoverage(base, it3.foo),
186 foo = it.foo;
187 foo.s[1] += 2;
188 foo.s[2] += 1;
189 foo.s[3] += 78;
190 foo.s[4] += 99;
191 foo.f[1] += 9;
192 foo.f[2] += 1;
193 foo.b[1][1] += 1;
194 foo.b[1][2] += 1;
195 foo.b[2][0] += 3;
196
197 test.deepEqual(ret, foo);
198 test.done();
199 },
200 "increment ignored totals": {
201 setUp: function (cb) {
202 it = {
203 l: {},
204 s: {
205 1: 0
206 },
207 b: {
208 1: [0, 0]
209 },
210 f: {
211 1: 0
212 },
213 statementMap: {
214 1: {skip: true}
215 },
216 branchMap: {
217 1: {
218 locations: [
219 {},
220 {skip: true}
221 ]
222 }
223 },
224 fnMap: {
225 1: {skip: true}
226 }
227 };
228
229 cb();
230 },
231 "should return file coverage object with incremented hits": function (test) {
232 var result = utils.incrementIgnoredTotals(it);
233 test.equal(1, result.s['1']);
234 test.deepEqual([0, 1], result.b['1']);
235 test.equal(1, result.f['1']);
236 test.done();
237 }
238 }
239 }
240 };
241