Skip to content

Commit d659de2

Browse files
authored
feat(stdlib): Marshal (#1352)
1 parent c3abbc9 commit d659de2

File tree

4 files changed

+1222
-0
lines changed

4 files changed

+1222
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * from "marshal"
2+
import Bytes from "bytes"
3+
import Result from "result"
4+
5+
let roundtripOk = value => unmarshal(marshal(value)) == Ok(value)
6+
7+
assert roundtripOk(void)
8+
assert roundtripOk(true)
9+
assert roundtripOk(42)
10+
assert roundtripOk(42l)
11+
assert roundtripOk(42L)
12+
assert roundtripOk(42.)
13+
assert roundtripOk(42.f)
14+
assert roundtripOk(42.d)
15+
assert roundtripOk(1/3)
16+
assert roundtripOk('c')
17+
assert roundtripOk("unicode string 💯🌾🙏🏽")
18+
assert roundtripOk(Bytes.fromString("unicode string 💯🌾🙏🏽"))
19+
assert roundtripOk((14, false, "hi"))
20+
assert roundtripOk([> 1, 2, 3])
21+
assert roundtripOk([> 1l, 2l, 3l])
22+
assert roundtripOk([1, 2, 3])
23+
assert roundtripOk([1l, 2l, 3l])
24+
25+
enum Foo<a> {
26+
Bar,
27+
Baz(a),
28+
Qux(String, a),
29+
Quux(Foo<a>),
30+
}
31+
32+
assert roundtripOk(Bar)
33+
assert roundtripOk(Baz("baz"))
34+
assert roundtripOk(Qux("qux", 42l))
35+
assert roundtripOk(Quux(Qux("qux", 42l)))
36+
37+
record Bing<a> {
38+
bang: Void,
39+
bop: a,
40+
swish: (
41+
String,
42+
a
43+
),
44+
pow: Foo<Int32>,
45+
}
46+
47+
assert roundtripOk(
48+
{ bang: void, bop: "bee", swish: ("bit", "but"), pow: Quux(Qux("qux", 42l)) }
49+
)
50+
51+
let make5 = () => "five"
52+
53+
let closureTest = () => {
54+
let val = make5()
55+
let foo = () => val
56+
assert Result.unwrap(unmarshal(marshal(foo)))() == "five"
57+
}
58+
59+
closureTest()
60+
61+
record Cyclic {
62+
mut self: List<Cyclic>,
63+
}
64+
65+
let cyclic = { self: [], }
66+
cyclic.self = [cyclic, cyclic, cyclic]
67+
68+
assert roundtripOk(cyclic)
69+
70+
assert Result.isErr(unmarshal(Bytes.empty))
71+
72+
let truncatedString = Bytes.slice(0, 16, marshal("beep boop bop"))
73+
assert Result.isErr(unmarshal(truncatedString))
74+
75+
let truncatedRecord = Bytes.slice(
76+
0,
77+
64,
78+
marshal(
79+
{
80+
bang: void,
81+
bop: "bee",
82+
swish: ("bit", "but"),
83+
pow: Quux(Qux("qux", 42l)),
84+
}
85+
)
86+
)
87+
assert Result.isErr(unmarshal(truncatedRecord))

compiler/test/suites/stdlib.re

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ describe("stdlib", ({test, testSkip}) => {
100100
assertStdlib("int64.test");
101101
assertStdlib("list.test");
102102
assertStdlib("map.test");
103+
assertStdlib("marshal.test");
103104
assertStdlib("number.test");
104105
assertStdlib("option.test");
105106
assertStdlib("queue.test");

0 commit comments

Comments
 (0)