-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_spec.js
More file actions
113 lines (89 loc) · 3.98 KB
/
tree_spec.js
File metadata and controls
113 lines (89 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// higher level spec
var vows = require('vows'),
assert = require('assert');
var TreeModule = require('./tree');
var addSumsToTable = TreeModule.addSumsToTable;
// utility function to make a table slightly more readable than a tab-delimited list
var makeTable = function (lines) {
var out = [];
var i;
for (i=0; i<lines.length; i++) {
out.push(lines[i].replace(/ /g, "\t"));
}
return out.join("\n");
};
vows.describe('Node Summarizer').addBatch({
'Calculating sub-tree sums': {
'when input is empty': {
topic: addSumsToTable(""),
'should output empty string': function (data) {
assert.equal(data.output, "");
}
},
'when header contains no numbers': {
topic: addSumsToTable("some\theader\ttext"),
'should ignore header and return empty result': function (data) {
assert.equal(data.output, "");
}
},
'when we have a single row with integer value': {
topic: addSumsToTable(makeTable([" root 10"])),
'should add value as sum of self': function (data) {
assert.equal(data.output, makeTable([" root 10 10"]));
}
},
'when we have an extra line at end': {
topic: addSumsToTable(makeTable([" root 15", ""])),
'should ignore extra empty line': function (data) {
assert.equal(data.output, makeTable([" root 15 15"]));
}
},
'when we have an extra line in the middle': {
topic: addSumsToTable(makeTable([" root 15", "", "root child 20"])),
'should ignore extra empty line': function (data) {
assert.equal(data.output, makeTable([" root 15 35",
"root child 20 20"]));
}
},
'when input data has both newline and linefeed': {
topic: addSumsToTable("\troot\t10\r\nroot\tchild\t15"),
'should output data delimited with normal newlines': function (data) {
assert.equal(data.output, makeTable([" root 10 25",
"root child 15 15"]));
}
},
'when input data has both linefeed instead of newline': {
topic: addSumsToTable("\troot\t10\rroot\tchild\t15"),
'should output data delimited with normal newlines': function (data) {
assert.equal(data.output, makeTable([" root 10 25",
"root child 15 15"]));
}
},
'when we have two rows with integer values': {
topic: addSumsToTable(makeTable([" root 10",
"root child 15"])),
'add sum to both rows to parent': function (data) {
assert.equal(data.output, makeTable([" root 10 25",
"root child 15 15"]));
}
},
'when we have two rows with child first': {
topic: addSumsToTable(makeTable(["root child 15",
" root 10"])),
'add sum to both rows, keeping order': function (data) {
assert.equal(data.output, makeTable(["root child 15 15",
" root 10 25"]));
}
},
'when we have two children with comma based decimal values': {
topic: addSumsToTable(makeTable([" root 10,5",
"root child1 15,3",
"root child2 25,4"])),
'add sum to both rows, keeping order': function (data) {
assert.equal(data.output, makeTable([" root 10,5 51,2",
"root child1 15,3 15,3",
"root child2 25,4 25,4"]));
}
}
}
}).run();