-
Notifications
You must be signed in to change notification settings - Fork 19
/
example_test.exs
70 lines (62 loc) · 1.58 KB
/
example_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
defmodule ExampleTest do
use ExUnit.Case
# This example shows how most protobuf types are translated into Elixir code.
use Protox,
schema: """
syntax = "proto3";
enum FooOrBar {
FOO = 0;
BAR = 1;
}
message SubMsg {
int32 a = 1;
string b = 2;
int64 c = 3;
double d = 4;
bytes e = 5;
bool f = 6;
FooOrBar g = 7;
repeated int32 h = 8;
map<string, int32> i = 9;
optional bool j = 10;
optional bool k = 11;
optional bool l = 12;
}
message Envelope {
oneof envelope {
string str = 1;
SubMsg sub_msg = 2;
}
}
"""
test "Example" do
# Here the oneof is set to a SubMsg.
sub_msg = %Envelope{
envelope:
{:sub_msg,
%SubMsg{
a: 1,
b: "foo",
c: 42,
d: 3.3,
e: <<1, 2, 3>>,
f: true,
g: :FOO,
h: [1, 2, 3],
i: %{"foo" => 42, "bar" => 33},
k: false,
l: true
}}
}
# Here the oneof is set to a string.
str = %Envelope{
envelope: {:str, "some string"}
}
encoded_sub_msg = sub_msg |> Envelope.encode!() |> :binary.list_to_bin()
decoded_sub_msg = Envelope.decode!(encoded_sub_msg)
assert decoded_sub_msg == sub_msg
assert %{envelope: {:sub_msg, %SubMsg{j: nil, k: false, l: true}}} = decoded_sub_msg
encoded_str = str |> Envelope.encode!() |> :binary.list_to_bin()
assert Envelope.decode!(encoded_str) == str
end
end