Skip to content

Commit 3e6b5da

Browse files
committed
link nullable and add tests
1 parent ef62f7e commit 3e6b5da

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

src/Core__Nullable.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type t<'a> = Js.Nullable.t<'a>
1+
@unboxed type t<'a> = Js.Nullable.t<'a> = Present('a) | @as(null) Null | @as(undefined) Undefined
22

33
external null: t<'a> = "#null"
44

src/Core__Nullable.resi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ Primarily useful when interoping with JavaScript when you don't know whether you
88
Type representing a nullable value.
99
A nullable value can be the value `'a`, `null` or `undefined`.
1010
*/
11-
type t<'a> = Js.Nullable.t<'a>
11+
@unboxed
12+
type t<'a> = Js.Nullable.t<'a> = Present('a) | @as(null) Null | @as(undefined) Undefined
1213

1314
/**
1415
The value `null`.

test/NullableTests.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
5+
function shouldHandleNullableValues(param) {
6+
var tNull = null;
7+
var tUndefined = undefined;
8+
var tPresent = "hello";
9+
var tmp;
10+
tmp = (tNull === null || tNull === undefined) && tNull === null ? true : false;
11+
Test.run([
12+
[
13+
"NullableTests.res",
14+
9,
15+
15,
16+
35
17+
],
18+
"Should handle null"
19+
], tmp, (function (prim0, prim1) {
20+
return prim0 === prim1;
21+
}), true);
22+
var tmp$1;
23+
tmp$1 = (tUndefined === null || tUndefined === undefined) && tUndefined !== null ? true : false;
24+
Test.run([
25+
[
26+
"NullableTests.res",
27+
19,
28+
15,
29+
40
30+
],
31+
"Should handle undefined"
32+
], tmp$1, (function (prim0, prim1) {
33+
return prim0 === prim1;
34+
}), true);
35+
var tmp$2;
36+
tmp$2 = tPresent === null || tPresent === undefined || tPresent !== "hello" ? false : true;
37+
Test.run([
38+
[
39+
"NullableTests.res",
40+
29,
41+
15,
42+
38
43+
],
44+
"Should handle present"
45+
], tmp$2, (function (prim0, prim1) {
46+
return prim0 === prim1;
47+
}), true);
48+
}
49+
50+
shouldHandleNullableValues(undefined);
51+
52+
export {
53+
shouldHandleNullableValues ,
54+
}
55+
/* Not a pure module */

test/NullableTests.res

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
open RescriptCore
2+
3+
let shouldHandleNullableValues = () => {
4+
let tNull: Nullable.t<string> = %raw("null")
5+
let tUndefined: Nullable.t<string> = %raw("undefined")
6+
let tPresent: Nullable.t<string> = %raw(`"hello"`)
7+
8+
Test.run(
9+
__POS_OF__("Should handle null"),
10+
switch tNull {
11+
| Null => true
12+
| Present(_) | Undefined => false
13+
},
14+
\"==",
15+
true,
16+
)
17+
18+
Test.run(
19+
__POS_OF__("Should handle undefined"),
20+
switch tUndefined {
21+
| Undefined => true
22+
| Present(_) | Null => false
23+
},
24+
\"==",
25+
true,
26+
)
27+
28+
Test.run(
29+
__POS_OF__("Should handle present"),
30+
switch tPresent {
31+
| Present("hello") => true
32+
| _ => false
33+
},
34+
\"==",
35+
true,
36+
)
37+
}
38+
39+
shouldHandleNullableValues()

test/TestSuite.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as ArrayTests from "./ArrayTests.mjs";
77
import * as ErrorTests from "./ErrorTests.mjs";
88
import * as PromiseTest from "./PromiseTest.mjs";
99
import * as ResultTests from "./ResultTests.mjs";
10+
import * as NullableTests from "./NullableTests.mjs";
1011

1112
var bign = TestTests.bign;
1213

@@ -38,6 +39,8 @@ var forEachIfErrorDoNotCallFunction = ResultTests.forEachIfErrorDoNotCallFunctio
3839

3940
var decodeJsonTest = JsonTests.decodeJsonTest;
4041

42+
var shouldHandleNullableValues = NullableTests.shouldHandleNullableValues;
43+
4144
export {
4245
bign ,
4346
TestError ,
@@ -54,5 +57,6 @@ export {
5457
forEachIfOkCallFunction ,
5558
forEachIfErrorDoNotCallFunction ,
5659
decodeJsonTest ,
60+
shouldHandleNullableValues ,
5761
}
5862
/* IntTests Not a pure module */

test/TestSuite.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ include ArrayTests
55
include IntTests
66
include ResultTests
77
include JsonTests
8+
include NullableTests

0 commit comments

Comments
 (0)