Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to node 4 syntax #1449

Merged
merged 5 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jasmine.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"**/*-spec.js"
],
"helpers": [
"helpers/**/*.js"
"helpers/helpers.js"
],
"stopSpecOnExpectationFailure": false,
"random": true
Expand Down
26 changes: 4 additions & 22 deletions test/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const marked = require('../../');
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});

const EXCERPT_LENGTH = 30;
const htmlDiffer = require('./html-differ.js');

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

if (result.pass) {
result.message = spec.markdown + '\n------\n\nExpected: Should Fail';
result.message = `${spec.markdown}\n------\n\nExpected: Should Fail`;
} else {
var expectedHtml = expected.replace(/\s/g, '');
var actualHtml = actual.replace(/\s/g, '');

for (var i = 0; i < expectedHtml.length; i++) {
if (actualHtml[i] !== expectedHtml[i]) {
actualHtml = actualHtml.substring(
Math.max(i - EXCERPT_LENGTH, 0),
Math.min(i + EXCERPT_LENGTH, actualHtml.length));

expectedHtml = expectedHtml.substring(
Math.max(i - EXCERPT_LENGTH, 0),
Math.min(i + EXCERPT_LENGTH, expectedHtml.length));

break;
}
}
result.message = 'Expected:\n' + expectedHtml + '\n\nActual:\n' + actualHtml;
const diff = htmlDiffer.firstDiff(actual, expected);
styfle marked this conversation as resolved.
Show resolved Hide resolved
result.message = `Expected: ${diff.expected}\n Actual: ${diff.actual}`;
}
return result;
}
Expand Down
38 changes: 38 additions & 0 deletions test/helpers/html-differ.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});

module.exports = {
isEqual: htmlDiffer.isEqual.bind(htmlDiffer),
firstDiff: (actual, expected, padding) => {
padding = padding || 30;
const result = htmlDiffer
.diffHtml(actual, expected)
.reduce((obj, diff) => {
if (diff.added) {
if (obj.firstIndex === null) {
obj.firstIndex = obj.expected.length;
}
obj.expected += diff.value;
} else if (diff.removed) {
if (obj.firstIndex === null) {
obj.firstIndex = obj.actual.length;
}
obj.actual += diff.value;
} else {
obj.actual += diff.value;
obj.expected += diff.value;
}

return obj;
}, {
firstIndex: null,
actual: '',
expected: ''
});

return {
actual: result.actual.substring(result.firstIndex - padding, result.firstIndex + padding),
expected: result.expected.substring(result.firstIndex - padding, result.firstIndex + padding)
};
}
};
Loading