Skip to content

Commit 8b96e7d

Browse files
Copilothossain-khan
andcommitted
Extract duplicated toAny() helper function to reusable utility class
Co-authored-by: hossain-khan <[email protected]>
1 parent b814aad commit 8b96e7d

File tree

4 files changed

+30
-54
lines changed

4 files changed

+30
-54
lines changed

lib/src/test/kotlin/dev/hossain/json5kt/JSON5ParseErrorsTest.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,7 @@ import org.junit.jupiter.api.Test
1313
*/
1414
@DisplayName("JSON5.parse errors")
1515
class JSON5ParseErrorsTest {
16-
/**
17-
* Helper function to convert JSON5Value back to raw Kotlin objects for testing compatibility.
18-
*/
19-
private fun JSON5Value.toAny(): Any? {
20-
return when (this) {
21-
is JSON5Value.Null -> null
22-
is JSON5Value.Boolean -> this.value
23-
is JSON5Value.String -> this.value
24-
is JSON5Value.Number.Integer -> this.value.toDouble() // Convert to Double for consistency with parseToAny
25-
is JSON5Value.Number.Decimal -> this.value
26-
is JSON5Value.Number.Hexadecimal -> this.value.toDouble() // Convert to Double for consistency
27-
is JSON5Value.Number.PositiveInfinity -> Double.POSITIVE_INFINITY
28-
is JSON5Value.Number.NegativeInfinity -> Double.NEGATIVE_INFINITY
29-
is JSON5Value.Number.NaN -> Double.NaN
30-
is JSON5Value.Object -> this.value.mapValues { it.value.toAny() }
31-
is JSON5Value.Array -> this.value.map { it.toAny() }
32-
}
33-
}
16+
3417

3518

3619
/**

lib/src/test/kotlin/dev/hossain/json5kt/JSON5ParseTest.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,7 @@ import kotlin.test.assertTrue
2121
@DisplayName("JSON5.parse")
2222
class JSON5ParseTest {
2323

24-
/**
25-
* Helper function to convert JSON5Value back to raw Kotlin objects for testing compatibility.
26-
*/
27-
private fun JSON5Value.toAny(): Any? {
28-
return when (this) {
29-
is JSON5Value.Null -> null
30-
is JSON5Value.Boolean -> this.value
31-
is JSON5Value.String -> this.value
32-
is JSON5Value.Number.Integer -> this.value.toDouble() // Convert to Double for consistency with parseToAny
33-
is JSON5Value.Number.Decimal -> this.value
34-
is JSON5Value.Number.Hexadecimal -> this.value.toDouble() // Convert to Double for consistency
35-
is JSON5Value.Number.PositiveInfinity -> Double.POSITIVE_INFINITY
36-
is JSON5Value.Number.NegativeInfinity -> Double.NEGATIVE_INFINITY
37-
is JSON5Value.Number.NaN -> Double.NaN
38-
is JSON5Value.Object -> this.value.mapValues { it.value.toAny() }
39-
is JSON5Value.Array -> this.value.map { it.toAny() }
40-
}
41-
}
24+
4225

4326
/**
4427
* Tests parsing of an empty JSON5 object.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dev.hossain.json5kt
2+
3+
/**
4+
* Test utilities for JSON5 parsing.
5+
* Contains helper functions for testing compatibility between the new type-safe API
6+
* and the legacy parseToAny behavior.
7+
*/
8+
9+
/**
10+
* Converts a JSON5Value to a raw Any? object for testing compatibility.
11+
* This helper function maintains compatibility with the behavior of the deprecated parseToAny API.
12+
*/
13+
fun JSON5Value.toAny(): Any? {
14+
return when (this) {
15+
is JSON5Value.Null -> null
16+
is JSON5Value.Boolean -> this.value
17+
is JSON5Value.String -> this.value
18+
is JSON5Value.Number.Integer -> this.value.toDouble() // Convert to Double for consistency with parseToAny
19+
is JSON5Value.Number.Decimal -> this.value
20+
is JSON5Value.Number.Hexadecimal -> this.value.toDouble() // Convert to Double for consistency
21+
is JSON5Value.Number.PositiveInfinity -> Double.POSITIVE_INFINITY
22+
is JSON5Value.Number.NegativeInfinity -> Double.NEGATIVE_INFINITY
23+
is JSON5Value.Number.NaN -> Double.NaN
24+
is JSON5Value.Object -> this.value.mapValues { it.value.toAny() }
25+
is JSON5Value.Array -> this.value.map { it.toAny() }
26+
}
27+
}

lib/src/test/kotlin/dev/hossain/json5kt/JSON5ValueTest.kt

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,7 @@ import org.junit.jupiter.api.DisplayName
1212
*/
1313
@DisplayName("JSON5.parse with JSON5Value")
1414
class JSON5ValueTest {
15-
/**
16-
* Helper function to convert JSON5Value back to raw Kotlin objects for testing compatibility.
17-
*/
18-
private fun JSON5Value.toAny(): Any? {
19-
return when (this) {
20-
is JSON5Value.Null -> null
21-
is JSON5Value.Boolean -> this.value
22-
is JSON5Value.String -> this.value
23-
is JSON5Value.Number.Integer -> this.value.toDouble() // Convert to Double for consistency with parseToAny
24-
is JSON5Value.Number.Decimal -> this.value
25-
is JSON5Value.Number.Hexadecimal -> this.value.toDouble() // Convert to Double for consistency
26-
is JSON5Value.Number.PositiveInfinity -> Double.POSITIVE_INFINITY
27-
is JSON5Value.Number.NegativeInfinity -> Double.NEGATIVE_INFINITY
28-
is JSON5Value.Number.NaN -> Double.NaN
29-
is JSON5Value.Object -> this.value.mapValues { it.value.toAny() }
30-
is JSON5Value.Array -> this.value.map { it.toAny() }
31-
}
32-
}
15+
3316

3417

3518
@Test

0 commit comments

Comments
 (0)