1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf.kotlin 9 10 import com.google.common.truth.Truth.assertThat 11 import com.google.protobuf.kotlin.generator.`in`.EvilNamesProto3OuterClass.Class 12 import com.google.protobuf.kotlin.generator.`in`.EvilNamesProto3OuterClass.EvilNamesProto3 13 import com.google.protobuf.kotlin.generator.`in`.EvilNamesProto3OuterClass.HardKeywordsAllTypesProto3 14 import com.google.protobuf.kotlin.generator.`in`.HardKeywordsAllTypesProto3Kt 15 import com.google.protobuf.kotlin.generator.`in`.class_ 16 import com.google.protobuf.kotlin.generator.`in`.evilNamesProto3 17 import com.google.protobuf.kotlin.generator.`in`.hardKeywordsAllTypesProto3 18 import proto3_unittest.TestAllTypesKt 19 import proto3_unittest.TestAllTypesKt.nestedMessage 20 import proto3_unittest.UnittestProto3.TestAllTypes 21 import proto3_unittest.UnittestProto3.TestAllTypes.NestedEnum 22 import proto3_unittest.UnittestProto3.TestEmptyMessage 23 import proto3_unittest.copy 24 import proto3_unittest.optionalForeignMessageOrNull 25 import proto3_unittest.optionalNestedMessageOrNull 26 import proto3_unittest.testAllTypes 27 import proto3_unittest.testEmptyMessage 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.junit.runners.JUnit4 31 32 @RunWith(JUnit4::class) 33 class Proto3Test { 34 @Suppress("CheckReturnValue") 35 @Test testGettersAndSettersnull36 fun testGettersAndSetters() { 37 testAllTypes { 38 optionalInt32 = 101 39 assertThat(optionalInt32).isEqualTo(101) 40 optionalString = "115" 41 assertThat(optionalString).isEqualTo("115") 42 optionalNestedMessage = TestAllTypesKt.nestedMessage { bb = 118 } 43 assertThat(optionalNestedMessage).isEqualTo(TestAllTypesKt.nestedMessage { bb = 118 }) 44 optionalNestedEnum = NestedEnum.BAZ 45 assertThat(optionalNestedEnum).isEqualTo(NestedEnum.BAZ) 46 assertThat(optionalNestedEnumValue).isEqualTo(3) 47 optionalNestedEnumValue = 1 48 assertThat(optionalNestedEnumValue).isEqualTo(1) 49 assertThat(optionalNestedEnum).isEqualTo(NestedEnum.FOO) 50 51 oneofUint32 = 601 52 assertThat(oneofUint32).isEqualTo(601) 53 } 54 } 55 56 @Suppress("CheckReturnValue") 57 @Test testRepeatedGettersAndSettersnull58 fun testRepeatedGettersAndSetters() { 59 testAllTypes { 60 repeatedInt32.addAll(listOf(1, 2)) 61 assertThat(repeatedInt32).isEqualTo(listOf(1, 2)) 62 repeatedInt32 += listOf(3, 4) 63 assertThat(repeatedInt32).isEqualTo(listOf(1, 2, 3, 4)) 64 repeatedInt32[0] = 5 65 assertThat(repeatedInt32).isEqualTo(listOf(5, 2, 3, 4)) 66 67 repeatedString.addAll(listOf("1", "2")) 68 assertThat(repeatedString).isEqualTo(listOf("1", "2")) 69 repeatedString += listOf("3", "4") 70 assertThat(repeatedString).isEqualTo(listOf("1", "2", "3", "4")) 71 repeatedString[0] = "5" 72 assertThat(repeatedString).isEqualTo(listOf("5", "2", "3", "4")) 73 74 repeatedNestedMessage.addAll(listOf(nestedMessage { bb = 1 }, nestedMessage { bb = 2 })) 75 assertThat(repeatedNestedMessage) 76 .isEqualTo(listOf(nestedMessage { bb = 1 }, nestedMessage { bb = 2 })) 77 repeatedNestedMessage += listOf(nestedMessage { bb = 3 }, nestedMessage { bb = 4 }) 78 assertThat(repeatedNestedMessage) 79 .isEqualTo( 80 listOf( 81 nestedMessage { bb = 1 }, 82 nestedMessage { bb = 2 }, 83 nestedMessage { bb = 3 }, 84 nestedMessage { bb = 4 }, 85 ) 86 ) 87 repeatedNestedMessage[0] = nestedMessage { bb = 5 } 88 assertThat(repeatedNestedMessage) 89 .isEqualTo( 90 listOf( 91 nestedMessage { bb = 5 }, 92 nestedMessage { bb = 2 }, 93 nestedMessage { bb = 3 }, 94 nestedMessage { bb = 4 }, 95 ) 96 ) 97 98 repeatedNestedEnum.addAll(listOf(NestedEnum.FOO, NestedEnum.BAR)) 99 assertThat(repeatedNestedEnum).isEqualTo(listOf(NestedEnum.FOO, NestedEnum.BAR)) 100 repeatedNestedEnum += listOf(NestedEnum.BAZ, NestedEnum.FOO) 101 assertThat(repeatedNestedEnum) 102 .isEqualTo(listOf(NestedEnum.FOO, NestedEnum.BAR, NestedEnum.BAZ, NestedEnum.FOO)) 103 repeatedNestedEnum[0] = NestedEnum.BAR 104 assertThat(repeatedNestedEnum) 105 .isEqualTo(listOf(NestedEnum.BAR, NestedEnum.BAR, NestedEnum.BAZ, NestedEnum.FOO)) 106 } 107 } 108 109 @Test testClearsnull110 fun testClears() { 111 assertThat( 112 testAllTypes { 113 optionalInt32 = 101 114 clearOptionalInt32() 115 116 optionalString = "115" 117 clearOptionalString() 118 119 optionalNestedMessage = TestAllTypesKt.nestedMessage { bb = 118 } 120 clearOptionalNestedMessage() 121 122 optionalNestedEnum = NestedEnum.BAZ 123 clearOptionalNestedEnum() 124 125 oneofUint32 = 601 126 clearOneofUint32() 127 } 128 ) 129 .isEqualTo(TestAllTypes.newBuilder().build()) 130 } 131 132 @Test testCopynull133 fun testCopy() { 134 val message = testAllTypes { 135 optionalInt32 = 101 136 optionalString = "115" 137 } 138 val modifiedMessage = message.copy { optionalInt32 = 201 } 139 140 assertThat(message) 141 .isEqualTo(TestAllTypes.newBuilder().setOptionalInt32(101).setOptionalString("115").build()) 142 assertThat(modifiedMessage) 143 .isEqualTo(TestAllTypes.newBuilder().setOptionalInt32(201).setOptionalString("115").build()) 144 } 145 146 @Test testOneofnull147 fun testOneof() { 148 val message = testAllTypes { 149 oneofString = "foo" 150 assertThat(oneofFieldCase).isEqualTo(TestAllTypes.OneofFieldCase.ONEOF_STRING) 151 assertThat(oneofString).isEqualTo("foo") 152 clearOneofField() 153 assertThat(oneofFieldCase).isEqualTo(TestAllTypes.OneofFieldCase.ONEOFFIELD_NOT_SET) 154 oneofUint32 = 5 155 } 156 157 assertThat(message.getOneofFieldCase()).isEqualTo(TestAllTypes.OneofFieldCase.ONEOF_UINT32) 158 assertThat(message.getOneofUint32()).isEqualTo(5) 159 } 160 161 @Test testEmptyMessagesnull162 fun testEmptyMessages() { 163 assertThat(testEmptyMessage {}).isEqualTo(TestEmptyMessage.newBuilder().build()) 164 } 165 166 @Test testEvilNamesnull167 fun testEvilNames() { 168 assertThat( 169 evilNamesProto3 { 170 initialized = true 171 hasFoo = true 172 bar = "foo" 173 isInitialized = true 174 fooBar = "foo" 175 aLLCAPS += "foo" 176 aLLCAPSMAP[1] = true 177 hasUnderbarPrecedingNumeric1Foo = true 178 hasUnderbarPrecedingNumeric42Bar = true 179 hasUnderbarPrecedingNumeric123Foo42BarBaz = true 180 extension += "foo" 181 class_ = "foo" 182 int = 1.0 183 long = true 184 boolean = 1L 185 sealed = "foo" 186 interface_ = 1F 187 in_ = 1 188 object_ = "foo" 189 cachedSize_ = "foo" 190 serializedSize_ = true 191 value = "foo" 192 index = 1L 193 values += "foo" 194 newValues += "foo" 195 builder = true 196 k[1] = 1 197 v["foo"] = "foo" 198 key["foo"] = 1 199 map[1] = "foo" 200 pairs["foo"] = 1 201 LeadingUnderscore = "foo" 202 option = 1 203 dEPRECATEDFoo = "foo" 204 iD = "foo" 205 aBNotification = "foo" 206 DEPRECATEDBar = "foo" 207 notDEPRECATEDFoo = "foo" 208 } 209 ) 210 .isEqualTo( 211 EvilNamesProto3.newBuilder() 212 .setInitialized(true) 213 .setHasFoo(true) 214 .setBar("foo") 215 .setIsInitialized(true) 216 .setFooBar("foo") 217 .addALLCAPS("foo") 218 .putALLCAPSMAP(1, true) 219 .setHasUnderbarPrecedingNumeric1Foo(true) 220 .setHasUnderbarPrecedingNumeric42Bar(true) 221 .setHasUnderbarPrecedingNumeric123Foo42BarBaz(true) 222 .addExtension("foo") 223 .setClass_("foo") 224 .setInt(1.0) 225 .setLong(true) 226 .setBoolean(1L) 227 .setSealed("foo") 228 .setInterface(1F) 229 .setIn(1) 230 .setObject("foo") 231 .setCachedSize_("foo") 232 .setSerializedSize_(true) 233 .setValue("foo") 234 .setIndex(1L) 235 .addValues("foo") 236 .addNewValues("foo") 237 .setBuilder(true) 238 .putK(1, 1) 239 .putV("foo", "foo") 240 .putKey("foo", 1) 241 .putMap(1, "foo") 242 .putPairs("foo", 1) 243 .setLeadingUnderscore("foo") 244 .setOption(1) 245 .setDEPRECATEDFoo("foo") 246 .setID("foo") 247 .setABNotification("foo") 248 .setDEPRECATEDBar("foo") 249 .setNotDEPRECATEDFoo("foo") 250 .build() 251 ) 252 253 assertThat(class_ {}).isEqualTo(Class.newBuilder().build()) 254 } 255 256 @Suppress("CheckReturnValue") 257 @Test testHardKeywordGettersAndSettersnull258 fun testHardKeywordGettersAndSetters() { 259 hardKeywordsAllTypesProto3 { 260 as_ = 1 261 assertThat(as_).isEqualTo(1) 262 263 in_ = "foo" 264 assertThat(in_).isEqualTo("foo") 265 266 break_ = HardKeywordsAllTypesProto3.NestedEnum.FOO 267 assertThat(break_).isEqualTo(HardKeywordsAllTypesProto3.NestedEnum.FOO) 268 269 do_ = HardKeywordsAllTypesProto3Kt.nestedMessage { while_ = 1 } 270 assertThat(do_).isEqualTo(HardKeywordsAllTypesProto3Kt.nestedMessage { while_ = 1 }) 271 272 continue_[1] = 1 273 assertThat(continue_[1]).isEqualTo(1) 274 275 else_ += 1 276 assertThat(else_).isEqualTo(listOf(1)) 277 278 for_ += "foo" 279 assertThat(for_).isEqualTo(listOf("foo")) 280 281 fun_ += HardKeywordsAllTypesProto3.NestedEnum.FOO 282 assertThat(fun_).isEqualTo(listOf(HardKeywordsAllTypesProto3.NestedEnum.FOO)) 283 284 if_ += HardKeywordsAllTypesProto3Kt.nestedMessage { while_ = 1 } 285 assertThat(if_).isEqualTo(listOf(HardKeywordsAllTypesProto3Kt.nestedMessage { while_ = 1 })) 286 } 287 } 288 289 @Suppress("CheckReturnValue") 290 @Test testHardKeywordHazzersnull291 fun testHardKeywordHazzers() { 292 hardKeywordsAllTypesProto3 { 293 as_ = 1 294 assertThat(hasAs_()).isTrue() 295 296 in_ = "foo" 297 assertThat(hasIn_()).isTrue() 298 299 break_ = HardKeywordsAllTypesProto3.NestedEnum.FOO 300 assertThat(hasBreak_()).isTrue() 301 302 do_ = HardKeywordsAllTypesProto3Kt.nestedMessage { while_ = 1 } 303 assertThat(hasDo_()).isTrue() 304 } 305 } 306 307 @Suppress("CheckReturnValue") 308 @Test testHardKeywordClearsnull309 fun testHardKeywordClears() { 310 hardKeywordsAllTypesProto3 { 311 as_ = 1 312 clearAs_() 313 assertThat(hasAs_()).isFalse() 314 315 in_ = "foo" 316 clearIn_() 317 assertThat(hasIn_()).isFalse() 318 319 break_ = HardKeywordsAllTypesProto3.NestedEnum.FOO 320 clearBreak_() 321 assertThat(hasBreak_()).isFalse() 322 323 do_ = HardKeywordsAllTypesProto3Kt.nestedMessage { while_ = 1 } 324 clearDo_() 325 assertThat(hasDo_()).isFalse() 326 } 327 } 328 329 @Test testMultipleFilesnull330 fun testMultipleFiles() { 331 assertThat(com.google.protobuf.kotlin.generator.multipleFilesMessageA {}) 332 .isEqualTo(com.google.protobuf.kotlin.generator.MultipleFilesMessageA.newBuilder().build()) 333 334 assertThat(com.google.protobuf.kotlin.generator.multipleFilesMessageB {}) 335 .isEqualTo(com.google.protobuf.kotlin.generator.MultipleFilesMessageB.newBuilder().build()) 336 } 337 338 @Test testGetOrNullnull339 fun testGetOrNull() { 340 val noNestedMessage = testAllTypes {} 341 assertThat(noNestedMessage.optionalNestedMessageOrNull).isEqualTo(null) 342 343 val someNestedMessage = testAllTypes { 344 optionalNestedMessage = TestAllTypesKt.nestedMessage { bb = 118 } 345 } 346 assertThat(someNestedMessage.optionalNestedMessageOrNull) 347 .isEqualTo(TestAllTypesKt.nestedMessage { bb = 118 }) 348 349 // No optional keyword, OrNull should still be generated 350 assertThat(someNestedMessage.optionalForeignMessageOrNull).isEqualTo(null) 351 } 352 } 353