Skip to content

Commit

Permalink
テスト追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kitakkun committed Dec 16, 2023
1 parent 1ff439c commit aedb68b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ import kotlinx.coroutines.flow.update
*/
@DebuggableStateHolder
class WeirdCodeStyleViewModel {
private val mutableLiveData = MutableLiveData("")
private val mutableLiveData1 = MutableLiveData("")
private val mutableLiveData2 = MutableLiveData("")
private val mutableStateFlow = MutableStateFlow("")
private val mutableState = mutableStateOf("")

fun mutateLiveData() {
println((mutableLiveData.setValue("Updated from setValue")).toString())
println((mutableLiveData1.setValue("Updated from setValue")).toString())
val lambda = {
((mutableLiveData.setValue("Updated from setValue inside block")).equals(null)).toString()
((mutableLiveData1.setValue("Updated from setValue inside block")).equals(null)).toString()
}
lambda.invoke()
doubleMutateByBlock(mutableLiveData1, mutableLiveData2) {
value = "Updated from <set-value>"
}
// mutableLiveData.setValueAndGet("Updated from setValueAndGet1") + mutableLiveData.setValueAndGet("Updated from setValueAndGet2")
// mutableLiveData.setValueAndGetInline("Updated from setValueAndGetInline1") + mutableLiveData.setValueAndGetInline("Updated from setValueAndGetInline2")
}

fun mutateStateFlow() {
Expand All @@ -40,3 +46,19 @@ class WeirdCodeStyleViewModel {
private fun <T> MutableState<T>.setValue(value: T) {
this.value = value
}

private fun <T> MutableLiveData<T>.setValueAndGet(value: T): T {
this.value = value
return value
}

@Suppress("NOTHING_TO_INLINE")
private inline fun <T> MutableLiveData<T>.setValueAndGetInline(value: T): T {
this.value = value
return value
}

private fun <T> doubleMutateByBlock(receiver1: MutableLiveData<T>, receiver2: MutableLiveData<T>, block: MutableLiveData<T>.() -> Unit) {
receiver1.block()
receiver2.block()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ class WeirdCodeStyleViewModelTest {
fun mutateLiveData() {
viewModel.mutateLiveData()

verify(exactly = 2) {
BackInTimeDebugService.notifyPropertyChanged(any(), "mutableLiveData", any(), any())
verify(exactly = 3) {
BackInTimeDebugService.notifyPropertyChanged(any(), "mutableLiveData1", any(), any())
}
verify(exactly = 1) {
BackInTimeDebugService.notifyPropertyChanged(any(), "mutableLiveData2", any(), any())
}
}

Expand Down

0 comments on commit aedb68b

Please sign in to comment.