diff options
Diffstat (limited to 'tests/run')
-rw-r--r-- | tests/run/t298.scala | 17 | ||||
-rw-r--r-- | tests/run/t3026.check | 2 | ||||
-rwxr-xr-x | tests/run/t3026.scala | 8 | ||||
-rw-r--r-- | tests/run/t3353.check | 1 | ||||
-rw-r--r-- | tests/run/t3353.scala | 10 | ||||
-rw-r--r-- | tests/run/unapply.scala | 122 |
6 files changed, 160 insertions, 0 deletions
diff --git a/tests/run/t298.scala b/tests/run/t298.scala new file mode 100644 index 000000000..5728bb6c9 --- /dev/null +++ b/tests/run/t298.scala @@ -0,0 +1,17 @@ +object Test extends dotty.runtime.LegacyApp { + implicit def anyList[T]: List[T] = Nil + + implicit def intList: List[Int] = 42::24::Nil + + def foo[T](implicit x: T) = x + + val s = foo[List[Int]] + + println(s) // correct - prints "List(42, 24)" + + implicit def tupleList[T](implicit t: List[T]): List[(T,T)] = t.map(x => (x,x)) + + val t = foo[List[Tuple2[Int,Int]]] + + println(t) // incorrect - prints "List()" +} diff --git a/tests/run/t3026.check b/tests/run/t3026.check new file mode 100644 index 000000000..8c29b615f --- /dev/null +++ b/tests/run/t3026.check @@ -0,0 +1,2 @@ +RED +YELLOW diff --git a/tests/run/t3026.scala b/tests/run/t3026.scala new file mode 100755 index 000000000..22dde9cc0 --- /dev/null +++ b/tests/run/t3026.scala @@ -0,0 +1,8 @@ +object Test { + abstract class Colour + case object RED extends Colour + case object YELLOW extends Colour + val items = Array(RED, YELLOW) + + def main(args: Array[String]): Unit = items foreach println +} diff --git a/tests/run/t3353.check b/tests/run/t3353.check new file mode 100644 index 000000000..8b4ae1fe6 --- /dev/null +++ b/tests/run/t3353.check @@ -0,0 +1 @@ +Got: foo and None diff --git a/tests/run/t3353.scala b/tests/run/t3353.scala new file mode 100644 index 000000000..472723b3c --- /dev/null +++ b/tests/run/t3353.scala @@ -0,0 +1,10 @@ +object Test extends dotty.runtime.LegacyApp { + + "foo" match { + case Matcher(result) => println(result) + } + + object Matcher{ + def unapply(s: String)(implicit secondParam: Option[String] = None) = Some("Got: " + s + " and " + secondParam) + } +} diff --git a/tests/run/unapply.scala b/tests/run/unapply.scala new file mode 100644 index 000000000..43f02b9f3 --- /dev/null +++ b/tests/run/unapply.scala @@ -0,0 +1,122 @@ +object Test { + def main(args: Array[String]): Unit = { + Foo.run() + Mas.run() + LisSeqArr.run() + StreamFoo.run() + Test1256.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 VarFoo { + def unapply(a : Int)(implicit b : Int) : Option[Int] = Some(a + b) +} + +object Foo { + def unapply(x: Any): Option[Product2[Int, String]] = x match { + case y: Bar => Some(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(): Unit = { + val b = new Bar + assert(doMatch1(b) == (50,"medium")) + assert(doMatch2(b) == null) + assert(doMatch3(b) == "medium") + assert(doMatch4(b) == "medium") + assert(doMatch5(b) == "medium") + implicit val bc: Int = 3 + assert(7 == (4 match { + case VarFoo(x) => x + })) + } +} + +// same, but now object is not top-level +object Mas { + object Gaz { + def unapply(x: Any): Option[Product2[Int, String]] = x match { + case y: Baz => Some(y.size, y.name) + case _ => None + } + } + class Baz { + var size: Int = 60 + var name: String = "too large" + } + def run(): Unit = { + val b = new Baz + assert((60,"too large") == (b match { + case Gaz(s:Int, n:String) => (s,n) + })) + } +} + +object LisSeqArr { + def run(): Unit = { + assert((1,2) == ((List(1,2,3): Any) match { case List(x,y,_*) => (x,y)})) + assert((1,2) == ((List(1,2,3): Any) match { case Seq(x,y,_*) => (x,y)})) + } +} + +object StreamFoo { + def sum(stream: Stream[Int]): Int = + stream match { + case Stream.Empty => 0 + case Stream.cons(hd, tl) => hd + sum(tl) + } + def run(): Unit = { + val str: Stream[Int] = List(1,2,3).toStream + assert(6 == sum(str)) + } +} + +object Test1256 { + class Sync { + def unapply(scrut: Any): Boolean = false + } + + class Buffer { + val Get = new Sync + val jp: PartialFunction[Any, Any] = { + case Get() => + } + } + + def run(): Unit = { + assert(!(new Buffer).jp.isDefinedAt(42)) + } +} |