package gs.predicate.v0.kv import cats.effect.IO import cats.effect.std.MapRef import gs.datagen.v0.Gen import gs.datagen.v0.generators.Size import gs.predicate.v0.api.Predicate import support.IOSuite class ValueInTests extends IOSuite: import ValueInTests.Data iotest("should find an exact match against some value") { ValueInTests.newProvider(Data.KeyValues).flatMap { provider => given KeyValueProvider[IO] = provider val p = ValueIn[IO]( Data.ExistingKey, Set( Data.ExistingValue, "", Gen.string.alphaNumeric(Size.Fixed(8)).gen() ) ) for result <- p.eval() yield assertEquals(result, Predicate.Result.matched()) } } iotest("should not find a value if it is not associated to a key") { ValueInTests.newProvider(Data.KeyValues).flatMap { provider => given KeyValueProvider[IO] = provider val p = ValueIn[IO](Data.ExistingKey, Set(Data.NotExistingValue)) for result <- p.eval() yield assertEquals(result, Predicate.Result.missed()) } } iotest("should not find a key that does not exist within some provider") { ValueInTests.newProvider(Data.KeyValues).flatMap { provider => given KeyValueProvider[IO] = provider val p = ValueIn[IO](Data.NotExistingKey, Set("")) for result <- p.eval() yield assertEquals(result, Predicate.Result.missed()) } } object ValueInTests: object Data: val ExistingKey: String = Gen.string.alphaNumeric(Size.Fixed(8)).gen() val NotExistingKey: String = Gen.string.alphaNumeric(Size.Fixed(6)).gen() val ExistingValue: String = Gen.string.alphaNumeric(Size.Fixed(10)).gen() val NotExistingValue: String = Gen.string.alphaNumeric(Size.Fixed(4)).gen() val KeyValues: Map[String, String] = Map(ExistingKey -> ExistingValue) end Data def newProvider(data: Map[String, String]): IO[KeyValueProvider[IO]] = for map <- MapRef.ofSingleImmutableMap[IO, String, String](data) yield new MemoryMapKeyValueProvider(map) end ValueInTests