From ac443414edc2b89aeedbc5bb6e3c09cb29edc720 Mon Sep 17 00:00:00 2001 From: Patryk Goworowski Date: Sat, 1 Jun 2024 20:33:50 +0200 Subject: [PATCH] Add unit tests for `ChartValues.getXSpacingMultiplier` --- build.gradle.kts | 4 + gradle/libs.versions.toml | 6 +- sample/build.gradle | 2 - vico/compose/build.gradle | 2 - vico/core/build.gradle | 4 +- .../vico/core/data/XSpacingMultiplierTest.kt | 82 +++++++++++++++++++ vico/views/build.gradle | 2 - 7 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 vico/core/src/test/java/com/patrykandpatrick/vico/core/data/XSpacingMultiplierTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index bf893c05d..37b367c63 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,3 +30,7 @@ plugins { apply("versions.gradle") tasks.register("clean") { delete(rootProject.layout.buildDirectory) } + +subprojects.forEach { project -> + project.tasks.withType().configureEach { useJUnitPlatform() } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c1d9bebdb..746e7d981 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,6 +11,7 @@ coroutines = "1.8.1" dokka = "1.9.20" jUnit = "4.13.2" jUnitExt = "1.1.5" +jupiter = "5.10.2" kotlin = "2.0.0" lifecycle = "2.8.1" material = "1.12.0" @@ -34,8 +35,9 @@ composeUI = { group = "androidx.compose.ui", name = "ui" } composeUITooling = { group = "androidx.compose.ui", name = "ui-tooling" } composeViewBinding = { group = "androidx.compose.ui", name = "ui-viewbinding" } coroutinesCore = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" } -jUnit = { group = "junit", name = "junit", version.ref = "jUnit" } -jUnitExt = { group = "androidx.test.ext", name = "junit", version.ref = "jUnitExt" } +jUnit = { module = "junit:junit", version.ref = "jUnit" } +jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "jupiter" } +jupiterParams = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "jupiter" } kotlinGradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } kotlinStdLib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlin" } kotlinTest = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlin" } diff --git a/sample/build.gradle b/sample/build.gradle index b4ec03cd1..d8537c2da 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -76,7 +76,5 @@ dependencies { implementation libs.systemUIController implementation libs.viewModelCompose debugImplementation libs.composeUITooling - testImplementation libs.JUnit - testImplementation libs.JUnitExt testImplementation libs.kotlinTest } diff --git a/vico/compose/build.gradle b/vico/compose/build.gradle index 9741c6b84..5d33619d1 100644 --- a/vico/compose/build.gradle +++ b/vico/compose/build.gradle @@ -76,7 +76,5 @@ dependencies { implementation libs.composeFoundation implementation libs.composeUI implementation libs.kotlinStdLib - testImplementation libs.JUnit - testImplementation libs.JUnitExt testImplementation libs.kotlinTest } diff --git a/vico/core/build.gradle b/vico/core/build.gradle index 8d01976c9..469c48e86 100644 --- a/vico/core/build.gradle +++ b/vico/core/build.gradle @@ -59,12 +59,12 @@ afterEvaluate { } dependencies { - implementation libs.androidXAnnotation implementation libs.coroutinesCore implementation libs.kotlinStdLib testImplementation libs.JUnit - testImplementation libs.JUnitExt + testImplementation libs.jupiter + testImplementation libs.jupiterParams testImplementation libs.kotlinTest testImplementation libs.mockK testImplementation libs.testCore diff --git a/vico/core/src/test/java/com/patrykandpatrick/vico/core/data/XSpacingMultiplierTest.kt b/vico/core/src/test/java/com/patrykandpatrick/vico/core/data/XSpacingMultiplierTest.kt new file mode 100644 index 000000000..0257eba76 --- /dev/null +++ b/vico/core/src/test/java/com/patrykandpatrick/vico/core/data/XSpacingMultiplierTest.kt @@ -0,0 +1,82 @@ +/* + * Copyright 2024 by Patryk Goworowski and Patrick Michalik. + * + * 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 + * + * http://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.patrykandpatrick.vico.core.data + +import com.patrykandpatrick.vico.core.cartesian.data.CartesianChartModel +import com.patrykandpatrick.vico.core.cartesian.data.ColumnCartesianLayerModel +import com.patrykandpatrick.vico.core.cartesian.data.MutableChartValues +import com.patrykandpatrick.vico.core.cartesian.data.getXSpacingMultiplier +import java.util.stream.Stream +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.Arguments +import org.junit.jupiter.params.provider.MethodSource + +public class XSpacingMultiplierTest { + + @ParameterizedTest + @MethodSource("getXWithValidXStep") + public fun `Valid xStep produces the correct xStepMultiplier`(x: List, xStep: Float) { + checkXSpacingMultiplier(x, xStep) + } + + @ParameterizedTest() + @MethodSource("getXWithInvalidXStep") + public fun `Invalid xStep throws an exception on xStepMultiplier calculation`( + x: List, + xStep: Float, + ) { + assertThrows { checkXSpacingMultiplier(x, xStep) } + } + + private fun checkXSpacingMultiplier(xCollection: List, xStep: Float) { + val chartValues = MutableChartValues() + val columnCartesianModel = + ColumnCartesianLayerModel.build { series(xCollection, xCollection.map { 1f }) } + val model = CartesianChartModel(columnCartesianModel) + chartValues.update(xStep, model) + + chartValues.tryUpdate( + columnCartesianModel.minX, + columnCartesianModel.maxX, + columnCartesianModel.minY, + columnCartesianModel.maxY, + null, + ) + xCollection.forEach { chartValues.getXSpacingMultiplier(it.toFloat()) } + } + + private companion object { + @JvmStatic + private fun getXWithValidXStep(): Stream = + Stream.of( + Arguments.of(listOf(1.35, 1.9, 2.59), 0.01f), + Arguments.of(listOf(0.1, 0.2), 0.1f), + Arguments.of(listOf(0.000001f, 0.000002f), 0.000001f), + Arguments.of(listOf(1000f, 0.000002f), 1000f), + ) + + @JvmStatic + private fun getXWithInvalidXStep(): Stream = + Stream.of( + Arguments.of(listOf(1.35, 1.9, 2.59), 0.1f), + Arguments.of(listOf(0.1, 0.2), 1f), + Arguments.of(listOf(0.000001f, 0.000002f), 10f), + Arguments.of(listOf(1000f, 0.000002f), 10000f), + ) + } +} diff --git a/vico/views/build.gradle b/vico/views/build.gradle index 9f2002d91..89d27bd5d 100644 --- a/vico/views/build.gradle +++ b/vico/views/build.gradle @@ -62,7 +62,5 @@ dependencies { implementation libs.androidXCore implementation libs.appcompat implementation libs.kotlinStdLib - testImplementation libs.JUnit - testImplementation libs.JUnitExt testImplementation libs.kotlinTest }