Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dautovicharis committed Jun 24, 2024
1 parent 0558b25 commit 91c51be
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.runComposeUiTest
import io.github.dautovicharis.charts.BarChartView
import io.github.dautovicharis.charts.common.model.ChartDataSet
import io.github.dautovicharis.charts.internal.TestTags
import io.github.dautovicharis.charts.internal.ValidationErrors.MIN_REQUIRED_LINE
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_DATA_POINTS_LESS_THAN_MIN
import io.github.dautovicharis.charts.internal.format
import io.github.dautovicharis.charts.mock.MockTest.TITLE
import io.github.dautovicharis.charts.mock.MockTest.dataSet
import kotlin.test.Test

Expand All @@ -28,4 +34,21 @@ class BarChartTest {
onNodeWithTag(TestTags.CHART_TITLE)
.assertTextEquals(expectedTitle).isDisplayed()
}

@OptIn(ExperimentalTestApi::class)
@Test
fun barChart_withInvalidData_displaysError () = runComposeUiTest {
val dataSet = ChartDataSet(
items = listOf(1f),
title = TITLE
)
val expectedError = RULE_DATA_POINTS_LESS_THAN_MIN.format(MIN_REQUIRED_LINE)

setContent {
BarChartView(dataSet)
}

onNodeWithTag(TestTags.CHART_ERROR).isDisplayed()
onNodeWithText("${expectedError}\n").isDisplayed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.runComposeUiTest
import io.github.dautovicharis.charts.LineChartView
import io.github.dautovicharis.charts.common.model.ChartDataSet
import io.github.dautovicharis.charts.internal.TestTags
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_COLORS_SIZE_MISMATCH
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_ITEM_POINTS_SIZE
import io.github.dautovicharis.charts.internal.ValidationErrors.MIN_REQUIRED_LINE
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_DATA_POINTS_LESS_THAN_MIN
import io.github.dautovicharis.charts.internal.format
import io.github.dautovicharis.charts.mock.MockTest.colors
import io.github.dautovicharis.charts.mock.MockTest.TITLE
import io.github.dautovicharis.charts.mock.MockTest.dataSet
import io.github.dautovicharis.charts.mock.MockTest.invalidMultiDataSet
import io.github.dautovicharis.charts.style.LineChartDefaults
import kotlin.test.Test

class LineChartTest {
Expand All @@ -39,34 +38,18 @@ class LineChartTest {

@OptIn(ExperimentalTestApi::class)
@Test
fun lineChart_withInvalidData_displaysError() = runComposeUiTest {
// Arrange
val dataSet = invalidMultiDataSet()
val index = 1
val colors = colors.drop(1)

val expectedPointsSize = dataSet.data.items.first().item.points.size
val pointsSize = dataSet.data.items[index].item.points.size
val expectedPointsError = RULE_ITEM_POINTS_SIZE.format(index, pointsSize, expectedPointsSize)

val expectedColorsSize = dataSet.data.items.size
val colorsSize = colors.size
val expectedColorsError = RULE_COLORS_SIZE_MISMATCH.format(colorsSize, expectedColorsSize)
fun lineChart_withInvalidData_displaysError () = runComposeUiTest {
val dataSet = ChartDataSet(
items = listOf(1f),
title = TITLE
)
val expectedError = RULE_DATA_POINTS_LESS_THAN_MIN.format(MIN_REQUIRED_LINE)

// Act
setContent {
val style = LineChartDefaults.style(
lineColors = colors
)
LineChartView(
dataSet = dataSet,
style = style
)
LineChartView(dataSet)
}

// Assert
onNodeWithTag(TestTags.CHART_ERROR).isDisplayed()
onNodeWithText("${expectedPointsError}\n").isDisplayed()
onNodeWithText("${expectedColorsError}\n").isDisplayed()
onNodeWithText("${expectedError}\n").isDisplayed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.runComposeUiTest
import io.github.dautovicharis.charts.LineChartView
import io.github.dautovicharis.charts.internal.TestTags
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_COLORS_SIZE_MISMATCH
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_ITEM_POINTS_SIZE
import io.github.dautovicharis.charts.internal.format
import io.github.dautovicharis.charts.mock.MockTest.colors
import io.github.dautovicharis.charts.mock.MockTest.invalidMultiDataSet
import io.github.dautovicharis.charts.mock.MockTest.multiDataSet
import io.github.dautovicharis.charts.style.LineChartDefaults
import kotlin.test.Test

class MultiLineChartTest {
Expand All @@ -29,4 +36,42 @@ class MultiLineChartTest {
.assertTextEquals(expectedTitle)
.isDisplayed()
}

@OptIn(ExperimentalTestApi::class)
@Test
fun multiLineChart_withInvalidData_displaysError() = runComposeUiTest {
// Arrange
val dataSet = invalidMultiDataSet()
val colors = colors.drop(1)
val firstIndex = 1
val thirdIndex = 3

val pointsSizeFirst = dataSet.data.items[firstIndex].item.points.size
val pointsSizeThird = dataSet.data.items[thirdIndex].item.points.size
val expectedPointsSize = dataSet.data.items.first().item.points.size

val expectedPointsErrorFirst = RULE_ITEM_POINTS_SIZE.format(firstIndex, pointsSizeFirst, expectedPointsSize)
val expectedPointsErrorSecond = RULE_ITEM_POINTS_SIZE.format(thirdIndex, pointsSizeThird, expectedPointsSize)

val expectedColorsSize = dataSet.data.items.size
val colorsSize = colors.size
val expectedColorsError = RULE_COLORS_SIZE_MISMATCH.format(colorsSize, expectedColorsSize)

// Act
setContent {
val style = LineChartDefaults.style(
lineColors = colors
)
LineChartView(
dataSet = dataSet,
style = style
)
}

// Assert
onNodeWithTag(TestTags.CHART_ERROR).isDisplayed()
onNodeWithText("${expectedPointsErrorFirst}\n").isDisplayed()
onNodeWithText("${expectedPointsErrorSecond}\n").isDisplayed()
onNodeWithText("${expectedColorsError}\n").isDisplayed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class PieChartTest {

@OptIn(ExperimentalTestApi::class)
@Test
fun pieChart_withInvalidDataColors_displaysError() = runComposeUiTest {
fun pieChart_withInvalidColors_displaysError() = runComposeUiTest {
// Arrange
val colors = colors.drop(2)
val pieChartStyle = mockPieChartStyle(colors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.runComposeUiTest
import io.github.dautovicharis.charts.StackedBarChartView
import io.github.dautovicharis.charts.internal.TestTags
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_COLORS_SIZE_MISMATCH
import io.github.dautovicharis.charts.internal.ValidationErrors.RULE_ITEM_POINTS_SIZE
import io.github.dautovicharis.charts.internal.format
import io.github.dautovicharis.charts.mock.MockTest.colors
import io.github.dautovicharis.charts.mock.MockTest.invalidMultiDataSet
import io.github.dautovicharis.charts.mock.MockTest.multiDataSet
import io.github.dautovicharis.charts.style.StackedBarChartDefaults
import kotlin.test.Test

class StackedBarChartTest {
Expand All @@ -29,4 +36,59 @@ class StackedBarChartTest {
.assertTextEquals(expectedTitle)
.isDisplayed()
}

@OptIn(ExperimentalTestApi::class)
@Test
fun stackedBarChart_withInvalidData_displaysError () = runComposeUiTest {
// Arrange
val dataSet = invalidMultiDataSet()
val firstIndex = 1
val thirdIndex = 3

val pointsSizeFirst = dataSet.data.items[firstIndex].item.points.size
val pointsSizeThird = dataSet.data.items[thirdIndex].item.points.size
val expectedPointsSize = dataSet.data.items.first().item.points.size

val expectedPointsErrorFirst = RULE_ITEM_POINTS_SIZE.format(firstIndex, pointsSizeFirst, expectedPointsSize)
val expectedPointsErrorSecond = RULE_ITEM_POINTS_SIZE.format(thirdIndex, pointsSizeThird, expectedPointsSize)

// Act
setContent {
val style = StackedBarChartDefaults.style()
StackedBarChartView(
dataSet = dataSet,
style = style
)
}

// Assert
onNodeWithTag(TestTags.CHART_ERROR).isDisplayed()
onNodeWithText("${expectedPointsErrorFirst}\n").isDisplayed()
onNodeWithText("${expectedPointsErrorSecond}\n").isDisplayed()
}

@OptIn(ExperimentalTestApi::class)
@Test
fun stackedBarChart_withInvalidColors_displaysError () = runComposeUiTest {
// Arrange
val dataSet = multiDataSet
val colors = colors.drop(1)

val expectedColorsSize = dataSet.data.items.size
val colorsSize = colors.size
val expectedColorsError = RULE_COLORS_SIZE_MISMATCH.format(colorsSize, expectedColorsSize)

// Act
setContent {
val style = StackedBarChartDefaults.style(barColors = colors)
StackedBarChartView(
dataSet = dataSet,
style = style
)
}

// Assert
onNodeWithTag(TestTags.CHART_ERROR).isDisplayed()
onNodeWithText("${expectedColorsError}\n").isDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.dautovicharis.charts.unit.validation

import io.github.dautovicharis.charts.internal.ValidationErrors
import io.github.dautovicharis.charts.internal.ValidationErrors.MIN_REQUIRED_BAR
import io.github.dautovicharis.charts.internal.ValidationErrors.MIN_REQUIRED_STACKED_BAR
import io.github.dautovicharis.charts.internal.format
import io.github.dautovicharis.charts.internal.validateBarData
import io.github.dautovicharis.charts.mock.MockTest.colors
Expand Down Expand Up @@ -88,7 +88,7 @@ class DataValidationBarTest {

// Assert
val expectedError =
ValidationErrors.RULE_DATA_POINTS_LESS_THAN_MIN.format(MIN_REQUIRED_BAR)
ValidationErrors.RULE_DATA_POINTS_LESS_THAN_MIN.format(MIN_REQUIRED_STACKED_BAR)
assertTrue(validationErrors.isNotEmpty())
assertEquals(validationErrors.first(), expectedError)
}
Expand Down

0 comments on commit 91c51be

Please sign in to comment.