Skip to content

Commit

Permalink
feat(stdlib): Marshal
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Jul 11, 2022
1 parent 944f71c commit 8dcbaf3
Show file tree
Hide file tree
Showing 4 changed files with 1,232 additions and 0 deletions.
87 changes: 87 additions & 0 deletions compiler/test/stdlib/marshal.test.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * from "marshal"
import Bytes from "bytes"
import Result from "result"

let roundtripOk = value => unmarshal(marshal(value)) == Ok(value)

assert roundtripOk(void)
assert roundtripOk(true)
assert roundtripOk(42)
assert roundtripOk(42l)
assert roundtripOk(42L)
assert roundtripOk(42.)
assert roundtripOk(42.f)
assert roundtripOk(42.d)
assert roundtripOk(1/3)
assert roundtripOk('c')
assert roundtripOk("unicode string 💯🌾🙏🏽")
assert roundtripOk(Bytes.fromString("unicode string 💯🌾🙏🏽"))
assert roundtripOk((14, false, "hi"))
assert roundtripOk([> 1, 2, 3])
assert roundtripOk([> 1l, 2l, 3l])
assert roundtripOk([1, 2, 3])
assert roundtripOk([1l, 2l, 3l])

enum Foo<a> {
Bar,
Baz(a),
Qux(String, a),
Quux(Foo<a>),
}

assert roundtripOk(Bar)
assert roundtripOk(Baz("baz"))
assert roundtripOk(Qux("qux", 42l))
assert roundtripOk(Quux(Qux("qux", 42l)))

record Bing<a> {
bang: Void,
bop: a,
swish: (
String,
a
),
pow: Foo<Int32>,
}

assert roundtripOk(
{ bang: void, bop: "bee", swish: ("bit", "but"), pow: Quux(Qux("qux", 42l)) }
)

let make5 = () => "five"

let closureTest = () => {
let val = make5()
let foo = () => val
assert Result.unwrap(unmarshal(marshal(foo)))() == "five"
}

closureTest()

record Cyclic {
mut self: List<Cyclic>,
}

let cyclic = { self: [], }
cyclic.self = [cyclic, cyclic, cyclic]

assert roundtripOk(cyclic)

assert Result.isErr(unmarshal(Bytes.empty))

let truncatedString = Bytes.slice(0, 16, marshal("beep boop bop"))
assert Result.isErr(unmarshal(truncatedString))

let truncatedRecord = Bytes.slice(
0,
64,
marshal(
{
bang: void,
bop: "bee",
swish: ("bit", "but"),
pow: Quux(Qux("qux", 42l)),
}
)
)
assert Result.isErr(unmarshal(truncatedRecord))
1 change: 1 addition & 0 deletions compiler/test/suites/stdlib.re
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe("stdlib", ({test, testSkip}) => {
assertStdlib("int64.test");
assertStdlib("list.test");
assertStdlib("map.test");
assertStdlib("marshal.test");
assertStdlib("number.test");
assertStdlib("option.test");
assertStdlib("queue.test");
Expand Down
Loading

0 comments on commit 8dcbaf3

Please sign in to comment.