diff options
author | buraq <buraq@epfl.ch> | 2004-05-26 10:44:15 +0000 |
---|---|---|
committer | buraq <buraq@epfl.ch> | 2004-05-26 10:44:15 +0000 |
commit | 1f7970f3c60575d1715d1c5dd022dfa0d08bb51a (patch) | |
tree | e941181649de5c3d1bc52e621014a70f312ead24 /test/files/jvm/xmlLiterals.scala | |
parent | bab61a5c3f2eada77158b217ab7e318c19fdbb64 (diff) | |
download | scala-1f7970f3c60575d1715d1c5dd022dfa0d08bb51a.tar.gz scala-1f7970f3c60575d1715d1c5dd022dfa0d08bb51a.tar.bz2 scala-1f7970f3c60575d1715d1c5dd022dfa0d08bb51a.zip |
moved to jvm, interpreter too brittle
Diffstat (limited to 'test/files/jvm/xmlLiterals.scala')
-rw-r--r-- | test/files/jvm/xmlLiterals.scala | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/test/files/jvm/xmlLiterals.scala b/test/files/jvm/xmlLiterals.scala new file mode 100644 index 0000000000..1a8d61aca6 --- /dev/null +++ b/test/files/jvm/xmlLiterals.scala @@ -0,0 +1,159 @@ +//############################################################################ +// XML Literals +//############################################################################ +// $Id$ + +import scala.testing.UnitTest._ ; + +import scala.xml._ ; +import scala.collection.immutable ; + +object Test with Application { + + val e = immutable.TreeMap.Empty[String,String]; + + def noWS(x:String):String = { + val res = new StringBuffer(); + var i = 0; while( i < x.length() ) { + val c = x.charAt( i ); + c match { + case ' ' | '\n' | '\t' => + case _ => res.append( c ) + } + i = i + 1; + } + res.toString(); + } + + + + /* */ + /* === tags, elements === */ + /* */ + + val x = <hello></hello>.toString(); /* see neg(1) */ + + /* whitespace (ws) handling */ + + val x2 = <hello > </hello>.toString(); /* ws in tags allowed */ + + assertEquals( x, noWS( x2 ) ); + + val x3 = <hello> + <world></world> + <test/> + <mars></mars></hello>.toString(); /* ws in element content */ + + + assertEquals( noWS( x3 ), + Elem("hello",e, + Elem("world",e), + Elem("test",e), + Elem("mars",e)).toString() ); + + + /* */ + /* === attributes === */ + /* */ + + val z = <html> + <body background="#FFFFFF"> + <h1>Hello World</h1> + <p>Check the <a href="scala.epfl.ch">scala</a> page!</p> + </body> + </html>.toString(); + + assertEquals( noWS( z ), noWS( + Elem("html",e, + Elem("body",e, + Elem("h1",e,Text("Hello World")), + Elem("p",e,Text("Check the "), + Elem("a", e,Text("scala")) + % Pair("href","scala.epfl.ch"), + Text("page!")) + ) % Pair("background","#FFFFFF") + ).toString() + )); + + /* todo: better way to deal with whitespace in content */ + /* (Canonical XML or even more aggressive normlization) */ + + /* */ + /* === embedded Scala blocks === */ + /* */ + + def computeDate() = { + Elem("date", e,Text("now!")); + } + + + /* embedding Scala strings as text and elements */ + val sc = <hello>{ "World" }{ Text("42") }{ computeDate() }</hello>; + + assertEquals( sc.child.elements.toList, + List( Text("World"), Text("42"), Elem( "date", e,Text("now!") ) ) ); + + assertEquals( sc.toString(), + Elem("hello",e,Text("World42"),Elem("date",e,Text("now!"))).toString() ); + + def foo( m:Node ):String = m match { + case <hello/> => "hello node" + case <hallo ></hallo > => "hallo node" + case <test>{ z }</test> => "test node:"+z + case <list>{ e1:Node }{ e2:Node }{ _* }</list> => e1.toString() + e2.toString(); + } + + assertEquals( foo(<hello/>), "hello node" ); + assertEquals( foo(<hallo/>), "hallo node" ); + assertEquals( foo(<test>42</test>), "test node:42" ); + assertEquals( foo(<list><a/><b><c/></b><d/><d/></list>), + <a/>.toString() + <b><c/></b>.toString() ); + + val rows = <tr> + <td>1.1</td><td>1.2</td> + </tr> + <tr> + <td>2.1</td><td>2.2</td> + </tr>; + + assertEquals( noWS( rows.toList.toString() ), + noWS( List(Elem("tr",e, + Elem("td",e,Text("1.1")),Elem("td",e,Text("1.2")) + ), + Elem("tr",e, + Elem("td",e,Text("2.1")),Elem("td",e,Text("2.2")) + ) + ).toString() ) + ); + val rows2 = <tr><!-- an XML comment --><td>1.1</td><td>1.2</td> + </tr> + <tr> + <td>2.1</td><td>2.2</td> + </tr>; + + assertEquals( noWS( rows.toList.toString() ), noWS( rows2.toList.toString() ) ); + + val sequence = <foo/> + <bar>Text</bar> + <foo/>; + + Console.println( sequence ); + + val onlyOne = <foo/>; + + Console.println( onlyOne ); + + val tryBrace = <try>Now we try escaped {{ braces } </try>; + + assertEquals( tryBrace, Elem("try",e,Text("Now we try escaped { braces }"))); + + val tryBrace2 = <try myAttrib={ (3+4).toString() }> cool ?</try>; + + assertEquals( tryBrace2.attribute("myAttrib"), "7" ); + + /* Scala comments are not allowed in XML literals. see neg(2) */ + val zzz = <hello>/* no comment */</hello>; + assertEquals( zzz, Elem("hello", e, Text("/* no comment */"))); + +} + |