forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock-regression-test.js
140 lines (120 loc) · 4.6 KB
/
stock-regression-test.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var grunt = require("grunt");
var phantomjs = require('grunt-lib-phantomjs').init(grunt);
require("./difflib.js");
module.exports = {
testStockExample: function (asyncDone, showDiff) {
var passed = false;
process.env.TZ = "UTC";
phantomjs.on('rendered', function(pageStr) {
require("fs").readFile(__dirname + '/rendered-stock-fixture.html', function (err, data) {
var fixtureStr = data.toString();
if (err) {
grunt.log.error("Failed to open stock example.");
} else {
var diffs = diffPages(fixtureStr, pageStr);
if (diffs.length > 0) {
grunt.log.error("Failed comparison to stock example.");
grunt.log.error("If these changes are intentional, please run `grunt update-stock-example` to overwrite the fixture.");
if (showDiff) {
grunt.log.writeln("\n" + diffs + "\n");
} else {
grunt.log.error("Run `grunt test-stock-example:diff` to see differences.");
}
} else {
grunt.log.writeln("Passed comparison to stock example.");
passed = true;
}
}
phantomjs.halt();
});
});
phantomjs.spawn('web/index.html', {
options: {
inject: __dirname + "/inject-serializer.js"
},
done: function () {
if (!passed) {
grunt.fatal("Failed regression test.");
}
asyncDone();
}
});
},
updateStockExample: function (asyncDone) {
var ok = false;
process.env.TZ = "UTC";
phantomjs.on('rendered', function(pageStr) {
require("fs").writeFile(__dirname + '/rendered-stock-fixture.html', pageStr, function (err) {
if (!err) {
grunt.log.writeln("Overwrote stock example.");
ok = true;
}
phantomjs.halt();
});
});
phantomjs.spawn('web/index.html', {
options: {
inject: __dirname + "/inject-serializer.js"
},
done: function () {
if (!ok) {
grunt.fatal("Failed to overwrite stock example.");
}
asyncDone();
}
});
}
};
function diffPages(first, second) {
first = filterExceptions(first);
second = filterExceptions(second);
var firstLines = difflib.stringAsLines(first);
var secondLines = difflib.stringAsLines(second);
var seq = new difflib.SequenceMatcher(firstLines, secondLines);
var ops = seq.get_opcodes();
var diffs = [];
for (var i = 0; i < ops.length; i++) {
var op = ops[i];
var firstDiff = firstLines.slice(op[1], op[2]).join("\n");
var secondDiff = secondLines.slice(op[3], op[4]).join("\n");
if (op[0] === 'replace') {
if (!onlyDiffersByDelta(firstDiff, secondDiff, 0.01)) {
diffs.push("Replacement:");
diffs.push(firstDiff.red);
diffs.push(secondDiff.green);
}
} else if (op[0] === 'insert') {
diffs.push("Insertion:");
diffs.push(secondDiff.green);
} else if (op[0] === 'delete') {
diffs.push("Deletion:");
diffs.push(firstDiff.red);
}
}
return diffs.join("\n\n");
}
// TODO: remove use of clientHeight in legend and remove this function
function filterExceptions(fixtureStr) {
fixtureStr = fixtureStr.replace(/<text x="15" y="[0-9.]+">Monthly Index Move<\/text>/, "EXCEPTION");
fixtureStr = fixtureStr.replace(/<text x="15" y="[0-9.]+">Monthly Index Average<\/text>/, "EXCEPTION");
return fixtureStr;
}
function onlyDiffersByDelta(firstLine, secondLine, delta) {
var findNums = /(?:[0-9]*\.[0-9]+|[0-9]+)/g;
var firstNums = firstLine.match(findNums);
var secondNums = secondLine.match(findNums);
var firstWithoutNums = firstLine.replace(findNums, "NUMBER");
var secondWithoutNums = secondLine.replace(findNums, "NUMBER");
if (firstWithoutNums !== secondWithoutNums) {
return false;
}
if (secondNums.length !== firstNums.length) {
return false;
}
for (var i = 0; i < firstNums.length; i++) {
if (Math.abs(+firstNums[i] - +secondNums[i]) > delta) {
return false;
}
}
return true;
}