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.optionalMessage = msg2 48 msg3.a = 200 49 msg.optionalGroup = msg3 50 msg.repeatedInt32Array.addValue(300) 51 msg.repeatedInt32Array.addValue(301) 52 msg.repeatedStringArray.addObject("mno") 53 msg.repeatedStringArray.addObject("pqr") 54 msg.repeatedEnumArray.addValue(Message2_Enum.Bar.rawValue) 55 msg.repeatedEnumArray.addValue(Message2_Enum.Baz.rawValue) 56 msg.mapInt32Int32.setValue(400, forKey:500) 57 msg.mapInt32Int32.setValue(401, forKey:501) 58 msg.mapStringString.setObject("foo", forKey:"bar") 59 msg.mapStringString.setObject("abc", forKey:"xyz") 60 msg.mapInt32Enum.setValue(Message2_Enum.Bar.rawValue, forKey:600) 61 msg.mapInt32Enum.setValue(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.optionalMessage === 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.valueAtIndex(0), Int32(300)) 84 XCTAssertEqual(msg.repeatedInt32Array.valueAtIndex(1), Int32(301)) 85 XCTAssertEqual(msg.repeatedStringArray.count, Int(2)) 86 XCTAssertEqual(msg.repeatedStringArray.objectAtIndex(0) as? String, "mno") 87 XCTAssertEqual(msg.repeatedStringArray.objectAtIndex(1) as? String, "pqr") 88 XCTAssertEqual(msg.repeatedEnumArray.count, UInt(2)) 89 XCTAssertEqual(msg.repeatedEnumArray.valueAtIndex(0), Message2_Enum.Bar.rawValue) 90 XCTAssertEqual(msg.repeatedEnumArray.valueAtIndex(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.valueForKey(500, value:&intValue)) 95 XCTAssertEqual(intValue, Int32(400)) 96 XCTAssertTrue(msg.mapInt32Int32.valueForKey(501, value:&intValue)) 97 XCTAssertEqual(intValue, Int32(401)) 98 XCTAssertEqual(msg.mapStringString.count, Int(2)) 99 XCTAssertEqual(msg.mapStringString.objectForKey("bar") as? String, "foo") 100 XCTAssertEqual(msg.mapStringString.objectForKey("xyz") as? String, "abc") 101 XCTAssertEqual(msg.mapInt32Enum.count, UInt(2)) 102 XCTAssertTrue(msg.mapInt32Enum.valueForKey(600, value:&intValue)) 103 XCTAssertEqual(intValue, Message2_Enum.Bar.rawValue) 104 XCTAssertTrue(msg.mapInt32Enum.valueForKey(601, value:&intValue)) 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.optionalMessage !== 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.optionalMessage = msg2 146 msg.repeatedInt32Array.addValue(300) 147 msg.repeatedInt32Array.addValue(301) 148 msg.repeatedStringArray.addObject("mno") 149 msg.repeatedStringArray.addObject("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.setValue(400, forKey:500) 155 msg.mapInt32Int32.setValue(401, forKey:501) 156 msg.mapStringString.setObject("foo", forKey:"bar") 157 msg.mapStringString.setObject("abc", forKey:"xyz") 158 msg.mapInt32Enum.setValue(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.optionalMessage === msg2) 171 XCTAssertEqual(msg.optionalEnum, Message3_Enum.Bar) 172 XCTAssertEqual(msg.repeatedInt32Array.count, UInt(2)) 173 XCTAssertEqual(msg.repeatedInt32Array.valueAtIndex(0), Int32(300)) 174 XCTAssertEqual(msg.repeatedInt32Array.valueAtIndex(1), Int32(301)) 175 XCTAssertEqual(msg.repeatedStringArray.count, Int(2)) 176 XCTAssertEqual(msg.repeatedStringArray.objectAtIndex(0) as? String, "mno") 177 XCTAssertEqual(msg.repeatedStringArray.objectAtIndex(1) as? String, "pqr") 178 XCTAssertEqual(msg.repeatedInt64Array.count, UInt(0)) 179 XCTAssertEqual(msg.repeatedEnumArray.count, UInt(2)) 180 XCTAssertEqual(msg.repeatedEnumArray.valueAtIndex(0), Message3_Enum.Bar.rawValue) 181 XCTAssertEqual(msg.repeatedEnumArray.valueAtIndex(1), Message3_Enum.GPBUnrecognizedEnumeratorValue.rawValue) 182 XCTAssertEqual(msg.repeatedEnumArray.rawValueAtIndex(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.valueForKey(500, value:&intValue)) 188 XCTAssertEqual(intValue, Int32(400)) 189 XCTAssertTrue(msg.mapInt32Int32.valueForKey(501, value:&intValue)) 190 XCTAssertEqual(intValue, Int32(401)) 191 XCTAssertEqual(msg.mapStringString.count, Int(2)) 192 XCTAssertEqual(msg.mapStringString.objectForKey("bar") as? String, "foo") 193 XCTAssertEqual(msg.mapStringString.objectForKey("xyz") as? String, "abc") 194 XCTAssertEqual(msg.mapInt32Enum.count, UInt(2)) 195 XCTAssertTrue(msg.mapInt32Enum.valueForKey(600, value:&intValue)) 196 XCTAssertEqual(intValue, Message2_Enum.Bar.rawValue) 197 XCTAssertTrue(msg.mapInt32Enum.valueForKey(601, value:&intValue)) 198 XCTAssertEqual(intValue, Message3_Enum.GPBUnrecognizedEnumeratorValue.rawValue) 199 XCTAssertTrue(msg.mapInt32Enum.valueForKey(601, rawValue:&intValue)) 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.optionalMessage = nil 208 XCTAssertFalse(msg.hasOptionalMessage) 209 XCTAssertTrue(msg.optionalMessage !== 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.optionalMessage !== 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.optionalMessage.optionalMessage 238 let msg4 = msg.optionalMessage 239 XCTAssertNotNil(msg2) 240 XCTAssertNotNil(msg3) 241 XCTAssertFalse(msg.hasOptionalGroup) 242 XCTAssertFalse(msg.optionalMessage.hasOptionalMessage) 243 XCTAssertFalse(msg.hasOptionalMessage) 244 245 // Setting things should trigger has* getting set. 246 msg.optionalGroup.a = 10 247 msg.optionalMessage.optionalMessage.optionalInt32 = 100 248 XCTAssertTrue(msg.hasOptionalGroup) 249 XCTAssertTrue(msg.optionalMessage.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.optionalMessage.optionalMessage) 255 XCTAssertTrue(msg4 === msg.optionalMessage) 256 257 // Clear gets us new objects next time around. 258 msg.clear() 259 XCTAssertFalse(msg.hasOptionalGroup) 260 XCTAssertFalse(msg.optionalMessage.hasOptionalMessage) 261 XCTAssertFalse(msg.hasOptionalMessage) 262 msg.optionalGroup.a = 20 263 msg.optionalMessage.optionalMessage.optionalInt32 = 200 264 XCTAssertTrue(msg.hasOptionalGroup) 265 XCTAssertTrue(msg.optionalMessage.hasOptionalMessage) 266 XCTAssertTrue(msg.hasOptionalMessage) 267 XCTAssertTrue(msg2 !== msg.optionalGroup) 268 XCTAssertTrue(msg3 !== msg.optionalMessage.optionalMessage) 269 XCTAssertTrue(msg4 !== msg.optionalMessage) 270 271 // Explicit set of a message, means autocreated object doesn't bind. 272 msg.clear() 273 let autoCreated = msg.optionalMessage 274 XCTAssertFalse(msg.hasOptionalMessage) 275 let msg5 = Message2() 276 msg5.optionalInt32 = 123 277 msg.optionalMessage = msg5 278 XCTAssertTrue(msg.hasOptionalMessage) 279 // Modifing the autocreated doesn't replaced the explicit set one. 280 autoCreated.optionalInt32 = 456 281 XCTAssertTrue(msg.hasOptionalMessage) 282 XCTAssertTrue(msg.optionalMessage === msg5) 283 XCTAssertEqual(msg.optionalMessage.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.oneofMessage // 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.oneofMessage === 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.oneofMessage === 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.oneofMessage === autoCreated) // Still the same 316 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.OneofEnum) 317 318 // Sets via the autocreated instance. 319 msg.oneofMessage.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.oneofMessage === autoCreated) // Still the same 324 XCTAssertEqual(msg.oneofMessage.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.oneofMessage // Default create one 333 XCTAssertNotNil(autoCreated2) 334 XCTAssertTrue(autoCreated2 !== autoCreated) // New instance 335 XCTAssertEqual(msg.oneofMessage.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.oneofMessage.optionalInt32 = 300 349 XCTAssertTrue(msg.oneofMessage !== autoCreated) // New instance 350 XCTAssertTrue(msg.oneofMessage !== autoCreated2) // New instance 351 XCTAssertEqual(msg.oneofMessage.optionalInt32, Int32(300)) 352 XCTAssertEqual(msg.oOneOfCase, Message2_O_OneOfCase.OneofMessage) 353 354 // Set message to nil clears the oneof. 355 msg.oneofMessage = nil 356 XCTAssertEqual(msg.oneofMessage.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.oneofMessage // 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.oneofMessage === 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.oneofMessage === 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.oneofMessage === autoCreated) // Still the same 390 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.OneofEnum) 391 392 // Sets via the autocreated instance. 393 msg.oneofMessage.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.oneofMessage === autoCreated) // Still the same 398 XCTAssertEqual(msg.oneofMessage.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.oneofMessage // Default create one 407 XCTAssertNotNil(autoCreated2) 408 XCTAssertTrue(autoCreated2 !== autoCreated) // New instance 409 XCTAssertEqual(msg.oneofMessage.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.oneofMessage.optionalInt32 = 300 423 XCTAssertTrue(msg.oneofMessage !== autoCreated) // New instance 424 XCTAssertTrue(msg.oneofMessage !== autoCreated2) // New instance 425 XCTAssertEqual(msg.oneofMessage.optionalInt32, Int32(300)) 426 XCTAssertEqual(msg.oOneOfCase, Message3_O_OneOfCase.OneofMessage) 427 428 // Set message to nil clears the oneof. 429 msg.oneofMessage = nil 430 XCTAssertEqual(msg.oneofMessage.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.addObject("abc") 441 msg.repeatedStringArray.addObject("def") 442 msg.mapInt32Int32.setValue(200, forKey:300) 443 msg.mapInt32Int32.setValue(201, forKey:201) 444 msg.mapStringString.setObject("foo", forKey:"bar") 445 msg.mapStringString.setObject("abc", forKey:"xyz") 446 447 let data = msg.data() 448 449 let msg2 = Message2(data: data!, error:nil) 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