-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from JD557/ref-tests
Add Ref tests
- Loading branch information
Showing
4 changed files
with
143 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
core/src/test/scala/eu/joaocosta/interim/api/RefSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package eu.joaocosta.interim.api | ||
|
||
class RefSpec extends munit.FunSuite: | ||
test("A Ref value can be correctly set and retrieved with := and value"): | ||
val x = Ref(1) | ||
assertEquals(x.value, 1) | ||
x := 2 | ||
assertEquals(x.value, 2) | ||
x.value = 3 | ||
assertEquals(x.value, 3) | ||
|
||
test("Ref values and raw values can be fetched with Ref.get"): | ||
val x = Ref(1) | ||
val y = 1 | ||
assertEquals(Ref.get[Int](x), Ref.get[Int](y)) | ||
|
||
test("Ref values and raw values can be set with Ref.set"): | ||
val x = Ref(1) | ||
val y = 1 | ||
|
||
assertEquals(Ref.set[Int](x, 2), 2) | ||
assertEquals(Ref.set[Int](y, 2), 2) | ||
assertEquals(x.value, 2) | ||
assertEquals(y, 1) | ||
|
||
test("Ref values and raw values can be modified with Ref.modify"): | ||
val x = Ref(1) | ||
val y = 1 | ||
|
||
assertEquals(Ref.modify[Int](x, _ + 1), 2) | ||
assertEquals(Ref.modify[Int](y, _ + 1), 2) | ||
assertEquals(x.value, 2) | ||
assertEquals(y, 1) | ||
|
||
test("withRef allows to use a temporary Ref value"): | ||
val result = Ref.withRef(0) { ref => | ||
Ref.modify[Int](ref, _ + 2) | ||
} | ||
assertEquals(result, 2) | ||
|
||
test("asRef allows to use a temporary Ref value"): | ||
import Ref.asRef | ||
val result = 0.asRef { ref => | ||
Ref.modify[Int](ref, _ + 2) | ||
} | ||
assertEquals(result, 2) |