summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2006-03-31 09:06:56 +0000
committerBurak Emir <emir@epfl.ch>2006-03-31 09:06:56 +0000
commitf9ea70db10c58b3f23e420d4e182e9622bcdb215 (patch)
treee198e3c3399681641316b0c159030b3d402d4051 /test
parent542401df8d377bc7b6f4c0b88edc37baa95dacf0 (diff)
downloadscala-f9ea70db10c58b3f23e420d4e182e9622bcdb215.tar.gz
scala-f9ea70db10c58b3f23e420d4e182e9622bcdb215.tar.bz2
scala-f9ea70db10c58b3f23e420d4e182e9622bcdb215.zip
test case for xml library
Diffstat (limited to 'test')
-rw-r--r--test/files/jvm/xml01.check24
-rw-r--r--test/files/jvm/xml01.scala206
2 files changed, 230 insertions, 0 deletions
diff --git a/test/files/jvm/xml01.check b/test/files/jvm/xml01.check
new file mode 100644
index 0000000000..b535e2c511
--- /dev/null
+++ b/test/files/jvm/xml01.check
@@ -0,0 +1,24 @@
+passed ok
+equality
+passed ok
+passed ok
+passed ok
+passed ok
+passed ok
+xpath \
+passed ok
+passed ok
+passed ok
+passed ok
+passed ok
+passed ok
+passed ok
+passed ok
+xpath \\ DESCENDANTS
+passed ok
+passed ok
+<book><author>Peter Buneman</author><author>Dan Suciu</author><title>Data on ze web</title></book>
+passed ok
+attribute value normalization
+passed ok
+passed ok
diff --git a/test/files/jvm/xml01.scala b/test/files/jvm/xml01.scala
new file mode 100644
index 0000000000..8eccdcbae9
--- /dev/null
+++ b/test/files/jvm/xml01.scala
@@ -0,0 +1,206 @@
+import java.io.StringReader;
+import org.xml.sax.InputSource;
+import scala.xml._;
+import scala.util.logging._;
+
+import scala.testing.UnitTest._ ;
+
+object Test {
+ def main(args:Array[String]) = {
+ val e: scala.xml.MetaData = Null; //Node.NoAttributes;
+ val sc: scala.xml.NamespaceBinding = TopScope;
+
+ val xmlFile1 = "<hello><world/></hello>";
+ val isrc1 = new InputSource( new StringReader(xmlFile1) );
+ val parsedxml1 = XML.load(isrc1);
+ val isrc11 = new InputSource( new StringReader( xmlFile1 ) );
+ val parsedxml11 = XML.load(isrc11);
+
+ val c = new Node {
+ def label = "hello";
+ override def hashCode() =
+ Utility.hashCode(prefix, label, attributes.hashCode(), scope.hashCode(), child);
+ def child = Elem(null, "world", e, sc);
+ //def attributes = e;
+ override def text = "";
+ };
+
+ assertSameElements( List( 3 ), List( 3 ));
+
+ Console.println("equality");
+ assertEquals(c, parsedxml11);
+ assertEquals(parsedxml1, parsedxml11);
+ assertSameElements( List(parsedxml1), List(parsedxml11));
+ assertSameElements( Iterator.fromArray(Predef.Array(parsedxml1)).toList, List(parsedxml11));
+
+ val x2 = "<book><author>Peter Buneman</author><author>Dan Suciu</author><title>Data on ze web</title></book>";
+
+ val i = new InputSource( new StringReader( x2 ));
+ val x2p = XML.load( i );
+
+ assertEquals(x2p, Elem(null, "book" , e, sc,
+ Elem(null, "author", e, sc,Text("Peter Buneman")),
+ Elem(null, "author", e, sc,Text("Dan Suciu")),
+ Elem(null, "title" , e, sc,Text("Data on ze web"))));
+
+ val xmlFile2 = "<bib><book><author>Peter Buneman</author><author>Dan Suciu</author><title>Data on ze web</title></book><book><author>John Mitchell</author><title>Foundations of Programming Languages</title></book></bib>";
+ val isrc2 = new InputSource( new StringReader( xmlFile2 ) );
+ val parsedxml2 = XML.load( isrc2 );
+
+ // xmlFile2/book -> book,book
+ Console.println("xpath \\");
+
+
+ assertSameElements( parsedxml1 \ "_" , List( Elem(null,"world",e,sc) ) );
+
+ assertSameElements( parsedxml1 \ "world", List( Elem(null,"world",e,sc) ) );
+
+/*
+ Console.println( parsedxml2 \ "_" );
+ Console.println( (parsedxml2 \ "_" ).elements);
+ for( val i <- (parsedxml2 \ "_" ).elements) {
+ Console.println( i );
+ };
+ */
+
+ assertSameElements(
+ parsedxml2 \ "_" ,
+
+ List(
+ Elem(null,"book", e, sc,
+ Elem(null,"author", e, sc, Text("Peter Buneman")),
+ Elem(null,"author", e, sc, Text("Dan Suciu")),
+ Elem(null,"title" , e, sc, Text("Data on ze web"))),
+ Elem(null,"book",e,sc,
+ Elem(null,"author",e,sc,Text("John Mitchell")),
+ Elem(null,"title",e,sc,Text("Foundations of Programming Languages"))))
+ );
+ assertEquals( (parsedxml2 \ "author").length, 0 );
+
+ assertSameElements(
+ parsedxml2 \ "book",
+
+ List(
+ Elem(null,"book",e,sc,
+ Elem(null,"author", e, sc, Text("Peter Buneman")),
+ Elem(null,"author", e, sc, Text("Dan Suciu")),
+ Elem(null,"title" , e, sc, Text("Data on ze web"))),
+ Elem(null,"book",e,sc,
+ Elem(null,"author", e, sc, Text("John Mitchell")),
+ Elem(null,"title" , e, sc, Text("Foundations of Programming Languages")))
+ )
+ );
+
+ assertSameElements(
+
+ parsedxml2 \ "_" \ "_",
+
+ List(
+ Elem(null,"author", e, sc, Text("Peter Buneman")),
+ Elem(null,"author", e, sc, Text("Dan Suciu")),
+ Elem(null,"title" , e, sc, Text("Data on ze web")),
+ Elem(null,"author", e, sc, Text("John Mitchell")),
+ Elem(null,"title" , e, sc, Text("Foundations of Programming Languages"))
+ )
+ );
+
+ assertSameElements(
+
+ parsedxml2 \ "_" \ "author",
+
+ List(
+ Elem(null,"author", e, sc, Text("Peter Buneman")),
+ Elem(null,"author", e, sc, Text("Dan Suciu")),
+ Elem(null,"author", e, sc, Text("John Mitchell"))
+ )
+
+ );
+
+ assertSameElements( (parsedxml2 \ "_" \ "_" \ "author"), List() );
+
+ Console.println("xpath \\\\ DESCENDANTS");
+
+ assertSameElements(
+
+ parsedxml2 \\ "author",
+
+ List(
+ Elem(null,"author", e, sc, Text("Peter Buneman")),
+ Elem(null,"author", e, sc, Text("Dan Suciu")),
+ Elem(null,"author", e, sc, Text("John Mitchell"))
+ )
+
+ );
+
+
+ assertSameElements(
+
+ parsedxml2 \\ "title",
+
+ List(
+ Elem(null,"title", e, sc, Text("Data on ze web")),
+ Elem(null,"title", e, sc, Text("Foundations of Programming Languages")))
+ );
+
+
+ Console.println(
+ (parsedxml2 \\ "book" ){ n:Node => n \ "title" == "Data on ze web" }
+ );
+
+ assertEquals(
+
+ (new NodeSeq { val theSeq = List( parsedxml2 ) }) \\ "_",
+
+ List(
+ Elem(null,"bib",e,sc,
+ Elem(null,"book",e,sc,
+ Elem(null, "author", e, sc, Text("Peter Buneman")),
+ Elem(null, "author", e, sc, Text("Dan Suciu")),
+ Elem(null, "title" , e, sc, Text("Data on ze web"))),
+ Elem(null,"book",e,sc,
+ Elem(null,"author",e,sc,Text("John Mitchell")),
+ Elem(null,"title",e,sc,Text("Foundations of Programming Languages")))),
+ Elem(null,"book",e,sc,
+ Elem(null,"author",e,sc,Text("Peter Buneman")),
+ Elem(null,"author",e,sc,Text("Dan Suciu")),
+ Elem(null,"title",e,sc,Text("Data on ze web"))),
+ Elem(null,"author",e,sc,Text("Peter Buneman")),
+ //Text("Peter Buneman"),
+ Elem(null,"author",e,sc,Text("Dan Suciu")),
+ //Text("Dan Suciu"),
+ Elem(null,"title",e,sc,Text("Data on ze web")),
+ //Text("Data on ze web"),
+ Elem(null,"book",e,sc,
+ Elem(null,"author",e,sc,Text("John Mitchell")),
+ Elem(null,"title",e,sc,Text("Foundations of Programming Languages"))),
+ Elem(null,"author",e,sc,Text("John Mitchell")),
+ //Text("John Mitchell"),
+ Elem(null,"title",e,sc,Text("Foundations of Programming Languages"))
+ //Text("Foundations of Programming Languages")
+ )
+ );
+
+ // test unicode escapes backslash u
+
+ Console println ("attribute value normalization");
+ val xmlAttrValueNorm = "<personne id='p0003' nom='&#x015e;ahingöz' />";
+ {
+ val isrcA = new InputSource( new StringReader(xmlAttrValueNorm) );
+ val parsedxmlA = XML.load(isrcA);
+ val c = (parsedxmlA \ "@nom").text.charAt(0);
+ //Console.println("char '"+c+"' \u015e");
+ assertTrue(c == '\u015e');
+ }
+ {
+ val isr = scala.io.Source.fromString(xmlAttrValueNorm);
+ val pxmlB = scala.xml.parsing.ConstructingParser.fromSource(isr,false);
+ val parsedxmlB = pxmlB.element(TopScope);
+ val c = (parsedxmlB \ "@nom").text.charAt(0);
+ //Console.println("char '"+c+"' \u015e");
+ assertTrue(c == '\u015e');
+ }
+
+ }
+
+
+}