Skip to content

Commit e92e5d6

Browse files
committed
Added hasCode, equals and toString support and tests
1 parent b1d7aa0 commit e92e5d6

File tree

5 files changed

+580
-2
lines changed

5 files changed

+580
-2
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.protobuf.test
6+
7+
import Equals
8+
import invoke
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertNotEquals
12+
13+
class ComparisonTest {
14+
@Test
15+
fun equal() {
16+
val msg1 = Equals {
17+
str1 = "hello"
18+
bytes1 = byteArrayOf(1, 2, 3)
19+
someEnum2 = Equals.SomeEnum.VALUE1
20+
}
21+
22+
val msg2 = Equals {
23+
str1 = "hello"
24+
bytes1 = byteArrayOf(1, 2, 3)
25+
someEnum2 = Equals.SomeEnum.VALUE1
26+
}
27+
28+
assertEquals(msg1, msg2)
29+
assertEquals(msg1.hashCode(), msg2.hashCode())
30+
}
31+
32+
@Test
33+
fun equalWithOptional() {
34+
val msg1 = Equals {
35+
str1 = "hello"
36+
str2 = "world"
37+
bytes1 = byteArrayOf(1, 2, 3)
38+
bytes2 = byteArrayOf(1, 2, 4)
39+
someEnum2 = Equals.SomeEnum.VALUE1
40+
}
41+
42+
val msg2 = Equals {
43+
str1 = "hello"
44+
str2 = "world"
45+
bytes1 = byteArrayOf(1, 2, 3)
46+
bytes2 = byteArrayOf(1, 2, 4)
47+
someEnum2 = Equals.SomeEnum.VALUE1
48+
}
49+
50+
assertEquals(msg1, msg2)
51+
assertEquals(msg1.hashCode(), msg2.hashCode())
52+
}
53+
54+
@Test
55+
fun equalWithUnsetOptionalReference() {
56+
val msg1 = Equals {
57+
str1 = "hello"
58+
bytes1 = byteArrayOf(1, 2, 3)
59+
nested = Equals.Nested {
60+
content = "hello"
61+
}
62+
someEnum2 = Equals.SomeEnum.VALUE1
63+
}
64+
65+
val msg2 = Equals {
66+
str1 = "hello"
67+
bytes1 = byteArrayOf(1, 2, 3)
68+
someEnum2 = Equals.SomeEnum.VALUE1
69+
}
70+
71+
assertNotEquals(msg1, msg2)
72+
assertNotEquals(msg1.hashCode(), msg2.hashCode())
73+
}
74+
75+
@Test
76+
fun notEqual() {
77+
val msg1 = Equals {
78+
str1 = "hello1"
79+
bytes1 = byteArrayOf(1, 2, 3)
80+
someEnum2 = Equals.SomeEnum.VALUE1
81+
}
82+
83+
val msg2 = Equals {
84+
str1 = "hello"
85+
bytes1 = byteArrayOf(1, 2, 3)
86+
someEnum2 = Equals.SomeEnum.VALUE1
87+
}
88+
89+
assertNotEquals(msg1, msg2)
90+
assertNotEquals(msg1.hashCode(), msg2.hashCode())
91+
}
92+
93+
@Test
94+
fun differentOneOf() {
95+
val msg1 = Equals {
96+
str1 = "hello"
97+
bytes1 = byteArrayOf(1, 2, 3)
98+
oneof = Equals.Oneof.Option1(42)
99+
someEnum2 = Equals.SomeEnum.VALUE1
100+
}
101+
102+
val msg2 = Equals {
103+
str1 = "hello"
104+
bytes1 = byteArrayOf(1, 2, 3)
105+
oneof = Equals.Oneof.Option2(42)
106+
someEnum2 = Equals.SomeEnum.VALUE1
107+
}
108+
109+
assertNotEquals(msg1, msg2)
110+
assertNotEquals(msg1.hashCode(), msg2.hashCode())
111+
}
112+
113+
@Test
114+
fun sameOneOf() {
115+
val msg1 = Equals {
116+
str1 = "hello"
117+
bytes1 = byteArrayOf(1, 2, 3)
118+
oneof = Equals.Oneof.Option1(42)
119+
someEnum2 = Equals.SomeEnum.VALUE1
120+
}
121+
122+
val msg2 = Equals {
123+
str1 = "hello"
124+
bytes1 = byteArrayOf(1, 2, 3)
125+
oneof = Equals.Oneof.Option1(42)
126+
someEnum2 = Equals.SomeEnum.VALUE1
127+
}
128+
129+
assertEquals(msg1, msg2)
130+
assertEquals(msg1.hashCode(), msg2.hashCode())
131+
}
132+
133+
@Test
134+
fun sameEnum() {
135+
val msg1 = Equals {
136+
str1 = "hello"
137+
bytes1 = byteArrayOf(1, 2, 3)
138+
someEnum = Equals.SomeEnum.VALUE1
139+
someEnum2 = Equals.SomeEnum.VALUE1
140+
}
141+
142+
val msg2 = Equals {
143+
str1 = "hello"
144+
bytes1 = byteArrayOf(1, 2, 3)
145+
someEnum = Equals.SomeEnum.VALUE1
146+
someEnum2 = Equals.SomeEnum.VALUE1
147+
}
148+
149+
assertEquals(msg1, msg2)
150+
assertEquals(msg1.hashCode(), msg2.hashCode())
151+
}
152+
153+
@Test
154+
fun differentEnum() {
155+
val msg1 = Equals {
156+
str1 = "hello"
157+
bytes1 = byteArrayOf(1, 2, 3)
158+
someEnum = Equals.SomeEnum.VALUE1
159+
someEnum2 = Equals.SomeEnum.VALUE2
160+
}
161+
162+
val msg2 = Equals {
163+
str1 = "hello"
164+
bytes1 = byteArrayOf(1, 2, 3)
165+
someEnum = Equals.SomeEnum.VALUE2
166+
someEnum2 = Equals.SomeEnum.VALUE2
167+
}
168+
169+
assertNotEquals(msg1, msg2)
170+
assertNotEquals(msg1.hashCode(), msg2.hashCode())
171+
}
172+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.protobuf.test
6+
7+
import ToString
8+
import invoke
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
class ToStringTest {
13+
@Test
14+
fun noOptionals() {
15+
val message = ToString {
16+
text = "hello"
17+
bytes = byteArrayOf(1, 2, 3)
18+
}
19+
20+
assertEquals("""
21+
ToString(
22+
text=hello,
23+
bytes=[1, 2, 3],
24+
optionalBytes=<unset>,
25+
nested=<unset>,
26+
enum=<unset>,
27+
list=[],
28+
map={},
29+
oneof=null,
30+
)
31+
""".trimIndent(), message.toString())
32+
}
33+
34+
@Test
35+
fun withOptionals() {
36+
val message = ToString {
37+
text = "hello"
38+
bytes = byteArrayOf(1, 2, 3)
39+
optionalBytes = byteArrayOf(1, 2, 4)
40+
nested = ToString.Nested { }
41+
enum = ToString.SomeEnum.VALUE1
42+
}
43+
44+
assertEquals("""
45+
ToString(
46+
text=hello,
47+
bytes=[1, 2, 3],
48+
optionalBytes=[1, 2, 4],
49+
nested=ToString.Nested(
50+
recursive=<unset>,
51+
),
52+
enum=VALUE1,
53+
list=[],
54+
map={},
55+
oneof=null,
56+
)
57+
""".trimIndent(), message.toString())
58+
}
59+
60+
@Test
61+
fun list() {
62+
val message = ToString {
63+
text = "hello"
64+
bytes = byteArrayOf(1, 2, 3)
65+
list = listOf("a", "b", "c")
66+
}
67+
assertEquals("""
68+
ToString(
69+
text=hello,
70+
bytes=[1, 2, 3],
71+
optionalBytes=<unset>,
72+
nested=<unset>,
73+
enum=<unset>,
74+
list=[a, b, c],
75+
map={},
76+
oneof=null,
77+
)
78+
""".trimIndent(), message.toString())
79+
}
80+
81+
@Test
82+
fun map() {
83+
val message = ToString {
84+
text = "hello"
85+
bytes = byteArrayOf(1, 2, 3)
86+
map = mapOf(1 to 1, 2 to 2, 3 to 3)
87+
}
88+
assertEquals("""
89+
ToString(
90+
text=hello,
91+
bytes=[1, 2, 3],
92+
optionalBytes=<unset>,
93+
nested=<unset>,
94+
enum=<unset>,
95+
list=[],
96+
map={1=1, 2=2, 3=3},
97+
oneof=null,
98+
)
99+
""".trimIndent(), message.toString())
100+
}
101+
102+
@Test
103+
fun oneOf() {
104+
val message = ToString {
105+
text = "hello"
106+
bytes = byteArrayOf(1, 2, 3)
107+
oneof = ToString.Oneof.Option1("option1_value")
108+
}
109+
assertEquals("""
110+
ToString(
111+
text=hello,
112+
bytes=[1, 2, 3],
113+
optionalBytes=<unset>,
114+
nested=<unset>,
115+
enum=<unset>,
116+
list=[],
117+
map={},
118+
oneof=Option1(value=option1_value),
119+
)
120+
""".trimIndent(), message.toString())
121+
122+
val message2 = ToString {
123+
text = "hello"
124+
bytes = byteArrayOf(1, 2, 3)
125+
oneof = ToString.Oneof.Option3(42)
126+
}
127+
assertEquals("""
128+
ToString(
129+
text=hello,
130+
bytes=[1, 2, 3],
131+
optionalBytes=<unset>,
132+
nested=<unset>,
133+
enum=<unset>,
134+
list=[],
135+
map={},
136+
oneof=Option3(value=42),
137+
)
138+
""".trimIndent(), message2.toString())
139+
}
140+
141+
@Test
142+
fun recursive() {
143+
val message = ToString {
144+
text = "hello"
145+
bytes = byteArrayOf(1, 2, 3)
146+
nested = ToString.Nested {
147+
recursive = ToString.Nested {
148+
recursive = ToString.Nested { }
149+
}
150+
}
151+
}
152+
153+
assertEquals("""
154+
ToString(
155+
text=hello,
156+
bytes=[1, 2, 3],
157+
optionalBytes=<unset>,
158+
nested=ToString.Nested(
159+
recursive=ToString.Nested(
160+
recursive=ToString.Nested(
161+
recursive=<unset>,
162+
),
163+
),
164+
),
165+
enum=<unset>,
166+
list=[],
167+
map={},
168+
oneof=null,
169+
)
170+
""".trimIndent(), message.toString())
171+
}
172+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
syntax = "proto2";
2+
3+
message Equals {
4+
required string str1 = 1;
5+
optional string str2 = 2 [default = "abc"];
6+
required bytes bytes1 = 3;
7+
optional bytes bytes2 = 4 [default = "abc"];
8+
oneof oneof {
9+
sint32 option1 = 5;
10+
sint32 option2 = 6;
11+
}
12+
optional Nested nested = 7;
13+
message Nested {
14+
required string content = 1;
15+
}
16+
optional SomeEnum someEnum = 8;
17+
required SomeEnum someEnum2 = 9;
18+
enum SomeEnum {
19+
VALUE1 = 0;
20+
VALUE2 = 1;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
syntax = "proto2";
2+
3+
message ToString {
4+
required string text = 1;
5+
required bytes bytes = 2;
6+
optional bytes optionalBytes = 3;
7+
message Nested {
8+
optional Nested recursive = 1;
9+
}
10+
optional Nested nested = 4;
11+
enum SomeEnum {
12+
VALUE1 = 0;
13+
VALUE2 = 1;
14+
}
15+
optional SomeEnum enum = 5;
16+
oneof oneof {
17+
string option1 = 6;
18+
string option2 = 7;
19+
int32 option3 = 8;
20+
}
21+
repeated string list = 9;
22+
map<int32, int32> map = 10;
23+
}

0 commit comments

Comments
 (0)