From 8c84ecf77194f98aaa41feab0ed903b8ac342899 Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Mon, 25 Dec 2006 11:31:06 +0000 Subject: test case, unapply <-> as in "Matching with Obj... test case, unapply <-> as in "Matching with Objects" --- test/pending/run/unapply.flags | 1 + test/pending/run/unapply.scala | 84 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 test/pending/run/unapply.flags create mode 100644 test/pending/run/unapply.scala diff --git a/test/pending/run/unapply.flags b/test/pending/run/unapply.flags new file mode 100644 index 0000000000..16fd6c4f1f --- /dev/null +++ b/test/pending/run/unapply.flags @@ -0,0 +1 @@ +-Xunapply diff --git a/test/pending/run/unapply.scala b/test/pending/run/unapply.scala new file mode 100644 index 0000000000..4cf126de5a --- /dev/null +++ b/test/pending/run/unapply.scala @@ -0,0 +1,84 @@ +import scala.testing.SUnit._ + +object Test { + def main(args:Array[String]) = { + Foo.run + Mas.run + Lis.run + } +} + +// this class is used for representation +class Bar { + var size: Int = 50 + var name: String = "medium" +} + +// test basic unapply for 0, 1 and 2 args and with precise type test +object Fii { + def unapply(x: Any): boolean = x.isInstanceOf[Bar] +} +object Faa { + def unapply(x: Any): Option[String] = if(x.isInstanceOf[Bar]) Some(x.asInstanceOf[Bar].name) else None +} +object FaaPrecise { + def unapply(x: Bar): Option[String] = Some(x.name) +} +object FaaPreciseSome { + def unapply(x: Bar) = Some(x.name) // return type Some[String] +} +object Foo extends Assert { + def unapply(x: Any): Option[Product2[Int, String]] = x match { + case y: Bar => Some(Tuple(y.size, y.name)) + case _ => None + } + def doMatch1(b:Bar) = b match { + case Foo(s:Int, n:String) => {s,n} + } + def doMatch2(b:Bar) = b match { + case Fii() => null + } + def doMatch3(b:Bar) = b match { + case Faa(n:String) => n + } + def doMatch4(b:Bar) = (b:Any) match { + case FaaPrecise(n:String) => n + } + def doMatch5(b:Bar) = (b:Any) match { + case FaaPreciseSome(n:String) => n + } + def run { + val b = new Bar + assertEquals(doMatch1(b),{50,"medium"}) + assertEquals(doMatch2(b),null) + assertEquals(doMatch3(b),"medium") + assertEquals(doMatch4(b),"medium") + assertEquals(doMatch5(b),"medium") + } +} + +// same, but now object is not top-level +object Mas extends Assert { + object Gaz { + def unapply(x: Any): Option[Product2[Int, String]] = x match { + case y: Baz => Some(Tuple(y.size, y.name)) + case _ => None + } + } + class Baz { + var size: Int = 60 + var name: String = "too large" + } + def run { + val b = new Baz + assertEquals(b match { + case Gaz(s:Int, n:String) => {s,n} + }, {60,"too large"}) + } +} + +object Lis extends Assert { + def run { + assertEquals(List(1,2,3) match { case List(x,y,_*) => {x,y}}, {1,2}) + } +} -- cgit v1.2.3