Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

LiteralInt #39

Closed
SDelgado-21 opened this issue Mar 14, 2024 · 0 comments
Closed

LiteralInt #39

SDelgado-21 opened this issue Mar 14, 2024 · 0 comments
Assignees

Comments

@SDelgado-21
Copy link
Collaborator

Epic: inline snapshot #8

implementation:

internal object LiteralInt : LiteralFormat<Int>() {
override fun encode(
value: Int,
language: Language,
encodingPolicy: EscapeLeadingWhitespace
): String {
return encodeUnderscores(StringBuilder(), value.toLong(), language).toString()
}
override fun parse(str: String, language: Language): Int {
return str.replace("_", "").toInt()
}
}

tests:

class LiteralIntTest {
@Test
fun encode() {
encode(0, "0")
encode(1, "1")
encode(-1, "-1")
encode(999, "999")
encode(-999, "-999")
encode(1_000, "1_000")
encode(-1_000, "-1_000")
encode(1_000_000, "1_000_000")
encode(-1_000_000, "-1_000_000")
encode(2400500, "2_400_500")
encode(2400501, "2_400_501")
encode(200, "200")
encode(1001, "1_001")
encode(1010, "1_010")
encode(10010, "10_010")
}
private fun encode(value: Int, expected: String) {
val actual = LiteralInt.encode(value, Language.JAVA, EscapeLeadingWhitespace.ALWAYS)
actual shouldBe expected
}
@Test
fun decode() {
decode("0", 0)
decode("1", 1)
decode("-1", -1)
decode("999", 999)
decode("9_99", 999)
decode("9_9_9", 999)
decode("-999", -999)
decode("-9_99", -999)
decode("-9_9_9", -999)
}
private fun decode(value: String, expected: Int) {
val actual = LiteralInt.parse(value, Language.JAVA)
actual shouldBe expected
}
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant