1- import scala .xml . _
1+ package scala .xml
22
3- object Test {
3+ import org .junit .Test
4+ import org .junit .Ignore
5+ import org .junit .runner .RunWith
6+ import org .junit .runners .JUnit4
7+ import org .junit .Assert .assertTrue
8+ import org .junit .Assert .assertFalse
9+ import org .junit .Assert .assertEquals
10+
11+ class XMLSyntaxTest {
412
513 private def handle [A ](x : Node ): A = {
6- println(x)
714 x.child(0 ).asInstanceOf [Atom [A ]].data
815 }
916
10- def main (args : Array [String ]) {
11- test1()
12- test2()
13- test3()
14- }
15-
16- private def test1 () {
17+ @ Test
18+ def test1 (): Unit = {
1719 val xNull = <hello >{null }</hello > // these used to be Atom(unit), changed to empty children
18-
19- println(xNull.child sameElements Nil )
20+ assertTrue(xNull.child sameElements Nil )
2021
2122 val x0 = <hello >{}</hello > // these used to be Atom(unit), changed to empty children
2223 val x00 = <hello >{ }</hello > // dto.
23-
2424 val xa = <hello >{ " world" }</hello >
2525
26-
27- println(x0.child sameElements Nil )
28- println(x00.child sameElements Nil )
29- println(handle[String ](xa) == " world" )
26+ assertTrue(x0.child sameElements Nil )
27+ assertTrue(x00.child sameElements Nil )
28+ assertEquals(" world" , handle[String ](xa))
3029
3130 val xb = <hello >{ 1.5 }</hello >
32-
33- println(handle[Double ](xb) == 1.5 )
31+ assertEquals(1.5 , handle[Double ](xb), 0.0 )
3432
3533 val xc = <hello >{ 5 }</hello >
36-
37- println(handle[Int ](xc) == 5 )
34+ assertEquals(5 , handle[Int ](xc))
3835
3936 val xd = <hello >{ true }</hello >
40-
41- println(handle[Boolean ](xd) == true )
37+ assertEquals(true , handle[Boolean ](xd))
4238
4339 val xe = <hello >{ 5 : Short }</hello >
44-
45- println(handle[Short ](xe) == (5 : Short ))
40+ assertEquals((5 : Short ), handle[Short ](xe))
4641
4742 val xf = <hello >{ val x = 27 ; x }</hello >
48-
49- println(handle[Int ](xf) == 27 )
43+ assertEquals(27 , handle[Int ](xf))
5044
5145 val xg = <hello >{ List (1 ,2 ,3 ,4 ) }</hello >
52-
53- println(xg)
54- for (z <- xg.child) {
55- println(z.toString() + {if (z.isInstanceOf [Text ]) " (is text node ' ')" else " " })
56- }
46+ assertEquals(" <hello>1 2 3 4</hello>" , xg.toString)
47+ assertFalse(xg.child.map(_.isInstanceOf [Text ]).exists(identity))
5748
5849 val xh = <hello >{ for (x <- List (1 ,2 ,3 ,4 ) if x % 2 == 0 ) yield x }</hello >
59-
60- println(xh)
61- for (z <- xh.child) {
62- println(z.toString() + {if (z.isInstanceOf [Text ]) " (is text node ' ')" else " " })
63- }
64- println
50+ assertEquals(" <hello>2 4</hello>" , xh.toString)
51+ assertFalse(xh.child.map(_.isInstanceOf [Text ]).exists(identity))
6552 }
6653
6754 /** see SVN r13821 (emir): support for <elem key={x:Option[Seq[Node]]} />,
6855 * so that Options can be used for optional attributes.
6956 */
70- private def test2 () {
57+ @ Test
58+ def test2 (): Unit = {
7159 val x1 : Option [Seq [Node ]] = Some (<b >hello</b >)
7260 val n1 = <elem key ={x1} />;
73- println( " node= " + n1 + " , key= " + n1.attribute(" key" ))
61+ assertEquals(x1, n1.attribute(" key" ))
7462
7563 val x2 : Option [Seq [Node ]] = None
7664 val n2 = <elem key ={x2} />;
77- println( " node= " + n2 + " , key= " + n2.attribute(" key" ))
65+ assertEquals(x2, n2.attribute(" key" ))
7866 }
7967
80- private def test3 () {
68+ @ Test
69+ def test3 (): Unit = {
8170 // this demonstrates how to handle entities
8271 val s = io.Source .fromString(" <a> </a>" )
8372 object parser extends xml.parsing.ConstructingParser (s, false /* ignore ws*/ ) {
@@ -91,7 +80,7 @@ object Test {
9180 }
9281 val parsed = parser.element(TopScope ) // parse the source as element
9382 // alternatively, we could call document()
94- println( parsed)
83+ assertEquals( " <a>Š</a> " , parsed.toString )
9584 }
9685
9786}
0 commit comments