Skip to content

Commit 1b61293

Browse files
committed
Port tests for InjectK to new tests for Inject
1 parent 47971ab commit 1b61293

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

core/src/main/scala/cats/Inject.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package cats
33
/**
44
* Inject is a type class to inject a value of type `A` into a
55
* coproduct of types `B` when `A :≺: B`. The coproduct `B` is
6-
* modelled with [[Either]].
6+
* modelled with [[scala.util.Either]].
77
*
88
* The injection type is described thoroughly in "Data types a la
99
* carte" (Swierstra 2008).
1010
*
1111
* @since 1.0
1212
* @note Prior to cats 1.0, Inject handled injection for type
13-
* constructors. For injection of type constructors, use [[InjectK]].
13+
* constructors. For injection of type constructors into [[data.EitherK]],
14+
* use [[InjectK]].
1415
*
1516
* @see [[http://www.staff.science.uu.nl/~swier004/publications/2008-jfp.pdf]]
16-
* @see [[InjectK]] for injection for [[EitherK]]
1717
*/
1818
sealed abstract class Inject[A, B] {
1919
def inj: A => B

core/src/main/scala/cats/InjectK.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import cats.data.EitherK
1414
* @note Prior to cats 1.0, InjectK was known as [[Inject]].
1515
*
1616
* @see [[http://www.staff.science.uu.nl/~swier004/publications/2008-jfp.pdf]]
17-
* @see [[Inject]] for injection for [[Either]]
17+
* @see [[Inject]] for injection for [[scala.util.Either]].
1818
*/
1919
sealed abstract class InjectK[F[_], G[_]] {
2020
def inj: FunctionK[F, G]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package cats
2+
3+
import cats.tests.CatsSuite
4+
5+
class InjectTests extends CatsSuite {
6+
7+
type StringOrInt = Either[String, Int]
8+
9+
test("inj & prj") {
10+
def distr[F](f1: F, f2: F)
11+
(implicit
12+
I0: Inject[String, F],
13+
I1: Inject[Int, F]
14+
): Option[String] =
15+
for {
16+
x <- I0.prj(f1)
17+
y <- I1.prj(f2)
18+
} yield s"$x $y"
19+
20+
forAll { (x: String, y: Int) =>
21+
val expr1: StringOrInt = Inject[String, StringOrInt].inj(x)
22+
val expr2: StringOrInt = Inject[Int, StringOrInt].inj(y)
23+
val res = distr(expr1, expr2)
24+
res should ===(Some(s"$x $y"))
25+
}
26+
}
27+
28+
test("apply & unapply") {
29+
def distr[F](f1: F, f2: F)
30+
(implicit
31+
I0: Inject[String, F],
32+
I1: Inject[Int, F]
33+
): Option[String] =
34+
for {
35+
x <- I0.unapply(f1)
36+
y <- I1.unapply(f2)
37+
} yield s"$x $y"
38+
39+
forAll { (x: String, y: Int) =>
40+
val expr1: StringOrInt = Inject[String, StringOrInt].apply(x)
41+
val expr2: StringOrInt = Inject[Int, StringOrInt].apply(y)
42+
val res = distr(expr1, expr2)
43+
res should ===(Some(s"$x $y"))
44+
}
45+
}
46+
47+
test("apply in left") {
48+
forAll { (y: String) =>
49+
Inject[String, StringOrInt].inj(y) == Left(y) should ===(true)
50+
}
51+
}
52+
53+
test("apply in right") {
54+
forAll { (y: Int) =>
55+
Inject[Int, StringOrInt].inj(y) == Right(y) should ===(true)
56+
}
57+
}
58+
59+
test("null identity") {
60+
val stringNull = null.asInstanceOf[String]
61+
Inject.catsReflexiveInjectInstance[String].inj(stringNull) should ===(stringNull)
62+
Inject.catsReflexiveInjectInstance[String].prj(stringNull) should ===(Some(stringNull))
63+
}
64+
65+
}

0 commit comments

Comments
 (0)