From 657d2fb2c16b75c790bf3c3a63d1f830a42893db Mon Sep 17 00:00:00 2001 From: Jon Pretty Date: Fri, 10 Nov 2017 12:59:57 +0000 Subject: More build cleanup, and a couple of extra tests --- tests/src/main/scala/tests.scala | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests') diff --git a/tests/src/main/scala/tests.scala b/tests/src/main/scala/tests.scala index f79a51b..10eb465 100644 --- a/tests/src/main/scala/tests.scala +++ b/tests/src/main/scala/tests.scala @@ -18,6 +18,14 @@ case class Company(name: String) extends Entity case class Person(name: String, age: Int) extends Entity case class Address(line1: String, occupant: Person) +case class Lunchbox(fruit: Fruit, drink: String) +object Fruit { + import examples._ + implicit val showFruit: Show[String, Fruit] = + new Show[String, Fruit] { def show(f: Fruit): String = f.name } +} +case class Fruit(name: String) + case class Item(name: String, quantity: Int = 1, price: Int) sealed trait Color @@ -45,6 +53,29 @@ object Tests extends TestApp { implicitly[Show[String, Branch[String]]].show(Branch(Leaf("LHS"), Leaf("RHS"))) }.assert(_ == "Branch(left=Leaf(value=LHS),right=Leaf(value=RHS))") + test("local implicit beats Magnolia") { + import magnolia.examples._ + implicit val showPerson: Show[String, Person] = new Show[String, Person] { def show(p: Person) = "nobody" } + implicitly[Show[String, Address]].show(Address("Home", Person("John Smith", 44))) + }.assert(_ == "Address(line1=Home,occupant=nobody)") + + test("even low-priority implicit beats Magnolia for nested case") { + import magnolia.examples._ + import Show.gen + implicitly[Show[String, Lunchbox]].show(Lunchbox(Fruit("apple"), "lemonade")) + }.assert(_ == "Lunchbox(fruit=apple,drink=lemonade)") + + test("low-priority implicit does not beat Magnolia when not nested") { + import magnolia.examples._ + import Show.gen + implicitly[Show[String, Fruit]].show(Fruit("apple")) + }.assert(_ == "Fruit(name=apple)") + + test("typeclass implicit scope has lower priority than ADT implicit scope") { + import magnolia.examples._ + implicitly[Show[String, Fruit]].show(Fruit("apple")) + }.assert(_ == "apple") + test("test equality false") { import examples._ Eq.gen[Entity].equal(Person("John Smith", 34), Person("", 0)) @@ -124,5 +155,12 @@ object Tests extends TestApp { | in parameter 'alpha' of product type Beta |""")) + val tupleDerivation = test("derive for a tuple") { + implicitly[Show[String, (Int, String)]] + }.returns() + + test("serialize a tuple") { + tupleDerivation().show((42, "Hello World")) + }.assert(_ == "Tuple2(_1=42,_2=Hello World)") } } -- cgit v1.2.3