-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to save non-mutable int in StateSnapshot #1508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (C) 2023 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.treehouse | ||
|
||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.mutableStateOf | ||
import assertk.assertThat | ||
import assertk.assertions.containsOnly | ||
import assertk.assertions.isEqualTo | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
import kotlinx.serialization.json.JsonPrimitive | ||
|
||
class StateSnapshotTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests! Love it 😄 |
||
|
||
@Test | ||
fun toValueMapWorksAsExpected() { | ||
val stateSnapshot = stateSnapshot() | ||
val valuesMap = stateSnapshot.toValuesMap() | ||
assertThat(valuesMap.entries.size).isEqualTo(4) | ||
assertTrue(valuesMap["key1"]!![0] is MutableState<*>) | ||
assertThat((valuesMap["key1"]!![0] as MutableState<*>).value).isEqualTo(JsonPrimitive(1)) | ||
|
||
assertThat(valuesMap["key2"]).isEqualTo(listOf(1.0)) | ||
|
||
assertThat(valuesMap["key3"]!![0] is MutableState<*>) | ||
assertThat((valuesMap["key3"]!![0] as MutableState<*>).value).isEqualTo(JsonPrimitive("str")) | ||
|
||
assertThat(valuesMap["key4"]).isEqualTo(listOf("str")) | ||
Comment on lines
+33
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could these test cases be simplified with the assertThat(valuesMap).containsOnly(
"key1" to listOf(mutableStateOf(JsonPrimitive(1)),
…
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
for the |
||
} | ||
|
||
@Test | ||
fun toStateSnapshotWorksAsExpected() { | ||
val storedStateSnapshot = storedStateSnapshot() | ||
val stateSnapshot = storedStateSnapshot.toStateSnapshot() | ||
assertThat(stateSnapshot.content).containsOnly( | ||
"key1" to listOf(Saveable(true, JsonPrimitive(1))), | ||
"key2" to listOf(Saveable(false, JsonPrimitive(1))), | ||
"key3" to listOf(Saveable(true, JsonPrimitive("str"))), | ||
"key4" to listOf(Saveable(false, JsonPrimitive("str"))), | ||
) | ||
} | ||
|
||
private fun stateSnapshot() = StateSnapshot( | ||
mapOf( | ||
"key1" to listOf(Saveable(true, JsonPrimitive(1))), | ||
"key2" to listOf(Saveable(false, JsonPrimitive(1))), | ||
"key3" to listOf(Saveable(true, JsonPrimitive("str"))), | ||
"key4" to listOf(Saveable(false, JsonPrimitive("str"))), | ||
), | ||
) | ||
|
||
private fun storedStateSnapshot() = mapOf( | ||
"key1" to listOf(mutableStateOf(1)), | ||
"key2" to listOf(1), | ||
"key3" to listOf(mutableStateOf("str")), | ||
"key4" to listOf("str"), | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could remove the need for the
Saveable
wrapper class if we wrote an adapter to convert betweenMutableState
andJsonElement
. Perhaps it isn't worth the effort though, considering we'll change up this mechanism in #1384.