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.

78 lines
1.8 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 CacheImplicits.given
2016-06-27 15:27:42 +02:00
2021-11-14 14:00:10 +01:00
import org.scalatest.flatspec.AnyFlatSpec
2016-06-27 15:27:42 +02:00
2021-11-14 14:00:10 +01:00
class CacheSpec extends AnyFlatSpec {
2016-06-27 15:27:42 +02:00
"A cache" should "NOT throw an exception if read without being written previously" in {
2021-12-20 08:00:30 +01:00
testCache[String, Int] { case (cache, store) =>
cache(store)("missing") match {
case Hit(_) => fail()
case Miss(_) => ()
}
2016-06-27 15:27:42 +02:00
}
}
it should "write a very simple value" in {
2021-12-20 08:00:30 +01:00
testCache[String, Int] { case (cache, store) =>
cache(store)("missing") match {
case Hit(_) => fail()
case Miss(update) => update(5)
}
2016-06-27 15:27:42 +02:00
}
}
it should "be updatable" in {
2021-12-20 08:00:30 +01:00
testCache[String, Int] { case (cache, store) =>
val value = 5
cache(store)("someKey") match {
case Hit(_) => fail()
case Miss(update) => update(value)
}
2016-06-27 15:27:42 +02:00
2021-12-20 08:00:30 +01:00
cache(store)("someKey") match {
case Hit(read) => assert(read === value); ()
case Miss(_) => fail()
}
2016-06-27 15:27:42 +02:00
}
}
it should "return the value that has been previously written" in {
2021-12-20 08:00:30 +01:00
testCache[String, Int] { case (cache, store) =>
val key = "someKey"
val value = 5
cache(store)(key) match {
case Hit(_) => fail()
case Miss(update) => update(value)
}
2016-06-27 15:27:42 +02:00
2021-12-20 08:00:30 +01:00
cache(store)(key) match {
case Hit(read) => assert(read === value); ()
case Miss(_) => fail()
}
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)
}
2017-08-10 12:24:29 +02:00
}