Skip to content

Commit

Permalink
Add testing for the IntFormat (found a bug!)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Oct 4, 2023
1 parent a6245aa commit 7f4de60
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class IntFormat : LiteralFormat<Int> {
return value.toString()
}
override fun parse(str: String): Int {
return str.toInt()
return str.replace("_", "").toInt()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.selfie

import io.kotest.matchers.shouldBe
import kotlin.test.Test

class IntFormatTest {
@Test
fun encode() {
encode(0, "0")
encode(1, "1")
encode(-1, "-1")
encode(999, "999")
encode(-999, "-999")
// TODO: add underscores
encode(1_000, "1000")
encode(-1_000, "-1000")
encode(1_000_000, "1000000")
encode(-1_000_000, "-1000000")
}
private fun encode(value: Int, expected: String) {
val actual = IntFormat().encode(value)
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 = IntFormat().parse(value)
actual shouldBe expected
}
}

0 comments on commit 7f4de60

Please sign in to comment.