forked from softwaremill/quicklens
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Target test for extension copy (softwaremill#234)
- Loading branch information
1 parent
7cf831e
commit b9c067b
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
quicklens/src/test/scala-3/com/softwaremill/quicklens/test/ExtensionCopyTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.softwaremill.quicklens | ||
package test | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
object ExtensionCopyTest { | ||
case class V(x: Double, y: Double) | ||
|
||
opaque type Vec = V | ||
|
||
object Vec { | ||
def apply(x: Double, y: Double): Vec = V(x, y) | ||
} | ||
|
||
extension (v: Vec) { | ||
def x: Double = v.x | ||
def y: Double = v.y | ||
def copy(x: Double = v.x, y: Double = v.y): Vec = V(x, y) | ||
} | ||
} | ||
|
||
class ExtensionCopyTest extends AnyFlatSpec with Matchers { | ||
it should "modify a class with an extension copy method" in { | ||
case class V(x: Double, y: Double) | ||
|
||
class Vec(val v: V) | ||
|
||
object Vec { | ||
def apply(x: Double, y: Double): Vec = new Vec(V(x, y)) | ||
} | ||
|
||
extension (v: Vec) { | ||
def x: Double = v.v.x | ||
def y: Double = v.v.y | ||
def copy(x: Double = v.x, y: Double = v.y): Vec = new Vec(V(x, y)) | ||
} | ||
val a = Vec(1, 2) | ||
val b = a.modify(_.x).using(_ + 1) | ||
println(b) | ||
} | ||
|
||
it should "modify an opaque type with an extension copy method" in { | ||
import ExtensionCopyTest.* | ||
|
||
val a = Vec(1, 2) | ||
val b = a.modify(_.x).using(_ + 1) | ||
println(b) | ||
} | ||
} |