Skip to content

Commit

Permalink
Add unit tests for ChartValues.getXSpacingMultiplier
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowsky committed Jun 1, 2024
1 parent 243aef1 commit ac44341
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 10 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ plugins {
apply("versions.gradle")

tasks.register<Delete>("clean") { delete(rootProject.layout.buildDirectory) }

subprojects.forEach { project ->
project.tasks.withType<Test>().configureEach { useJUnitPlatform() }
}
6 changes: 4 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" }
Expand Down
2 changes: 0 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,5 @@ dependencies {
implementation libs.systemUIController
implementation libs.viewModelCompose
debugImplementation libs.composeUITooling
testImplementation libs.JUnit
testImplementation libs.JUnitExt
testImplementation libs.kotlinTest
}
2 changes: 0 additions & 2 deletions vico/compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,5 @@ dependencies {
implementation libs.composeFoundation
implementation libs.composeUI
implementation libs.kotlinStdLib
testImplementation libs.JUnit
testImplementation libs.JUnitExt
testImplementation libs.kotlinTest
}
4 changes: 2 additions & 2 deletions vico/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Number>, xStep: Float) {
checkXSpacingMultiplier(x, xStep)
}

@ParameterizedTest()
@MethodSource("getXWithInvalidXStep")
public fun `Invalid xStep throws an exception on xStepMultiplier calculation`(
x: List<Number>,
xStep: Float,
) {
assertThrows<IllegalStateException> { checkXSpacingMultiplier(x, xStep) }
}

private fun checkXSpacingMultiplier(xCollection: List<Number>, 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<Arguments> =
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<Arguments> =
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),
)
}
}
2 changes: 0 additions & 2 deletions vico/views/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,5 @@ dependencies {
implementation libs.androidXCore
implementation libs.appcompat
implementation libs.kotlinStdLib
testImplementation libs.JUnit
testImplementation libs.JUnitExt
testImplementation libs.kotlinTest
}

0 comments on commit ac44341

Please sign in to comment.