Skip to content

Commit e042948

Browse files
authored
test: fix printing of non-JSON values (#91)
1 parent dc85ee6 commit e042948

File tree

6 files changed

+254
-4
lines changed

6 files changed

+254
-4
lines changed

test/Test.mjs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import * as Fs from "fs";
44
import * as Path from "path";
5+
import * as Util from "util";
56
import * as Curry from "rescript/lib/es6/curry.js";
7+
import * as Core__Option from "../src/Core__Option.mjs";
68
import * as CodeFrame from "@babel/code-frame";
79

810
var dirname = (new URL('.', import.meta.url).pathname);
@@ -26,6 +28,17 @@ function cleanUpStackTrace(stack) {
2628
}).join("\n");
2729
}
2830

31+
function print(value) {
32+
var match = typeof value;
33+
if (match === "object" || match === "bigint") {
34+
return Util.inspect(value);
35+
} else if (match === "string") {
36+
return Core__Option.getExn(JSON.stringify(value));
37+
} else {
38+
return String(value);
39+
}
40+
}
41+
2942
function run(loc, left, comparator, right) {
3043
if (Curry._2(comparator, left, right)) {
3144
return ;
@@ -36,8 +49,8 @@ function run(loc, left, comparator, right) {
3649
var fileContent = Fs.readFileSync(Path.join(dirname, file), {
3750
encoding: "utf-8"
3851
});
39-
var left$1 = JSON.stringify(left);
40-
var right$1 = JSON.stringify(right);
52+
var left$1 = print(left);
53+
var right$1 = print(right);
4154
var codeFrame = CodeFrame.codeFrameColumns(fileContent, {
4255
start: {
4356
line: line
@@ -55,6 +68,7 @@ function run(loc, left, comparator, right) {
5568
export {
5669
dirname ,
5770
cleanUpStackTrace ,
71+
print ,
5872
run ,
5973
}
6074
/* dirname Not a pure module */

test/Test.res

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,20 @@ let cleanUpStackTrace = stack => {
3434
->Array.joinWith("\n")
3535
}
3636

37+
@val @module("util") external inspect: _ => string = "inspect"
38+
let print = value =>
39+
switch Type.typeof(value) {
40+
| #string => JSON.stringifyAny(value)->Option.getExn // uses " instead of '
41+
| #object | #bigint => inspect(value)
42+
| _ => String.make(value)
43+
}
44+
3745
let run = (loc, left, comparator, right) => {
3846
if !comparator(left, right) {
3947
let ((file, line, _, _), _) = loc
4048
let fileContent = readFileSync(join(dirname, file), {"encoding": "utf-8"})
41-
let left = JSON.stringifyAny(left)
42-
let right = JSON.stringifyAny(right)
49+
let left = print(left)
50+
let right = print(right)
4351
let codeFrame = codeFrameColumns(
4452
fileContent,
4553
{"start": {"line": line}},

test/TestSuite.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as IntTests from "./IntTests.mjs";
4+
import * as TestTests from "./TestTests.mjs";
45
import * as ArrayTests from "./ArrayTests.mjs";
56
import * as ErrorTests from "./ErrorTests.mjs";
67
import * as PromiseTest from "./PromiseTest.mjs";
78

9+
var bign = TestTests.bign;
10+
811
var TestError = PromiseTest.TestError;
912

1013
var fail = PromiseTest.fail;
@@ -28,6 +31,7 @@ var eq = IntTests.eq;
2831
var $$catch = IntTests.$$catch;
2932

3033
export {
34+
bign ,
3135
TestError ,
3236
fail ,
3337
equal ,

test/TestSuite.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
include TestTests
12
include PromiseTest
23
include ErrorTests
34
include ArrayTests

test/TestTests.mjs

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
5+
import * as Pervasives from "rescript/lib/es6/pervasives.js";
6+
7+
var eq = Caml_obj.equal;
8+
9+
var bign = BigInt(Number.MAX_VALUE);
10+
11+
var bign$1 = bign + bign;
12+
13+
Test.run([
14+
[
15+
"TestTests.res",
16+
8,
17+
20,
18+
32
19+
],
20+
"print null"
21+
], Test.print(null), eq, "null");
22+
23+
Test.run([
24+
[
25+
"TestTests.res",
26+
9,
27+
20,
28+
37
29+
],
30+
"print undefined"
31+
], Test.print(undefined), eq, "undefined");
32+
33+
Test.run([
34+
[
35+
"TestTests.res",
36+
10,
37+
20,
38+
31
39+
],
40+
"print NaN"
41+
], Test.print(Number.NaN), eq, "NaN");
42+
43+
Test.run([
44+
[
45+
"TestTests.res",
46+
11,
47+
20,
48+
36
49+
],
50+
"print infinity"
51+
], Test.print(Pervasives.infinity), eq, "Infinity");
52+
53+
Test.run([
54+
[
55+
"TestTests.res",
56+
12,
57+
20,
58+
29
59+
],
60+
"print 0"
61+
], Test.print(0), eq, "0");
62+
63+
Test.run([
64+
[
65+
"TestTests.res",
66+
13,
67+
20,
68+
31
69+
],
70+
"print int"
71+
], Test.print(42), eq, "42");
72+
73+
Test.run([
74+
[
75+
"TestTests.res",
76+
14,
77+
20,
78+
33
79+
],
80+
"print float"
81+
], Test.print(4.2), eq, "4.2");
82+
83+
Test.run([
84+
[
85+
"TestTests.res",
86+
15,
87+
20,
88+
34
89+
],
90+
"print string"
91+
], Test.print("foo"), eq, "\"foo\"");
92+
93+
Test.run([
94+
[
95+
"TestTests.res",
96+
16,
97+
20,
98+
32
99+
],
100+
"print bool"
101+
], Test.print(true), eq, "true");
102+
103+
Test.run([
104+
[
105+
"TestTests.res",
106+
17,
107+
20,
108+
34
109+
],
110+
"print object"
111+
], Test.print({
112+
x: 42
113+
}), eq, "{ x: 42 }");
114+
115+
Test.run([
116+
[
117+
"TestTests.res",
118+
18,
119+
20,
120+
33
121+
],
122+
"print array"
123+
], Test.print([
124+
1,
125+
2,
126+
3
127+
]), eq, "[ 1, 2, 3 ]");
128+
129+
Test.run([
130+
[
131+
"TestTests.res",
132+
19,
133+
20,
134+
34
135+
],
136+
"print symbol"
137+
], Test.print(Symbol("foo")), eq, "Symbol(foo)");
138+
139+
Test.run([
140+
[
141+
"TestTests.res",
142+
21,
143+
13,
144+
29
145+
],
146+
"print function"
147+
], Test.print(function (param) {
148+
return 42;
149+
}), eq, "function (param) {\n return 42;\n }");
150+
151+
Test.run([
152+
[
153+
"TestTests.res",
154+
26,
155+
20,
156+
40
157+
],
158+
"print es6 function"
159+
], Test.print((() => 42)), eq, "() => 42");
160+
161+
Test.run([
162+
[
163+
"TestTests.res",
164+
28,
165+
13,
166+
27
167+
],
168+
"print bigint"
169+
], Test.print(bign$1), eq, "359538626972463141629054847463408713596141135051689993197834953606314521560057077521179117265533756343080917907028764928468642653778928365536935093407075033972099821153102564152490980180778657888151737016910267884609166473806445896331617118664246696549595652408289446337476354361838599762500808052368249716736n");
170+
171+
Test.run([
172+
[
173+
"TestTests.res",
174+
33,
175+
20,
176+
31
177+
],
178+
"print set"
179+
], Test.print(new Set([
180+
1,
181+
2,
182+
2,
183+
3
184+
])), eq, "Set(3) { 1, 2, 3 }");
185+
186+
export {
187+
eq ,
188+
bign$1 as bign,
189+
}
190+
/* bign Not a pure module */

test/TestTests.res

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
let bign = BigInt.fromFloat(Float.Constants.maxValue)
6+
let bign = BigInt.add(bign, bign)
7+
8+
Test.run(__POS_OF__("print null"), Test.print(null), eq, "null")
9+
Test.run(__POS_OF__("print undefined"), Test.print(undefined), eq, "undefined")
10+
Test.run(__POS_OF__("print NaN"), Test.print(nan), eq, "NaN")
11+
Test.run(__POS_OF__("print infinity"), Test.print(infinity), eq, "Infinity")
12+
Test.run(__POS_OF__("print 0"), Test.print(0), eq, "0")
13+
Test.run(__POS_OF__("print int"), Test.print(42), eq, "42")
14+
Test.run(__POS_OF__("print float"), Test.print(4.2), eq, "4.2")
15+
Test.run(__POS_OF__("print string"), Test.print("foo"), eq, `"foo"`)
16+
Test.run(__POS_OF__("print bool"), Test.print(true), eq, "true")
17+
Test.run(__POS_OF__("print object"), Test.print({"x": 42}), eq, `{ x: 42 }`)
18+
Test.run(__POS_OF__("print array"), Test.print([1, 2, 3]), eq, "[ 1, 2, 3 ]")
19+
Test.run(__POS_OF__("print symbol"), Test.print(Symbol.make("foo")), eq, "Symbol(foo)")
20+
Test.run(
21+
__POS_OF__("print function"),
22+
Test.print(() => 42),
23+
eq,
24+
"function (param) {\n return 42;\n }",
25+
)
26+
Test.run(__POS_OF__("print es6 function"), Test.print(%raw("() => 42")), eq, "() => 42")
27+
Test.run(
28+
__POS_OF__("print bigint"),
29+
Test.print(bign),
30+
eq,
31+
"359538626972463141629054847463408713596141135051689993197834953606314521560057077521179117265533756343080917907028764928468642653778928365536935093407075033972099821153102564152490980180778657888151737016910267884609166473806445896331617118664246696549595652408289446337476354361838599762500808052368249716736n",
32+
)
33+
Test.run(__POS_OF__("print set"), Test.print(Set.fromArray([1, 2, 2, 3])), eq, "Set(3) { 1, 2, 3 }")

0 commit comments

Comments
 (0)