Skip to content

Commit cc1b397

Browse files
committed
Add exhaustive tests for JsonKey usage in value class
1 parent 13429c0 commit cc1b397

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.ser.valueClass.jsonKey
2+
3+
import com.fasterxml.jackson.annotation.JsonKey
4+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class NonNullObjectTest {
9+
@JvmInline
10+
value class NonNull(val v: String) {
11+
@JsonKey
12+
fun jsonValue() = v + "_modified"
13+
}
14+
15+
@Test
16+
fun nonNullTest() {
17+
assertEquals(
18+
"""{"test_modified":null}""",
19+
defaultMapper.writeValueAsString(mapOf(NonNull("test") to null)),
20+
)
21+
}
22+
23+
@JvmInline
24+
value class Nullable(val v: String) {
25+
@JsonKey
26+
fun jsonValue() = v.takeIf { it.length % 2 == 0 }?.let { it + "_modified" }
27+
}
28+
29+
// The case of returning null as a key is unnecessary because it will result in an error
30+
@Test
31+
fun nullableTest() {
32+
assertEquals(
33+
"""{"test_modified":null}""",
34+
defaultMapper.writeValueAsString(mapOf(Nullable("test") to null)),
35+
)
36+
}
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.ser.valueClass.jsonKey
2+
3+
import com.fasterxml.jackson.annotation.JsonKey
4+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class NullableObjectTest {
9+
@JvmInline
10+
value class Value(val v: String?) {
11+
@JsonKey
12+
fun jsonValue() = v?.let { it + "_modified" }
13+
}
14+
15+
// The case of returning null as a key is unnecessary because it will result in an error
16+
@Test
17+
fun test() {
18+
assertEquals(
19+
"""{"test_modified":null}""",
20+
defaultMapper.writeValueAsString(mapOf(Value("test") to null)),
21+
)
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.ser.valueClass.jsonKey
2+
3+
import com.fasterxml.jackson.annotation.JsonKey
4+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class NullablePrimitiveTest {
9+
@JvmInline
10+
value class Value(val v: Int?) {
11+
@JsonKey
12+
fun jsonValue() = v?.let { it + 100 }
13+
}
14+
15+
// The case of returning null as a key is unnecessary because it will result in an error
16+
@Test
17+
fun test() {
18+
assertEquals(
19+
"""{"100":null}""",
20+
defaultMapper.writeValueAsString(mapOf(Value(0) to null)),
21+
)
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.ser.valueClass.jsonKey
2+
3+
import com.fasterxml.jackson.annotation.JsonKey
4+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Test
7+
8+
class PrimitiveTest {
9+
@JvmInline
10+
value class NonNull(val v: Int) {
11+
@JsonKey
12+
fun jsonValue() = v + 100
13+
}
14+
15+
@Test
16+
fun nonNullTest() {
17+
assertEquals(
18+
"""{"100":null}""",
19+
defaultMapper.writeValueAsString(mapOf(NonNull(0) to null)),
20+
)
21+
}
22+
23+
@JvmInline
24+
value class Nullable(val v: Int) {
25+
@JsonKey
26+
fun jsonValue() = v.takeIf { it % 2 == 0 }?.let { it + 100 }
27+
}
28+
29+
// The case of returning null as a key is unnecessary because it will result in an error
30+
@Test
31+
fun nullableTest() {
32+
assertEquals(
33+
"""{"100":null}""",
34+
defaultMapper.writeValueAsString(mapOf(Nullable(0) to null)),
35+
)
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.ser.valueClass.jsonKey
2+
3+
import com.fasterxml.jackson.annotation.JsonKey
4+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
5+
import io.github.projectmapk.jackson.module.kogera.zIntegration.ser.valueClass.jsonKey.PrimitiveTest.NonNull
6+
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Test
8+
9+
class TwoUnitPrimitiveTest {
10+
@JvmInline
11+
value class NonNull(val v: Long) {
12+
@JsonKey
13+
fun jsonValue() = v + 100
14+
}
15+
16+
@Test
17+
fun nonNullTest() {
18+
assertEquals(
19+
"""{"100":null}""",
20+
defaultMapper.writeValueAsString(mapOf(NonNull(0) to null)),
21+
)
22+
}
23+
24+
@JvmInline
25+
value class Nullable(val v: Long) {
26+
@JsonKey
27+
fun jsonValue() = v.takeIf { it % 2L == 0L }?.let { it + 100 }
28+
}
29+
30+
// The case of returning null as a key is unnecessary because it will result in an error
31+
@Test
32+
fun nullableTest() {
33+
assertEquals(
34+
"""{"100":null}""",
35+
defaultMapper.writeValueAsString(mapOf(Nullable(0) to null)),
36+
)
37+
}
38+
}

0 commit comments

Comments
 (0)