//############################################################################
// XML Literals
//############################################################################
// $Id$
import scala.testing.UnitTest._ ;
import scala.xml._ ;
object Test with Application {
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 = .toString(); /* see neg(1) */
/* whitespace (ws) handling */
val x2 = .toString(); /* ws in tags allowed */
assertEquals( x, noWS( x2 ) );
val x3 =
.toString(); /* ws in element content */
assertEquals( noWS( x3 ),
Elem("hello",
Elem("world"),
Elem("test"),
Elem("mars")).toString() );
/* */
/* === attributes === */
/* */
val z =
Hello World
Check the scala page!
.toString();
assertEquals( noWS( z ), noWS(
Elem("html",
Elem("body",
Elem("h1",Text("Hello World")),
Elem("p",Text("Check the "),
Elem("a", Text("scala"))
% ('href <= "scala.epfl.ch" ),
Text("page!"))
) %('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", Text("now!"));
}
/* embedding Scala strings as text and elements */
val sc = { "World" }{ Text("42") }{ computeDate() };
assertEquals( sc.child.elements.toList,
List( Text("World"), Text("42"), Elem( "date", Text("now!") ) ) );
assertEquals( sc.toString(),
Elem("hello",Text("World42"),Elem("date",Text("now!"))).toString() );
def foo( m:Node ):String = m match {
case => "hello node"
case => "hallo node"
case { z } => "test node:"+z
case { e1:Node }{ e2:Node }{ _* }
=> e1.toString() + e2.toString();
}
assertEquals( foo(), "hello node" );
assertEquals( foo(), "hallo node" );
assertEquals( foo(42), "test node:42" );
assertEquals( foo(
),
.toString() + .toString() );
val rows =
1.1 | 1.2 |
2.1 | 2.2 |
;
assertEquals( noWS( rows.toList.toString() ),
noWS( List(Elem("tr",
Elem("td",Text("1.1")),Elem("td",Text("1.2"))
),
Elem("tr",
Elem("td",Text("2.1")),Elem("td",Text("2.2"))
)
).toString() )
);
val rows2 = 1.1 | 1.2 |
2.1 | 2.2 |
;
assertEquals( noWS( rows.toList.toString() ), noWS( rows2.toList.toString() ) );
val sequence =
Text
;
Console.println( sequence );
val onlyOne = ;
Console.println( onlyOne );
val tryBrace = Now we try escaped {{ braces } ;
assertEquals( tryBrace, Elem("try",Text("Now we try escaped { braces }")));
val tryBrace2 = cool ?;
assertEquals( tryBrace2("myAttrib").get, "7" );
/* Scala comments are not allowed in XML literals. see neg(2) */
val zzz = /* no comment */;
assertEquals( zzz, Elem("hello", Text("/* no comment */")));
}