• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2015 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 import Foundation
32 import XCTest
33 
34 // Test some usage of the ObjC library from Swift.
35 
36 class GPBBridgeTests: XCTestCase {
37 
testProto2Basicsnull38   func testProto2Basics() {
39     let msg = Message2()
40     let msg2 = Message2()
41     let msg3 = Message2_OptionalGroup()
42 
43     msg.optionalInt32 = 100
44     msg.optionalString = "abc"
45     msg.optionalEnum = .bar
46     msg2.optionalString = "other"
47     msg.optional = msg2
48     msg3.a = 200
49     msg.optionalGroup = msg3
50     msg.repeatedInt32Array.addValue(300)
51     msg.repeatedInt32Array.addValue(301)
52     msg.repeatedStringArray.add("mno")
53     msg.repeatedStringArray.add("pqr")
54     msg.repeatedEnumArray.addValue(Message2_Enum.bar.rawValue)
55     msg.repeatedEnumArray.addValue(Message2_Enum.baz.rawValue)
56     msg.mapInt32Int32.setInt32(400, forKey:500)
57     msg.mapInt32Int32.setInt32(401, forKey:501)
58     msg.mapStringString.setObject("foo", forKey:"bar" as NSString)
59     msg.mapStringString.setObject("abc", forKey:"xyz" as NSString)
60     msg.mapInt32Enum.setEnum(Message2_Enum.bar.rawValue, forKey:600)
61     msg.mapInt32Enum.setEnum(Message2_Enum.baz.rawValue, forKey:601)
62 
63     // Check has*.
64     XCTAssertTrue(msg.hasOptionalInt32)
65     XCTAssertTrue(msg.hasOptionalString)
66     XCTAssertTrue(msg.hasOptionalEnum)
67     XCTAssertTrue(msg2.hasOptionalString)
68     XCTAssertTrue(msg.hasOptionalMessage)
69     XCTAssertTrue(msg3.hasA)
70     XCTAssertTrue(msg.hasOptionalGroup)
71     XCTAssertFalse(msg.hasOptionalInt64)
72     XCTAssertFalse(msg.hasOptionalFloat)
73 
74     // Check values.
75     XCTAssertEqual(msg.optionalInt32, Int32(100))
76     XCTAssertEqual(msg.optionalString, "abc")
77     XCTAssertEqual(msg2.optionalString, "other")
78     XCTAssertTrue(msg.optional === msg2)
79     XCTAssertEqual(msg.optionalEnum, Message2_Enum.bar)
80     XCTAssertEqual(msg3.a, Int32(200))
81     XCTAssertTrue(msg.optionalGroup === msg3)
82     XCTAssertEqual(msg.repeatedInt32Array.count, UInt(2))
83     XCTAssertEqual(msg.repeatedInt32Array.value(at: 0), Int32(300))
84     XCTAssertEqual(msg.repeatedInt32Array.value(at: 1), Int32(301))
85     XCTAssertEqual(msg.repeatedStringArray.count, Int(2))
86     XCTAssertEqual(msg.repeatedStringArray.object(at: 0) as? String, "mno")
87     XCTAssertEqual(msg.repeatedStringArray.object(at: 1) as? String, "pqr")
88     XCTAssertEqual(msg.repeatedEnumArray.count, UInt(2))
89     XCTAssertEqual(msg.repeatedEnumArray.value(at: 0), Message2_Enum.bar.rawValue)
90     XCTAssertEqual(msg.repeatedEnumArray.value(at: 1), Message2_Enum.baz.rawValue)
91     XCTAssertEqual(msg.repeatedInt64Array.count, UInt(0))
92     XCTAssertEqual(msg.mapInt32Int32.count, UInt(2))
93     var intValue: Int32 = 0
94     XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey: 500))
95     XCTAssertEqual(intValue, Int32(400))
96     XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey: 501))
97     XCTAssertEqual(intValue, Int32(401))
98     XCTAssertEqual(msg.mapStringString.count, Int(2))
99     XCTAssertEqual(msg.mapStringString.object(forKey: "bar") as? String, "foo")
100     XCTAssertEqual(msg.mapStringString.object(forKey: "xyz") as? String, "abc")
101     XCTAssertEqual(msg.mapInt32Enum.count, UInt(2))
102     XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:600))
103     XCTAssertEqual(intValue, Message2_Enum.bar.rawValue)
104     XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:601))
105     XCTAssertEqual(intValue, Message2_Enum.baz.rawValue)
106 
107     // Clearing a string with nil.
108     msg2.optionalString = nil
109     XCTAssertFalse(msg2.hasOptionalString)
110     XCTAssertEqual(msg2.optionalString, "")
111 
112     // Clearing a message with nil.
113     msg.optionalGroup = nil
114     XCTAssertFalse(msg.hasOptionalGroup)
115     XCTAssertTrue(msg.optionalGroup !== msg3)  // New instance
116 
117     // Clear.
118     msg.clear()
119     XCTAssertFalse(msg.hasOptionalInt32)
120     XCTAssertFalse(msg.hasOptionalString)
121     XCTAssertFalse(msg.hasOptionalEnum)
122     XCTAssertFalse(msg.hasOptionalMessage)
123     XCTAssertFalse(msg.hasOptionalInt64)
124     XCTAssertFalse(msg.hasOptionalFloat)
125     XCTAssertEqual(msg.optionalInt32, Int32(0))
126     XCTAssertEqual(msg.optionalString, "")
127     XCTAssertTrue(msg.optional !== msg2)  // New instance
128     XCTAssertEqual(msg.optionalEnum, Message2_Enum.foo)  // Default
129     XCTAssertEqual(msg.repeatedInt32Array.count, UInt(0))
130     XCTAssertEqual(msg.repeatedStringArray.count, Int(0))
131     XCTAssertEqual(msg.repeatedEnumArray.count, UInt(0))
132     XCTAssertEqual(msg.mapInt32Int32.count, UInt(0))
133     XCTAssertEqual(msg.mapStringString.count, Int(0))
134     XCTAssertEqual(msg.mapInt32Enum.count, UInt(0))
135   }
136 
testProto3Basicsnull137   func testProto3Basics() {
138     let msg = Message3()
139     let msg2 = Message3()
140 
141     msg.optionalInt32 = 100
142     msg.optionalString = "abc"
143     msg.optionalEnum = .bar
144     msg2.optionalString = "other"
145     msg.optional = msg2
146     msg.repeatedInt32Array.addValue(300)
147     msg.repeatedInt32Array.addValue(301)
148     msg.repeatedStringArray.add("mno")
149     msg.repeatedStringArray.add("pqr")
150     // "proto3" syntax lets enum get unknown values.
151     msg.repeatedEnumArray.addValue(Message3_Enum.bar.rawValue)
152     msg.repeatedEnumArray.addRawValue(666)
153     SetMessage3_OptionalEnum_RawValue(msg2, 666)
154     msg.mapInt32Int32.setInt32(400, forKey:500)
155     msg.mapInt32Int32.setInt32(401, forKey:501)
156     msg.mapStringString.setObject("foo", forKey:"bar" as NSString)
157     msg.mapStringString.setObject("abc", forKey:"xyz" as NSString)
158     msg.mapInt32Enum.setEnum(Message2_Enum.bar.rawValue, forKey:600)
159     // "proto3" syntax lets enum get unknown values.
160     msg.mapInt32Enum.setRawValue(666, forKey:601)
161 
162     // Has only exists on for message fields.
163     XCTAssertTrue(msg.hasOptionalMessage)
164     XCTAssertFalse(msg2.hasOptionalMessage)
165 
166     // Check values.
167     XCTAssertEqual(msg.optionalInt32, Int32(100))
168     XCTAssertEqual(msg.optionalString, "abc")
169     XCTAssertEqual(msg2.optionalString, "other")
170     XCTAssertTrue(msg.optional === msg2)
171     XCTAssertEqual(msg.optionalEnum, Message3_Enum.bar)
172     XCTAssertEqual(msg.repeatedInt32Array.count, UInt(2))
173     XCTAssertEqual(msg.repeatedInt32Array.value(at: 0), Int32(300))
174     XCTAssertEqual(msg.repeatedInt32Array.value(at: 1), Int32(301))
175     XCTAssertEqual(msg.repeatedStringArray.count, Int(2))
176     XCTAssertEqual(msg.repeatedStringArray.object(at: 0) as? String, "mno")
177     XCTAssertEqual(msg.repeatedStringArray.object(at: 1) as? String, "pqr")
178     XCTAssertEqual(msg.repeatedInt64Array.count, UInt(0))
179     XCTAssertEqual(msg.repeatedEnumArray.count, UInt(2))
180     XCTAssertEqual(msg.repeatedEnumArray.value(at: 0), Message3_Enum.bar.rawValue)
181     XCTAssertEqual(msg.repeatedEnumArray.value(at: 1), Message3_Enum.gpbUnrecognizedEnumeratorValue.rawValue)
182     XCTAssertEqual(msg.repeatedEnumArray.rawValue(at: 1), 666)
183     XCTAssertEqual(msg2.optionalEnum, Message3_Enum.gpbUnrecognizedEnumeratorValue)
184     XCTAssertEqual(Message3_OptionalEnum_RawValue(msg2), Int32(666))
185     XCTAssertEqual(msg.mapInt32Int32.count, UInt(2))
186     var intValue: Int32 = 0
187     XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey:500))
188     XCTAssertEqual(intValue, Int32(400))
189     XCTAssertTrue(msg.mapInt32Int32.getInt32(&intValue, forKey:501))
190     XCTAssertEqual(intValue, Int32(401))
191     XCTAssertEqual(msg.mapStringString.count, Int(2))
192     XCTAssertEqual(msg.mapStringString.object(forKey: "bar") as? String, "foo")
193     XCTAssertEqual(msg.mapStringString.object(forKey: "xyz") as? String, "abc")
194     XCTAssertEqual(msg.mapInt32Enum.count, UInt(2))
195     XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:600))
196     XCTAssertEqual(intValue, Message2_Enum.bar.rawValue)
197     XCTAssertTrue(msg.mapInt32Enum.getEnum(&intValue, forKey:601))
198     XCTAssertEqual(intValue, Message3_Enum.gpbUnrecognizedEnumeratorValue.rawValue)
199     XCTAssertTrue(msg.mapInt32Enum.getRawValue(&intValue, forKey:601))
200     XCTAssertEqual(intValue, 666)
201 
202     // Clearing a string with nil.
203     msg2.optionalString = nil
204     XCTAssertEqual(msg2.optionalString, "")
205 
206     // Clearing a message with nil.
207     msg.optional = nil
208     XCTAssertFalse(msg.hasOptionalMessage)
209     XCTAssertTrue(msg.optional !== msg2)  // New instance
210 
211     // Clear.
212     msg.clear()
213     XCTAssertFalse(msg.hasOptionalMessage)
214     XCTAssertEqual(msg.optionalInt32, Int32(0))
215     XCTAssertEqual(msg.optionalString, "")
216     XCTAssertTrue(msg.optional !== msg2)  // New instance
217     XCTAssertEqual(msg.optionalEnum, Message3_Enum.foo)  // Default
218     XCTAssertEqual(msg.repeatedInt32Array.count, UInt(0))
219     XCTAssertEqual(msg.repeatedStringArray.count, Int(0))
220     XCTAssertEqual(msg.repeatedEnumArray.count, UInt(0))
221     msg2.clear()
222     XCTAssertEqual(msg2.optionalEnum, Message3_Enum.foo)  // Default
223     XCTAssertEqual(Message3_OptionalEnum_RawValue(msg2), Message3_Enum.foo.rawValue)
224     XCTAssertEqual(msg.mapInt32Int32.count, UInt(0))
225     XCTAssertEqual(msg.mapStringString.count, Int(0))
226     XCTAssertEqual(msg.mapInt32Enum.count, UInt(0))
227   }
228 
testAutoCreationnull229   func testAutoCreation() {
230     let msg = Message2()
231 
232     XCTAssertFalse(msg.hasOptionalGroup)
233     XCTAssertFalse(msg.hasOptionalMessage)
234 
235     // Access shouldn't result in has* but should return objects.
236     let msg2 = msg.optionalGroup
237     let msg3 = msg.optional.optional
238     let msg4 = msg.optional
239     XCTAssertNotNil(msg2)
240     XCTAssertNotNil(msg3)
241     XCTAssertFalse(msg.hasOptionalGroup)
242     XCTAssertFalse(msg.optional.hasOptionalMessage)
243     XCTAssertFalse(msg.hasOptionalMessage)
244 
245     // Setting things should trigger has* getting set.
246     msg.optionalGroup.a = 10
247     msg.optional.optional.optionalInt32 = 100
248     XCTAssertTrue(msg.hasOptionalGroup)
249     XCTAssertTrue(msg.optional.hasOptionalMessage)
250     XCTAssertTrue(msg.hasOptionalMessage)
251 
252     // And they should be the same pointer as before.
253     XCTAssertTrue(msg2 === msg.optionalGroup)
254     XCTAssertTrue(msg3 === msg.optional.optional)
255     XCTAssertTrue(msg4 === msg.optional)
256 
257     // Clear gets us new objects next time around.
258     msg.clear()
259     XCTAssertFalse(msg.hasOptionalGroup)
260     XCTAssertFalse(msg.optional.hasOptionalMessage)
261     XCTAssertFalse(msg.hasOptionalMessage)
262     msg.optionalGroup.a = 20
263     msg.optional.optional.optionalInt32 = 200
264     XCTAssertTrue(msg.hasOptionalGroup)
265     XCTAssertTrue(msg.optional.hasOptionalMessage)
266     XCTAssertTrue(msg.hasOptionalMessage)
267     XCTAssertTrue(msg2 !== msg.optionalGroup)
268     XCTAssertTrue(msg3 !== msg.optional.optional)
269     XCTAssertTrue(msg4 !== msg.optional)
270 
271     // Explicit set of a message, means autocreated object doesn't bind.
272     msg.clear()
273     let autoCreated = msg.optional
274     XCTAssertFalse(msg.hasOptionalMessage)
275     let msg5 = Message2()
276     msg5.optionalInt32 = 123
277     msg.optional = msg5
278     XCTAssertTrue(msg.hasOptionalMessage)
279     // Modifying the autocreated doesn't replaced the explicit set one.
280     autoCreated?.optionalInt32 = 456
281     XCTAssertTrue(msg.hasOptionalMessage)
282     XCTAssertTrue(msg.optional === msg5)
283     XCTAssertEqual(msg.optional.optionalInt32, Int32(123))
284   }
285 
testProto2OneOfSupportnull286   func testProto2OneOfSupport() {
287     let msg = Message2()
288 
289     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.gpbUnsetOneOfCase)
290     XCTAssertEqual(msg.oneofInt32, Int32(100))  // Default
291     XCTAssertEqual(msg.oneofFloat, Float(110.0))  // Default
292     XCTAssertEqual(msg.oneofEnum, Message2_Enum.baz)  // Default
293     let autoCreated = msg.oneof  // Default create one.
294     XCTAssertNotNil(autoCreated)
295     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.gpbUnsetOneOfCase)
296 
297     msg.oneofInt32 = 10
298     XCTAssertEqual(msg.oneofInt32, Int32(10))
299     XCTAssertEqual(msg.oneofFloat, Float(110.0))  // Default
300     XCTAssertEqual(msg.oneofEnum, Message2_Enum.baz)  // Default
301     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
302     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.oneofInt32)
303 
304     msg.oneofFloat = 20.0
305     XCTAssertEqual(msg.oneofInt32, Int32(100))  // Default
306     XCTAssertEqual(msg.oneofFloat, Float(20.0))
307     XCTAssertEqual(msg.oneofEnum, Message2_Enum.baz)  // Default
308     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
309     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.oneofFloat)
310 
311     msg.oneofEnum = .bar
312     XCTAssertEqual(msg.oneofInt32, Int32(100))  // Default
313     XCTAssertEqual(msg.oneofFloat, Float(110.0))  // Default
314     XCTAssertEqual(msg.oneofEnum, Message2_Enum.bar)
315     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
316     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.oneofEnum)
317 
318     // Sets via the autocreated instance.
319     msg.oneof.optionalInt32 = 200
320     XCTAssertEqual(msg.oneofInt32, Int32(100))  // Default
321     XCTAssertEqual(msg.oneofFloat, Float(110.0))  // Default
322     XCTAssertEqual(msg.oneofEnum, Message2_Enum.baz)  // Default
323     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
324     XCTAssertEqual(msg.oneof.optionalInt32, Int32(200))
325     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.oneofMessage)
326 
327     // Clear the oneof.
328     Message2_ClearOOneOfCase(msg)
329     XCTAssertEqual(msg.oneofInt32, Int32(100))  // Default
330     XCTAssertEqual(msg.oneofFloat, Float(110.0))  // Default
331     XCTAssertEqual(msg.oneofEnum, Message2_Enum.baz)  // Default
332     let autoCreated2 = msg.oneof  // Default create one
333     XCTAssertNotNil(autoCreated2)
334     XCTAssertTrue(autoCreated2 !== autoCreated)  // New instance
335     XCTAssertEqual(msg.oneof.optionalInt32, Int32(0))  // Default
336     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.gpbUnsetOneOfCase)
337 
338     msg.oneofInt32 = 10
339     XCTAssertEqual(msg.oneofInt32, Int32(10))
340     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.oneofInt32)
341 
342     // Confirm Message.clear() handles the oneof correctly.
343     msg.clear()
344     XCTAssertEqual(msg.oneofInt32, Int32(100))  // Default
345     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.gpbUnsetOneOfCase)
346 
347     // Sets via the autocreated instance.
348     msg.oneof.optionalInt32 = 300
349     XCTAssertTrue(msg.oneof !== autoCreated)  // New instance
350     XCTAssertTrue(msg.oneof !== autoCreated2)  // New instance
351     XCTAssertEqual(msg.oneof.optionalInt32, Int32(300))
352     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.oneofMessage)
353 
354     // Set message to nil clears the oneof.
355     msg.oneof = nil
356     XCTAssertEqual(msg.oneof.optionalInt32, Int32(0))  // Default
357     XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.gpbUnsetOneOfCase)
358   }
359 
testProto3OneOfSupportnull360   func testProto3OneOfSupport() {
361     let msg = Message3()
362 
363     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.gpbUnsetOneOfCase)
364     XCTAssertEqual(msg.oneofInt32, Int32(0))  // Default
365     XCTAssertEqual(msg.oneofFloat, Float(0.0))  // Default
366     XCTAssertEqual(msg.oneofEnum, Message3_Enum.foo)  // Default
367     let autoCreated = msg.oneof  // Default create one.
368     XCTAssertNotNil(autoCreated)
369     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.gpbUnsetOneOfCase)
370 
371     msg.oneofInt32 = 10
372     XCTAssertEqual(msg.oneofInt32, Int32(10))
373     XCTAssertEqual(msg.oneofFloat, Float(0.0))  // Default
374     XCTAssertEqual(msg.oneofEnum, Message3_Enum.foo)  // Default
375     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
376     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.oneofInt32)
377 
378     msg.oneofFloat = 20.0
379     XCTAssertEqual(msg.oneofInt32, Int32(0))  // Default
380     XCTAssertEqual(msg.oneofFloat, Float(20.0))
381     XCTAssertEqual(msg.oneofEnum, Message3_Enum.foo)  // Default
382     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
383     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.oneofFloat)
384 
385     msg.oneofEnum = .bar
386     XCTAssertEqual(msg.oneofInt32, Int32(0))  // Default
387     XCTAssertEqual(msg.oneofFloat, Float(0.0))  // Default
388     XCTAssertEqual(msg.oneofEnum, Message3_Enum.bar)
389     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
390     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.oneofEnum)
391 
392     // Sets via the autocreated instance.
393     msg.oneof.optionalInt32 = 200
394     XCTAssertEqual(msg.oneofInt32, Int32(0))  // Default
395     XCTAssertEqual(msg.oneofFloat, Float(0.0))  // Default
396     XCTAssertEqual(msg.oneofEnum, Message3_Enum.foo)  // Default
397     XCTAssertTrue(msg.oneof === autoCreated)  // Still the same
398     XCTAssertEqual(msg.oneof.optionalInt32, Int32(200))
399     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.oneofMessage)
400 
401     // Clear the oneof.
402     Message3_ClearOOneOfCase(msg)
403     XCTAssertEqual(msg.oneofInt32, Int32(0))  // Default
404     XCTAssertEqual(msg.oneofFloat, Float(0.0))  // Default
405     XCTAssertEqual(msg.oneofEnum, Message3_Enum.foo)  // Default
406     let autoCreated2 = msg.oneof  // Default create one
407     XCTAssertNotNil(autoCreated2)
408     XCTAssertTrue(autoCreated2 !== autoCreated)  // New instance
409     XCTAssertEqual(msg.oneof.optionalInt32, Int32(0))  // Default
410     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.gpbUnsetOneOfCase)
411 
412     msg.oneofInt32 = 10
413     XCTAssertEqual(msg.oneofInt32, Int32(10))
414     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.oneofInt32)
415 
416     // Confirm Message.clear() handles the oneof correctly.
417     msg.clear()
418     XCTAssertEqual(msg.oneofInt32, Int32(0))  // Default
419     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.gpbUnsetOneOfCase)
420 
421     // Sets via the autocreated instance.
422     msg.oneof.optionalInt32 = 300
423     XCTAssertTrue(msg.oneof !== autoCreated)  // New instance
424     XCTAssertTrue(msg.oneof !== autoCreated2)  // New instance
425     XCTAssertEqual(msg.oneof.optionalInt32, Int32(300))
426     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.oneofMessage)
427 
428     // Set message to nil clears the oneof.
429     msg.oneof = nil
430     XCTAssertEqual(msg.oneof.optionalInt32, Int32(0))  // Default
431     XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.gpbUnsetOneOfCase)
432   }
433 
testSerializationnull434   func testSerialization() {
435     let msg = Message2()
436 
437     msg.optionalInt32 = 100
438     msg.optionalInt64 = 101
439     msg.optionalGroup.a = 102
440     msg.repeatedStringArray.add("abc")
441     msg.repeatedStringArray.add("def")
442     msg.mapInt32Int32.setInt32(200, forKey:300)
443     msg.mapInt32Int32.setInt32(201, forKey:201)
444     msg.mapStringString.setObject("foo", forKey:"bar" as NSString)
445     msg.mapStringString.setObject("abc", forKey:"xyz" as NSString)
446 
447     let data = msg.data()
448 
449     let msg2 = try! Message2(data: data!)
450     XCTAssertTrue(msg2 !== msg)  // New instance
451     XCTAssertEqual(msg.optionalInt32, Int32(100))
452     XCTAssertEqual(msg.optionalInt64, Int64(101))
453     XCTAssertEqual(msg.optionalGroup.a, Int32(102))
454     XCTAssertEqual(msg.repeatedStringArray.count, Int(2))
455     XCTAssertEqual(msg.mapInt32Int32.count, UInt(2))
456     XCTAssertEqual(msg.mapStringString.count, Int(2))
457     XCTAssertEqual(msg2, msg)
458   }
459 
460 }
461