sbt/util-cache/src/test/scala/CacheSpec.scala

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

67 lines
2.0 KiB
Scala
Raw Normal View History

/*
* sbt
2023-06-20 13:42:07 +02:00
* Copyright 2023, Scala center
* Copyright 2011 - 2022, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/
package sbt.util
2016-06-27 15:27:42 +02:00
import sbt.io.IO
import sbt.io.syntax.*
import verify.BasicTestSuite
2016-06-27 15:27:42 +02:00
import CacheImplicits.given
2016-06-27 15:27:42 +02:00
object CacheSpec extends BasicTestSuite:
2016-06-27 15:27:42 +02:00
test("A cache should NOT throw an exception if read without being written previously"):
testCache[String, Int] { (cache, store) =>
cache(store)("missing") match
case Hit(_) => assert(false, "Expected Miss but got Hit")
2021-12-20 08:00:30 +01:00
case Miss(_) => ()
2016-06-27 15:27:42 +02:00
}
test("A cache should write a very simple value"):
testCache[String, Int] { (cache, store) =>
cache(store)("missing") match
case Hit(_) => assert(false, "Expected Miss but got Hit")
2021-12-20 08:00:30 +01:00
case Miss(update) => update(5)
2016-06-27 15:27:42 +02:00
}
test("A cache should be updatable"):
testCache[String, Int] { (cache, store) =>
2021-12-20 08:00:30 +01:00
val value = 5
cache(store)("someKey") match
case Hit(_) => assert(false, "Expected Miss but got Hit")
2021-12-20 08:00:30 +01:00
case Miss(update) => update(value)
2016-06-27 15:27:42 +02:00
cache(store)("someKey") match
case Hit(read) => assert(read == value)
case Miss(_) => assert(false, "Expected Hit but got Miss")
2016-06-27 15:27:42 +02:00
}
test("A cache should return the value that has been previously written"):
testCache[String, Int] { (cache, store) =>
2021-12-20 08:00:30 +01:00
val key = "someKey"
val value = 5
cache(store)(key) match
case Hit(_) => assert(false, "Expected Miss but got Hit")
2021-12-20 08:00:30 +01:00
case Miss(update) => update(value)
2016-06-27 15:27:42 +02:00
cache(store)(key) match
case Hit(read) => assert(read == value)
case Miss(_) => assert(false, "Expected Hit but got Miss")
2016-06-27 15:27:42 +02:00
}
private def testCache[K, V](f: (Cache[K, V], CacheStore) => Unit)(using
2021-12-20 08:00:30 +01:00
cache: Cache[K, V]
2017-08-10 12:24:29 +02:00
): Unit =
2016-06-27 15:27:42 +02:00
IO.withTemporaryDirectory { tmp =>
val store = new FileBasedStore(tmp / "cache-store")
2016-06-27 15:27:42 +02:00
f(cache, store)
}
end CacheSpec