Skip to content

Commit

Permalink
Target test for extension copy (softwaremill#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejSpanel committed Dec 1, 2024
1 parent 7cf831e commit b9c067b
Showing 1 changed file with 50 additions and 0 deletions.
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)
}
}

0 comments on commit b9c067b

Please sign in to comment.