Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 1 | %% |
| 2 | %% Licensed to the Apache Software Foundation (ASF) under one |
| 3 | %% or more contributor license agreements. See the NOTICE file |
| 4 | %% distributed with this work for additional information |
| 5 | %% regarding copyright ownership. The ASF licenses this file |
| 6 | %% to you under the Apache License, Version 2.0 (the |
| 7 | %% "License"); you may not use this file except in compliance |
| 8 | %% with the License. You may obtain a copy of the License at |
| 9 | %% |
| 10 | %% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | %% |
| 12 | %% Unless required by applicable law or agreed to in writing, |
| 13 | %% software distributed under the License is distributed on an |
| 14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | %% KIND, either express or implied. See the License for the |
| 16 | %% specific language governing permissions and limitations |
| 17 | %% under the License. |
| 18 | %% |
| 19 | |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 20 | -module(test_omit). |
| 21 | |
| 22 | -include("gen-erl/thrift_omit_with_types.hrl"). |
| 23 | |
| 24 | -ifdef(TEST). |
| 25 | -include_lib("eunit/include/eunit.hrl"). |
| 26 | |
| 27 | omit_struct1_test() -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 28 | %% In this test, the field that is deleted is a basic type (an i32). |
| 29 | A = #test1{one = 1, three = 3}, |
| 30 | B = #test1{one = 1, two = 2, three = 3}, |
| 31 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 32 | {ok, P0} = thrift_binary_protocol:new(Transport), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 33 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 34 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, A)}}, A}), |
| 35 | {P2, {ok, O0}} = thrift_protocol:read(P1, {struct, {thrift_omit_without_types, element(1, A)}}), |
| 36 | ?assertEqual(element(1, A), element(1, O0)), |
| 37 | ?assertEqual(element(2, A), element(2, O0)), |
| 38 | ?assertEqual(element(4, A), element(3, O0)), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 39 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 40 | {P3, ok} = thrift_protocol:write(P2, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 41 | {_P4, {ok, O1}} = thrift_protocol:read( |
| 42 | P3, {struct, {thrift_omit_without_types, element(1, A)}} |
| 43 | ), |
| 44 | ?assertEqual(element(1, A), element(1, O1)), |
| 45 | ?assertEqual(element(2, A), element(2, O1)), |
| 46 | ?assertEqual(element(4, A), element(3, O1)), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 47 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 48 | ok. |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 49 | |
| 50 | omit_struct2_test() -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 51 | %% In this test, the field that is deleted is a struct. |
| 52 | A = #test2{one = 1, two = #test2{one = 10, three = 30}, three = 3}, |
| 53 | B = #test2{one = 1, two = #test2{one = 10, two = #test2{one = 100}, three = 30}, three = 3}, |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 54 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 55 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 56 | {ok, P0} = thrift_binary_protocol:new(Transport), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 57 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 58 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, A)}}, A}), |
| 59 | {P2, {ok, O0}} = thrift_protocol:read(P1, {struct, {thrift_omit_without_types, element(1, A)}}), |
| 60 | ?assertEqual(element(1, A), element(1, O0)), |
| 61 | ?assertEqual(element(2, A), element(2, O0)), |
| 62 | ?assertEqual(element(4, A), element(3, O0)), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 63 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 64 | {P3, ok} = thrift_protocol:write(P2, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 65 | {_P4, {ok, O1}} = thrift_protocol:read( |
| 66 | P3, {struct, {thrift_omit_without_types, element(1, A)}} |
| 67 | ), |
| 68 | ?assertEqual(element(1, A), element(1, O1)), |
| 69 | ?assertEqual(element(2, A), element(2, O1)), |
| 70 | ?assertEqual(element(4, A), element(3, O1)), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 71 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 72 | ok. |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 73 | |
| 74 | omit_list_test() -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 75 | %% In this test, the field that is deleted is a list. |
| 76 | A = #test1{one = 1, two = 2, three = 3}, |
| 77 | B = #test3{one = 1, two = [A]}, |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 78 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 79 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 80 | {ok, P0} = thrift_binary_protocol:new(Transport), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 81 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 82 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 83 | {_P2, {ok, O0}} = thrift_protocol:read( |
| 84 | P1, {struct, {thrift_omit_without_types, element(1, B)}} |
| 85 | ), |
| 86 | ?assertEqual(element(2, B), element(2, O0)), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 87 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 88 | ok. |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 89 | |
| 90 | omit_map_test() -> |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 91 | %% In this test, the field that is deleted is a map. |
| 92 | A = #test1{one = 1, two = 2, three = 3}, |
| 93 | B = #test4{one = 1, two = dict:from_list([{2, A}])}, |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 94 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 95 | {ok, Transport} = thrift_membuffer_transport:new(), |
| 96 | {ok, P0} = thrift_binary_protocol:new(Transport), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 97 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 98 | {P1, ok} = thrift_protocol:write(P0, {{struct, {thrift_omit_with_types, element(1, B)}}, B}), |
| 99 | {_P2, {ok, O0}} = thrift_protocol:read( |
| 100 | P1, {struct, {thrift_omit_without_types, element(1, B)}} |
| 101 | ), |
| 102 | ?assertEqual(element(2, B), element(2, O0)), |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 103 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 104 | ok. |
David Hull | e544a89 | 2017-07-27 02:15:00 +0200 | [diff] [blame] | 105 | |
Sergei Elin | 4576409 | 2022-09-23 23:21:31 +0300 | [diff] [blame] | 106 | %% TEST |
| 107 | -endif. |