-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaseClassSpec.scala
79 lines (61 loc) · 1.69 KB
/
CaseClassSpec.scala
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
package com.github.atais
import com.github.atais.CaseClassSpec.EnumLike._
import com.github.atais.CaseClassSpec._
import com.holdenkarau.spark.testing.DatasetSuiteBase
import org.scalatest.Assertion
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class CaseClassSpec extends AnyFlatSpec with Matchers with DatasetSuiteBase {
import spark.implicits._
import scala.collection.JavaConverters._
val foos: Seq[TestContainer] = Seq(
TestContainer(One),
TestContainer(Two),
TestContainer(Three)
)
private val scenario: EnumLike => Assertion = {
case v@One =>
v.value shouldBe One.value
v.getClass shouldBe One.getClass
special(v) shouldBe 1
case v@Two =>
v.value shouldBe Two.value
v.getClass shouldBe Two.getClass
special(v) shouldBe 2
case v@Three =>
v.value shouldBe Three.value
v.getClass shouldBe Three.getClass
special(v) shouldBe 3
case _ =>
fail("we do not have other cases!")
}
it should "propely pattern match in raw example" in {
foos.map(_.v).foreach {
scenario.apply
}
}
it should "propely pattern match in ds example" in {
foos.toDS().show(false)
foos.toDS()
.collectAsList().asScala
.map(_.v)
.foreach {
scenario.apply
}
}
}
private[atais] object CaseClassSpec {
case class EnumLike(value: String)
object EnumLike {
final val One = EnumLike("One")
final val Two = EnumLike("Two")
final val Three = EnumLike("Three")
}
def special(v: EnumLike): Int = v match {
case One => 1
case Two => 2
case Three => 3
case _ => ???
}
case class TestContainer(v: EnumLike)
}