Skip to content

test: fix printing of non-JSON values #91

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

Merged
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
18 changes: 16 additions & 2 deletions test/Test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 ;
Expand All @@ -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
Expand All @@ -55,6 +68,7 @@ function run(loc, left, comparator, right) {
export {
dirname ,
cleanUpStackTrace ,
print ,
run ,
}
/* dirname Not a pure module */
12 changes: 10 additions & 2 deletions test/Test.res
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand Down
4 changes: 4 additions & 0 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,6 +31,7 @@ var eq = IntTests.eq;
var $$catch = IntTests.$$catch;

export {
bign ,
TestError ,
fail ,
equal ,
Expand Down
1 change: 1 addition & 0 deletions test/TestSuite.res
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include TestTests
include PromiseTest
include ErrorTests
include ArrayTests
Expand Down
190 changes: 190 additions & 0 deletions test/TestTests.mjs
Original file line number Diff line number Diff line change
@@ -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 */
33 changes: 33 additions & 0 deletions test/TestTests.res
Original file line number Diff line number Diff line change
@@ -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 }")