aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/xyz/driver/core/tagging/TaggingTest.scala
blob: 14dfaf9d705493a12f7bc992ac4478c3d126aaf2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package xyz.driver.core.tagging

import org.scalatest.{Matchers, WordSpec}
import xyz.driver.core.{@@, Name}

/**
  * @author sergey
  * @since 9/11/18
  */
class TaggingTest extends WordSpec with Matchers {

  "@@ Trimmed" should {
    "produce values transparently from Strings and Names (by default)" in {
      val s: String @@ Trimmed    = " trimmed "
      val n: Name[Int] @@ Trimmed = Name(" trimmed ")

      s shouldBe "trimmed"
      n shouldBe Name[Int]("trimmed")
    }

    "produce values transparently from values that have an implicit conversion defined" in {
      import scala.language.implicitConversions
      implicit def stringSeq2Trimmed(stringSeq: Seq[String]): Seq[String] @@ Trimmed =
        stringSeq.map(_.trim()).tagged[Trimmed]

      val strings: Seq[String] @@ Trimmed = Seq(" trimmed1 ", " trimmed2 ")
      strings shouldBe Seq("trimmed1", "trimmed2")
    }

    "produce values transparently from Options of values that have Trimmed implicits" in {
      val maybeStringDirect: Option[String @@ Trimmed]  = Some(" trimmed ")
      val maybeStringFromMap: Option[String @@ Trimmed] = Map("s" -> " trimmed ").get("s")

      val maybeNameDirect: Option[Name[Int] @@ Trimmed]  = Some(Name(" trimmed "))
      val maybeNameFromMap: Option[Name[Int] @@ Trimmed] = Map("s" -> Name[Int](" trimmed ")).get("s")

      maybeStringDirect shouldBe Some("trimmed")
      maybeStringFromMap shouldBe Some("trimmed")
      maybeNameDirect shouldBe Some(Name[Int]("trimmed"))
      maybeNameFromMap shouldBe Some(Name[Int]("trimmed"))
    }

    "produce values transparently from collections of values that have Trimmed implicits" in {
      val strings = Seq("s" -> " trimmed1 ", "s" -> " trimmed2 ")
      val names = strings.map {
        case (k, v) => k -> Name[Int](v)
      }

      val trimmedStrings: Seq[String @@ Trimmed]  = strings.groupBy(_._1)("s").map(_._2)
      val trimmedNames: Seq[Name[Int] @@ Trimmed] = names.groupBy(_._1)("s").map(_._2)

      trimmedStrings shouldBe Seq("trimmed1", "trimmed2")
      trimmedNames shouldBe Seq("trimmed1", "trimmed2").map(Name[Int])
    }

    "have Ordering" in {
      val names: Seq[Name[Int] @@ Trimmed] = Seq(" 2 ", " 1 ", "3").map(Name[Int])

      names.sorted should contain inOrderOnly (Name("1"), Name("2"), Name("3"))
    }
  }

}