Skip to content

Commit a9ffe88

Browse files
committed
Make WorkflowTurbineTest better by actually testing snapshot values
1 parent a3e3fc0 commit a9ffe88

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/TreeSnapshot.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public class TreeSnapshot internal constructor(
6363
sink.readByteString()
6464
}
6565

66+
fun workflowSnapshotByteString(): ByteString? {
67+
return workflowSnapshot?.bytes
68+
}
69+
6670
override fun equals(other: Any?): Boolean = when {
6771
other === this -> true
6872
other !is TreeSnapshot -> false

workflow-testing/src/test/java/com/squareup/workflow1/testing/WorkflowTurbineTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.squareup.workflow1.StatefulWorkflow
55
import com.squareup.workflow1.TreeSnapshot
66
import com.squareup.workflow1.Workflow
77
import com.squareup.workflow1.action
8+
import com.squareup.workflow1.parse
89
import com.squareup.workflow1.stateful
910
import kotlin.test.Test
1011
import kotlin.test.assertEquals
@@ -44,6 +45,8 @@ class WorkflowTurbineTest {
4445
// First snapshot should exist
4546
val firstSnapshot = awaitNextSnapshot()
4647
assertNotNull(firstSnapshot)
48+
val actualSnapshotValue = firstSnapshot.readIntValue()
49+
assertEquals(42, actualSnapshotValue)
4750
}
4851
}
4952

@@ -77,6 +80,7 @@ class WorkflowTurbineTest {
7780
assertNotNull(firstSnapshot)
7881
// awaitNextSnapshot should return the same value
7982
assertEquals(firstSnapshot, awaitNextSnapshot())
83+
assertEquals("hello", firstSnapshot.readStringValue())
8084
}
8185
}
8286

@@ -114,6 +118,7 @@ class WorkflowTurbineTest {
114118
// Now get first snapshot - should still be for state 0
115119
val snapshot0 = awaitNextSnapshot()
116120
assertNotNull(snapshot0)
121+
assertEquals(0, snapshot0.readIntValue())
117122

118123
// Now get second rendering - should be state 1
119124
val (value1, increment1) = awaitNextRendering()
@@ -123,6 +128,7 @@ class WorkflowTurbineTest {
123128
val snapshot1 = awaitNextSnapshot()
124129
assertNotNull(snapshot1)
125130
assertNotEquals(snapshot0, snapshot1)
131+
assertEquals(1, snapshot1.readIntValue())
126132

127133
// Trigger another change
128134
increment1()
@@ -135,6 +141,7 @@ class WorkflowTurbineTest {
135141
val snapshot2 = awaitNextSnapshot()
136142
assertNotNull(snapshot2)
137143
assertNotEquals(snapshot1, snapshot2)
144+
assertEquals(2, snapshot2.readIntValue())
138145
}
139146
}
140147

@@ -153,10 +160,12 @@ class WorkflowTurbineTest {
153160

154161
val snapshot0 = awaitNextSnapshot()
155162
assertNotNull(snapshot0)
163+
assertEquals(0, snapshot0.readIntValue())
156164

157165
val snapshot1 = awaitNextSnapshot()
158166
assertNotNull(snapshot1)
159167
assertNotEquals(snapshot0, snapshot1)
168+
assertEquals(1, snapshot1.readIntValue())
160169
}
161170
}
162171

@@ -177,12 +186,15 @@ class WorkflowTurbineTest {
177186
// Now consume snapshots - should have all 3 available because shareIn broadcasted to both
178187
val snapshot0 = awaitNextSnapshot()
179188
assertNotNull(snapshot0)
189+
assertEquals(0, snapshot0.readIntValue())
180190

181191
val snapshot1 = awaitNextSnapshot()
182192
assertNotNull(snapshot1)
193+
assertEquals(1, snapshot1.readIntValue())
183194

184195
val snapshot2 = awaitNextSnapshot()
185196
assertNotNull(snapshot2)
197+
assertEquals(2, snapshot2.readIntValue())
186198

187199
// All snapshots should be different
188200
assertNotEquals(snapshot0, snapshot1)
@@ -309,10 +321,27 @@ class WorkflowTurbineTest {
309321
assertNotNull(snapshot2)
310322
assertNotNull(snapshot3)
311323

324+
assertEquals(0, snapshot0.readIntValue())
325+
assertEquals(1, snapshot1.readIntValue())
326+
assertEquals(2, snapshot2.readIntValue())
327+
assertEquals(3, snapshot3.readIntValue())
328+
312329
// All should be different
313330
assertNotEquals(snapshot0, snapshot1)
314331
assertNotEquals(snapshot1, snapshot2)
315332
assertNotEquals(snapshot2, snapshot3)
316333
}
317334
}
318335
}
336+
337+
/**
338+
* Extension function to read an Int value from a TreeSnapshot.
339+
*/
340+
private fun TreeSnapshot.readIntValue(): Int? =
341+
workflowSnapshotByteString()?.parse { it.readInt() }
342+
343+
/***
344+
* Extension function to read a String value from a TreeSnapshot.
345+
*/
346+
private fun TreeSnapshot.readStringValue(): String? =
347+
workflowSnapshotByteString()?.parse { it.readUtf8() }

0 commit comments

Comments
 (0)