Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib): Marshal #1352

Merged
merged 2 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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