Mark Eibes
2020-08-13T17:15:44Z

Diff.js

| "use strict"
| var diff2html = require("diff2html").Diff2Html;
| var unidiff = require('unidiff');
| var stringify = require('json-stable-stringify');
|
| exports.stableStringify = function(x) {
| return stringify(x);
| }
|
| exports.isJsonEqual = function(a) {
| return function(b) {
| return stringify(a) === stringify(b)
| }
| }
|
| exports.getPrettyHtml = function(filename) {
| return function(a) {
| return function(b) {
| var aa = stringify(a, {space: 4});
| var bb = stringify(b, {space: 4});
| var diff = unidiff.diffAsText(bb, aa, { aname: filename, bname: "actual"});
| return Diff2Html.getPrettyHtml(
| diff,
| {inputFormat: 'diff', showFiles: true, matching: 'lines', outputFormat: 'side-by-side'}
| );
| };
| };
| }

Diff.purs

| module Test.Spec.Assertions.Diff
| ( shouldHaveNoDiff
| , shouldBeGolden
| , Actual(..)
| , Expected(..)
| , GoldenFile(..)
| , (=|=)
| ) where
|
| import Prelude
| import Data.Newtype (class Newtype)
| import Effect.Aff (Aff)
| import Foreign (Foreign)
| import Node.Encoding (Encoding(..))
| import Node.FS.Aff (exists, writeTextFile)
| import Test.Spec.Assertions (fail)
| import Test.Spec.File (fileAsForeign, writeToTmpFile)
|
| foreign import getPrettyHtml ∷ String -> Foreign -> Foreign -> String
|
| foreign import isJsonEqual ∷ Foreign -> Foreign -> Boolean
|
| foreign import stableStringify ∷ Foreign -> String
|
| newtype Actual = Actual Foreign
|
| derive instance ntActual ∷ Newtype Actual _
| newtype Expected = Expected Foreign
|
| derive instance ntExpected ∷ Newtype Expected _
| newtype GoldenFile = GoldenFile String
|
| derive instance ntGoldenFile ∷ Newtype GoldenFile _
| infix 4 shouldHaveNoDiff as =|=
|
| shouldBeGolden ∷ Actual -> GoldenFile -> Aff Unit
| shouldBeGolden actual@(Actual content) (GoldenFile path) = do
| exist <- exists path
| if exist then do
| expected <- fileAsForeign path
| shouldHaveNoDiff' actual path (Expected expected)
| else do
| writeTextFile UTF8 path (stableStringify content)
| fail $ "Recreated " <> path
|
| shouldHaveNoDiff' ∷ Actual -> String -> Expected -> Aff Unit
| shouldHaveNoDiff' (Actual v1) expectedName (Expected v2) =
| unless (v1 `isJsonEqual` v2)
| $ do
| let
| before =
| """
|
|
| <link rel="stylesheet"
| type="text/css"
| href="https://cdnjs.cloudflare.com/ajax/libs/diff2html/2.5.0/diff2html.min.css">
|
| .d2h-code-side-linenumber, .d2h-code-line-prefix {
| -webkit-touch-callout: none;
| -webkit-user-select: none;
| -khtml-user-select: none;
| -moz-user-select: none;
| -ms-user-select: none;
| user-select: none;
| }
|
| diff2html
|
|
|
| """
| let
| after = " "
| let
| diff = getPrettyHtml expectedName v1 v2
| path <- writeToTmpFile $ before <> diff <> after
| fail <<< show $ "The two JSON values are different. The diff is saved here: file://" <> path
|
| shouldHaveNoDiff ∷ Actual -> Expected -> Aff Unit
| shouldHaveNoDiff a = shouldHaveNoDiff' a "expected"