David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 1 | -module(test_omit). |
| 2 | |
| 3 | -include("gen-erl/thrift_omit_with_types.hrl"). |
| 4 | |
| 5 | -ifdef(TEST). |
| 6 | -include_lib("eunit/include/eunit.hrl"). |
| 7 | |
| 8 | omit_struct1_test() -> |
| 9 | %% In this test, the field that is deleted is a basic type (an i32). |
| 10 | A = #test1{one = 1, three = 3}, |
| 11 | B = #test1{one = 1, two = 2, three = 3}, |
| 12 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 13 | {ok, P0} = thrift_binary_protocol:new(Transport), |
| 14 | |
| 15 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, A)}}, A}), |
| 16 | {P2, {ok, O0}} = thrift_protocol:read(P1, {struct, {thrift_omit_without_types, element(1, A)}}), |
| 17 | ?assertEqual(element(1, A), element(1, O0)), |
| 18 | ?assertEqual(element(2, A), element(2, O0)), |
| 19 | ?assertEqual(element(4, A), element(3, O0)), |
| 20 | |
| 21 | {P3, ok} = thrift_protocol:write(P2, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 22 | {_P4, {ok, O1}} = thrift_protocol:read(P3, {struct, {thrift_omit_without_types, element(1, A)}}), |
| 23 | ?assertEqual(element(1, A), element(1, O1)), |
| 24 | ?assertEqual(element(2, A), element(2, O1)), |
| 25 | ?assertEqual(element(4, A), element(3, O1)), |
| 26 | |
| 27 | ok. |
| 28 | |
| 29 | omit_struct2_test() -> |
| 30 | %% In this test, the field that is deleted is a struct. |
| 31 | A = #test2{one = 1, two = #test2{one = 10, three = 30}, three = 3}, |
| 32 | B = #test2{one = 1, two = #test2{one = 10, two = #test2{one = 100}, three = 30}, three = 3}, |
| 33 | |
| 34 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 35 | {ok, P0} = thrift_binary_protocol:new(Transport), |
| 36 | |
| 37 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, A)}}, A}), |
| 38 | {P2, {ok, O0}} = thrift_protocol:read(P1, {struct, {thrift_omit_without_types, element(1, A)}}), |
| 39 | ?assertEqual(element(1, A), element(1, O0)), |
| 40 | ?assertEqual(element(2, A), element(2, O0)), |
| 41 | ?assertEqual(element(4, A), element(3, O0)), |
| 42 | |
| 43 | {P3, ok} = thrift_protocol:write(P2, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 44 | {_P4, {ok, O1}} = thrift_protocol:read(P3, {struct, {thrift_omit_without_types, element(1, A)}}), |
| 45 | ?assertEqual(element(1, A), element(1, O1)), |
| 46 | ?assertEqual(element(2, A), element(2, O1)), |
| 47 | ?assertEqual(element(4, A), element(3, O1)), |
| 48 | |
| 49 | ok. |
| 50 | |
| 51 | omit_list_test() -> |
| 52 | %% In this test, the field that is deleted is a list. |
| 53 | A = #test1{one = 1, two = 2, three = 3}, |
| 54 | B = #test3{one = 1, two = [ A ]}, |
| 55 | |
| 56 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 57 | {ok, P0} = thrift_binary_protocol:new(Transport), |
| 58 | |
| 59 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 60 | {_P2, {ok, O0}} = thrift_protocol:read(P1, {struct, {thrift_omit_without_types, element(1, B)}}), |
| 61 | ?assertEqual(element(2, B), element(2, O0)), |
| 62 | |
| 63 | ok. |
| 64 | |
| 65 | omit_map_test() -> |
| 66 | %% In this test, the field that is deleted is a map. |
| 67 | A = #test1{one = 1, two = 2, three = 3}, |
| 68 | B = #test4{one = 1, two = dict:from_list([ {2, A} ])}, |
| 69 | |
| 70 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 71 | {ok, P0} = thrift_binary_protocol:new(Transport), |
| 72 | |
| 73 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 74 | {_P2, {ok, O0}} = thrift_protocol:read(P1, {struct, {thrift_omit_without_types, element(1, B)}}), |
| 75 | ?assertEqual(element(2, B), element(2, O0)), |
| 76 | |
| 77 | ok. |
| 78 | |
| 79 | -endif. %% TEST |