Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Mar 16, 2024
1 parent 523fc80 commit 4341d43
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/com/kylecorry/luna/cache/MemoizedValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MemoizedValue<T> {
cachedHash = hash
}
// This cast is safe because the value is only set in the above block - it also handles if T is nullable
@Suppress("UNCHECKED_CAST")
cachedValue as T
}

Expand Down
60 changes: 60 additions & 0 deletions src/test/kotlin/com/kylecorry/luna/cache/HooksTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,66 @@ class HooksTest {
assertEquals(1, count2)
}

@Test
fun memoWithNullValue(){
val hooks = Hooks()
var count = 0

val value = hooks.memo<Int?>("test", 1, "Test") {
count++
null
}

assertNull(value)
assertEquals(1, count)

val value2 = hooks.memo<Int?>("test", 1, "Test") {
count++
null
}

assertNull(value2)
assertEquals(1, count)
}

@Test
fun memoWithNoDependencies(){
val hooks = Hooks()
var count = 0

val value = hooks.memo("test") {
count++
1
}

assertEquals(1, value)
assertEquals(1, count)

val value2 = hooks.memo("test") {
count++
1
}

assertEquals(1, value2)
assertEquals(1, count)
}

@Test
fun effectWithNoDependencies(){
val hooks = Hooks()
var count = 0

hooks.effect("test") {
count++
}

hooks.effect("test") {
count++
}

assertEquals(1, count)
}

@Test
fun resetEffects() {
val hooks = Hooks()
Expand Down

0 comments on commit 4341d43

Please sign in to comment.