Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/8a9e008b0404ceb2b390464f0fceced4 to your computer and use it in GitHub Desktop.
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/dcd727f74be70ce6f9871ac47c07785eea211d1a
// 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.1.2"
//> using dep "com.softwaremill.quicklens::quicklens:1.8.8"
//> using dep "dev.zio::zio-test:2.0.0-RC6"
// ---------------------
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