1+ package scala .xml
2+
3+ import org .junit .Test
4+ import org .junit .Assert .assertTrue
5+ import org .junit .Assert .assertEquals
6+
7+ class PatternMatching extends {
8+ @ Test
9+ def unprefixedAttribute : Unit = {
10+ val li = List (" 1" , " 2" , " 3" , " 4" )
11+ assertTrue(matchSeq(li))
12+ assertTrue(matchList(li))
13+ }
14+
15+ def matchSeq (args : Seq [String ]) = args match {
16+ case Seq (a, b, c, d @ _* ) => true
17+ }
18+
19+ def matchList (args : List [String ]) =
20+ Elem (null , " bla" , Null , TopScope , minimizeEmpty = true , (args map { x => Text (x) }): _* ) match {
21+ case Elem (_, _, _, _, Text (" 1" ), _* ) => true
22+ }
23+
24+ @ Test
25+ def simpleNode =
26+ assertTrue(<hello /> match {
27+ case <hello /> => true
28+ })
29+
30+ @ Test
31+ def nameSpaced =
32+ assertTrue(<x : ga xmlns : x =" z" /> match {
33+ case <x : ga /> => true
34+ })
35+
36+ val cx = <z : hello foo =" bar" xmlns : z =" z" x : foo =" baz" xmlns : x =" the namespace from outer space" >
37+ crazy text world
38+ </z : hello >
39+
40+ @ Test
41+ def nodeContents = {
42+ assertTrue(Utility .trim(cx) match {
43+ case n @ <hello >crazy text world</hello > if (n \ " @foo" ) xml_== " bar" => true
44+ })
45+ assertTrue(Utility .trim(cx) match {
46+ case n @ <z : hello >crazy text world</z : hello > if (n \ " @foo" ) xml_== " bar" => true
47+ })
48+ assertTrue(<x : foo xmlns : x =" gaga" /> match {
49+ case scala.xml.QNode (" gaga" , " foo" , md, child @ _* ) => true
50+ })
51+
52+ assertTrue(<x : foo xmlns : x =" gaga" /> match {
53+ case scala.xml.Node (" foo" , md, child @ _* ) => true
54+ })
55+
56+ }
57+
58+ object SafeNodeSeq {
59+ def unapplySeq (any : Any ): Option [Seq [Node ]] = any match {
60+ case s : Seq [_] => Some (s flatMap (_ match {
61+ case n : Node => n case _ => NodeSeq .Empty
62+ })) case _ => None
63+ }
64+ }
65+
66+ @ Test
67+ def nodeSeq = { // t0646
68+ import scala .xml .NodeSeq
69+
70+ val books =
71+ <bks >
72+ <title >Blabla </title >
73+ <title >Blubabla </title >
74+ <title >Baaaaaaalabla </title >
75+ </bks >;
76+
77+ assertTrue(new NodeSeq { val theSeq = books.child } match {
78+ case t @ Seq (<title >Blabla </title >) => false
79+ case _ => true
80+ })
81+
82+ // SI-1059
83+ var m : PartialFunction [Any , Any ] = { case SafeNodeSeq (s @ _* ) => s }
84+
85+ assertEquals(m(<a /> ++ <b />), List (<a />, <b />))
86+ assertTrue(m.isDefinedAt(<a /> ++ <b />))
87+ }
88+
89+ @ Test
90+ def SI_4124 = {
91+ val body : Node = <elem >hi</elem >
92+
93+ assertTrue((body : AnyRef , " foo" ) match {
94+ case (node : Node , " bar" ) => false
95+ case (ser : Serializable , " foo" ) => true
96+ })
97+
98+ assertTrue((body, " foo" ) match {
99+ case (node : Node , " bar" ) => false
100+ case (ser : Serializable , " foo" ) => true
101+ })
102+
103+ assertTrue((body : AnyRef , " foo" ) match {
104+ case (node : Node , " foo" ) => true
105+ case (ser : Serializable , " foo" ) => false
106+ })
107+
108+ assertTrue((body : AnyRef , " foo" ) match {
109+ case (node : Node , " foo" ) => true
110+ case (ser : Serializable , " foo" ) => false
111+ })
112+ }
113+ }
0 commit comments