Skip to content

Commit 6eec528

Browse files
authored
Merge pull request #1449 from UziTech/use-htmldiffer
Update tests to node 4 syntax
2 parents d069d0d + 0cd0333 commit 6eec528

File tree

5 files changed

+151
-185
lines changed

5 files changed

+151
-185
lines changed

jasmine.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"**/*-spec.js"
55
],
66
"helpers": [
7-
"helpers/**/*.js"
7+
"helpers/helpers.js"
88
],
99
"stopSpecOnExpectationFailure": false,
1010
"random": true

test/helpers/helpers.js

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
const marked = require('../../');
2-
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
3-
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});
4-
5-
const EXCERPT_LENGTH = 30;
2+
const htmlDiffer = require('./html-differ.js');
63

74
beforeEach(() => {
85
marked.setOptions(marked.getDefaults());
@@ -16,25 +13,10 @@ beforeEach(() => {
1613
result.pass = htmlDiffer.isEqual(expected, actual);
1714

1815
if (result.pass) {
19-
result.message = spec.markdown + '\n------\n\nExpected: Should Fail';
16+
result.message = `${spec.markdown}\n------\n\nExpected: Should Fail`;
2017
} else {
21-
var expectedHtml = expected.replace(/\s/g, '');
22-
var actualHtml = actual.replace(/\s/g, '');
23-
24-
for (var i = 0; i < expectedHtml.length; i++) {
25-
if (actualHtml[i] !== expectedHtml[i]) {
26-
actualHtml = actualHtml.substring(
27-
Math.max(i - EXCERPT_LENGTH, 0),
28-
Math.min(i + EXCERPT_LENGTH, actualHtml.length));
29-
30-
expectedHtml = expectedHtml.substring(
31-
Math.max(i - EXCERPT_LENGTH, 0),
32-
Math.min(i + EXCERPT_LENGTH, expectedHtml.length));
33-
34-
break;
35-
}
36-
}
37-
result.message = 'Expected:\n' + expectedHtml + '\n\nActual:\n' + actualHtml;
18+
const diff = htmlDiffer.firstDiff(actual, expected);
19+
result.message = `Expected: ${diff.expected}\n Actual: ${diff.actual}`;
3820
}
3921
return result;
4022
}

test/helpers/html-differ.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
2+
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});
3+
4+
module.exports = {
5+
isEqual: htmlDiffer.isEqual.bind(htmlDiffer),
6+
firstDiff: (actual, expected, padding) => {
7+
padding = padding || 30;
8+
const result = htmlDiffer
9+
.diffHtml(actual, expected)
10+
.reduce((obj, diff) => {
11+
if (diff.added) {
12+
if (obj.firstIndex === null) {
13+
obj.firstIndex = obj.expected.length;
14+
}
15+
obj.expected += diff.value;
16+
} else if (diff.removed) {
17+
if (obj.firstIndex === null) {
18+
obj.firstIndex = obj.actual.length;
19+
}
20+
obj.actual += diff.value;
21+
} else {
22+
obj.actual += diff.value;
23+
obj.expected += diff.value;
24+
}
25+
26+
return obj;
27+
}, {
28+
firstIndex: null,
29+
actual: '',
30+
expected: ''
31+
});
32+
33+
return {
34+
actual: result.actual.substring(result.firstIndex - padding, result.firstIndex + padding),
35+
expected: result.expected.substring(result.firstIndex - padding, result.firstIndex + padding)
36+
};
37+
}
38+
};

0 commit comments

Comments
 (0)