From 980517d2d6df1d8b8108e891129231fbdb735087 Mon Sep 17 00:00:00 2001 From: Alexey Rykhalskiy Date: Thu, 12 Dec 2024 21:17:07 +0200 Subject: [PATCH] -- mockito --- .../scala/mockitox/LearningMockitoSpy.scala | 26 +++++++++++++++++++ .../main/scala/winitzki/Fundamentals6.scala | 11 +++++--- project/Libraries.scala | 15 +++++------ project/LibrariesLihaoyi.scala | 20 +++++++------- 4 files changed, 49 insertions(+), 23 deletions(-) create mode 100644 ce3/src/main/scala/mockitox/LearningMockitoSpy.scala diff --git a/ce3/src/main/scala/mockitox/LearningMockitoSpy.scala b/ce3/src/main/scala/mockitox/LearningMockitoSpy.scala new file mode 100644 index 00000000..14e18402 --- /dev/null +++ b/ce3/src/main/scala/mockitox/LearningMockitoSpy.scala @@ -0,0 +1,26 @@ +package mockitox + +import org.mockito.IdiomaticMockito +import org.scalatest.funsuite.AnyFunSuite +import org.scalatest.matchers.should.Matchers + +class LearningMockitoSpy extends AnyFunSuite with Matchers with IdiomaticMockito { + + class Calculator { + def add0(a: Int, b: Int): Int = a + b + def add(a: Int, b: Int): Int = add0(a, b) + + def sub(a: Int, b: Int): Int = a - b + } + + val realCalculator = new Calculator + val spyCalculator = spy(realCalculator) + + test("1") { + spyCalculator.add0(1, 2).returns(113) + + val x = spyCalculator.add(1, 2) + pprint.log(x) // 113 + } + +} diff --git a/plain2/src/main/scala/winitzki/Fundamentals6.scala b/plain2/src/main/scala/winitzki/Fundamentals6.scala index 5077a737..68b7642e 100644 --- a/plain2/src/main/scala/winitzki/Fundamentals6.scala +++ b/plain2/src/main/scala/winitzki/Fundamentals6.scala @@ -405,11 +405,11 @@ class Fundamentals6 extends Base { def durationToLong: FiniteDuration => Long = _.toNanos // now we can derive Semigroup[FiniteDuration] (profunctor) - val combiner = Invariant[Semigroup].imap(combinerLong)(longToDuration)(durationToLong) + val combiner: Semigroup[FiniteDuration] = Invariant[Semigroup].imap(combinerLong)(longToDuration)(durationToLong) - import scala.concurrent.duration._ // it takes type A, converts to B, does B |+| B, converts to A back - val combined: FiniteDuration = combiner.combine(2.seconds, 3.seconds) + val combined1: FiniteDuration = combiner.combine(2.seconds, 3.seconds) + val combined2: FiniteDuration = 2.seconds |+| 3.seconds sealed trait List[A] final case class Empty[A]() extends List[A] @@ -513,5 +513,8 @@ class Fundamentals6 extends Base { h2(3) shouldBe 301 } - test("6.2.6.4 - p 204") {} + test("6.2.4 - p 204 ???") { + type Const[Z, A] = Z + def cmap[Z, A, B](f: B => A): Const[Z, A] => Const[Z, B] = identity[Z] + } } diff --git a/project/Libraries.scala b/project/Libraries.scala index 1bb49671..ef6714c0 100644 --- a/project/Libraries.scala +++ b/project/Libraries.scala @@ -6,7 +6,6 @@ object Libraries { val newtype = "io.estatico" %% "newtype" % "0.4.4" val refinedCore = "eu.timepit" %% "refined" % "0.10.3" val refinedCats = "eu.timepit" %% "refined-cats" % "0.10.3" - val refinedScalaz = "eu.timepit" %% "refined-scalaz" % "0.10.3" // basic functional abstractions val cats = "org.typelevel" %% "cats-core" % "2.10.0" val catsLaws = "org.typelevel" %% "cats-laws" % "2.10.0" @@ -23,26 +22,24 @@ object Libraries { val logback = "ch.qos.logback" % "logback-classic" % "1.4.7" // https://scalacheck.org // https://mvnrepository.com/artifact/org.scalacheck/scalacheck - val scalaCheck = "org.scalacheck" %% "scalacheck" % "1.17.0" + val scalaCheck = "org.scalacheck" %% "scalacheck" % "1.18.1" // https://www.scalatest.org // https://mvnrepository.com/artifact/org.scalatest/scalatest - val scalaTest = "org.scalatest" %% "scalatest" % "3.2.17" - val scalaTestShould = "org.scalatest" %% "scalatest-shouldmatchers" % "3.2.17" - val scalaTestFunSpec = "org.scalatest" %% "scalatest-funspec" % "3.2.17" + val scalaTest = "org.scalatest" %% "scalatest" % "3.2.19" // https://github.com/alexarchambault/scalacheck-shapeless val scalaCheckShapeless = "com.github.alexarchambault" %% "scalacheck-shapeless_1.16" % "1.3.1" // https://www.scalactic.org // https://mvnrepository.com/artifact/org.scalactic/scalactic // val scalactic_ = "org.scalactic" %% "scalactic" % Versions.scalaTest - val scalaCheckIntegration = "org.scalatestplus" %% "scalacheck-1-17" % "3.2.17.0" - val scalaMockito = "org.mockito" %% "mockito-scala-scalatest" % "1.17.30" // tr: "org.mockito" % "mockito-core" % "4.8.1" + val scalaCheckIntegration = "org.scalatestplus" %% "scalacheck-1-18" % "3.2.19.0" + val scalaMockito = "org.mockito" %% "mockito-scala-scalatest" % "1.17.37" // tr: "org.mockito" % "mockito-core" % "4.8.1" // https://index.scala-lang.org/ghik/silencer/silencer-plugin/1.4.2?target=_2.13 // look for the plugin corresponding val silencerAnnotation = "com.github.ghik" % "silencer-lib" % "1.6.0" % Provided cross CrossVersion.full val testingToolkit2: Seq[ModuleID] = Seq( scalaTest, // runners, matchers - scalaCheck, // property based testing + scalaCheck, // property-based testing scalaCheckIntegration, // scalaTest integration scalaCheckShapeless, // Shapeless scalacheck Arbitrary[A] derivation, doesn't have scala3 things scalaMockito, // mock traits/classes, doesn't have scala3 things @@ -51,7 +48,7 @@ object Libraries { val testingToolkit3: Seq[ModuleID] = Seq( scalaTest, // runners, matchers - scalaCheck, // property based testing + scalaCheck, // property-based testing scalaCheckIntegration, // scalaTest integration LibrariesLihaoyi.pprint // println colored + colored console output + implicits to get source module/file/line/etc ) diff --git a/project/LibrariesLihaoyi.scala b/project/LibrariesLihaoyi.scala index a4451ae2..520ad063 100644 --- a/project/LibrariesLihaoyi.scala +++ b/project/LibrariesLihaoyi.scala @@ -2,15 +2,15 @@ import sbt.* object LibrariesLihaoyi { - val fansi = "com.lihaoyi" %% "fansi" % "0.4.0" - val pprint = "com.lihaoyi" %% "pprint" % "0.8.1" - val sourcecode = "com.lihaoyi" %% "sourcecode" % "0.3.0" - val upickle = "com.lihaoyi" %% "upickle" % "3.1.2" // http://www.lihaoyi.com/upickle - val ujson = "com.lihaoyi" %% "ujson" % "3.1.2" - val osLib = "com.lihaoyi" %% "os-lib" % "0.9.1" - val scalatags = "com.lihaoyi" %% "scalatags" % "0.12.0" - val requests = "com.lihaoyi" %% "requests" % "0.8.0" - val geny = "com.lihaoyi" %% "geny" % "1.0.0" - val fastparse = "com.lihaoyi" %% "fastparse" % "3.0.2" // https://www.lihaoyi.com/fastparse/ + val fansi = "com.lihaoyi" %% "fansi" % "0.5.0" + val pprint = "com.lihaoyi" %% "pprint" % "0.9.0" + val sourcecode = "com.lihaoyi" %% "sourcecode" % "0.4.2" + val upickle = "com.lihaoyi" %% "upickle" % "4.0.2" // http://www.lihaoyi.com/upickle + val ujson = "com.lihaoyi" %% "ujson" % "4.0.2" + val osLib = "com.lihaoyi" %% "os-lib" % "0.11.3" + val scalatags = "com.lihaoyi" %% "scalatags" % "0.13.1" + val requests = "com.lihaoyi" %% "requests" % "0.9.0" + val geny = "com.lihaoyi" %% "geny" % "1.1.1" + val fastparse = "com.lihaoyi" %% "fastparse" % "3.1.1" // https://www.lihaoyi.com/fastparse/ }