Skip to content

Commit 0ca5675

Browse files
committed
Slight optimization of Value class tests
1 parent 2fb7f58 commit 0ca5675

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

firebase-common/src/commonTest/kotlin/dev/gitlive/firebase/EncodersTest.kt

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ object TestObject {
2323
}
2424

2525
@Serializable
26-
data class TestData(val map: Map<String, String>, val otherMap: Map<Int, Int>, val bool: Boolean = false, val nullableBool: Boolean? = null)
26+
@JvmInline
27+
value class ValueClass(val int: Int)
28+
29+
@Serializable
30+
data class TestData(
31+
val map: Map<String, String>,
32+
val otherMap: Map<Int, Int>,
33+
val bool: Boolean = false,
34+
val nullableBool: Boolean? = null,
35+
val valueClass: ValueClass,
36+
)
2737

2838
@Serializable
2939
sealed class SealedClass {
@@ -56,13 +66,6 @@ data class NestedClass(
5666
val abstractMap: Map<AbstractClass, AbstractClass>
5767
)
5868

59-
@Serializable
60-
@JvmInline
61-
value class ValueClass(val int: Int)
62-
63-
@Serializable
64-
data class ValueClassWrapper(val value: ValueClass)
65-
6669
class EncodersTest {
6770

6871
@Test
@@ -96,35 +99,46 @@ class EncodersTest {
9699
assertEquals(TestObject, decoded)
97100
}
98101

102+
@Test
103+
fun encodeDecodeValueClass() {
104+
val testValueClass = ValueClass(42)
105+
val encoded = encode(ValueClass.serializer(), testValueClass) { encodeDefaults = false }
106+
107+
nativeAssertEquals(42, encoded)
108+
109+
val decoded = decode(ValueClass.serializer(), encoded)
110+
assertEquals(testValueClass, decoded)
111+
}
112+
99113
@Test
100114
fun encodeDecodeClass() {
101-
val testDataClass = TestData(mapOf("key" to "value"), mapOf(1 to 1), true)
115+
val testDataClass = TestData(mapOf("key" to "value"), mapOf(1 to 1), true, null, ValueClass(42))
102116
val encoded = encode(TestData.serializer(), testDataClass) { encodeDefaults = false }
103117

104-
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true), encoded)
118+
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "valueClass" to 42), encoded)
105119

106120
val decoded = decode(TestData.serializer(), encoded)
107121
assertEquals(testDataClass, decoded)
108122
}
109123

110124
@Test
111125
fun encodeDecodeClassNullableValue() {
112-
val testDataClass = TestData(mapOf("key" to "value"), mapOf(1 to 1), true, nullableBool = true)
126+
val testDataClass = TestData(mapOf("key" to "value"), mapOf(1 to 1), true, nullableBool = true, ValueClass(42))
113127
val encoded = encode(TestData.serializer(), testDataClass) { encodeDefaults = true }
114128

115-
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true), encoded)
129+
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true, "valueClass" to 42), encoded)
116130

117131
val decoded = decode(TestData.serializer(), encoded)
118132
assertEquals(testDataClass, decoded)
119133
}
120134

121135
@Test
122136
fun encodeDecodeGenericClass() {
123-
val innerClass = TestData(mapOf("key" to "value"), mapOf(1 to 1), true)
137+
val innerClass = TestData(mapOf("key" to "value"), mapOf(1 to 1), true, valueClass = ValueClass(42))
124138
val genericClass = GenericClass(innerClass)
125139
val encoded = encode(GenericClass.serializer(TestData.serializer()), genericClass) { encodeDefaults = true }
126140

127-
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to null)), encoded)
141+
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to null, "valueClass" to 42)), encoded)
128142

129143
val decoded = decode(GenericClass.serializer(TestData.serializer()), encoded)
130144
assertEquals(genericClass, decoded)
@@ -199,28 +213,6 @@ class EncodersTest {
199213
assertEquals(nestedClass, decoded)
200214
}
201215

