Skip to content
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
34 changes: 27 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,43 @@ pomExtra := (
// default value must be set here
TestKeys.includeTestDependencies := true

libraryDependencies ++= Seq("junit" % "junit" % "4.11" % "test", "com.novocode" % "junit-interface" % "0.10" % "test")

// default
TestKeys.partestVersion := "1.0.0-RC6"

// the actual partest the interface calls into -- must be binary version close enough to ours
// so that it can link to the compiler/lib we're using (testing)
// NOTE: not sure why, but the order matters (maybe due to the binary version conflicts for xml/parser combinators pulled in for scaladoc?)
libraryDependencies ++= (
if (TestKeys.includeTestDependencies.value)
Seq("org.scala-lang.modules" %% "scala-partest-interface" % "0.2" % "test",
"org.scala-lang.modules" %% "scala-partest" % TestKeys.partestVersion.value % "test")
if (TestKeys.includeTestDependencies.value) {
/**
* Exclude all transitive dependencies of partest that include scala-.xml.
* This way we avoid having two (or more) versions of scala-xml on a classpath.
* This fixes problem described here:
* https://github.com/scala/scala-xml/pull/6#issuecomment-26614894
*
* Note that we are using ModuleID.exclude instead of more flexible ModuleID.excludeAll
* (describe here: http://www.scala-sbt.org/release/docs/Detailed-Topics/Library-Management#exclude-transitive-dependencies)
* because only plain excludes are incorporated in generated pom.xml. There are two ways
* to address this problem:
*
* 1. Figure out how to depend on partest in non-transitive way: not include that dependency
* in generated pom.xml for scala-xml.
* 2. Declare dependencies in partest as provided so they are not includeded transitively.
*
*/
def excludeScalaXml(dep: ModuleID): ModuleID =
dep.exclude("org.scala-lang.modules", "scala-xml_2.11.0-M5").
exclude("org.scala-lang.modules", "scala-xml_2.11.0-M4").
exclude("org.scalacheck", "scalacheck_2.11.0-M5")
Seq("org.scala-lang.modules" % "scala-partest-interface_2.11.0-M5" % "0.2" % "test",
"org.scala-lang.modules" % "scala-partest_2.11.0-M5" % TestKeys.partestVersion.value % "test").
map(excludeScalaXml)
}
else Seq.empty
)


// necessary for partest -- see comments in its build.sbt
conflictWarning ~= { _.copy(failOnConflict = false) }

fork in Test := true

javaOptions in Test += "-Xmx1G"
Expand Down
68 changes: 68 additions & 0 deletions src/test/scala/scala/xml/AttributeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package scala.xml

import org.junit.Test
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Assert.assertEquals

class AttributeTest {
@Test
def unprefixedAttribute: Unit = {
val x = new UnprefixedAttribute("foo","bar", Null)
assertEquals(Some(Text("bar")), x.get("foo"))
assertEquals(Text("bar"), x("foo"))
assertEquals(None, x.get("no_foo"))
assertEquals(null, x("no_foo"))

val y = x.remove("foo")
assertEquals(Null, y)

val z = new UnprefixedAttribute("foo", null:NodeSeq, x)
assertEquals(None, z.get("foo"))

var appended = x append x append x append x
var len = 0; while (appended ne Null) {
appended = appended.next
len = len + 1
}
assertEquals("removal of duplicates for unprefixed attributes in append", 1, len)
}

@Test
def attributeWithOption: Unit = {
val x = new UnprefixedAttribute("foo", Some(Text("bar")), Null)

assertEquals(Some(Text("bar")), x.get("foo"))
assertEquals(Text("bar"), x("foo"))
assertEquals(None, x.get("no_foo"))
assertEquals(null, x("no_foo"))

val attr1 = Some(Text("foo value"))
val attr2 = None
val y = <b foo={attr1} bar={attr2} />
assertEquals(Some(Text("foo value")), y.attributes.get("foo"))
assertEquals(Text("foo value"), y.attributes("foo"))
assertEquals(None, y.attributes.get("bar"))
assertEquals(null, y.attributes("bar"))

val z = new UnprefixedAttribute("foo", None, x)
assertEquals(None, z.get("foo"))
}

@Test
def attributeToString: Unit = {
val expected: String = """<b x="&amp;"/>"""
assertEquals(expected, (<b x="&amp;"/>).toString)
assertEquals(expected, (<b x={"&"}/>).toString)
}

@Test
def attributeOperator: Unit = {
val xml = <foo bar="apple" />
assertEquals("apple", xml \@ "bar")
}

}
8 changes: 8 additions & 0 deletions src/test/scala/scala/xml/JUnitAssertsForXML.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package scala.xml

object JUnitAssertsForXML {

private[xml] def assertEquals(expected: String, actual: NodeSeq): Unit =
org.junit.Assert.assertEquals(expected, actual.toString)

}
60 changes: 60 additions & 0 deletions src/test/scala/scala/xml/MetaDataTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package scala.xml

import org.junit.Test
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Assert.assertEquals

class MetaDataTest {

@Test
def absentElementPrefixed1: Unit = {
// type ascription to help overload resolution pick the right variant
assertEquals(null: Object, Null("za://foo.com", TopScope, "bar"))
assertEquals(null, Null("bar"))
}

@Test
def absentElementPrefixed2: Unit = {
assertEquals(None, Null.get("za://foo.com", TopScope, "bar" ))
assertEquals(None, Null.get("bar"))
}

@Test
def presentElement1: Unit = {
val x = new PrefixedAttribute("zo","bar", new Atom(42), Null)
val s = new NamespaceBinding("zo","za://foo.com", TopScope)
assertEquals(new Atom(42), x("za://foo.com", s, "bar" ))
assertEquals(null, x("bar"))
assertEquals(Some(new Atom(42)), x.get("za://foo.com", s, "bar"))
assertEquals(None, x.get("bar"))
}

@Test
def presentElement2: Unit = {
val s = new NamespaceBinding("zo","za://foo.com", TopScope)
val x1 = new PrefixedAttribute("zo","bar", new Atom(42), Null)
val x = new UnprefixedAttribute("bar","meaning", x1)
assertEquals(null, x(null, s, "bar"))
assertEquals(Text("meaning"), x("bar"))
assertEquals(None, x.get(null, s, "bar" ))
assertEquals(Some(Text("meaning")), x.get("bar"))
}

@Test
def attributeExtractor: Unit = {
def domatch(x:Node): Node = {
x match {
case Node("foo", md @ UnprefixedAttribute(_, value, _), _*) if !value.isEmpty =>
md("bar")(0)
case _ => new Atom(3)
}
}
val z = <foo bar="gar"/>
val z2 = <foo/>
assertEquals(Text("gar"), domatch(z))
assertEquals(new Atom(3), domatch(z2))
}

}
58 changes: 58 additions & 0 deletions src/test/scala/scala/xml/PrintEmptyElementsTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package scala.xml

import org.junit.Test
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import JUnitAssertsForXML.assertEquals

class PrintEmptyElementsTest {

@Test
def representEmptyXMLElementsInShortForm: Unit = {
val expected: String =
"""|
|<hi/> <!-- literal short -->
|<there></there> <!-- literal long -->
|<guys who="you all"></guys> <!-- literal long with attribute-->
|<hows it="going"/> <!-- literal short with attribute -->
|<this>is pretty cool</this> <!-- literal not empty -->
|""".stripMargin
// the xml snippet is not indented because indentation affects pretty printing
// results
val actual: NodeSeq =
<xml:group>
<hi/> <!-- literal short -->
<there></there> <!-- literal long -->
<guys who="you all"></guys> <!-- literal long with attribute-->
<hows it="going"/> <!-- literal short with attribute -->
<this>is pretty cool</this> <!-- literal not empty -->
</xml:group>
assertEquals(expected, actual)
}

@Test
def programmaticLong: Unit = {
assertEquals("<emptiness></emptiness> <!--programmatic long-->",
Elem(null, "emptiness", Null, TopScope, false) ++ Text(" ") ++ Comment("programmatic long"))
}

@Test
def programmaticShort: Unit = {
assertEquals("<vide/> <!--programmatic short-->",
Elem(null, "vide", Null, TopScope, true) ++ Text(" ") ++ Comment("programmatic short"))
}

@Test
def programmaticShortWithAttribute: Unit = {
assertEquals("""<elem attr="value"/> <!--programmatic short with attribute-->""",
Elem(null, "elem", Attribute("attr", Text("value"), Null), TopScope, true) ++ Text(" ") ++ Comment ("programmatic short with attribute"))
}

@Test
def programmaticLongWithAttribute: Unit = {
assertEquals("""<elem2 attr2="value2"></elem2> <!--programmatic long with attribute-->""",
Elem(null, "elem2", Attribute("attr2", Text("value2"), Null), TopScope, false) ++ Text(" ") ++ Comment ("programmatic long with attribute"))
}

}
57 changes: 57 additions & 0 deletions src/test/scala/scala/xml/UtilityTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package scala.xml

import org.junit.Test
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Assert.assertEquals

class UtilityTest {

@Test
def isNameStart: Unit = {
assertTrue(Utility.isNameStart('b'))
assertFalse(Utility.isNameStart(':'))
}

@Test
def trim: Unit = {
val x = <foo>
<toomuchws/>
</foo>
val y = xml.Utility.trim(x)
assertEquals(1, y match { case <foo><toomuchws/></foo> => 1 })

val x2 = <foo>
<toomuchws> a b b a </toomuchws>
</foo>
val y2 = xml.Utility.trim(x2)
assertEquals(2, y2 match { case <foo><toomuchws>a b b a</toomuchws></foo> => 2 })
}

@Test
def aposEscaping: Unit = {
val z = <bar>''</bar>
val z1 = z.toString
assertEquals("<bar>''</bar>", z1)
}

@Test
def sort: Unit = {
val q = xml.Utility.sort(<a g='3' j='2' oo='2' a='2'/>)
assertEquals(" a=\"2\" g=\"3\" j=\"2\" oo=\"2\"", xml.Utility.sort(q.attributes).toString)
val pp = new xml.PrettyPrinter(80,5)
assertEquals("<a a=\"2\" g=\"3\" j=\"2\" oo=\"2\"/>", pp.format(q))
}

@Test
def issue777: Unit = {
<hi>
<there/>
<guys/>
</hi>.hashCode // Bug #777
}

}
23 changes: 23 additions & 0 deletions src/test/scala/scala/xml/XMLEmbeddingTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package scala.xml

import org.junit.Test
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Assert.assertTrue
import org.junit.Assert.assertFalse
import org.junit.Assert.assertEquals

class XMLEmbeddingTest {

@Test
def basic: Unit = {
val ya = <x>{{</x>
assertEquals("{", ya.text)
val ua = <x>}}</x>
assertEquals("}", ua.text)
val za = <x>{{}}{{}}{{}}</x>
assertEquals("{}{}{}", za.text)
}

}
Loading