Skip to content

Commit

Permalink
Tests for QuicklensMapAtFunctor implicits
Browse files Browse the repository at this point in the history
  • Loading branch information
yu.badalyants committed Dec 10, 2021
1 parent c84d471 commit 63a1fc4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ sealed trait LowPriorityImplicits {

import quicklens._

/**
* `QuicklensEach` is in `LowPriorityImplicits` to not conflict with the `QuicklensMapAtFunctor` on `each` calls.
*/
implicit class QuicklensEach[F[_], T](t: F[T])(implicit f: QuicklensFunctor[F, T]) {
@compileTimeOnly(canOnlyBeUsedInsideModify("each"))
def each: T = sys.error("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ sealed trait LowPriorityImplicits {

import quicklens._

/**
* `QuicklensEach` is in `LowPriorityImplicits` to not conflict with the `QuicklensMapAtFunctor` on `each` calls.
*/
implicit class QuicklensEach[F[_], T](t: F[T])(implicit f: QuicklensFunctor[F, T]) {
@compileTimeOnly(canOnlyBeUsedInsideModify("each"))
def each: T = sys.error("")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.softwaremill.quicklens

import com.softwaremill.quicklens.TestData._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class QuicklensMapAtFunctorTest extends AnyFlatSpec with Matchers {

"QuicklensMapAtFunctor" should "work for types which is not a subtype of Map" in {
case class MapLike[K, V](underlying: Map[K, V])

implicit def instance[K, T]: QuicklensMapAtFunctor[MapLike, K, T] = new QuicklensMapAtFunctor[MapLike, K, T] {
private val mapInstance: QuicklensMapAtFunctor[Map, K, T] =
implicitly[QuicklensMapAtFunctor[Map, K, T]]

def at(fa: MapLike[K, T], idx: K)(f: T => T): MapLike[K, T] =
MapLike(mapInstance.at(fa.underlying, idx)(f))
def atOrElse(fa: MapLike[K, T], idx: K, default: => T)(f: T => T): MapLike[K, T] =
MapLike(mapInstance.atOrElse(fa.underlying, idx, default)(f))
def index(fa: MapLike[K, T], idx: K)(f: T => T): MapLike[K, T] =
MapLike(mapInstance.index(fa.underlying, idx)(f))
def each(fa: MapLike[K, T])(f: T => T): MapLike[K, T] =
MapLike(mapInstance.each(fa.underlying)(f))
}

modify(MapLike(m1))(_.at("K1").a5.name).using(duplicate) should be(MapLike(m1dup))
}
}

0 comments on commit 63a1fc4

Please sign in to comment.