-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from KacperFKorban/pr-self-typed-this
Issue with this in presence of self alias + & types
- Loading branch information
Showing
3 changed files
with
191 additions
and
28 deletions.
There are no files selected for viewing
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
88 changes: 88 additions & 0 deletions
88
quicklens/src/test/scala-3/com/softwaremill/quicklens/test/ModifyAndTypeTest.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,88 @@ | ||
package com.softwaremill.quicklens | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import ModifyAndTypeTest._ | ||
|
||
object ModifyAndTypeTest { | ||
case class A(a: Int) extends B | ||
trait B { | ||
def a: Int | ||
} | ||
|
||
case class A1(a: Int) | ||
|
||
sealed trait T | ||
case class C(a: Int) extends T with B | ||
|
||
sealed trait T1 | ||
case class C1(a: Int) extends T1 | ||
} | ||
|
||
class ModifyAndTypeTest extends AnyFlatSpec with Matchers { | ||
it should "modify an & type object" in { | ||
val ab: A & B = A(0) | ||
|
||
val modified = ab.modify(_.a).setTo(1) | ||
|
||
modified.a shouldBe 1 | ||
} | ||
|
||
it should "modify an & type object 1" in { | ||
val ab: B & A = A(0) | ||
|
||
val modified = ab.modify(_.a).setTo(1) | ||
|
||
modified.a shouldBe 1 | ||
} | ||
|
||
it should "modify an & type object 2" in { | ||
val ab: B & A1 = new A1(0) with B | ||
|
||
val modified = ab.modify(_.a).setTo(1) | ||
|
||
modified.a shouldBe 1 | ||
} | ||
|
||
it should "modify an & type object 3" in { | ||
val ab: A1 & B = new A1(0) with B | ||
|
||
val modified = ab.modify(_.a).setTo(1) | ||
|
||
modified.a shouldBe 1 | ||
} | ||
|
||
// TODO this is an implemenation limitation for now, since anonymous classes crash on runtime | ||
// it should "modify an & type object with a sealed trait" in { | ||
// val tb: T & B = C(0) | ||
|
||
// val modified = tb.modify(_.a).setTo(1) | ||
|
||
// modified.a shouldBe 1 | ||
// } | ||
|
||
// it should "modify an & type object with a sealed trait 1" in { | ||
// val tb: B & T = C(0) | ||
|
||
// val modified = tb.modify(_.a).setTo(1) | ||
|
||
// modified.a shouldBe 1 | ||
// } | ||
|
||
// it should "modify an & type object with a sealed trait 2" in { | ||
// val tb: B & T1 = new C1(0) with B | ||
|
||
// val modified = tb.modify(_.a).setTo(1) | ||
|
||
// modified.a shouldBe 1 | ||
// } | ||
|
||
// it should "modify an & type object with a sealed trait 3" in { | ||
// val tb: T1 & B = new C1(0) with B | ||
|
||
// val modified = tb.modify(_.a).setTo(1) | ||
|
||
// modified.a shouldBe 1 | ||
// } | ||
} |
41 changes: 41 additions & 0 deletions
41
quicklens/src/test/scala/com/softwaremill/quicklens/ModifySelfThisTest.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,41 @@ | ||
package com.softwaremill.quicklens | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import ModifySelfThisTest._ | ||
|
||
object ModifySelfThisTest { | ||
|
||
case class State(x: Int) { self => | ||
|
||
def mod: State = this.modify(_.x).setTo(1) | ||
} | ||
|
||
trait A { | ||
def a: Unit | ||
} | ||
|
||
case class State1(x: Int) extends A { self: A => | ||
|
||
def mod: State1 = this.modify(_.x).setTo(1) | ||
|
||
def a: Unit = () | ||
} | ||
} | ||
|
||
class ModifySelfThisTest extends AnyFlatSpec with Matchers { | ||
it should "modify an object even in presence of self alias" in { | ||
val s = State(0) | ||
val modified = s.mod | ||
|
||
modified.x shouldBe 1 | ||
} | ||
|
||
it should "modify an object even in presence of self type" in { | ||
val s = State(0) | ||
val modified = s.mod | ||
|
||
modified.x shouldBe 1 | ||
} | ||
} |