blob: a51f1ed5a47db7df80ccce8888031b01814b540a [file] [log] [blame]
Sergei Elin45764092022-09-23 23:21:31 +03001%%
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 Hulle544a892017-07-27 02:15:00 +020020-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
27omit_struct1_test() ->
Sergei Elin45764092022-09-23 23:21:31 +030028 %% 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 Hulle544a892017-07-27 02:15:00 +020033
Sergei Elin45764092022-09-23 23:21:31 +030034 {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 Hulle544a892017-07-27 02:15:00 +020039
Sergei Elin45764092022-09-23 23:21:31 +030040 {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 Hulle544a892017-07-27 02:15:00 +020047
Sergei Elin45764092022-09-23 23:21:31 +030048 ok.
David Hulle544a892017-07-27 02:15:00 +020049
50omit_struct2_test() ->
Sergei Elin45764092022-09-23 23:21:31 +030051 %% 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 Hulle544a892017-07-27 02:15:00 +020054
Sergei Elin45764092022-09-23 23:21:31 +030055 {ok, Transport} = thrift_membuffer_transport:new(),
56 {ok, P0} = thrift_binary_protocol:new(Transport),
David Hulle544a892017-07-27 02:15:00 +020057
Sergei Elin45764092022-09-23 23:21:31 +030058 {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 Hulle544a892017-07-27 02:15:00 +020063
Sergei Elin45764092022-09-23 23:21:31 +030064 {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 Hulle544a892017-07-27 02:15:00 +020071
Sergei Elin45764092022-09-23 23:21:31 +030072 ok.
David Hulle544a892017-07-27 02:15:00 +020073
74omit_list_test() ->
Sergei Elin45764092022-09-23 23:21:31 +030075 %% 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 Hulle544a892017-07-27 02:15:00 +020078
Sergei Elin45764092022-09-23 23:21:31 +030079 {ok, Transport} = thrift_membuffer_transport:new(),
80 {ok, P0} = thrift_binary_protocol:new(Transport),
David Hulle544a892017-07-27 02:15:00 +020081
Sergei Elin45764092022-09-23 23:21:31 +030082 {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 Hulle544a892017-07-27 02:15:00 +020087
Sergei Elin45764092022-09-23 23:21:31 +030088 ok.
David Hulle544a892017-07-27 02:15:00 +020089
90omit_map_test() ->
Sergei Elin45764092022-09-23 23:21:31 +030091 %% 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 Hulle544a892017-07-27 02:15:00 +020094
Sergei Elin45764092022-09-23 23:21:31 +030095 {ok, Transport} = thrift_membuffer_transport:new(),
96 {ok, P0} = thrift_binary_protocol:new(Transport),
David Hulle544a892017-07-27 02:15:00 +020097
Sergei Elin45764092022-09-23 23:21:31 +030098 {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 Hulle544a892017-07-27 02:15:00 +0200103
Sergei Elin45764092022-09-23 23:21:31 +0300104 ok.
David Hulle544a892017-07-27 02:15:00 +0200105
Sergei Elin45764092022-09-23 23:21:31 +0300106%% TEST
107-endif.