Skip to content

turn remainder of the pos/run/jvm partest tests into JUnit ones #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/test/scala/scala/xml/PatternMatching.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package scala.xml

import org.junit.Test
import org.junit.Assert.assertTrue
import org.junit.Assert.assertEquals

class PatternMatching extends {
@Test
def unprefixedAttribute: Unit = {
val li = List("1", "2", "3", "4")
assertTrue(matchSeq(li))
assertTrue(matchList(li))
}

def matchSeq(args: Seq[String]) = args match {
case Seq(a, b, c, d @ _*) => true
}

def matchList(args: List[String]) =
Elem(null, "bla", Null, TopScope, minimizeEmpty = true, (args map { x => Text(x) }): _*) match {
case Elem(_, _, _, _, Text("1"), _*) => true
}

@Test
def simpleNode =
assertTrue(<hello/> match {
case <hello/> => true
})

@Test
def nameSpaced =
assertTrue(<x:ga xmlns:x="z"/> match {
case <x:ga/> => true
})

val cx = <z:hello foo="bar" xmlns:z="z" x:foo="baz" xmlns:x="the namespace from outer space">
crazy text world
</z:hello>

@Test
def nodeContents = {
assertTrue(Utility.trim(cx) match {
case n @ <hello>crazy text world</hello> if (n \ "@foo") xml_== "bar" => true
})
assertTrue(Utility.trim(cx) match {
case n @ <z:hello>crazy text world</z:hello> if (n \ "@foo") xml_== "bar" => true
})
assertTrue(<x:foo xmlns:x="gaga"/> match {
case scala.xml.QNode("gaga", "foo", md, child @ _*) => true
})

assertTrue(<x:foo xmlns:x="gaga"/> match {
case scala.xml.Node("foo", md, child @ _*) => true
})

}

object SafeNodeSeq {
def unapplySeq(any: Any): Option[Seq[Node]] = any match {
case s: Seq[_] => Some(s flatMap (_ match {
case n: Node => n case _ => NodeSeq.Empty
})) case _ => None
}
}

@Test
def nodeSeq = { // t0646
import scala.xml.NodeSeq

val books =
<bks>
<title>Blabla</title>
<title>Blubabla</title>
<title>Baaaaaaalabla</title>
</bks>;

assertTrue(new NodeSeq { val theSeq = books.child } match {
case t @ Seq(<title>Blabla</title>) => false
case _ => true
})

// SI-1059
var m: PartialFunction[Any, Any] = { case SafeNodeSeq(s @ _*) => s }

assertEquals(m(<a/> ++ <b/>), List(<a/>, <b/>))
assertTrue(m.isDefinedAt(<a/> ++ <b/>))
}

@Test
def SI_4124 = {
val body: Node = <elem>hi</elem>

assertTrue((body: AnyRef, "foo") match {
case (node: Node, "bar") => false
case (ser: Serializable, "foo") => true
})

assertTrue((body, "foo") match {
case (node: Node, "bar") => false
case (ser: Serializable, "foo") => true
})

assertTrue((body: AnyRef, "foo") match {
case (node: Node, "foo") => true
case (ser: Serializable, "foo") => false
})

assertTrue((body: AnyRef, "foo") match {
case (node: Node, "foo") => true
case (ser: Serializable, "foo") => false
})
}
}
95 changes: 95 additions & 0 deletions src/test/scala/scala/xml/ShouldCompile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package scala.xml

// these tests depend on xml, so they ended up here,
// though really they are compiler tests

import scala.collection._
import scala.collection.mutable.ArrayBuffer

// t1626
object o {
val n = <a xmlns=""/>
n.namespace == null
}

// t1761
class Foo {
val elements: Seq[Node] = Nil
val innerTransform: PartialFunction[Elem, String] = {
case Elem(_, l: String, _, _, _@ _*) if elements.exists(_.label == l) =>
l
}
}

// t2281
class A {
def f(x: Boolean) = if (x) <br/><br/> else <br/>
}

class B {
def splitSentences(text: String): ArrayBuffer[String] = {
val outarr = new ArrayBuffer[String]
var outstr = new StringBuffer
var prevspace = false
val ctext = text.replaceAll("\n+", "\n")
ctext foreach { c =>
outstr append c
if (c == '.' || c == '!' || c == '?' || c == '\n' || c == ':' || c == ';' || (prevspace && c == '-')) {
outarr += outstr.toString
outstr = new StringBuffer
}
if (c == '\n') {
outarr += "\n\n"
}
prevspace = c == ' '
}
if (outstr.length > 0) {
outarr += outstr.toString
}
outarr
}

def spanForSentence(x: String, picktext: String) =
if (x == "\n\n") {
<br/><br/>
} else {
<span class='clicksentence' style={ if (x == picktext) "background-color: yellow" else "" }>{ x }</span>
}

def selectableSentences(text: String, picktext: String) = {
val sentences = splitSentences(text)
sentences.map(x => spanForSentence(x, picktext))
}
}

// SI-5858
object Test {
new Elem(null, null, Null, TopScope, Nil: _*) // was ambiguous
}

class Floozy {
def fooz(x: Node => String) = {}
def foo(m: Node): Unit = fooz {
case Elem(_, _, _, _, n, _*) if (n == m) => "gaga"
}
}

object guardedMatch { // SI-3705
// guard caused verifyerror in oldpatmat -- TODO: move this to compiler test suite
def updateNodes(ns: Seq[Node]): Seq[Node] =
for (subnode <- ns) yield subnode match {
case <d>{ _ }</d> if true => <d>abc</d>
case Elem(prefix, label, attribs, scope, children @ _*) =>
Elem(prefix, label, attribs, scope, minimizeEmpty = true, updateNodes(children): _*)
case other => other
}
updateNodes(<b/>)
}

// SI-6897
object shouldCompile {
val html = (null: Any) match {
case 1 => <xml:group></xml:group>
case 2 => <p></p>
}
}
59 changes: 59 additions & 0 deletions src/test/scala/scala/xml/Transformers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package scala.xml

import scala.xml.transform._

import org.junit.Test
import org.junit.Assert.assertTrue
import org.junit.Assert.assertEquals

class Transformers {


def transformer = new RuleTransformer(new RewriteRule {
override def transform(n: Node): NodeSeq = n match {
case <t>{ _* }</t> => <q/>
case n => n
}
})

@Test
def transform = // SI-2124
assertEquals(transformer.transform(<p><lost/><t><s><r></r></s></t></p>),
<p><lost/><q/></p>)

@Test
def transformNamespaced = // SI-2125
assertEquals(transformer.transform(<xml:group><p><lost/><t><s><r></r></s></t></p></xml:group>),
Group(<p><lost/><q/></p>))

@Test
def rewriteRule = { // SI-2276
val inputXml: Node =
<root>
<subnode>
<version>1</version>
</subnode>
<contents>
<version>1</version>
</contents>
</root>

object t1 extends RewriteRule {
override def transform(n: Node): Seq[Node] = n match {
case <version>{ x }</version> if x.toString.toInt < 4 => <version>{ x.toString.toInt + 1 }</version>
case other => other
}
}

val ruleTransformer = new RuleTransformer(t1)
JUnitAssertsForXML.assertEquals(ruleTransformer(inputXml).toString, // TODO: why do we need toString?
<root>
<subnode>
<version>2</version>
</subnode>
<contents>
<version>2</version>
</contents>
</root>)
}
}
Loading