|
1 | 1 | /*
|
2 | 2 | * Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
|
3 | 3 | */
|
| 4 | + |
| 5 | +package kotlinx.rpc.grpc.pb |
| 6 | + |
| 7 | +import kotlinx.io.Buffer |
| 8 | +import kotlinx.rpc.grpc.test.common.* |
| 9 | +import kotlin.test.Test |
| 10 | +import kotlin.test.assertEquals |
| 11 | + |
| 12 | +class ProtosTest { |
| 13 | + |
| 14 | + private fun <T : Any> decodeEncode( |
| 15 | + msg: T, |
| 16 | + enc: T.(WireEncoder) -> Unit, |
| 17 | + dec: (WireDecoder) -> T? |
| 18 | + ): T? { |
| 19 | + val buffer = Buffer() |
| 20 | + val encoder = WireEncoder(buffer) |
| 21 | + |
| 22 | + msg.enc(encoder) |
| 23 | + encoder.flush() |
| 24 | + |
| 25 | + return WireDecoder(buffer).use { |
| 26 | + dec(it) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + @Test |
| 32 | + fun testAllPrimitiveProto() { |
| 33 | + val msg = AllPrimitivesCommon { |
| 34 | + int32 = 12 |
| 35 | + int64 = 1234567890123456789L |
| 36 | + uint32 = 12345u |
| 37 | + uint64 = 1234567890123456789uL |
| 38 | + sint32 = -12 |
| 39 | + sint64 = -1234567890123456789L |
| 40 | + fixed32 = 12345u |
| 41 | + fixed64 = 1234567890123456789uL |
| 42 | + sfixed32 = -12345 |
| 43 | + sfixed64 = -1234567890123456789L |
| 44 | + bool = true |
| 45 | + float = 1.0f |
| 46 | + double = 3.0 |
| 47 | + string = "test" |
| 48 | + bytes = byteArrayOf(1, 2, 3) |
| 49 | + } |
| 50 | + |
| 51 | + val decoded = decodeEncode(msg, { encodeWith(it) }, AllPrimitivesCommon::decodeWith) |
| 52 | + |
| 53 | + assertEquals(msg.double, decoded?.double) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun testRepeatedProto() { |
| 58 | + val msg = RepeatedCommon { |
| 59 | + listFixed32 = listOf(1, 2, 3).map { it.toUInt() } |
| 60 | + listInt32 = listOf(4, 5, 6) |
| 61 | + listString = listOf("a", "b", "c") |
| 62 | + } |
| 63 | + |
| 64 | + val decoded = decodeEncode(msg, { encodeWith(it) }, RepeatedCommon::decodeWith) |
| 65 | + |
| 66 | + assertEquals(msg.listInt32, decoded?.listInt32) |
| 67 | + assertEquals(msg.listFixed32, decoded?.listFixed32) |
| 68 | + assertEquals(msg.listString, decoded?.listString) |
| 69 | + } |
| 70 | + |
| 71 | +} |
0 commit comments