Skip to content

Commit 6a58a77

Browse files
committed
🧪 enhance unit tests for sparse array parsing and encoding behavior
1 parent 2339451 commit 6a58a77

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

qs-kotlin/src/test/kotlin/io/github/techouse/qskotlin/unit/QsParserSpec.kt

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -351,35 +351,50 @@ class QsParserSpec :
351351
}
352352

353353
it("should compact sparse arrays") {
354-
val options20 = DecodeOptions(listLimit = 20)
354+
val options = DecodeOptions(listLimit = 20)
355355

356-
decode("a[10]=1&a[2]=2", options20) shouldBe mapOf("a" to listOf("2", "1"))
356+
decode("a[10]=1&a[2]=2", options) shouldBe mapOf("a" to listOf("2", "1"))
357357

358-
decode("a[1][b][2][c]=1", options20) shouldBe
358+
decode("a[1][b][2][c]=1", options) shouldBe
359359
mapOf("a" to listOf(mapOf("b" to listOf(mapOf("c" to "1")))))
360360

361-
decode("a[1][2][3][c]=1", options20) shouldBe
361+
decode("a[1][2][3][c]=1", options) shouldBe
362362
mapOf("a" to listOf(listOf(listOf(mapOf("c" to "1")))))
363363

364-
decode("a[1][2][3][c][1]=1", options20) shouldBe
364+
decode("a[1][2][3][c][1]=1", options) shouldBe
365365
mapOf("a" to listOf(listOf(listOf(mapOf("c" to listOf("1"))))))
366366
}
367367

368368
it("should parse sparse arrays") {
369-
// Note: Kotlin implementation doesn't have allowSparse option,
370-
// sparse arrays are handled by default behavior
371-
val options = DecodeOptions()
369+
val optionsAllowSparse = DecodeOptions(allowSparse = true)
372370

373-
decode("a[4]=1&a[1]=2", options) shouldBe mapOf("a" to mapOf(1 to "2", 4 to "1"))
371+
decode("a[4]=1&a[1]=2", optionsAllowSparse) shouldBe
372+
mapOf("a" to listOf(null, "2", null, null, "1"))
374373

375-
decode("a[1][b][2][c]=1", options) shouldBe
376-
mapOf("a" to listOf(mapOf("b" to listOf(mapOf("c" to "1")))))
374+
decode("a[1][b][2][c]=1", optionsAllowSparse) shouldBe
375+
mapOf("a" to listOf(null, mapOf("b" to listOf(null, null, mapOf("c" to "1")))))
377376

378-
decode("a[1][2][3][c]=1", options) shouldBe
379-
mapOf("a" to listOf(listOf(listOf(mapOf("c" to "1")))))
377+
decode("a[1][2][3][c]=1", optionsAllowSparse) shouldBe
378+
mapOf(
379+
"a" to
380+
listOf(
381+
null,
382+
listOf(null, null, listOf(null, null, null, mapOf("c" to "1"))),
383+
)
384+
)
380385

381-
decode("a[1][2][3][c][1]=1", options) shouldBe
382-
mapOf("a" to listOf(listOf(listOf(mapOf("c" to listOf("1"))))))
386+
decode("a[1][2][3][c][1]=1", optionsAllowSparse) shouldBe
387+
mapOf(
388+
"a" to
389+
listOf(
390+
null,
391+
listOf(
392+
null,
393+
null,
394+
listOf(null, null, null, mapOf("c" to listOf(null, "1"))),
395+
),
396+
)
397+
)
383398
}
384399

385400
it("should parse jQuery param strings") {
@@ -694,15 +709,17 @@ class QsParserSpec :
694709

695710
it("should stringify falsy values") {
696711
encode(null) shouldBe ""
712+
encode(null, EncodeOptions(strictNullHandling = true)) shouldBe ""
713+
encode(false) shouldBe ""
714+
encode(0) shouldBe ""
697715
encode(emptyMap<String, Any>()) shouldBe ""
698-
encode(mapOf("a" to false)) shouldBe ""
699-
encode(mapOf("a" to 0)) shouldBe ""
700716
}
701717

702718
it("should stringify integers with custom encoder") {
703719
val encoder: ValueEncoder = { value, _, _ ->
704720
val stringValue = value.toString()
705-
if (stringValue.toIntOrNull() != null) "${stringValue}n" else stringValue
721+
// Only encode actual integer values, not string representations of indices
722+
if (value is Int) "${stringValue}n" else stringValue
706723
}
707724

708725
val options = EncodeOptions(encoder = encoder)

0 commit comments

Comments
 (0)