From feb0dfd19d07fc7ad47f7a22131b5ce41d1cc932 Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Thu, 24 Mar 2022 09:59:05 -0700 Subject: [PATCH 1/2] Attempt to Law Test - still needs arb --- fetch/src/test/scala/FetchLawTests.scala | 27 ++++++++++++++++++++++++ project/ProjectPlugin.scala | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 fetch/src/test/scala/FetchLawTests.scala diff --git a/fetch/src/test/scala/FetchLawTests.scala b/fetch/src/test/scala/FetchLawTests.scala new file mode 100644 index 00000000..482d3dac --- /dev/null +++ b/fetch/src/test/scala/FetchLawTests.scala @@ -0,0 +1,27 @@ +/* + * Copyright 2016-2022 47 Degrees Open Source + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package fetch + +import cats.effect._ +import cats.implicits._ +import cats.laws.discipline._ +import org.scalatestplus.scalacheck.Checkers +import org.typelevel.discipline.scalatest.Discipline + +class FetchLawTests extends FetchSpec with Discipline with Checkers { + checkAll("Fetch", MonadTests[Fetch[IO, *]].monad[Int, Int, Int]) +} \ No newline at end of file diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 741f8723..323ed51b 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -16,7 +16,9 @@ object ProjectPlugin extends AutoPlugin { libraryDependencies ++= Seq( "org.typelevel" %%% "cats-effect" % "3.3.8", - "org.scalatest" %%% "scalatest" % "3.2.11" % "test" + "org.scalatest" %%% "scalatest" % "3.2.11" % "test", + "org.typelevel" %%% "cats-laws" % "2.7.0" % "test", + "org.typelevel" %%% "discipline-scalatest" % "2.0.0" % "test" ) ) From 487dd719c34701afce97dd0a409dafeacd1deac1 Mon Sep 17 00:00:00 2001 From: Christopher Davenport Date: Thu, 24 Mar 2022 11:19:15 -0700 Subject: [PATCH 2/2] Add Law Tests that somehow pass... --- fetch/src/test/scala/FetchLawTests.scala | 42 ++++++++++++++++++++++-- project/ProjectPlugin.scala | 1 + 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/fetch/src/test/scala/FetchLawTests.scala b/fetch/src/test/scala/FetchLawTests.scala index 482d3dac..49e29f93 100644 --- a/fetch/src/test/scala/FetchLawTests.scala +++ b/fetch/src/test/scala/FetchLawTests.scala @@ -16,12 +16,50 @@ package fetch +import cats._ import cats.effect._ import cats.implicits._ import cats.laws.discipline._ import org.scalatestplus.scalacheck.Checkers -import org.typelevel.discipline.scalatest.Discipline +import org.typelevel.discipline.scalatest._ +import _root_.org.scalacheck._ +import cats.effect.testkit.TestInstances +import org.scalatest.funspec.AnyFunSpec -class FetchLawTests extends FetchSpec with Discipline with Checkers { +object FetchArbitrary extends TestInstances { + import TestHelper._ + implicit val arb: Arbitrary[Fetch[IO, Int]] = Arbitrary( + Gen.oneOf( + one[IO](1), + one[IO](2), + // many[IO](1), + // many[IO](2), + anotherOne[IO](1), + anotherOne[IO](2), + never[IO] + ) + ) + + implicit val arbF: Arbitrary[Fetch[IO, Int => Int]] = Arbitrary( + Gen.oneOf( + one[IO](1).map(i => {_: Int => i}), + one[IO](2).map(i => {_: Int => i}), + // many[IO](1), + // many[IO](2), + anotherOne[IO](1).map(i => {_: Int => i}), + anotherOne[IO](2).map(i => {_: Int => i}), + never[IO].map(i => {_: Int => i}) + ) + ) + + implicit def testEq[A: Eq](implicit ticker: Ticker): Eq[Fetch[IO, A]] = Eq.instance{ + case (a1, a2) => + unsafeRun(Fetch.run(a1)) === unsafeRun(Fetch.run(a2)) + } +} + +class FetchLawTests extends AnyFunSpec with Discipline with Checkers with FunSpecDiscipline { + import FetchArbitrary.{arb, arbF, testEq, Ticker} + implicit val MyTicker: Ticker = Ticker() checkAll("Fetch", MonadTests[Fetch[IO, *]].monad[Int, Int, Int]) } \ No newline at end of file diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 323ed51b..e565e4e4 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -16,6 +16,7 @@ object ProjectPlugin extends AutoPlugin { libraryDependencies ++= Seq( "org.typelevel" %%% "cats-effect" % "3.3.8", + "org.typelevel" %%% "cats-effect-testkit" % "3.3.8" % "test", "org.scalatest" %%% "scalatest" % "3.2.11" % "test", "org.typelevel" %%% "cats-laws" % "2.7.0" % "test", "org.typelevel" %%% "discipline-scalatest" % "2.0.0" % "test"