Skip to content

Commit

Permalink
Tests of PB-style defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
esstrifork committed Jul 4, 2011
1 parent 877fd7b commit 01725d6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
19 changes: 18 additions & 1 deletion test/erlang_protobuffs_SUITE_data/hasdefault.proto
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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"];
}

42 changes: 38 additions & 4 deletions test/protobuffs_eqc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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).

Expand Down Expand Up @@ -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() ->
Expand All @@ -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.
6 changes: 2 additions & 4 deletions test/protobuffs_generic_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ empty_test() ->


default_test() ->
DataGen = fun() -> {withdefault,default(undefined, real()),
DataGen = fun() -> {requiredwithdefault,default(undefined, real()),
default(undefined, real()),
default(undefined, sint32()),
default(undefined, sint64()),
Expand All @@ -49,7 +49,7 @@ default_test() ->
default(undefined, string())}
end,
compile_pb("hasdefault.proto"),
check_decode_loop(hasdefault_pb, "hasdefault.proto", 'WithDefault', DataGen, 100).
check_decode_loop(hasdefault_pb, "hasdefault.proto", 'RequiredWithDefault', DataGen, 100).

%%====================
compile_pb(ProtoFile) ->
Expand All @@ -67,8 +67,6 @@ check_decode(Module, ProtoFile, MsgName, Record) ->
Encoded = Module:encode(Record),
DecodedA = Module:decode(MsgNameLower, Encoded),
DecodedB = generic_decode_message(Encoded, ProtoFile, MsgName),
io:format(user, "DB| DecodedA=~p\n", [DecodedA]),
io:format(user, "DB| DecodedB=~p\n", [DecodedB]),
?assertEqual({a,DecodedA}, {a,DecodedB}).


Expand Down
17 changes: 16 additions & 1 deletion test/protobuffs_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
%%--------------------------------------------------------------------
Expand Down

0 comments on commit 01725d6

Please sign in to comment.