Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests of PB-style defaults.

Conflicts:
	test/protobuffs_generic_tests.erl
  • Loading branch information...
commit 5920e1f789eaa81455d8298a9e5511ea80174c8c 1 parent 4b087bd
@eriksoe eriksoe authored
View
19 test/erlang_protobuffs_SUITE_data/hasdefault.proto
@@ -1,5 +1,5 @@
-message WithDefault {
+message RequiredWithDefault {
required double real1 = 1 [default = 1.0];
required float real2 = 2 [default = 2.0];
required int32 int1 = 3 [default = 1];
@@ -16,3 +16,20 @@ message WithDefault {
required string str1 = 14 [default = "test"];
}
+message OptionalWithDefault {
+ optional double real1 = 1 [default = 1.0];
+ optional float real2 = 2 [default = 2.0];
+ optional int32 int1 = 3 [default = 1];
+ optional int64 int2 = 4 [default = 2];
+ optional uint32 int3 = 5 [default = 3];
+ optional uint64 int4 = 6 [default = 4];
+ optional sint32 int5 = 7 [default = 5];
+ optional sint64 int6 = 8 [default = 6];
+ optional fixed32 int7 = 9 [default = 7];
+ optional fixed64 int8 = 10 [default = 8];
+ optional sfixed32 int9 = 11 [default = 9];
+ optional sfixed64 int10 = 12 [default = 10];
+ optional bool val1 = 13 [default = true];
+ optional string str1 = 14 [default = "test"];
+}
+
View
42 test/protobuffs_eqc.erl
@@ -149,9 +149,10 @@ check_with_default(undefined,Result,Default,Fun) ->
check_with_default(Expected,Result,_Default,Fun) ->
Fun(Expected,Result).
-prop_protobuffs_has_default() ->
+prop_protobuffs_has_default1() ->
+ error_logger:info_msg("DB| EQC: line=~p\n", [?LINE]),
?FORALL({Withdefault},
- {{withdefault,default(undefined, real()),
+ {{requiredwithdefault,default(undefined, real()),
default(undefined, real()),
default(undefined, sint32()),
default(undefined, sint64()),
@@ -166,7 +167,29 @@ prop_protobuffs_has_default() ->
default(undefined, bool()),
default(undefined, string())}},
begin
- Decoded = hasdefault_pb:decode_withdefault(hasdefault_pb:encode_withdefault(Withdefault)),
+ Decoded = hasdefault_pb:decode_requiredwithdefault(hasdefault_pb:encode_requiredwithdefault(Withdefault)),
+ compare_messages(Withdefault,Decoded)
+ end).
+
+prop_protobuffs_has_default2() ->
+ error_logger:info_msg("DB| EQC: line=~p\n", [?LINE]),
+ ?FORALL({Withdefault},
+ {{optionalwithdefault,default(1.0, real()),
+ default(2.0, real()),
+ default(1, sint32()),
+ default(2, sint64()),
+ default(3, uint32()),
+ default(4, uint64()),
+ default(5, sint32()),
+ default(6, sint64()),
+ default(7, uint32()),
+ default(8, uint64()),
+ default(9, sint32()),
+ default(10, sint64()),
+ default(true, bool()),
+ default("test", string())}},
+ begin
+ Decoded = hasdefault_pb:decode_optionalwithdefault(hasdefault_pb:encode_optionalwithdefault(Withdefault)),
compare_messages(Withdefault,Decoded)
end).
@@ -363,11 +386,13 @@ single() ->
{message, uint32()}.
prop_protobuffs_single() ->
+ MSpec =
?FORALL({Single},
{single()},
begin
Decoded = single_pb:decode_message (single_pb:encode_message(Single)),
- compare_messages (Single, Decoded)
+ Decoded2 = generic_decode (single_pb:encode_message(Single), "single", 'Message'),
+ compare_messages (Single, Decoded) andalso compare_messages (Single, Decoded2)
end).
prop_protobuffs_extend() ->
@@ -377,4 +402,13 @@ prop_protobuffs_extend() ->
Decoded = extend_pb:decode_extendable(extend_pb:encode_extendable(Extend)),
compare_messages(Extend, Decoded)
end).
+
+generic_decode_message(Bytes, ProtoFile, MsgName) ->
+ DataDir = "../test/erlang_protobuffs_SUITE_data",
+ PBSpec = protobuffs_generic:proto_file_to_spec(filename:join(DataDir, ProtoFile++".proto")),
+ MSpec = protobuffs_generic:msg_spec_for(PBSpec, MsgName),
+ Decoded = protobuffs_generic:decode(MSpec, Bytes),
+ error_logger:error_msg("DB| decoded ~p to ~p\n", [Bytes, Decoded]),
+ Decoded.
+
-endif.
View
17 test/protobuffs_tests.erl
@@ -63,7 +63,7 @@ parse_empty_file_test_() ->
parse_has_default_test_() ->
Path = filename:absname("../test/erlang_protobuffs_SUITE_data/hasdefault.proto"),
- [{message, "WithDefault", Messages}] = parse(Path),
+ [{message, "RequiredWithDefault", Messages},_] = parse(Path),
[?_assertMatch({1,required,"double","real1",1.0},lists:keyfind(1,1,Messages)),
?_assertMatch({2,required,"float","real2",2.0},lists:keyfind(2,1,Messages)),
?_assertMatch({3,required,"int32","int1",1},lists:keyfind(3,1,Messages)),
@@ -248,6 +248,21 @@ parse_extend_in_reserved_range_test_() ->
Error = (catch protobuffs_compile:scan_file(Path, [{imports_dir, [DataDir]}])),
[?_assertEqual(out_of_range, Error)].
+
+should_encode_to_empty_test_() ->
+ DataDir = "../test/erlang_protobuffs_SUITE_data",
+ Path = filename:absname(filename:join([DataDir,"hasdefault.proto"])),
+ ok = protobuffs_compile:scan_file(Path),
+
+ EncodeToEmpty1 = {optionalwithdefault,
+ 1.0, 2.0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, true, "test"},
+ EncodeToEmpties = [setelement(N, EncodeToEmpty1, undefined)
+ || N <- lists:seq(2,tuple_size(EncodeToEmpty1))],
+ error_logger:info_msg("should_encode_to_empty_test_: ~p\n", [[EncodeToEmpty1 | EncodeToEmpties]]),
+ [?_assertEqual(<<>>, hasdefault_pb:encode_optionalwithdefault(R))
+ || R <- [EncodeToEmpty1 | EncodeToEmpties]].
+
+
%%--------------------------------------------------------------------
%% Help functions
%%--------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.