@@ -79,65 +79,11 @@ auto pb_meta(const TestMessage &)
79
79
hpp::proto::field_meta<113, 3, hpp::proto::field_option::explicit_presence>,
80
80
hpp::proto::field_meta<114, 4, hpp::proto::field_option::explicit_presence>>>;
81
81
82
- TestMessage get_source () {
83
- TestMessage source;
84
- // Optional fields
85
- source.optional_int32 = 1 ; // only source
86
- source.optional_int64 = 2 ; // both source and dest
87
- source.optional_bytes = " abc" _bytes;
88
-
89
- // Optional fields with defaults
90
- source.default_int32 = 13 ; // only source
91
- source.default_int64 = 14 ; // both source and dest
92
-
93
- // Nested message field
94
- source.optional_foreign_message .emplace (ForeignMessage{.c = 1 , .d = 0 , .e = " uvw" _bytes});
95
-
96
- // Repeated fields
97
- source.repeated_int32 .assign ({5 , 6 }); // only source
98
- source.repeated_int64 .assign ({7LL , 8LL }); // both source and dest
99
-
100
- // Map fields
101
-
102
- source.map_int32_int32 [2 ] = 12 ; // both source and dest
103
- source.map_int32_int32 [3 ] = 13 ; // only source
104
- return source;
105
- }
106
-
107
- TestMessage get_dest () {
108
- TestMessage dest;
109
-
110
- // Optional fields
111
- dest.optional_int64 = 3 ;
112
- dest.optional_uint32 = 4 ; // only dest
113
- dest.optional_bytes = " def" _bytes;
114
-
115
- // Optional fields with defaults
116
- dest.default_int64 = 15 ;
117
- dest.default_uint32 = 16 ; // only dest
118
-
119
- // Nested message field
120
- dest.optional_foreign_message .emplace (ForeignMessage{.c = 0 , .d = 2 , .e = " xyz" _bytes});
121
-
122
- // Repeated fields
123
- dest.repeated_int64 .assign ({9LL , 10LL });
124
- dest.repeated_uint32 .assign ({11U , 12U }); // only dest
125
-
126
- // Map fields
127
- dest.map_int32_int32 [1 ] = 1 ; // only dest
128
- dest.map_int32_int32 [2 ] = 2 ; // both source and dest
129
-
130
- return dest;
131
- }
132
-
133
82
const boost::ut::suite merge_test_suite = [] {
134
83
using namespace boost ::ut;
135
84
using namespace boost ::ut::literals;
136
85
137
86
" merge" _test = [] {
138
- TestMessage source = get_source ();
139
- TestMessage dest = get_dest ();
140
-
141
87
auto verify_merge = [](const TestMessage &dest) {
142
88
// Optional fields: source overwrites dest if source is specified
143
89
expect (eq (1 , dest.optional_int32 .value ())); // only source: use source
@@ -163,22 +109,105 @@ const boost::ut::suite merge_test_suite = [] {
163
109
expect (std::vector<int64_t >{9LL , 10LL , 7LL , 8LL } == dest.repeated_int64 );
164
110
expect (std::vector<uint32_t >{11U , 12U } == dest.repeated_uint32 );
165
111
expect (dest.repeated_uint64 .empty ());
112
+ };
113
+
114
+ TestMessage dest;
115
+ TestMessage source;
116
+
117
+ // Optional fields
118
+ source.optional_int32 = 1 ; // only source
119
+ source.optional_int64 = 2 ; // both source and dest
120
+ source.optional_bytes = " abc" _bytes;
121
+
122
+ // Optional fields with defaults
123
+ source.default_int32 = 13 ; // only source
124
+ source.default_int64 = 14 ; // both source and dest
125
+
126
+ // Nested message field
127
+ source.optional_foreign_message .emplace (ForeignMessage{.c = 1 , .d = 0 , .e = " uvw" _bytes});
128
+
129
+ // Repeated fields
130
+ source.repeated_int32 .assign ({5 , 6 }); // only source
131
+ source.repeated_int64 .assign ({7LL , 8LL }); // both source and dest
132
+
133
+ // Optional fields
134
+ dest.optional_int64 = 3 ;
135
+ dest.optional_uint32 = 4 ; // only dest
136
+ dest.optional_bytes = " def" _bytes;
137
+
138
+ // Optional fields with defaults
139
+ dest.default_int64 = 15 ;
140
+ dest.default_uint32 = 16 ; // only dest
166
141
167
- // Map fields
168
- expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 12 }, {3 , 13 }} == dest.map_int32_int32 );
142
+ // Nested message field
143
+ dest.optional_foreign_message .emplace (ForeignMessage{.c = 0 , .d = 2 , .e = " xyz" _bytes});
144
+
145
+ // Repeated fields
146
+ dest.repeated_int64 .assign ({9LL , 10LL });
147
+ dest.repeated_uint32 .assign ({11U , 12U }); // only dest
148
+
149
+ should (" merge const reference" ) = [=]() mutable {
150
+ hpp::proto::merge (dest, source);
151
+ verify_merge (dest);
169
152
};
170
153
171
- hpp::proto::merge (dest, source);
172
- verify_merge (dest);
154
+ should (" merge rvalue reference" ) = [=]() mutable {
155
+ hpp::proto::merge (dest, std::move (source));
156
+ verify_merge (dest);
157
+ };
158
+ };
173
159
174
- dest = get_dest ();
175
- hpp::proto::merge (dest, std::move (source));
176
- verify_merge (dest);
160
+ " map_merge" _test = [] {
161
+ {
162
+ // only source
163
+ TestMessage dest;
164
+ TestMessage source;
165
+ source.map_int32_int32 = {{1 , 1 }, {2 , 2 }};
166
+
167
+ should (" merge const reference" ) = [=]() mutable {
168
+ hpp::proto::merge (dest, source);
169
+ expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 2 }} == dest.map_int32_int32 );
170
+ };
171
+ should (" merge rvalue reference" ) = [=]() mutable {
172
+ hpp::proto::merge (dest, std::move (source));
173
+ expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 2 }} == dest.map_int32_int32 );
174
+ };
175
+ }
176
+ {
177
+ // only dest
178
+ TestMessage dest;
179
+ TestMessage source;
180
+ dest.map_int32_int32 = {{1 , 1 }, {2 , 2 }};
181
+
182
+ should (" merge const reference" ) = [=]() mutable {
183
+ hpp::proto::merge (dest, source);
184
+ expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 2 }} == dest.map_int32_int32 );
185
+ };
186
+ should (" merge rvalue reference" ) = [=]() mutable {
187
+ hpp::proto::merge (dest, std::move (source));
188
+ expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 2 }} == dest.map_int32_int32 );
189
+ };
190
+ }
191
+ {
192
+ // both dest and source
193
+ TestMessage dest;
194
+ TestMessage source;
195
+ dest.map_int32_int32 = {{1 , 1 }, {2 , 2 }};
196
+ source.map_int32_int32 = {{2 , 12 }, {3 , 13 }};
197
+ should (" merge const reference" ) = [=]() mutable {
198
+ hpp::proto::merge (dest, source);
199
+ expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 12 }, {3 , 13 }} == dest.map_int32_int32 );
200
+ };
201
+ should (" merge rvalue reference" ) = [=]() mutable {
202
+ hpp::proto::merge (dest, std::move (source));
203
+ expect (hpp::proto::flat_map<int32_t , int32_t >{{1 , 1 }, {2 , 12 }, {3 , 13 }} == dest.map_int32_int32 );
204
+ };
205
+ }
177
206
};
178
207
179
208
" oneof_merge" _test = [] {
180
209
using namespace std ::string_literals;
181
- {
210
+ { // only source
182
211
TestMessage dest;
183
212
TestMessage source;
184
213
source.oneof_field = 1U ;
@@ -187,7 +216,7 @@ const boost::ut::suite merge_test_suite = [] {
187
216
expect (fatal (eq (dest.oneof_field .index (), TestMessage::oneof_uint32)));
188
217
expect (eq (1U , std::get<uint32_t >(dest.oneof_field )));
189
218
}
190
- {
219
+ { // only dest
191
220
TestMessage dest;
192
221
TestMessage source;
193
222
dest.oneof_field = 1U ;
@@ -196,7 +225,7 @@ const boost::ut::suite merge_test_suite = [] {
196
225
expect (fatal (eq (dest.oneof_field .index (), TestMessage::oneof_uint32)));
197
226
expect (eq (1U , std::get<uint32_t >(dest.oneof_field )));
198
227
}
199
- {
228
+ { // both source and dest, different types
200
229
TestMessage dest;
201
230
TestMessage source;
202
231
dest.oneof_field = 1U ;
@@ -205,7 +234,7 @@ const boost::ut::suite merge_test_suite = [] {
205
234
expect (fatal (eq (dest.oneof_field .index (), TestMessage::oneof_string)));
206
235
expect (eq (" abc" s, std::get<std::string>(dest.oneof_field )));
207
236
}
208
- {
237
+ { // both source and dest, same types
209
238
TestMessage dest;
210
239
TestMessage source;
211
240
dest.oneof_field .emplace <TestMessage::oneof_foreign_message>().c = 1 ;
0 commit comments