Skip to content

Commit 6129aae

Browse files
committed
Add recursionLimitTriggersOnDeepEncoding test
1 parent 05d07f2 commit 6129aae

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Tests/ToonFormatTests/EncoderTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,37 @@ struct EncoderTests {
15721572
#expect(result == expected)
15731573
}
15741574

1575+
@Test func recursionLimitTriggersOnDeepEncoding() async throws {
1576+
indirect enum NestableValue: Codable, Equatable {
1577+
case int(Int)
1578+
case array([NestableValue])
1579+
}
1580+
1581+
struct Container: Codable, Equatable {
1582+
let value: NestableValue
1583+
}
1584+
1585+
func makeDeepNest(depth: Int) -> NestableValue {
1586+
var value: NestableValue = .int(1)
1587+
for _ in 0 ..< depth {
1588+
value = .array([value])
1589+
}
1590+
return value
1591+
}
1592+
1593+
let encoder = TOONEncoder()
1594+
encoder.recursionLimit = 10
1595+
1596+
do {
1597+
_ = try encoder.encode(Container(value: makeDeepNest(depth: 20)))
1598+
#expect(Bool(false))
1599+
} catch EncodingError.invalidValue(_, let context) {
1600+
#expect(context.debugDescription.contains("Recursion limit"))
1601+
} catch {
1602+
#expect(Bool(false))
1603+
}
1604+
}
1605+
15751606
// MARK: - Collision Avoidance Tests (TOON 3.0)
15761607

15771608
@Test func keyFoldingCollisionAvoidance() async throws {

0 commit comments

Comments
 (0)