Skip to content

Commit

Permalink
Use Burst to replace manual variant testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Oct 25, 2024
1 parent 28da06e commit 3b0b15c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 140 deletions.
3 changes: 3 additions & 0 deletions redwood-layout-shared-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ redwoodBuild {
sharedSnapshotTests()
}

apply plugin: 'app.cash.burst'

kotlin {
sourceSets {
commonMain {
Expand All @@ -17,6 +19,7 @@ kotlin {
api projects.redwoodWidget
api projects.redwoodYoga
api libs.kotlin.test
implementation libs.burst
}
}
jvmMain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package app.cash.redwood.layout

import app.cash.burst.Burst
import app.cash.redwood.Modifier
import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
Expand All @@ -40,6 +41,7 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

@Burst
abstract class AbstractFlexContainerTest<T : Any> {
abstract val widgetFactory: TestWidgetFactory<T>

Expand Down Expand Up @@ -75,65 +77,23 @@ abstract class AbstractFlexContainerTest<T : Any> {

abstract fun snapshotter(widget: T): Snapshotter

@Test fun testEmptyLayout_Column() {
emptyLayout(FlexDirection.Column)
}

@Test fun testEmptyLayout_Row() {
emptyLayout(FlexDirection.Row)
}

private fun emptyLayout(
flexDirection: FlexDirection,
@Test fun testEmptyLayout(
flexDirection: FlexDirections,
) {
assumeTrue(flexDirection in listOf(FlexDirection.Row, FlexDirection.Column))
val container = flexContainer(flexDirection)
val container = flexContainer(flexDirection.value)
container.crossAxisAlignment(CrossAxisAlignment.Start)
container.onEndChanges()
snapshotter(container.value).snapshot()
}

@Test fun testLayoutWithConstraints_Column_Wrap_Wrap() {
layoutWithConstraints(FlexDirection.Column, Constraint.Wrap, Constraint.Wrap)
}

@Test fun testLayoutWithConstraints_Column_Wrap_Fill() {
layoutWithConstraints(FlexDirection.Column, Constraint.Wrap, Constraint.Fill)
}

@Test fun testLayoutWithConstraints_Column_Fill_Wrap() {
layoutWithConstraints(FlexDirection.Column, Constraint.Fill, Constraint.Wrap)
}

@Test fun testLayoutWithConstraints_Column_Fill_Fill() {
layoutWithConstraints(FlexDirection.Column, Constraint.Fill, Constraint.Fill)
}

@Test fun testLayoutWithConstraints_Row_Wrap_Wrap() {
layoutWithConstraints(FlexDirection.Row, Constraint.Wrap, Constraint.Wrap)
}

@Test fun testLayoutWithConstraints_Row_Wrap_Fill() {
layoutWithConstraints(FlexDirection.Row, Constraint.Wrap, Constraint.Fill)
}

@Test fun testLayoutWithConstraints_Row_Fill_Wrap() {
layoutWithConstraints(FlexDirection.Row, Constraint.Fill, Constraint.Wrap)
}

@Test fun testLayoutWithConstraints_Row_Fill_Fill() {
layoutWithConstraints(FlexDirection.Row, Constraint.Fill, Constraint.Fill)
}

private fun layoutWithConstraints(
flexDirection: FlexDirection,
width: Constraint,
height: Constraint,
@Test fun testLayoutWithConstraints(
flexDirection: FlexDirections,
width: Constraints,
height: Constraints,
) {
assumeTrue(flexDirection in listOf(FlexDirection.Row, FlexDirection.Column))
val container = flexContainer(flexDirection)
container.width(width)
container.height(height)
val container = flexContainer(flexDirection.value)
container.width(width.value)
container.height(height.value)
container.add(
widgetFactory.text(
text = movies.first(),
Expand All @@ -146,19 +106,10 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter(container.value).snapshot()
}

@Test fun testShortLayout_Column() {
shortLayout(FlexDirection.Column)
}

@Test fun testShortLayout_Row() {
shortLayout(FlexDirection.Row)
}

private fun shortLayout(
flexDirection: FlexDirection,
@Test fun testShortLayout(
flexDirection: FlexDirections,
) {
assumeTrue(flexDirection in listOf(FlexDirection.Row, FlexDirection.Column))
val container = flexContainer(flexDirection)
val container = flexContainer(flexDirection.value)
container.crossAxisAlignment(CrossAxisAlignment.Start)
movies.take(5).forEach { movie ->
container.add(widgetFactory.text(movie))
Expand All @@ -167,19 +118,10 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter(container.value).snapshot()
}

@Test fun testLongLayout_Column() {
longLayout(FlexDirection.Column)
}

@Test fun testLongLayout_Row() {
longLayout(FlexDirection.Row)
}

private fun longLayout(
flexDirection: FlexDirection,
@Test fun testLongLayout(
flexDirection: FlexDirections,
) {
assumeTrue(flexDirection in listOf(FlexDirection.Row, FlexDirection.Column))
val container = flexContainer(flexDirection)
val container = flexContainer(flexDirection.value)
container.crossAxisAlignment(CrossAxisAlignment.Start)
movies.forEach { movie ->
container.add(widgetFactory.text(movie))
Expand All @@ -188,19 +130,10 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter(container.value).snapshot()
}

@Test fun testLayoutWithMarginAndDifferentAlignments_Column() {
layoutWithMarginAndDifferentAlignments(FlexDirection.Column)
}

@Test fun testLayoutWithMarginAndDifferentAlignments_Row() {
layoutWithMarginAndDifferentAlignments(FlexDirection.Row)
}

private fun layoutWithMarginAndDifferentAlignments(
flexDirection: FlexDirection,
@Test fun testLayoutWithMarginAndDifferentAlignments(
flexDirection: FlexDirections,
) {
assumeTrue(flexDirection in listOf(FlexDirection.Row, FlexDirection.Column))
val container = flexContainer(flexDirection)
val container = flexContainer(flexDirection.value)
container.width(Constraint.Fill)
container.height(Constraint.Fill)
container.margin(Margin(start = 5.dp, end = 10.dp, top = 20.dp, bottom = 20.dp))
Expand All @@ -217,47 +150,14 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter(container.value).snapshot()
}

@Test fun testLayoutWithCrossAxisAlignment_Column_Start() {
layoutWithCrossAxisAlignment(FlexDirection.Column, CrossAxisAlignment.Start)
}

@Test fun testLayoutWithCrossAxisAlignment_Column_Center() {
layoutWithCrossAxisAlignment(FlexDirection.Column, CrossAxisAlignment.Center)
}

@Test fun testLayoutWithCrossAxisAlignment_Column_End() {
layoutWithCrossAxisAlignment(FlexDirection.Column, CrossAxisAlignment.End)
}

@Test fun testLayoutWithCrossAxisAlignment_Column_Stretch() {
layoutWithCrossAxisAlignment(FlexDirection.Column, CrossAxisAlignment.Stretch)
}

@Test fun testLayoutWithCrossAxisAlignment_Row_Start() {
layoutWithCrossAxisAlignment(FlexDirection.Row, CrossAxisAlignment.Start)
}

@Test fun testLayoutWithCrossAxisAlignment_Row_Center() {
layoutWithCrossAxisAlignment(FlexDirection.Row, CrossAxisAlignment.Center)
}

@Test fun testLayoutWithCrossAxisAlignment_Row_End() {
layoutWithCrossAxisAlignment(FlexDirection.Row, CrossAxisAlignment.End)
}

@Test fun testLayoutWithCrossAxisAlignment_Row_Stretch() {
layoutWithCrossAxisAlignment(FlexDirection.Row, CrossAxisAlignment.Stretch)
}

private fun layoutWithCrossAxisAlignment(
flexDirection: FlexDirection,
crossAxisAlignment: CrossAxisAlignment,
@Test fun testLayoutWithCrossAxisAlignment(
flexDirection: FlexDirections,
crossAxisAlignment: CrossAxisAlignments,
) {
assumeTrue(flexDirection in listOf(FlexDirection.Row, FlexDirection.Column))
val container = flexContainer(flexDirection)
val container = flexContainer(flexDirection.value)
container.width(Constraint.Fill)
container.height(Constraint.Fill)
container.crossAxisAlignment(crossAxisAlignment)
container.crossAxisAlignment(crossAxisAlignment.value)
movies.forEach { movie ->
container.add(widgetFactory.text(movie))
}
Expand All @@ -281,27 +181,22 @@ abstract class AbstractFlexContainerTest<T : Any> {
snapshotter.snapshot("FlexEnd")
}

@Test fun testColumnWithMainAxisAlignment_Center() {
columnWithMainAxisAlignment(MainAxisAlignment.Center)
}

@Test fun testColumnWithMainAxisAlignment_SpaceBetween() {
columnWithMainAxisAlignment(MainAxisAlignment.SpaceBetween)
}

@Test fun testColumnWithMainAxisAlignment_SpaceAround() {
columnWithMainAxisAlignment(MainAxisAlignment.SpaceAround)
private enum class ColumnMainAxisAlignments(
val value: MainAxisAlignment,
) {
Center(MainAxisAlignment.Center),
SpaceBetween(MainAxisAlignment.SpaceBetween),
SpaceAround(MainAxisAlignment.SpaceAround),
}

private fun columnWithMainAxisAlignment(
mainAxisAlignment: MainAxisAlignment,
private fun testColumnWithMainAxisAlignment(
mainAxisAlignment: ColumnMainAxisAlignments,
) {
assumeTrue(mainAxisAlignment in listOf(MainAxisAlignment.Center, MainAxisAlignment.SpaceBetween, MainAxisAlignment.SpaceAround))
val container = flexContainer(FlexDirection.Column)
container.width(Constraint.Fill)
container.height(Constraint.Fill)
container.crossAxisAlignment(CrossAxisAlignment.Start)
container.mainAxisAlignment(mainAxisAlignment)
container.mainAxisAlignment(mainAxisAlignment.value)
movies.forEach { movie ->
container.add(widgetFactory.text(movie))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* 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 app.cash.redwood.layout

import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
import app.cash.redwood.yoga.FlexDirection

enum class Constraints(
val value: Constraint,
) {
Wrap(Constraint.Wrap),
Fill(Constraint.Fill),
;
}

enum class CrossAxisAlignments(
val value: CrossAxisAlignment,
) {
Start(CrossAxisAlignment.Start),
Center(CrossAxisAlignment.Center),
End(CrossAxisAlignment.End),
Stretch(CrossAxisAlignment.Stretch),
;
}

enum class FlexDirections(
val value: FlexDirection,
) {
Row(FlexDirection.Row),
Column(FlexDirection.Column),
;
}

0 comments on commit 3b0b15c

Please sign in to comment.