From b9d99af59d63543ea60e2abe2d98ef1e8e799616 Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Sat, 9 Mar 2019 23:39:50 -0600 Subject: [PATCH] add toRender matcher --- test/helpers/helpers.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/test/helpers/helpers.js b/test/helpers/helpers.js index d9244a880b..25f0f4b63c 100644 --- a/test/helpers/helpers.js +++ b/test/helpers/helpers.js @@ -1,5 +1,42 @@ -var marked = require('../../lib/marked.js'); +const marked = require('../../'); +const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer; +const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true}); beforeEach(function () { marked.setOptions(marked.getDefaults()); + + jasmine.addMatchers({ + toRender: function () { + return { + compare: function (spec, expected) { + const result = {}; + const actual = marked(spec.markdown, spec.options); + result.pass = htmlDiffer.isEqual(expected, actual); + + if (result.pass) { + 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 - 30, 0), + Math.min(i + 30, actualHtml.length)); + + expectedHtml = expectedHtml.substring( + Math.max(i - 30, 0), + Math.min(i + 30, expectedHtml.length)); + + break; + } + } + result.message = 'Expected:\n' + expectedHtml + '\n\nActual:\n' + actualHtml; + } + return result; + } + }; + } + }); });