202-
@Test
203-
fun encodeDecodeValueClassWrapper() {
204-
val testValueClassWrapper = ValueClassWrapper(ValueClass(42))
205-
val encoded = encode(ValueClassWrapper.serializer(), testValueClassWrapper) { encodeDefaults = false }
206-
207-
nativeAssertEquals(nativeMapOf("value" to 42), encoded)
208-
209-
val decoded = decode(ValueClassWrapper.serializer(), encoded)
210-
assertEquals(testValueClassWrapper, decoded)
211-
}
212-
213-
@Test
214-
fun encodeDecodeValueClass() {
215-
val testValueClass = ValueClass(42)
216-
val encoded = encode(ValueClass.serializer(), testValueClass) { encodeDefaults = false }
217-
218-
nativeAssertEquals(42, encoded)
219-
220-
val decoded = decode(ValueClass.serializer(), encoded)
221-
assertEquals(testValueClass, decoded)
222-
}
223-
224216
@Test
225217
fun reencodeTransformationList() {
226218
val reencoded = reencodeTransformation<List<String>>(nativeListOf("One", "Two", "Three")) {
@@ -249,26 +241,39 @@ class EncodersTest {
249241
nativeAssertEquals(nativeMapOf(), reencoded)
250242
}
251243

244+
@Test
245+
fun reencodeTransformationValueClass() {
246+
val reencoded = reencodeTransformation<ValueClass>(
247+
42,
248+
{ encodeDefaults = false }
249+
) {
250+
assertEquals(ValueClass(42), it)
251+
ValueClass(23)
252+
}
253+
254+
nativeAssertEquals(23, reencoded)
255+
}
256+
252257
@Test
253258
fun reencodeTransformationClass() {
254259
val reencoded = reencodeTransformation<TestData>(
255-
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true),
260+
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true, "valueClass" to 42),
256261
{ encodeDefaults = false }
257262
) {
258-
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool = true, nullableBool = true), it)
263+
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool = true, nullableBool = true, ValueClass(42)), it)
259264
it.copy(map = mapOf("newKey" to "newValue"), nullableBool = null)
260265
}
261266

262-
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("newKey" to "newValue"), "otherMap" to nativeMapOf(1 to 1), "bool" to true), reencoded)
267+
nativeAssertEquals(nativeMapOf("map" to nativeMapOf("newKey" to "newValue"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "valueClass" to 42), reencoded)
263268
}
264269

265270
@Test
266271
fun reencodeTransformationNullableValue() {
267272
val reencoded = reencodeTransformation<TestData?>(
268-
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true),
273+
nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to true, "valueClass" to 42),
269274
{ encodeDefaults = false }
270275
) {
271-
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool = true, nullableBool = true), it)
276+
assertEquals(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool = true, nullableBool = true, valueClass = ValueClass(42)), it)
272277
null
273278
}
274279

@@ -279,17 +284,17 @@ class EncodersTest {
279284
fun reencodeTransformationGenericClass() {
280285
val reencoded = reencodeTransformation(
281286
GenericClass.serializer(TestData.serializer()),
282-
nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to false)),
287+
nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("key" to "value"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "nullableBool" to false, "valueClass" to 42)),
283288
{ encodeDefaults = false }
284289
) {
285290
assertEquals(
286-
GenericClass(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool = true, nullableBool = false)),
291+
GenericClass(TestData(mapOf("key" to "value"), mapOf(1 to 1), bool = true, nullableBool = false, valueClass = ValueClass(42))),
287292
it
288293
)
289294
GenericClass(it.inner.copy(map = mapOf("newKey" to "newValue"), nullableBool = null))
290295
}
291296

292-
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("newKey" to "newValue"), "otherMap" to nativeMapOf(1 to 1), "bool" to true)), reencoded)
297+
nativeAssertEquals(nativeMapOf("inner" to nativeMapOf("map" to nativeMapOf("newKey" to "newValue"), "otherMap" to nativeMapOf(1 to 1), "bool" to true, "valueClass" to 42)), reencoded)
293298
}
294299

295300
@Test

0 commit comments

Comments
 (0)