Skip to content

Commit 3f049c4

Browse files
committed
Add Core tests
1 parent 411b7b5 commit 3f049c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+6379
-0
lines changed

tests/tests/src/core/Core_ArrayTests.mjs

Lines changed: 477 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
Test.run(__POS_OF__("make"), Array.make(~length=6, 7), eq, [7, 7, 7, 7, 7, 7])
6+
7+
Test.run(__POS_OF__("getUnsafe - existing"), [0, 1, 2]->Array.getUnsafe(1), eq, 1)
8+
Test.run(__POS_OF__("getUnsafe - missing"), [0, 1, 2]->Array.getUnsafe(10), eq, %raw(`undefined`))
9+
10+
Test.run(
11+
__POS_OF__("fromInitializer"),
12+
Array.fromInitializer(~length=7, i => i + 3),
13+
eq,
14+
[3, 4, 5, 6, 7, 8, 9],
15+
)
16+
17+
Test.run(__POS_OF__("reduce"), Array.reduce([1, 2, 3], list{}, List.add), eq, list{3, 2, 1})
18+
Test.run(__POS_OF__("reduce - empty"), Array.reduce([], list{}, List.add), eq, list{})
19+
20+
Test.run(
21+
__POS_OF__("reduceWithIndex"),
22+
Array.reduceWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}),
23+
eq,
24+
list{5, 3, 1},
25+
)
26+
Test.run(
27+
__POS_OF__("reduceWithIndex - empty"),
28+
Array.reduceWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}),
29+
eq,
30+
list{},
31+
)
32+
33+
Test.run(
34+
__POS_OF__("reduceRight"),
35+
Array.reduceRight([1, 2, 3], list{}, List.add),
36+
eq,
37+
list{1, 2, 3},
38+
)
39+
Test.run(__POS_OF__("reduceRight - empty"), Array.reduceRight([], list{}, List.add), eq, list{})
40+
41+
Test.run(
42+
__POS_OF__("reduceEightWithIndex"),
43+
Array.reduceRightWithIndex([1, 2, 3], list{}, (acc, v, i) => list{v + i, ...acc}),
44+
eq,
45+
list{1, 3, 5},
46+
)
47+
Test.run(
48+
__POS_OF__("reduceWithIndex - empty"),
49+
Array.reduceRightWithIndex([], list{}, (acc, v, i) => list{v + i, ...acc}),
50+
eq,
51+
list{},
52+
)
53+
54+
Test.run(__POS_OF__("toShuffled - length"), Array.toShuffled([1, 2, 3])->Array.length, eq, 3)
55+
56+
Test.run(
57+
__POS_OF__("shuffle - length"),
58+
{
59+
let arr = [1, 2, 3]
60+
Array.shuffle(arr)
61+
arr->Array.length
62+
},
63+
eq,
64+
3,
65+
)
66+
67+
Test.run(
68+
__POS_OF__("filterMap"),
69+
Array.filterMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n * n) : None),
70+
eq,
71+
[4, 16, 36],
72+
)
73+
Test.run(__POS_OF__("filterMap - no match"), Array.filterMap([1, 2, 3, 4, 5, 6], _ => None), eq, [])
74+
Test.run(
75+
__POS_OF__("filterMap - empty"),
76+
Array.filterMap([], n => mod(n, 2) == 0 ? Some(n * n) : None),
77+
eq,
78+
[],
79+
)
80+
81+
Test.run(__POS_OF__("keepSome"), Array.keepSome([Some(1), None, Some(3)]), eq, [1, 3])
82+
Test.run(
83+
__POS_OF__("keepSome - all Some"),
84+
Array.keepSome([Some(1), Some(2), Some(3)]),
85+
eq,
86+
[1, 2, 3],
87+
)
88+
Test.run(__POS_OF__("keepSome - all None"), Array.keepSome([None, None, None]), eq, [])
89+
Test.run(__POS_OF__("keepSome - empty"), Array.keepSome([]), eq, [])
90+
91+
Test.run(
92+
__POS_OF__("findMap"),
93+
Array.findMap([1, 2, 3, 4, 5, 6], n => mod(n, 2) == 0 ? Some(n - 8) : None),
94+
eq,
95+
Some(-6),
96+
)
97+
Test.run(__POS_OF__("findMap - no match"), Array.findMap([1, 2, 3, 4, 5, 6], _ => None), eq, None)
98+
Test.run(
99+
__POS_OF__("findMap - empty"),
100+
Array.findMap([], n => mod(n, 2) == 0 ? Some(n * n) : None),
101+
eq,
102+
None,
103+
)
104+
105+
Test.run(
106+
__POS_OF__("fromIterator"),
107+
Array.fromIterator(Map.fromArray([(1, 3), (2, 4)])->Map.values),
108+
eq,
109+
[3, 4],
110+
)
111+
112+
Test.run(__POS_OF__("last - with items"), [1, 2, 3]->Array.last, eq, Some(3))
113+
Test.run(__POS_OF__("last - empty"), []->Array.last, eq, None)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
6+
var eq = Caml_obj.equal;
7+
8+
Test.run([
9+
[
10+
"DictTests.res",
11+
5,
12+
20,
13+
26
14+
],
15+
"make"
16+
], {}, eq, {});
17+
18+
Test.run([
19+
[
20+
"DictTests.res",
21+
7,
22+
20,
23+
31
24+
],
25+
"fromArray"
26+
], Object.fromEntries([[
27+
"foo",
28+
"bar"
29+
]]), eq, {foo: "bar"});
30+
31+
Test.run([
32+
[
33+
"DictTests.res",
34+
10,
35+
13,
36+
35
37+
],
38+
"getUnsafe - existing"
39+
], Object.fromEntries([[
40+
"foo",
41+
"bar"
42+
]])["foo"], eq, "bar");
43+
44+
Test.run([
45+
[
46+
"DictTests.res",
47+
16,
48+
13,
49+
34
50+
],
51+
"getUnsafe - missing"
52+
], ({})["foo"], eq, undefined);
53+
54+
export {
55+
eq ,
56+
}
57+
/* Not a pure module */
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
Test.run(__POS_OF__("make"), Dict.make(), eq, %raw(`{}`))
6+
7+
Test.run(__POS_OF__("fromArray"), Dict.fromArray([("foo", "bar")]), eq, %raw(`{foo: "bar"}`))
8+
9+
Test.run(
10+
__POS_OF__("getUnsafe - existing"),
11+
Dict.fromArray([("foo", "bar")])->Dict.getUnsafe("foo"),
12+
eq,
13+
"bar",
14+
)
15+
Test.run(
16+
__POS_OF__("getUnsafe - missing"),
17+
Dict.make()->Dict.getUnsafe("foo"),
18+
eq,
19+
%raw(`undefined`),
20+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Js_exn from "rescript/lib/es6/js_exn.js";
5+
import * as RescriptCore from "../src/RescriptCore.mjs";
6+
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
7+
8+
function panicTest() {
9+
var caught;
10+
try {
11+
caught = RescriptCore.panic("uh oh");
12+
}
13+
catch (raw_err){
14+
var err = Caml_js_exceptions.internalToOCamlException(raw_err);
15+
if (err.RE_EXN_ID === Js_exn.$$Error) {
16+
caught = err._1.message;
17+
} else {
18+
throw err;
19+
}
20+
}
21+
Test.run([
22+
[
23+
"ErrorTests.res",
24+
8,
25+
22,
26+
43
27+
],
28+
"Should resolve test"
29+
], caught, (function (prim0, prim1) {
30+
return prim0 === prim1;
31+
}), "Panic! uh oh");
32+
}
33+
34+
panicTest();
35+
36+
export {
37+
panicTest ,
38+
}
39+
/* Not a pure module */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
open RescriptCore
2+
3+
let panicTest = () => {
4+
let caught = try panic("uh oh") catch {
5+
| Exn.Error(err) => Error.message(err)
6+
}
7+
8+
Test.run(__POS_OF__("Should resolve test"), caught, \"==", Some("Panic! uh oh"))
9+
}
10+
11+
panicTest()

0 commit comments

Comments
 (0)