Skip to content

Commit

Permalink
Fix name
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Jul 7, 2024
1 parent 3210539 commit c6a0c3e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ inline fun <reified T> describeBehaviors(
val builder = TestCaseTreeBuilder<T>()
builder.block()
val root = builder.build(name = name)
return generateTestCases(root)
return generateBehaviors(root)
}

suspend fun <T> DescribedBehavior<T>.execute(robot: T) {
for ((index, step) in steps.withIndex()) {
println("Executing step: $index ($description)")
when (step) {
is TestNode.Run -> step.action(robot)
is TestNode.It -> {
is TestNode.ItShould -> {
if (step.description == targetCheckDescription) {
step.action(robot)
}
Expand All @@ -30,7 +30,7 @@ suspend fun <T> DescribedBehavior<T>.execute(robot: T) {
sealed class TestNode<T> {
data class Describe<T>(val description: String, val children: List<TestNode<T>>) : TestNode<T>()
data class Run<T>(val action: suspend T.() -> Unit) : TestNode<T>()
data class It<T>(val description: String, val action: suspend T.() -> Unit) : TestNode<T>()
data class ItShould<T>(val description: String, val action: suspend T.() -> Unit) : TestNode<T>()
}

data class DescribedBehavior<T>(
Expand All @@ -49,7 +49,7 @@ data class AncestryNode<T>(
data class CheckNode<T>(
val description: String,
val fullDescription: String,
val node: TestNode.It<T>,
val node: TestNode.ItShould<T>,
val ancestry: List<AncestryNode<T>>,
)

Expand All @@ -66,21 +66,20 @@ class TestCaseTreeBuilder<T> {
children.add(TestNode.Run { action() })
}

fun it(description: String, action: suspend T.() -> Unit) {
children.add(TestNode.It(description) { action() })
fun itShould(description: String, action: suspend T.() -> Unit) {
children.add(TestNode.ItShould(description) { action() })
}

fun build(name: String): TestNode.Describe<T> = TestNode.Describe(name, children)
}

fun <T> generateTestCases(root: TestNode.Describe<T>): List<DescribedBehavior<T>> {
fun <T> generateBehaviors(root: TestNode.Describe<T>): List<DescribedBehavior<T>> {
val checkNodes = collectCheckNodes(root)
return checkNodes.map { createTestCase(it) }
}

/**
* Collect all check nodes from the test tree
* it will be O(N)
*/
private fun <T> collectCheckNodes(root: TestNode.Describe<T>): List<CheckNode<T>> {
val checkNodes = mutableListOf<CheckNode<T>>()
Expand All @@ -96,9 +95,9 @@ private fun <T> collectCheckNodes(root: TestNode.Describe<T>): List<CheckNode<T>
}
}

is TestNode.It -> {
is TestNode.ItShould -> {
val fullDescription = if (parentDescription.isNotBlank()) {
"$parentDescription - it ${node.description}"
"$parentDescription - it should ${node.description}"
} else {
node.description
}
Expand All @@ -115,8 +114,6 @@ private fun <T> collectCheckNodes(root: TestNode.Describe<T>): List<CheckNode<T>

/**
* Create a test case from a check node
* We only run the steps that are necessary to reach the check node
* so the time complexity might be O(logN)
*/
private fun <T> createTestCase(checkNode: CheckNode<T>): DescribedBehavior<T> {
val steps = mutableListOf<TestNode<T>>()
Expand All @@ -139,7 +136,7 @@ private fun <T> createTestCase(checkNode: CheckNode<T>): DescribedBehavior<T> {
steps.add(node)
}

is TestNode.It -> {
is TestNode.ItShould -> {
if (node == checkNode.node) {
steps.add(node)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,26 @@ class TimetableItemDetailScreenTest(private val testCase: DescribedBehavior<Time
run {
setupScreenContent()
}
it("should show session detail title") {
itShould("show session detail title") {
// FIXME: Add check for session detail title
captureScreenWithChecks()
}
it("should be appropriately accessible") {
itShould("be appropriately accessible") {
checkAccessibilityCapture()
}
describe("click bookmark") {
run {
clickBookmarkButton()
}
it("should show bookmarked session") {
itShould("show bookmarked session") {
// FIXME: Add check for bookmarked session
captureScreenWithChecks()
}
describe("click bookmark again") {
run {
clickBookmarkButton()
}
it("should show unbookmarked session") {
itShould("show unbookmarked session") {
wait5Seconds()
// FIXME: Add check for unbookmarked session
captureScreenWithChecks()
Expand All @@ -84,7 +84,7 @@ class TimetableItemDetailScreenTest(private val testCase: DescribedBehavior<Time
run {
scroll()
}
it("should show scrolled session detail") {
itShould("show scrolled session detail") {
// FIXME: Add check for scrolled session detail
captureScreenWithChecks()
}
Expand All @@ -95,7 +95,7 @@ class TimetableItemDetailScreenTest(private val testCase: DescribedBehavior<Time
setFontScale(0.5f)
setupScreenContent()
}
it("should show small font session detail") {
itShould("show small font session detail") {
captureScreenWithChecks()
}
}
Expand All @@ -104,7 +104,7 @@ class TimetableItemDetailScreenTest(private val testCase: DescribedBehavior<Time
setFontScale(1.5f)
setupScreenContent()
}
it("should show small font session detail") {
itShould("show small font session detail") {
captureScreenWithChecks()
}
}
Expand All @@ -113,7 +113,7 @@ class TimetableItemDetailScreenTest(private val testCase: DescribedBehavior<Time
setFontScale(2.0f)
setupScreenContent()
}
it("should show small font session detail") {
itShould("show small font session detail") {
captureScreenWithChecks()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TimetableScreenTest(private val testCase: DescribedBehavior<TimetableScree
setupTimetableServer(ServerStatus.Operational)
setupTimetableScreenContent()
}
it("should show timetable items") {
itShould("show timetable items") {
captureScreenWithChecks(checks = {
checkTimetableItemsDisplayed()
})
Expand All @@ -51,7 +51,7 @@ class TimetableScreenTest(private val testCase: DescribedBehavior<TimetableScree
run {
clickFirstSessionBookmark()
}
it("should show bookmarked session") {
itShould("show bookmarked session") {
// FIXME: Add check for bookmarked session
captureScreenWithChecks()
}
Expand All @@ -60,15 +60,15 @@ class TimetableScreenTest(private val testCase: DescribedBehavior<TimetableScree
run {
clickFirstSession()
}
it("should show session detail") {
itShould("show session detail") {
checkClickedItemsExists()
}
}
describe("click timetable ui type change") {
run {
clickTimetableUiTypeChangeButton()
}
it("should change timetable ui type") {
itShould("change timetable ui type") {
// FIXME: Add check for timetable ui type change
captureScreenWithChecks()
}
Expand All @@ -79,7 +79,7 @@ class TimetableScreenTest(private val testCase: DescribedBehavior<TimetableScree
setupTimetableServer(ServerStatus.Error)
setupTimetableScreenContent()
}
it("should show error message") {
itShould("show error message") {
// FIXME: Add check for error message
captureScreenWithChecks()
}
Expand Down

0 comments on commit c6a0c3e

Please sign in to comment.