generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kotest and property-based tests for number formatting.
Found some nice corner cases, like handling of e.g. PLUS_INFINITY or -0.0
- Loading branch information
1 parent
824a47a
commit 648a7d5
Showing
3 changed files
with
50 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 33 additions & 59 deletions
92
plugin/src/test/kotlin/ch/kleis/lcaac/plugin/ui/toolwindow/shared/QuantityRendererTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,40 @@ | ||
package ch.kleis.lcaac.plugin.ui.toolwindow.shared | ||
|
||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
import io.kotest.core.names.DuplicateTestNameMode | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldMatch | ||
import io.kotest.property.assume | ||
import io.kotest.property.checkAll | ||
import kotlin.math.abs | ||
|
||
@RunWith(Parameterized::class) | ||
class QuantityRendererTest( | ||
private val value: Double, | ||
private val expected: String, | ||
) { | ||
companion object { | ||
@Parameterized.Parameters | ||
@JvmStatic | ||
fun getDisplayedStrings(): Collection<Array<Any>> { | ||
return listOf( | ||
arrayOf(0.0, "0"), | ||
arrayOf(0.0001, "1E-4"), | ||
arrayOf(0.001, "1E-3"), | ||
arrayOf(0.01, "1E-2"), | ||
arrayOf(0.1, "1E-1"), | ||
arrayOf(1.0, "1"), | ||
arrayOf(10.0, "1E1"), | ||
arrayOf(100.0, "1E2"), | ||
arrayOf(1000.0, "1E3"), | ||
|
||
arrayOf(0.000999, "9.99E-4"), | ||
arrayOf(0.00999, "9.99E-3"), | ||
arrayOf(0.0999, "9.99E-2"), | ||
arrayOf(0.999, "9.99E-1"), | ||
arrayOf(9.99, "9.99"), | ||
arrayOf(99.9, "9.99E1"), | ||
arrayOf(999.0, "9.99E2"), | ||
|
||
arrayOf(0.00123, "1.23E-3"), | ||
arrayOf(0.0123, "1.23E-2"), | ||
arrayOf(0.123, "1.23E-1"), | ||
arrayOf(1.2345, "1.23"), | ||
arrayOf(12.345, "1.23E1"), | ||
arrayOf(123.45, "1.23E2"), | ||
arrayOf(1234.5, "1.23E3"), | ||
arrayOf(12345.123, "1.23E4"), | ||
|
||
arrayOf(-0.0, "0"), | ||
arrayOf(-0.00123, "-1.23E-3"), | ||
arrayOf(-0.0123, "-1.23E-2"), | ||
arrayOf(-0.123, "-1.23E-1"), | ||
arrayOf(-1.2345, "-1.23"), | ||
arrayOf(-12.345, "-1.23E1"), | ||
arrayOf(-123.45, "-1.23E2"), | ||
arrayOf(-1234.5, "-1.23E3"), | ||
arrayOf(-12345.123, "-1.23E4"), | ||
) | ||
class QuantityRendererTest : DescribeSpec({ | ||
duplicateTestNameMode = DuplicateTestNameMode.Silent | ||
describe("QuantityRenderer") { | ||
// Properties | ||
it("should use scientific notation with 3 significant digits for values not in [-10;-1[U]1;10]") { | ||
val notationRegex = Regex("-?([0-9](\\.[0-9]{1,2})?(E-?[1-9][0-9]*)?|∞)") | ||
checkAll<Double> { value -> | ||
assume(abs(value).let { it >= 10 || it > 1}) | ||
QuantityRenderer.formatDouble(value) shouldMatch notationRegex | ||
} | ||
} | ||
it("should not display an exponent part for values in [-10;-1[U]1;10]") { | ||
val notationRegex = Regex("-?[0-9](\\.[0-9]{1,2})?") | ||
checkAll<Double> { value -> | ||
assume(abs(value).let { it < 10 && it >= 1}) | ||
QuantityRenderer.formatDouble(value) shouldMatch notationRegex | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun run() { | ||
// when | ||
val actual = QuantityRenderer.formatDouble(value) | ||
// Non-regression of corner cases found by fuzzing | ||
it("should properly encode infinities") { | ||
QuantityRenderer.formatDouble(Double.POSITIVE_INFINITY) shouldBe "∞" | ||
QuantityRenderer.formatDouble(Double.NEGATIVE_INFINITY) shouldBe "-∞" | ||
} | ||
|
||
// then | ||
Assert.assertEquals(expected, actual) | ||
it("should properly deal with -0.0") { | ||
QuantityRenderer.formatDouble(-0.0) shouldBe "0" | ||
} | ||
} | ||
} | ||
}) |