@@ -19,6 +19,64 @@ defmodule Protobuf.AnyTest do
1919 end
2020 end
2121
22+ describe "unpack/2" do
23+ test "unpacks a message from Any" do
24+ message = % Google.Protobuf.Duration { seconds: 42 }
25+ any = Protobuf.Any . pack ( message )
26+
27+ assert { :ok , unpacked } = Protobuf.Any . unpack ( any , Protobuf.AnyTypeProviderSupport )
28+ assert unpacked == message
29+ end
30+
31+ test "returns error for unknown type_url" do
32+ any = % Google.Protobuf.Any {
33+ type_url: "custom.prefix/unknown.Type" ,
34+ value: << >>
35+ }
36+
37+ assert { :error , "Unknown type_url" } =
38+ Protobuf.Any . unpack ( any , Protobuf.AnyTypeProviderSupport )
39+ end
40+
41+ test "returns error for unmapped type_url" do
42+ any = % Google.Protobuf.Any {
43+ type_url: "type.googleapis.com/unknown.Message" ,
44+ value: << >>
45+ }
46+
47+ assert { :error , "Unknown type_url" } =
48+ Protobuf.Any . unpack ( any , Protobuf.AnyTypeProviderSupport )
49+ end
50+
51+ test "returns error when type provider returns a non-atom module" do
52+ defmodule BadTypeProvider do
53+ @ behaviour Protobuf.Any.TypeProvider
54+
55+ def to_module ( _type_url ) , do: { :ok , "not_an_atom" }
56+ end
57+
58+ any = % Google.Protobuf.Any {
59+ type_url: "type.googleapis.com/google.protobuf.Duration" ,
60+ value: << >>
61+ }
62+
63+ assert { :error , % ArgumentError { message: message } } =
64+ Protobuf.Any . unpack ( any , BadTypeProvider )
65+
66+ assert message =~ "expected type provider to return an atom module"
67+ end
68+
69+ test "returns error when decode fails" do
70+ any = % Google.Protobuf.Any {
71+ type_url: "type.googleapis.com/google.protobuf.Duration" ,
72+ value: << 255 , 255 , 255 >>
73+ }
74+
75+ assert { :error , % Protobuf.DecodeError { } } =
76+ Protobuf.Any . unpack ( any , Protobuf.AnyTypeProviderSupport )
77+ end
78+ end
79+
2280 describe "type_url_to_module/1" do
2381 test "returns the module for a valid type_url" do
2482 assert Protobuf.Any . type_url_to_module ( "type.googleapis.com/google.protobuf.Duration" ) ==
0 commit comments