File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed
scala-core-modules/scala-core/src
main/scala-3/com/baeldung/scala/patternmatching
test/scala-3/com/baeldung/scala/patternmatching Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .baeldung .scala .patternmatching
2
+
3
+ sealed trait Command
4
+ case object Start extends Command
5
+ case object Stop extends Command
6
+ case object Report extends Command
7
+ case class CustomCommand (cmd : String ) extends Command
8
+
9
+ def executeCommand (command : Command ): String = command match {
10
+ case Start | CustomCommand (" begin" ) =>
11
+ " System Starting."
12
+ case Stop | CustomCommand (" halt" ) =>
13
+ " System Stopping."
14
+ case Report | CustomCommand (" status" ) =>
15
+ " Generating Report."
16
+ case _ =>
17
+ " Unknown Command."
18
+ }
19
+
20
+ def httpResponse (response : Int ): String = response match {
21
+ case 200 | 201 | 202 => " Success"
22
+ case 400 | 404 | 500 => " Error"
23
+ case _ => " Unknown status"
24
+ }
25
+
26
+ def multipleTypePatterns (obj : Any ): String = obj match {
27
+ case _ : String | _ : Int => " It's either a String or an Int"
28
+ case _ => " It's something else"
29
+ }
30
+
31
+ def unionTypePattern (obj : Any ): String = obj match {
32
+ case _ : (String | Int ) => " It's either a String or an Int"
33
+ case _ => " It's something else"
34
+ }
Original file line number Diff line number Diff line change
1
+ package com.baeldung.scala.patternmatching
2
+
3
+ import org.scalatest.flatspec.AnyFlatSpec
4
+
5
+ class MultipleMatchesUnitTest extends AnyFlatSpec {
6
+
7
+ "executeCommand" should "start the system when given the command" in {
8
+ val result = executeCommand(Start)
9
+ assert("" == result)
10
+ }
11
+
12
+ }
You can’t perform that action at this time.
0 commit comments