100 lines
3.2 KiB
Scala
100 lines
3.2 KiB
Scala
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 ValueEndsWithTests extends IOSuite:
|
|
|
|
import ValueEndsWithTests.Data
|
|
|
|
iotest("should find a string as a prefix of the input") {
|
|
ValueEndsWithTests.newProvider(Data.KeyValues).flatMap { provider =>
|
|
given KeyValueProvider[IO] = provider
|
|
val p1 = ValueEndsWith[IO](Data.PassingKey, Data.Substring1)
|
|
val p2 = ValueEndsWith[IO](Data.PassingKey, Data.Substring2)
|
|
val p3 = ValueEndsWith[IO](Data.PassingKey, Data.Substring3)
|
|
for
|
|
r1 <- p1.eval()
|
|
r2 <- p2.eval()
|
|
r3 <- p3.eval()
|
|
yield
|
|
assertEquals(r1, Predicate.Result.matched())
|
|
assertEquals(r2, Predicate.Result.matched())
|
|
assertEquals(r3, Predicate.Result.matched())
|
|
}
|
|
}
|
|
|
|
iotest("should not find a key that does not exist within some provider") {
|
|
ValueEndsWithTests.newProvider(Data.KeyValues).flatMap { provider =>
|
|
given KeyValueProvider[IO] = provider
|
|
val p = ValueEndsWith[IO](Data.NotExistingKey, "")
|
|
for result <- p.eval()
|
|
yield assertEquals(result, Predicate.Result.missed())
|
|
}
|
|
}
|
|
|
|
iotest("should match if an empty substring is provided") {
|
|
ValueEndsWithTests.newProvider(Data.KeyValues).flatMap { provider =>
|
|
given KeyValueProvider[IO] = provider
|
|
val p1 = ValueEndsWith[IO](Data.EmptyStringKey, "")
|
|
val p2 = ValueEndsWith[IO](Data.PassingKey, "")
|
|
for
|
|
r1 <- p1.eval()
|
|
r2 <- p2.eval()
|
|
yield
|
|
assertEquals(r1, Predicate.Result.matched())
|
|
assertEquals(r2, Predicate.Result.matched())
|
|
}
|
|
}
|
|
|
|
iotest(
|
|
"should not match if the target value is not the suffix of the input"
|
|
) {
|
|
ValueEndsWithTests.newProvider(Data.KeyValues).flatMap { provider =>
|
|
given KeyValueProvider[IO] = provider
|
|
val p1 = ValueEndsWith[IO](Data.PassingKey, Data.Substring3 + "z")
|
|
val p2 =
|
|
ValueEndsWith[IO](Data.PassingKey, Data.Substring3.reverse)
|
|
val p3 =
|
|
ValueEndsWith[IO](Data.PassingKey, Data.Substring2.reverse)
|
|
for
|
|
r1 <- p1.eval()
|
|
r2 <- p2.eval()
|
|
r3 <- p3.eval()
|
|
yield
|
|
assertEquals(r1, Predicate.Result.missed())
|
|
assertEquals(r2, Predicate.Result.missed())
|
|
assertEquals(r3, Predicate.Result.missed())
|
|
}
|
|
}
|
|
|
|
object ValueEndsWithTests:
|
|
|
|
object Data:
|
|
|
|
val PassingKey: String = Gen.string.alphaNumeric(Size.Fixed(8)).gen()
|
|
val PassingValue: String = "abcdefghi"
|
|
val Substring1: String = "i"
|
|
val Substring2: String = "hi"
|
|
val Substring3: String = "abcdefghi"
|
|
|
|
val NotExistingKey: String = Gen.string.alphaNumeric(Size.Fixed(6)).gen()
|
|
|
|
val EmptyStringKey: String = "empty"
|
|
|
|
val KeyValues: Map[String, String] = Map(
|
|
PassingKey -> PassingValue,
|
|
EmptyStringKey -> ""
|
|
)
|
|
|
|
end Data
|
|
|
|
def newProvider(data: Map[String, String]): IO[KeyValueProvider[IO]] =
|
|
for map <- MapRef.ofSingleImmutableMap[IO, String, String](data)
|
|
yield new MemoryMapKeyValueProvider(map)
|
|
|
|
end ValueEndsWithTests
|