Last active
March 29, 2025 08:39
-
-
Save dacr/8a9e008b0404ceb2b390464f0fceced4 to your computer and use it in GitHub Desktop.
deep case/data class changes / published by https://github.com/dacr/code-examples-manager #c198c8b6-672a-4a5a-8554-4f234ac665c2/66a0a79ce92cfec2740991e36bc74718b885de6f
This file contains hidden or 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
// summary : deep case/data class changes | |
// keywords : scala, lens, changes, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : c198c8b6-672a-4a5a-8554-4f234ac665c2 | |
// created-on : 2022-05-18T18:32:33+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.6.4" | |
//> using dep "com.softwaremill.quicklens::quicklens:1.9.12" | |
//> using dep "dev.zio::zio-test:2.1.16" | |
// --------------------- | |
import zio.* | |
import zio.test.* | |
import zio.test.TestAspect.* | |
import com.softwaremill.quicklens.* | |
import zio.test.TestAspect.* | |
import java.time.* | |
// ---------------------------------------------------------------- | |
object QuickLensTest extends ZIOSpecDefault { | |
case class Editor(name: String) | |
case class Owner(firstName: String, lastName: String, birthYear: Int) | |
case class ComicAuthor(name: String, birthYear: Int) | |
case class ComicBook(title: String, volume: Int, editor: Editor, publishedDate: LocalDate, author: ComicAuthor) | |
case class ComicSeries(name: String, comics: List[ComicBook]) | |
case class ComicCollection(owner: Owner, series: List[ComicSeries]) | |
val dargaud = Editor("dargaud") | |
val leo = ComicAuthor("leo", 1944) | |
val aldebaran1 = ComicBook("la catastrophe", 1, dargaud, LocalDate.parse("1994-01-01"), leo) | |
val aldebaran2 = ComicBook("la blonde", 2, dargaud, LocalDate.parse("1995-01-01"), leo) | |
val aldebaran3 = ComicBook("la photo", 3, dargaud, LocalDate.parse("1996-01-01"), leo) | |
val aldebaran4 = ComicBook("le groupe", 4, dargaud, LocalDate.parse("1997-01-01"), leo) | |
val aldebaran5 = ComicBook("la créature", 5, dargaud, LocalDate.parse("1998-01-01"), leo) | |
val aldebaran = ComicSeries("aldebaran", List(aldebaran1, aldebaran2, aldebaran3, aldebaran4, aldebaran5)) | |
val kenya1 = ComicBook("apparitions", 1, dargaud, LocalDate.parse("2001-10-01"), leo) | |
val kenya2 = ComicBook("rencontres", 2, dargaud, LocalDate.parse("2003-01-01"), leo) | |
val kenya3 = ComicBook("aberrations", 3, dargaud, LocalDate.parse("2004-06-01"), leo) | |
val kenya4 = ComicBook("interventions", 4, dargaud, LocalDate.parse("2006-01-01"), leo) | |
val kenya5 = ComicBook("illusions", 5, dargaud, LocalDate.parse("2008-06-01"), leo) | |
val joe = Owner("john", "doe", 1942) | |
val joeCollection = ComicCollection(joe, List(aldebaran)) | |
override def spec = suite("quick lens tests")( | |
test("basic usages") { | |
val updatedCollection = | |
joeCollection | |
.modify(_.owner.lastName) | |
.using(_.toUpperCase) | |
.modify(_.series.each.comics.each.title) | |
.using(_.capitalize) | |
assertTrue( | |
updatedCollection.owner.lastName.forall(_.isUpper), | |
updatedCollection.series.flatMap(_.comics).forall(_.title.head.isUpper) | |
) | |
} | |
) | |
} | |
QuickLensTest.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment