summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBurak Emir <emir@epfl.ch>2007-03-02 19:07:57 +0000
committerBurak Emir <emir@epfl.ch>2007-03-02 19:07:57 +0000
commit61b2debaa00361250fbee05cbb71f2c989ad2c03 (patch)
tree7e4ed39511700f88f7b68a695fcf18ef67366ecf
parenta1c87639762b2b9172c1a775fbd3cfaa66536ab0 (diff)
downloadscala-61b2debaa00361250fbee05cbb71f2c989ad2c03.tar.gz
scala-61b2debaa00361250fbee05cbb71f2c989ad2c03.tar.bz2
scala-61b2debaa00361250fbee05cbb71f2c989ad2c03.zip
* small lib addition to testing.SUnit, made Uni...
* small lib addition to testing.SUnit, made UnitTest @deprecated added * xml pattern matching on prefixes (in SymbolicXMLBuilder) xml name * parsing ensures that names do not end in a colon (namespaces) rewrote * xml02 to use SUnit
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala31
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala14
-rw-r--r--src/library/scala/testing/SUnit.scala83
-rw-r--r--src/library/scala/testing/UnitTest.scala2
-rw-r--r--test/files/jvm/unittest_xml.scala17
-rw-r--r--test/files/jvm/xml02.check13
-rw-r--r--test/files/jvm/xml02.scala51
-rw-r--r--test/files/neg/xmlcorner.check7
-rw-r--r--test/files/neg/xmlcorner.scala6
9 files changed, 148 insertions, 76 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala
index 1da2a5e779..3c1a7f3dd1 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/MarkupParsers.scala
@@ -390,23 +390,30 @@ class MarkupParser(unit: CompilationUnit, s: Scanner, p: Parser, presWS: boolean
}
- /** Name ::= (Letter | '_' | ':') (NameChar)*
+ /** actually, Name ::= (Letter | '_' | ':') (NameChar)* but starting with ':' cannot happen
+ ** Name ::= (Letter | '_') (NameChar)*
*
* see [5] of XML 1.0 specification
+ *
+ * pre-condition: ch != ':' // assured by definition of XMLSTART token
+ * post-condition: name does neither start, nor end in ':'
*/
/*[Duplicate]*/ def xName: String = {
- if( xml.Parsing.isNameStart( ch ) ) {
- do {
- putChar( ch );
- nextch;
- } while( xml.Parsing.isNameChar( ch ) );
- val n = cbuf.toString().intern();
- cbuf.setLength( 0 );
- n
- } else {
- reportSyntaxError( "name expected, but char '"+ch+"' cannot start a name" );
- new String();
+ if( !xml.Parsing.isNameStart( ch ) ) {
+ reportSyntaxError( "name expected, but char '"+ch+"' cannot start a name" )
+ return ""
}
+ do {
+ putChar( ch )
+ nextch
+ } while( xml.Parsing.isNameChar( ch ) )
+ if(':' == cbuf.charAt(cbuf.length-1)) {
+ reportSyntaxError( "name cannot end in ':'" )
+ cbuf.setLength(cbuf.length-1)
+ }
+ val n = cbuf.toString().intern()
+ cbuf.setLength( 0 )
+ n
}
diff --git a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
index e06581bdbc..de28a75e64 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/SymbolicXMLBuilder.scala
@@ -158,14 +158,22 @@ abstract class SymbolicXMLBuilder(make: TreeBuilder, p: Parsers # Parser, preser
New(_scala_xml_ProcInstr, LL(target, txt))
/** @todo: attributes */
- def makeXMLpat(pos: int, n: String, args: mutable.Buffer[Tree]): Tree =
+ def makeXMLpat(pos: int, n: String, args: mutable.Buffer[Tree]): Tree = {
+ val (prepat, labpat) = n.indexOf(':') match {
+ case -1 => (Ident(nme.WILDCARD), Literal(Constant(n)))
+ //case 0 => // is erroneous, but cannot happen
+ case i => //if(i+1<n.length) // we ensure i+1<n.length in method xName
+ (Literal(Constant(n.substring(0,i))), Literal(Constant(n.substring(i+1,n.length))))
+ //else { p.syntaxError(pos,"nonsensical qualified name in XML"); return Ident(nme.WILDCARD).setPos(pos)}
+ }
mkXML(pos,
true,
- Ident( nme.WILDCARD ),
- Literal(Constant(n)),
+ prepat, //Ident( nme.WILDCARD ),
+ labpat, //Literal(Constant(n)),
null, //Array[Tree](),
null,
args);
+ }
protected def convertToTextPat(t: Tree): Tree = t match {
case _:Literal => makeTextPat(t)
diff --git a/src/library/scala/testing/SUnit.scala b/src/library/scala/testing/SUnit.scala
index 63d0dd6c34..4ab93db976 100644
--- a/src/library/scala/testing/SUnit.scala
+++ b/src/library/scala/testing/SUnit.scala
@@ -40,9 +40,26 @@ import compat.StringBuilder
* Console.println(tf.toString())
* }
* </pre>
+ *
+ * The trait TestConsoleMain contains this code as a main method, for convenience.
*/
object SUnit {
+ /** convenience trait, mix it in a TestMain object and implement "suite" to get this code
+ * <b>val</b> r = <b>new</b> TestResult()
+ * suite.run(r)
+ * <b>for</b> (<b>val</b> tf &lt;- r.failures()) {
+ * Console.println(tf.toString())
+ */
+ trait TestConsoleMain {
+ def suite: TestSuite
+ def main(args:Array[String]) {
+ val r = new TestResult()
+ suite.run(r)
+ for (val tf <- r.failures())
+ Console.println(tf.toString())
+ }
+ }
/** a Test can be run with its result being collected */
trait Test {
def run(r: TestResult): Unit
@@ -131,59 +148,85 @@ object SUnit {
/** this class defined useful assert methods */
trait Assert {
- /** equality */
+ /** fails if expected != actual */
def assertEquals[A](msg: String, expected: A, actual: => A): Unit =
if (expected != actual) fail(msg)
- /** equality */
+ /** fails if expected != actual */
def assertEquals[A](expected: A, actual: => A): Unit =
assertEquals("(no message)", expected, actual)
- /** falseness */
+ /** succeeds if actual is false */
def assertFalse(msg: String, actual: => Boolean): Unit =
assertEquals(msg, false, actual)
- /** falseness */
+ /** succeeds if actual is false */
def assertFalse(actual: => Boolean): Unit =
assertFalse("(no message)", actual)
- /** not null */
+ /** fails if null eq actual */
def assertNotNull(msg:String, actual: => AnyRef): Unit =
- if (null == actual) fail(msg)
+ if (null eq actual) fail(msg)
- /** not null */
+ /** fails if null eq actual */
def assertNotNull(actual: => AnyRef): Unit =
assertNotNull("(no message)", actual)
- /** reference inequality */
- def assertNotSame(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
+ /**
+ * @deprecated use assertNotEq instead
+ */
+ @deprecated def assertNotSame(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
if (expected.eq(actual)) fail(msg)
- /** reference inequality */
- def assertNotSame(expected: => AnyRef, actual: => AnyRef): Unit =
- assertNotSame("(no message)", expected, actual)
+ /**
+ * @deprecated use assertNotEq instead
+ */
+ @deprecated def assertNotSame(expected: => AnyRef, actual: => AnyRef): Unit =
+ assertNotEq("(no message)", expected, actual)
+
+ /** fail if expected eq actual */
+ def assertNotEq(msg: String, expected => AnyRef, actual: => AnyRef) {
+ if (expected eq actual) fail(msg)
+ }
- /** null */
+ /** fail if expected eq actual */
+ def assertNotEq(msg: String, expected => AnyRef, actual: => AnyRef) {
+ assertNotEq("(no message)", expected, actual)
+ }
+
+ /** fails if actual ne null */
def assertNull(msg: String, actual: => AnyRef): Unit =
- if (null != actual) fail(msg)
+ if (null ne actual) fail(msg)
- /** null */
+ /** fails if actual ne null */
def assertNull(actual: => AnyRef): Unit =
assertNull("(no message)", actual)
- /** reference equality */
+ /**
+ * @deprecated use assertEq instead
+ */
def assertSame(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
if(!expected.eq(actual)) fail(msg)
- /** reference equality */
+ /**
+ * @deprecated use assertEq instead
+ */
def assertSame(expected: => AnyRef, actual: => AnyRef): Unit =
- assertSame("(no message)", expected, actual)
+ assertEq("(no message)", expected, actual)
+
+ /** fails if expected ne actual */
+ def assertEq(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
+ if(expected ne actual) fail(msg)
+
+ /** fails if expected ne actual */
+ def assertEq(msg: String, expected: => AnyRef, actual: => AnyRef): Unit =
+ assertEq("(no message)", expected, actual)
- /** trueness */
+ /** succeeds if actual == true */
def assertTrue(msg: String, actual: => Boolean): Unit =
assertEquals(msg, true, actual)
- /** trueness */
+ /** succeeds if actual == true */
def assertTrue(actual: => Boolean): Unit =
assertTrue("(no message)", actual)
diff --git a/src/library/scala/testing/UnitTest.scala b/src/library/scala/testing/UnitTest.scala
index 302ab6dc4a..db2eab6f4e 100644
--- a/src/library/scala/testing/UnitTest.scala
+++ b/src/library/scala/testing/UnitTest.scala
@@ -15,7 +15,9 @@ package scala.testing
/**
* Some simple methods to support unit testing with assertions
* to contain more JUnit style assertions which use Scala's features.
+ * @deprecated use SUnit instead
*/
+@deprecated
object UnitTest {
class Report(report_ok: () => Unit, report_fail: (String,String) => Unit) {
diff --git a/test/files/jvm/unittest_xml.scala b/test/files/jvm/unittest_xml.scala
index c302ef1e24..b6d3da00f5 100644
--- a/test/files/jvm/unittest_xml.scala
+++ b/test/files/jvm/unittest_xml.scala
@@ -7,12 +7,20 @@ object Test {
class ParsingTest extends TestCase("scala.xml.Parsing") with Assert {
override def runTest = {
assertTrue(Parsing.isNameStart('b'))
+ assertTrue(Parsing.isNameStart(':'))
}
}
class MetaDataTest extends TestCase("scala.xml.MetaData") with Assert {
- import scala.xml.{TopScope, NamespaceBinding, Atom, Text }
+ import scala.xml.{HasKeyValue, TopScope, NamespaceBinding, Node, Atom, Text }
+ def domatch(x:Node): Node = {
+ val hasBar = new HasKeyValue("bar")
+ x match {
+ case Node("foo", hasBar(z), _*) => z
+ case _ => new Atom(3)
+ }
+ }
override def runTest = {
var x: MetaData = Null
@@ -44,6 +52,12 @@ object Test {
assertEquals("present element (prefixed) 6", None, x.get(null, s, "bar" ))
assertEquals("present element (unprefix) 6", Some(Text("meaning")), x.get("bar"))
+ val z = <foo bar="gar"/>
+ val z2 = <foo/>
+
+ assertEquals("attribute extractor 1", Text("gar"), domatch(z))
+ assertEquals("attribute extractor 2", new Atom(3), domatch(z2))
+
}
}
@@ -74,6 +88,7 @@ object Test {
assertEquals("sort attrib"+xml.Utility.sort(q.attributes).toString, " a=\"2\" g=\"3\" j=\"2\" oo=\"2\"", xml.Utility.sort(q.attributes).toString)
val pp = new xml.PrettyPrinter(80,5)
assertEquals("pretty print sorted attrib:"+pp.format(q), "<a a=\"2\" g=\"3\" j=\"2\" oo=\"2\"></a>", pp.format(q))
+
}
def main(args:Array[String]) = {
diff --git a/test/files/jvm/xml02.check b/test/files/jvm/xml02.check
deleted file mode 100644
index dc9e15dd2b..0000000000
--- a/test/files/jvm/xml02.check
+++ /dev/null
@@ -1,13 +0,0 @@
-attributes
-one
-passed ok
-two
-passed ok
-three
-passed ok
-four
-passed ok
-five
-passed ok
-patterns
-passed ok
diff --git a/test/files/jvm/xml02.scala b/test/files/jvm/xml02.scala
index d0d97e64eb..ac400d7adf 100644
--- a/test/files/jvm/xml02.scala
+++ b/test/files/jvm/xml02.scala
@@ -1,39 +1,32 @@
-object Test {
-def main(args:Array[String]) = {
+import testing.SUnit._
+
+object Test extends TestConsoleMain {
+
import scala.xml.NodeSeq
import NodeSeq.view
- import testing.UnitTest._
-
val ax = <hello foo="bar">
<world/>
</hello>
- Console.println("attributes");
-
- Console.println("one");
- assertEquals(ax \ "@foo", "bar")
- Console.println("two");
- assertEquals(ax \ "@foo", xml.Text("bar"))
-
val bx = <hello foo="bar&amp;x"></hello>
- Console.println("three");
- assertEquals(bx \ "@foo", "bar&x")
- Console.println("four");
- assertSameElements(bx \ "@foo", List(xml.Text("bar&x")))
- //assertSameElements(bx \ "@foo", List(xml.Text("bar"),xml.EntityRef("amp"),xml.Text("x")))
-
- Console.println("five");
- assertEquals(bx.toString, "<hello foo=\"bar&amp;x\"></hello>")
-
-
- /* patterns */
- Console.println("patterns");
- assertEquals(<hello/> match { case <hello/> => true; case _ => false; },
- true);
+ class XmlEx extends TestCase("attributes") with Assert {
+ override def run = {
+ assertEquals("@one", "bar", ax \ "@foo")
+ assertEquals("@two", xml.Text("bar"), ax \ "@foo")
+ assertEquals("@three", "bar&x", bx \ "@foo")
+ assertTrue ("@four", (bx \ "@foo") sameElements List(xml.Text("bar&x")))
+ //assertTrue("@four", (bx \ "@foo") sameElements List(xml.Text("bar"),xml.EntityRef("amp"),xml.Text("x")))
+ assertEquals("@five", "<hello foo=\"bar&amp;x\"></hello>", bx.toString)
+ }
+ }
+ class XmlPat extends TestCase("patterns") with Assert {
+ override def run = {
+ assertTrue(<hello/> match { case <hello/> => true; case _ => false; })
+ assertTrue(<x:ga xmlns:x="z"/> match { case <x:ga/> => true; case _ => false; });
/*
assertEquals(ax match { case x @ <hello>
@@ -51,6 +44,10 @@ def main(args:Array[String]) = {
case _ => false; },
true);
*/
-}
-
+ }
+ }
+ def suite = new TestSuite(
+ new XmlEx,
+ new XmlPat
+ )
}
diff --git a/test/files/neg/xmlcorner.check b/test/files/neg/xmlcorner.check
new file mode 100644
index 0000000000..8791829e50
--- /dev/null
+++ b/test/files/neg/xmlcorner.check
@@ -0,0 +1,7 @@
+xmlcorner.scala:2: error: illegal start of simple expression
+ val wrong = <:bla/>
+ ^
+xmlcorner.scala:5: error: in XML literal: name cannot end in ':'
+ val wrong = <bla: />
+ ^
+two errors found
diff --git a/test/files/neg/xmlcorner.scala b/test/files/neg/xmlcorner.scala
new file mode 100644
index 0000000000..9f1c55bce4
--- /dev/null
+++ b/test/files/neg/xmlcorner.scala
@@ -0,0 +1,6 @@
+class foo {
+ val wrong = <:bla/>
+}
+class bar {
+ val wrong = <bla: />
+}