summaryrefslogtreecommitdiff
path: root/test/files/run/unapply.scala
blob: acbce58d35554240e4ec6ee37a42440ffd4113a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import scala.testing.SUnit._

object Test extends TestConsoleMain {
  def suite = new TestSuite(
    Foo,
    Mas,
    LisSeqArr,
    StreamFoo
  )
}

// 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 extends TestCase("Foo") with 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
  }
  override def runTest {
    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")
    implicit val bc: Int = 3
    assertEquals(4 match {
      case VarFoo(x) => x
    }, 7)
  }
}

// same, but now object is not top-level
object Mas extends TestCase("Mas") with 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 runTest {
    val b = new Baz
    assertEquals(b match {
      case Gaz(s:Int, n:String) => (s,n)
    }, (60,"too large"))
  }
}

object LisSeqArr extends TestCase("LisSeqArr") with Assert {
//  def foo[A](x:List[A]) {}
  def runTest {
    assertEquals((List(1,2,3): Any) match { case   List(x,y,_*) => (x,y)}, (1,2))
    assertEquals((List(1,2,3): Any) match { case    Seq(x,y,_*) => (x,y)}, (1,2))
    //assertEquals((Array(1,2,3): Any) match { case   Seq(x,y,_*) => (x,y)}, (1,2))
    //assertEquals((Array(1,2,3): Any) match { case Array(x,y,_*) => {x,y}}, {1,2})

    // just compile, feature request #1196
//    (List(1,2,3): Any) match {
//      case a @ List(x,y,_*) => foo(a)
//    }

  }
}


object StreamFoo extends TestCase("unapply for Streams") with Assert {
  //val x:Stream[Int] = Stream.cons(1,x)

  def sum(stream: Stream[Int]): Int =
    stream match {
      case Stream.Empty => 0
      case Stream.cons(hd, tl) => hd + sum(tl)
    }
  override def runTest {
    val str: Stream[Int] = Stream.fromIterator(List(1,2,3).iterator)
    assertEquals(sum(str), 6)
  }
}

object Test1256 extends TestCase("1256") {
  class Sync {
    def unapply(scrut: Any): Boolean = false
  }

  class Buffer {
    val Get = new Sync

    val jp: PartialFunction[Any, Any] = {
      case Get() =>
    }
  }

  override def runTest { assertFalse((new Buffer).jp.isDefinedAt(42)) }
}