| 1 |
var path = require('path'), |
| 2 |
SEP = path.sep || '/', |
| 3 |
TreeSummarizer = require('../../lib/util/tree-summarizer'), |
| 4 |
utils = require('../../lib/object-utils'), |
| 5 |
summarizer, |
| 6 |
tree, |
| 7 |
s1, |
| 8 |
s2, |
| 9 |
s3; |
| 10 |
|
| 11 |
module.exports = { |
| 12 |
setUp: function (cb) { |
| 13 |
summarizer = new TreeSummarizer(); |
| 14 |
s1 = { |
| 15 |
statements: { covered: 5, total: 10, pct: 50, skipped: 0 }, |
| 16 |
lines: { covered: 5, total: 10, pct: 50, skipped: 0 }, |
| 17 |
functions: { covered: 15, total: 20, pct: 75, skipped: 0 }, |
| 18 |
branches: { covered: 100, total: 200, pct: 50, skipped: 0 }, |
| 19 |
linesCovered: { '320': 1, '420': 10 } |
| 20 |
}; |
| 21 |
s2 = { |
| 22 |
statements: { covered: 10, total: 20, pct: 50, skipped: 0 }, |
| 23 |
lines: { covered: 10, total: 20, pct: 50, skipped: 0 }, |
| 24 |
functions: { covered: 75, total: 100, pct: 75, skipped: 0 }, |
| 25 |
branches: { covered: 1, total: 2, pct: 50, skipped: 0 }, |
| 26 |
linesCovered: { '320': 3, '500': 0 } |
| 27 |
}; |
| 28 |
s3 = { |
| 29 |
statements: { covered: 9, total: 10, pct: 90, skipped: 0 }, |
| 30 |
lines: { covered: 9, total: 10, pct: 90, skipped: 0 }, |
| 31 |
functions: { covered: 15, total: 15, pct: 100, skipped: 0 }, |
| 32 |
branches: { covered: 101, total: 101, pct: 100, skipped: 0 }, |
| 33 |
linesCovered: {} |
| 34 |
}; |
| 35 |
cb(); |
| 36 |
}, |
| 37 |
"with a few files in a few dirs": { |
| 38 |
setUp: function (cb) { |
| 39 |
summarizer.addFileCoverageSummary(SEP + path.join('tmp', 'lib', 'foo.js'), s1); |
| 40 |
summarizer.addFileCoverageSummary(SEP + path.join('tmp', 'lib', 'bar.js'), s2); |
| 41 |
summarizer.addFileCoverageSummary(SEP + path.join('tmp', 'lib', 'util', 'baz.js'), s3); |
| 42 |
tree = summarizer.getTreeSummary(); |
| 43 |
cb(); |
| 44 |
}, |
| 45 |
"should be JSON serializable": function (test) { |
| 46 |
test.ok(JSON.stringify(tree)); |
| 47 |
test.done(); |
| 48 |
}, |
| 49 |
"should have the correct tree with root hoisted one level above code": function (test) { |
| 50 |
var node = tree.root, |
| 51 |
utilSummary = utils.mergeSummaryObjects(s3), |
| 52 |
libSummary = utils.mergeSummaryObjects(s1, s2), |
| 53 |
fullSummary = utils.mergeSummaryObjects(utilSummary, libSummary); |
| 54 |
test.equal(SEP + 'tmp' + SEP, node.fullPath()); |
| 55 |
test.equal('', node.displayShortName()); |
| 56 |
test.equal(2, node.children.length); |
| 57 |
test.deepEqual(fullSummary, node.metrics); |
| 58 |
test.ok(node === tree.getNode('')); |
| 59 |
node = tree.root.children[0]; |
| 60 |
test.equal(SEP + ['tmp', 'lib'].join(SEP) + SEP, node.fullPath()); |
| 61 |
test.equal('lib' + SEP, node.displayShortName()); |
| 62 |
test.equal(2, node.children.length); |
| 63 |
test.deepEqual(libSummary, node.metrics); |
| 64 |
test.deepEqual(s2, node.children[0].metrics); |
| 65 |
test.deepEqual(s1, node.children[1].metrics); |
| 66 |
test.ok(node === tree.getNode('lib' + SEP)); |
| 67 |
node = tree.root.children[1]; |
| 68 |
test.equal(SEP + ['tmp', 'lib', 'util'].join(SEP) + SEP, node.fullPath()); |
| 69 |
test.equal(['lib', 'util'].join(SEP) + SEP, node.displayShortName()); |
| 70 |
test.equal(1, node.children.length); |
| 71 |
test.deepEqual(utilSummary, node.metrics); |
| 72 |
test.deepEqual(s3, node.children[0].metrics); |
| 73 |
test.ok(node === tree.getNode(path.join('lib', 'util') + SEP)); |
| 74 |
test.ok(tree.getNode(path.join('lib', 'foo.js'))); |
| 75 |
test.ok(tree.getNode(path.join('lib', 'bar.js'))); |
| 76 |
test.ok(tree.getNode(path.join('lib', 'util', 'baz.js'))); |
| 77 |
test.done(); |
| 78 |
}, |
| 79 |
"should combine linesCovered across multiple summary objects": function (test) { |
| 80 |
var summary = utils.mergeSummaryObjects(s1, s2); |
| 81 |
test.equal(4, summary.linesCovered['320']); |
| 82 |
test.equal(10, summary.linesCovered['420']); |
| 83 |
test.equal(0, summary.linesCovered['500']); |
| 84 |
test.done(); |
| 85 |
} |
| 86 |
}, |
| 87 |
"with the same few files organized differently": { |
| 88 |
setUp: function (cb) { |
| 89 |
summarizer.addFileCoverageSummary(SEP + path.join('tmp', 'lib', 'main', 'foo.js'), s1); |
| 90 |
summarizer.addFileCoverageSummary(SEP + path.join('tmp', 'lib', 'main', 'bar.js'), s2); |
| 91 |
summarizer.addFileCoverageSummary(SEP + path.join('tmp', 'lib', 'util', 'baz.js'), s3); |
| 92 |
tree = summarizer.getTreeSummary(); |
| 93 |
cb(); |
| 94 |
}, |
| 95 |
"should have the correct tree with no root hoisting": function (test) { |
| 96 |
var node = tree.root, |
| 97 |
utilSummary = utils.mergeSummaryObjects(s3), |
| 98 |
libSummary = utils.mergeSummaryObjects(s1, s2), |
| 99 |
fullSummary = utils.mergeSummaryObjects(utilSummary, libSummary); |
| 100 |
test.equal(SEP + ['tmp', 'lib'].join(SEP) + SEP, node.fullPath()); |
| 101 |
test.equal('', node.displayShortName()); |
| 102 |
test.equal(2, node.children.length); |
| 103 |
test.deepEqual(fullSummary, node.metrics); |
| 104 |
test.ok(node === tree.getNode('')); |
| 105 |
node = tree.root.children[0]; |
| 106 |
test.equal(SEP + ['tmp', 'lib', 'main'].join(SEP) + SEP, node.fullPath()); |
| 107 |
test.equal('main' + SEP, node.displayShortName()); |
| 108 |
test.equal(2, node.children.length); |
| 109 |
test.deepEqual(libSummary, node.metrics); |
| 110 |
test.deepEqual(s2, node.children[0].metrics); |
| 111 |
test.deepEqual(s1, node.children[1].metrics); |
| 112 |
test.ok(node === tree.getNode('main' + SEP)); |
| 113 |
node = tree.root.children[1]; |
| 114 |
test.equal(SEP + ['tmp', 'lib', 'util'].join(SEP) + SEP, node.fullPath()); |
| 115 |
test.equal('util' + SEP, node.displayShortName()); |
| 116 |
test.equal(1, node.children.length); |
| 117 |
test.deepEqual(utilSummary, node.metrics); |
| 118 |
test.deepEqual(s3, node.children[0].metrics); |
| 119 |
test.ok(node === tree.getNode('util' + SEP)); |
| 120 |
test.ok(tree.getNode(path.join('main', 'foo.js'))); |
| 121 |
test.ok(tree.getNode(path.join('main', 'bar.js'))); |
| 122 |
test.ok(tree.getNode(path.join('util', 'baz.js'))); |
| 123 |
test.done(); |
| 124 |
} |
| 125 |
}, |
| 126 |
"with no room for hoisting": { |
| 127 |
setUp: function (cb) { |
| 128 |
summarizer.addFileCoverageSummary(SEP + 'foo.js', s1); |
| 129 |
summarizer.addFileCoverageSummary(SEP + 'bar.js', s2); |
| 130 |
summarizer.addFileCoverageSummary(SEP + path.join('util', 'baz.js'), s3); |
| 131 |
tree = summarizer.getTreeSummary(); |
| 132 |
//console.log(JSON.stringify(tree, undefined, 2)); |
| 133 |
cb(); |
| 134 |
}, |
| 135 |
"should build a correct tree but with a mangled name for the main dir": function (test) { |
| 136 |
var node = tree.root, |
| 137 |
utilSummary = utils.mergeSummaryObjects(s3), |
| 138 |
libSummary = utils.mergeSummaryObjects(s1, s2), |
| 139 |
fullSummary = utils.mergeSummaryObjects(utilSummary, libSummary); |
| 140 |
test.equal(SEP, node.fullPath()); |
| 141 |
test.equal('', node.displayShortName()); |
| 142 |
test.equal(2, node.children.length); |
| 143 |
test.deepEqual(fullSummary, node.metrics); |
| 144 |
test.ok(node === tree.getNode('')); |
| 145 |
node = tree.root.children[0]; |
| 146 |
test.equal(SEP + '__root__' + SEP, node.fullPath()); |
| 147 |
test.equal('__root__' + SEP, node.displayShortName()); |
| 148 |
test.equal(2, node.children.length); |
| 149 |
test.deepEqual(libSummary, node.metrics); |
| 150 |
test.deepEqual(s2, node.children[0].metrics); |
| 151 |
test.deepEqual(s1, node.children[1].metrics); |
| 152 |
test.ok(node === tree.getNode('__root__' + SEP)); |
| 153 |
node = tree.root.children[1]; |
| 154 |
test.equal(SEP + 'util' + SEP, node.fullPath()); |
| 155 |
test.equal('util' + SEP, node.displayShortName()); |
| 156 |
test.equal(1, node.children.length); |
| 157 |
test.deepEqual(utilSummary, node.metrics); |
| 158 |
test.deepEqual(s3, node.children[0].metrics); |
| 159 |
test.ok(node === tree.getNode('util' + SEP)); |
| 160 |
test.ok(tree.getNode('foo.js')); |
| 161 |
test.ok(tree.getNode('bar.js')); |
| 162 |
test.ok(tree.getNode(path.join('util', 'baz.js'))); |
| 163 |
test.done(); |
| 164 |
} |
| 165 |
}, |
| 166 |
"with no summaries provided at all": { |
| 167 |
setUp: function (cb) { |
| 168 |
tree = summarizer.getTreeSummary(); |
| 169 |
cb(); |
| 170 |
}, |
| 171 |
"should build a blank tree with a root": function (test) { |
| 172 |
var node = tree.root, |
| 173 |
blank = utils.blankSummary(); |
| 174 |
|
| 175 |
blank.statements.pct = blank.lines.pct = blank.branches.pct = blank.functions.pct = 100; |
| 176 |
test.ok(node); |
| 177 |
test.equal(SEP, node.fullPath()); |
| 178 |
test.equal('', node.displayShortName()); |
| 179 |
test.equal(0, node.children.length); |
| 180 |
test.deepEqual(blank, node.metrics); |
| 181 |
test.done(); |
| 182 |
} |
| 183 |
} |
| 184 |
}; |