|
| 1 | +package peschke.cats |
| 2 | + |
| 3 | +import cats.Comparison |
| 4 | +import cats.Eq |
| 5 | +import cats.Order |
| 6 | +import cats.data.Validated |
| 7 | +import cats.syntax.all._ |
| 8 | +import cats.~> |
| 9 | +import org.scalatest.prop.TableFor2 |
| 10 | +import peschke.TableSpec |
| 11 | +import peschke.cats.syntax._ |
| 12 | +import peschke.cats.syntaxTest.Foo |
| 13 | + |
| 14 | +class syntaxTest extends TableSpec { |
| 15 | + "F[G[A]].innerMap" should { |
| 16 | + "enable modifying the contained value" in { |
| 17 | + List( |
| 18 | + 5.asRight, |
| 19 | + "ignored".asLeft, |
| 20 | + 0.asRight |
| 21 | + ).innerMap(_ + 5) mustBe List(10.asRight, "ignored".asLeft, 5.asRight) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + "F[G[A]].innerFlatMap" should { |
| 26 | + "enable modifying the inner effect" in { |
| 27 | + List( |
| 28 | + 5.asRight, |
| 29 | + "ignored".asLeft, |
| 30 | + 0.asRight |
| 31 | + ).innerFlatMap(v => if (v eqv 0) "<0>".asLeft else v.asRight) mustBe List( |
| 32 | + 5.asRight, |
| 33 | + "ignored".asLeft, |
| 34 | + "<0>".asLeft |
| 35 | + ) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + "F[G[A]].mapK" should { |
| 40 | + "allow changing the outer effect, while retaining the inner effect" in { |
| 41 | + 5.asRight[String].some.mapK(Lambda[Option ~> List](_.toList)) mustBe List( |
| 42 | + 5.asRight[String] |
| 43 | + ) |
| 44 | + |
| 45 | + List(5.asRight[String], "discarded".asLeft[Int]) |
| 46 | + .mapK(Lambda[List ~> Option](_.headOption)) mustBe 5 |
| 47 | + .asRight[String].some |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + "F[G[A]].innerMapK" should { |
| 52 | + "allow changing the inner effect, while retaining the outer effect" in { |
| 53 | + List( |
| 54 | + 5.asRight, |
| 55 | + "discarded".asLeft, |
| 56 | + 0.asRight |
| 57 | + ).innerMapK( |
| 58 | + Lambda[Lambda[r => Either[String, r]] ~> Option](_.toOption) |
| 59 | + ) mustBe List( |
| 60 | + 5.some, |
| 61 | + none, |
| 62 | + 0.some |
| 63 | + ) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + "Validated.andThenF" should { |
| 68 | + val t |
| 69 | + : TableFor2[Validated[String, Int], Int => Option[Validated[String, Int]]] = |
| 70 | + Table[Validated[String, Int], Int => Option[Validated[String, Int]]]( |
| 71 | + ("input", "function"), |
| 72 | + (5.valid, _.valid.some), |
| 73 | + ("hi".invalid, _.valid.some), |
| 74 | + (5.valid, i => s"$i".invalid.some), |
| 75 | + ("hi".invalid, i => s"$i".invalid.some), |
| 76 | + (5.valid, _ => none), |
| 77 | + ("hi".invalid, _ => none) |
| 78 | + ) |
| 79 | + |
| 80 | + "behave the same way as flatTraverse does for Either" in forAll(t) { |
| 81 | + (validatedInput, validatedFunction) => |
| 82 | + val eitherInput: Either[String, Int] = validatedInput.toEither |
| 83 | + val eitherFunction: Int => Option[Either[String, Int]] = |
| 84 | + validatedFunction(_).map(_.toEither) |
| 85 | + |
| 86 | + val validatedOutput = validatedInput.andThenF(validatedFunction) |
| 87 | + val eitherOutput = eitherInput.flatTraverse(eitherFunction) |
| 88 | + |
| 89 | + validatedOutput.map(_.toEither) mustBe eitherOutput |
| 90 | + eitherOutput.map(_.toValidated) mustBe validatedOutput |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + "Order.builder" should { |
| 95 | + "add subsequent instances as tiebreakers, in the correct order" in { |
| 96 | + val onlyS = Order.by[Foo, String](_.s) |
| 97 | + val onlyI = Order.builder[Foo].by(_.i).build |
| 98 | + val onlyIS = Order.builder[Foo].by(_.i).andThen(onlyS).build |
| 99 | + val all = Order.builder[Foo].by(_.i).by(_.s).by(_.c).build |
| 100 | + |
| 101 | + onlyI.comparison( |
| 102 | + Foo(4, "a", 'b'), |
| 103 | + Foo(4, "z", 'z') |
| 104 | + ) mustBe Comparison.EqualTo |
| 105 | + onlyI.comparison( |
| 106 | + Foo(4, "z", 'z'), |
| 107 | + Foo(5, "a", 'a') |
| 108 | + ) mustBe Comparison.LessThan |
| 109 | + onlyI.comparison( |
| 110 | + Foo(4, "a", 'a'), |
| 111 | + Foo(3, "z", 'z') |
| 112 | + ) mustBe Comparison.GreaterThan |
| 113 | + |
| 114 | + withClue("[Should defer to i]") { |
| 115 | + onlyIS.comparison( |
| 116 | + Foo(4, "z", 'z'), |
| 117 | + Foo(5, "a", 'a') |
| 118 | + ) mustBe Comparison.LessThan |
| 119 | + onlyIS.comparison( |
| 120 | + Foo(4, "a", 'a'), |
| 121 | + Foo(3, "z", 'z') |
| 122 | + ) mustBe Comparison.GreaterThan |
| 123 | + } |
| 124 | + onlyIS.comparison( |
| 125 | + Foo(4, "a", 'z'), |
| 126 | + Foo(4, "z", 'a') |
| 127 | + ) mustBe Comparison.LessThan |
| 128 | + onlyIS.comparison( |
| 129 | + Foo(4, "z", 'a'), |
| 130 | + Foo(4, "a", 'z') |
| 131 | + ) mustBe Comparison.GreaterThan |
| 132 | + onlyIS.comparison( |
| 133 | + Foo(4, "a", 'z'), |
| 134 | + Foo(4, "a", 'z') |
| 135 | + ) mustBe Comparison.EqualTo |
| 136 | + |
| 137 | + withClue("[Should defer to i]") { |
| 138 | + all.comparison( |
| 139 | + Foo(4, "z", 'z'), |
| 140 | + Foo(5, "a", 'a') |
| 141 | + ) mustBe Comparison.LessThan |
| 142 | + all.comparison( |
| 143 | + Foo(4, "a", 'a'), |
| 144 | + Foo(3, "z", 'z') |
| 145 | + ) mustBe Comparison.GreaterThan |
| 146 | + } |
| 147 | + withClue("[Should defer to s]") { |
| 148 | + all.comparison( |
| 149 | + Foo(4, "a", 'z'), |
| 150 | + Foo(4, "z", 'a') |
| 151 | + ) mustBe Comparison.LessThan |
| 152 | + all.comparison( |
| 153 | + Foo(4, "z", 'a'), |
| 154 | + Foo(4, "a", 'z') |
| 155 | + ) mustBe Comparison.GreaterThan |
| 156 | + } |
| 157 | + all.comparison( |
| 158 | + Foo(4, "a", 'a'), |
| 159 | + Foo(4, "a", 'z') |
| 160 | + ) mustBe Comparison.LessThan |
| 161 | + all.comparison( |
| 162 | + Foo(4, "a", 'z'), |
| 163 | + Foo(4, "a", 'a') |
| 164 | + ) mustBe Comparison.GreaterThan |
| 165 | + all.comparison( |
| 166 | + Foo(4, "a", 'a'), |
| 167 | + Foo(4, "a", 'a') |
| 168 | + ) mustBe Comparison.EqualTo |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + "Eq.builder" should { |
| 173 | + "require all instances to agree something is equal to return true" in { |
| 174 | + val onlyS = Eq.by[Foo, String](_.s) |
| 175 | + val onlyI = Eq.builder[Foo].by(_.i).build |
| 176 | + val onlyIS = Eq.builder[Foo].by(_.i).and(onlyS).build |
| 177 | + val all = Eq.builder[Foo].by(_.i).by(_.s).by(_.c).build |
| 178 | + |
| 179 | + onlyI.eqv(Foo(4, "a", 'a'), Foo(3, "a", 'a')) mustBe false |
| 180 | + onlyI.eqv(Foo(4, "a", 'a'), Foo(4, "z", 'z')) mustBe true |
| 181 | + |
| 182 | + onlyIS.eqv(Foo(4, "a", 'a'), Foo(3, "a", 'a')) mustBe false |
| 183 | + onlyIS.eqv(Foo(4, "a", 'a'), Foo(4, "z", 'a')) mustBe false |
| 184 | + onlyIS.eqv(Foo(4, "a", 'z'), Foo(4, "a", 'a')) mustBe true |
| 185 | + |
| 186 | + all.eqv(Foo(4, "a", 'a'), Foo(3, "a", 'a')) mustBe false |
| 187 | + all.eqv(Foo(4, "a", 'a'), Foo(4, "z", 'a')) mustBe false |
| 188 | + all.eqv(Foo(4, "a", 'z'), Foo(4, "a", 'a')) mustBe false |
| 189 | + all.eqv(Foo(4, "a", 'a'), Foo(4, "a", 'a')) mustBe true |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | +object syntaxTest { |
| 194 | + final case class Foo(i: Int, s: String, c: Char) |
| 195 | +} |
0 commit comments