Skip to content

Instantly share code, notes, and snippets.

@i-am-the-slime
Created August 13, 2020 17:15
Show Gist options
  • Save i-am-the-slime/08d3f565fb3b589b4586d4976094b51e to your computer and use it in GitHub Desktop.
Save i-am-the-slime/08d3f565fb3b589b4586d4976094b51e to your computer and use it in GitHub Desktop.
"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'}
);
};
};
}
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 =
"""<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet"
type="text/css"
href="https://cdnjs.cloudflare.com/ajax/libs/diff2html/2.5.0/diff2html.min.css">
<style>
.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;
}
</style>
<title>diff2html</title>
</head>
<body>
<div>
"""
let
after = "</div></body></html>"
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment