From 31154d64a52dcb4dffea308bea8b2bf8193b9654 Mon Sep 17 00:00:00 2001 From: glennsl Date: Mon, 6 Mar 2023 21:34:41 +0100 Subject: [PATCH] test: fix printing of non-JSON values --- test/Test.mjs | 18 ++++- test/Test.res | 12 ++- test/TestSuite.mjs | 4 + test/TestSuite.res | 1 + test/TestTests.mjs | 190 +++++++++++++++++++++++++++++++++++++++++++++ test/TestTests.res | 33 ++++++++ 6 files changed, 254 insertions(+), 4 deletions(-) create mode 100644 test/TestTests.mjs create mode 100644 test/TestTests.res diff --git a/test/Test.mjs b/test/Test.mjs index e5481d6b..3dffa8f0 100644 --- a/test/Test.mjs +++ b/test/Test.mjs @@ -2,7 +2,9 @@ import * as Fs from "fs"; import * as Path from "path"; +import * as Util from "util"; import * as Curry from "rescript/lib/es6/curry.js"; +import * as Core__Option from "../src/Core__Option.mjs"; import * as CodeFrame from "@babel/code-frame"; var dirname = (new URL('.', import.meta.url).pathname); @@ -26,6 +28,17 @@ function cleanUpStackTrace(stack) { }).join("\n"); } +function print(value) { + var match = typeof value; + if (match === "object" || match === "bigint") { + return Util.inspect(value); + } else if (match === "string") { + return Core__Option.getExn(JSON.stringify(value)); + } else { + return String(value); + } +} + function run(loc, left, comparator, right) { if (Curry._2(comparator, left, right)) { return ; @@ -36,8 +49,8 @@ function run(loc, left, comparator, right) { var fileContent = Fs.readFileSync(Path.join(dirname, file), { encoding: "utf-8" }); - var left$1 = JSON.stringify(left); - var right$1 = JSON.stringify(right); + var left$1 = print(left); + var right$1 = print(right); var codeFrame = CodeFrame.codeFrameColumns(fileContent, { start: { line: line @@ -55,6 +68,7 @@ function run(loc, left, comparator, right) { export { dirname , cleanUpStackTrace , + print , run , } /* dirname Not a pure module */ diff --git a/test/Test.res b/test/Test.res index e229eb1f..8a7c325f 100644 --- a/test/Test.res +++ b/test/Test.res @@ -34,12 +34,20 @@ let cleanUpStackTrace = stack => { ->Array.joinWith("\n") } +@val @module("util") external inspect: _ => string = "inspect" +let print = value => + switch Type.typeof(value) { + | #string => JSON.stringifyAny(value)->Option.getExn // uses " instead of ' + | #object | #bigint => inspect(value) + | _ => String.make(value) + } + let run = (loc, left, comparator, right) => { if !comparator(left, right) { let ((file, line, _, _), _) = loc let fileContent = readFileSync(join(dirname, file), {"encoding": "utf-8"}) - let left = JSON.stringifyAny(left) - let right = JSON.stringifyAny(right) + let left = print(left) + let right = print(right) let codeFrame = codeFrameColumns( fileContent, {"start": {"line": line}}, diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index 77733822..c968bd8f 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -1,10 +1,13 @@ // Generated by ReScript, PLEASE EDIT WITH CARE import * as IntTests from "./IntTests.mjs"; +import * as TestTests from "./TestTests.mjs"; import * as ArrayTests from "./ArrayTests.mjs"; import * as ErrorTests from "./ErrorTests.mjs"; import * as PromiseTest from "./PromiseTest.mjs"; +var bign = TestTests.bign; + var TestError = PromiseTest.TestError; var fail = PromiseTest.fail; @@ -28,6 +31,7 @@ var eq = IntTests.eq; var $$catch = IntTests.$$catch; export { + bign , TestError , fail , equal , diff --git a/test/TestSuite.res b/test/TestSuite.res index 4185534f..6277bf57 100644 --- a/test/TestSuite.res +++ b/test/TestSuite.res @@ -1,3 +1,4 @@ +include TestTests include PromiseTest include ErrorTests include ArrayTests diff --git a/test/TestTests.mjs b/test/TestTests.mjs new file mode 100644 index 00000000..20de5b92 --- /dev/null +++ b/test/TestTests.mjs @@ -0,0 +1,190 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Test from "./Test.mjs"; +import * as Caml_obj from "rescript/lib/es6/caml_obj.js"; +import * as Pervasives from "rescript/lib/es6/pervasives.js"; + +var eq = Caml_obj.equal; + +var bign = BigInt(Number.MAX_VALUE); + +var bign$1 = bign + bign; + +Test.run([ + [ + "TestTests.res", + 8, + 20, + 32 + ], + "print null" + ], Test.print(null), eq, "null"); + +Test.run([ + [ + "TestTests.res", + 9, + 20, + 37 + ], + "print undefined" + ], Test.print(undefined), eq, "undefined"); + +Test.run([ + [ + "TestTests.res", + 10, + 20, + 31 + ], + "print NaN" + ], Test.print(Number.NaN), eq, "NaN"); + +Test.run([ + [ + "TestTests.res", + 11, + 20, + 36 + ], + "print infinity" + ], Test.print(Pervasives.infinity), eq, "Infinity"); + +Test.run([ + [ + "TestTests.res", + 12, + 20, + 29 + ], + "print 0" + ], Test.print(0), eq, "0"); + +Test.run([ + [ + "TestTests.res", + 13, + 20, + 31 + ], + "print int" + ], Test.print(42), eq, "42"); + +Test.run([ + [ + "TestTests.res", + 14, + 20, + 33 + ], + "print float" + ], Test.print(4.2), eq, "4.2"); + +Test.run([ + [ + "TestTests.res", + 15, + 20, + 34 + ], + "print string" + ], Test.print("foo"), eq, "\"foo\""); + +Test.run([ + [ + "TestTests.res", + 16, + 20, + 32 + ], + "print bool" + ], Test.print(true), eq, "true"); + +Test.run([ + [ + "TestTests.res", + 17, + 20, + 34 + ], + "print object" + ], Test.print({ + x: 42 + }), eq, "{ x: 42 }"); + +Test.run([ + [ + "TestTests.res", + 18, + 20, + 33 + ], + "print array" + ], Test.print([ + 1, + 2, + 3 + ]), eq, "[ 1, 2, 3 ]"); + +Test.run([ + [ + "TestTests.res", + 19, + 20, + 34 + ], + "print symbol" + ], Test.print(Symbol("foo")), eq, "Symbol(foo)"); + +Test.run([ + [ + "TestTests.res", + 21, + 13, + 29 + ], + "print function" + ], Test.print(function (param) { + return 42; + }), eq, "function (param) {\n return 42;\n }"); + +Test.run([ + [ + "TestTests.res", + 26, + 20, + 40 + ], + "print es6 function" + ], Test.print((() => 42)), eq, "() => 42"); + +Test.run([ + [ + "TestTests.res", + 28, + 13, + 27 + ], + "print bigint" + ], Test.print(bign$1), eq, "359538626972463141629054847463408713596141135051689993197834953606314521560057077521179117265533756343080917907028764928468642653778928365536935093407075033972099821153102564152490980180778657888151737016910267884609166473806445896331617118664246696549595652408289446337476354361838599762500808052368249716736n"); + +Test.run([ + [ + "TestTests.res", + 33, + 20, + 31 + ], + "print set" + ], Test.print(new Set([ + 1, + 2, + 2, + 3 + ])), eq, "Set(3) { 1, 2, 3 }"); + +export { + eq , + bign$1 as bign, +} +/* bign Not a pure module */ diff --git a/test/TestTests.res b/test/TestTests.res new file mode 100644 index 00000000..754de992 --- /dev/null +++ b/test/TestTests.res @@ -0,0 +1,33 @@ +open RescriptCore + +let eq = (a, b) => a == b + +let bign = BigInt.fromFloat(Float.Constants.maxValue) +let bign = BigInt.add(bign, bign) + +Test.run(__POS_OF__("print null"), Test.print(null), eq, "null") +Test.run(__POS_OF__("print undefined"), Test.print(undefined), eq, "undefined") +Test.run(__POS_OF__("print NaN"), Test.print(nan), eq, "NaN") +Test.run(__POS_OF__("print infinity"), Test.print(infinity), eq, "Infinity") +Test.run(__POS_OF__("print 0"), Test.print(0), eq, "0") +Test.run(__POS_OF__("print int"), Test.print(42), eq, "42") +Test.run(__POS_OF__("print float"), Test.print(4.2), eq, "4.2") +Test.run(__POS_OF__("print string"), Test.print("foo"), eq, `"foo"`) +Test.run(__POS_OF__("print bool"), Test.print(true), eq, "true") +Test.run(__POS_OF__("print object"), Test.print({"x": 42}), eq, `{ x: 42 }`) +Test.run(__POS_OF__("print array"), Test.print([1, 2, 3]), eq, "[ 1, 2, 3 ]") +Test.run(__POS_OF__("print symbol"), Test.print(Symbol.make("foo")), eq, "Symbol(foo)") +Test.run( + __POS_OF__("print function"), + Test.print(() => 42), + eq, + "function (param) {\n return 42;\n }", +) +Test.run(__POS_OF__("print es6 function"), Test.print(%raw("() => 42")), eq, "() => 42") +Test.run( + __POS_OF__("print bigint"), + Test.print(bign), + eq, + "359538626972463141629054847463408713596141135051689993197834953606314521560057077521179117265533756343080917907028764928468642653778928365536935093407075033972099821153102564152490980180778657888151737016910267884609166473806445896331617118664246696549595652408289446337476354361838599762500808052368249716736n", +) +Test.run(__POS_OF__("print set"), Test.print(Set.fromArray([1, 2, 2, 3])), eq, "Set(3) { 1, 2, 3 }")