Skip to content

Commit aefc6b9

Browse files
committed
Merge pull request #6 from gkossakowski/master
Migrate majority of partest jvm tests to JUnit
2 parents dd1e0e4 + e36604e commit aefc6b9

26 files changed

+617
-598
lines changed

build.sbt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,43 @@ pomExtra := (
8181
// default value must be set here
8282
TestKeys.includeTestDependencies := true
8383

84+
libraryDependencies ++= Seq("junit" % "junit" % "4.11" % "test", "com.novocode" % "junit-interface" % "0.10" % "test")
85+
8486
// default
8587
TestKeys.partestVersion := "1.0.0-RC6"
8688

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

97-
98-
// necessary for partest -- see comments in its build.sbt
99-
conflictWarning ~= { _.copy(failOnConflict = false) }
100-
101121
fork in Test := true
102122

103123
javaOptions in Test += "-Xmx1G"
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package scala.xml
2+
3+
import org.junit.Test
4+
import org.junit.Ignore
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Assert.assertFalse
9+
import org.junit.Assert.assertEquals
10+
11+
class AttributeTest {
12+
@Test
13+
def unprefixedAttribute: Unit = {
14+
val x = new UnprefixedAttribute("foo","bar", Null)
15+
assertEquals(Some(Text("bar")), x.get("foo"))
16+
assertEquals(Text("bar"), x("foo"))
17+
assertEquals(None, x.get("no_foo"))
18+
assertEquals(null, x("no_foo"))
19+
20+
val y = x.remove("foo")
21+
assertEquals(Null, y)
22+
23+
val z = new UnprefixedAttribute("foo", null:NodeSeq, x)
24+
assertEquals(None, z.get("foo"))
25+
26+
var appended = x append x append x append x
27+
var len = 0; while (appended ne Null) {
28+
appended = appended.next
29+
len = len + 1
30+
}
31+
assertEquals("removal of duplicates for unprefixed attributes in append", 1, len)
32+
}
33+
34+
@Test
35+
def attributeWithOption: Unit = {
36+
val x = new UnprefixedAttribute("foo", Some(Text("bar")), Null)
37+
38+
assertEquals(Some(Text("bar")), x.get("foo"))
39+
assertEquals(Text("bar"), x("foo"))
40+
assertEquals(None, x.get("no_foo"))
41+
assertEquals(null, x("no_foo"))
42+
43+
val attr1 = Some(Text("foo value"))
44+
val attr2 = None
45+
val y = <b foo={attr1} bar={attr2} />
46+
assertEquals(Some(Text("foo value")), y.attributes.get("foo"))
47+
assertEquals(Text("foo value"), y.attributes("foo"))
48+
assertEquals(None, y.attributes.get("bar"))
49+
assertEquals(null, y.attributes("bar"))
50+
51+
val z = new UnprefixedAttribute("foo", None, x)
52+
assertEquals(None, z.get("foo"))
53+
}
54+
55+
@Test
56+
def attributeToString: Unit = {
57+
val expected: String = """<b x="&amp;"/>"""
58+
assertEquals(expected, (<b x="&amp;"/>).toString)
59+
assertEquals(expected, (<b x={"&"}/>).toString)
60+
}
61+
62+
@Test
63+
def attributeOperator: Unit = {
64+
val xml = <foo bar="apple" />
65+
assertEquals("apple", xml \@ "bar")
66+
}
67+
68+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package scala.xml
2+
3+
object JUnitAssertsForXML {
4+
5+
private[xml] def assertEquals(expected: String, actual: NodeSeq): Unit =
6+
org.junit.Assert.assertEquals(expected, actual.toString)
7+
8+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package scala.xml
2+
3+
import org.junit.Test
4+
import org.junit.Ignore
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import org.junit.Assert.assertEquals
8+
9+
class MetaDataTest {
10+
11+
@Test
12+
def absentElementPrefixed1: Unit = {
13+
// type ascription to help overload resolution pick the right variant
14+
assertEquals(null: Object, Null("za://foo.com", TopScope, "bar"))
15+
assertEquals(null, Null("bar"))
16+
}
17+
18+
@Test
19+
def absentElementPrefixed2: Unit = {
20+
assertEquals(None, Null.get("za://foo.com", TopScope, "bar" ))
21+
assertEquals(None, Null.get("bar"))
22+
}
23+
24+
@Test
25+
def presentElement1: Unit = {
26+
val x = new PrefixedAttribute("zo","bar", new Atom(42), Null)
27+
val s = new NamespaceBinding("zo","za://foo.com", TopScope)
28+
assertEquals(new Atom(42), x("za://foo.com", s, "bar" ))
29+
assertEquals(null, x("bar"))
30+
assertEquals(Some(new Atom(42)), x.get("za://foo.com", s, "bar"))
31+
assertEquals(None, x.get("bar"))
32+
}
33+
34+
@Test
35+
def presentElement2: Unit = {
36+
val s = new NamespaceBinding("zo","za://foo.com", TopScope)
37+
val x1 = new PrefixedAttribute("zo","bar", new Atom(42), Null)
38+
val x = new UnprefixedAttribute("bar","meaning", x1)
39+
assertEquals(null, x(null, s, "bar"))
40+
assertEquals(Text("meaning"), x("bar"))
41+
assertEquals(None, x.get(null, s, "bar" ))
42+
assertEquals(Some(Text("meaning")), x.get("bar"))
43+
}
44+
45+
@Test
46+
def attributeExtractor: Unit = {
47+
def domatch(x:Node): Node = {
48+
x match {
49+
case Node("foo", md @ UnprefixedAttribute(_, value, _), _*) if !value.isEmpty =>
50+
md("bar")(0)
51+
case _ => new Atom(3)
52+
}
53+
}
54+
val z = <foo bar="gar"/>
55+
val z2 = <foo/>
56+
assertEquals(Text("gar"), domatch(z))
57+
assertEquals(new Atom(3), domatch(z2))
58+
}
59+
60+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package scala.xml
2+
3+
import org.junit.Test
4+
import org.junit.Ignore
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import JUnitAssertsForXML.assertEquals
8+
9+
class PrintEmptyElementsTest {
10+
11+
@Test
12+
def representEmptyXMLElementsInShortForm: Unit = {
13+
val expected: String =
14+
"""|
15+
|<hi/> <!-- literal short -->
16+
|<there></there> <!-- literal long -->
17+
|<guys who="you all"></guys> <!-- literal long with attribute-->
18+
|<hows it="going"/> <!-- literal short with attribute -->
19+
|<this>is pretty cool</this> <!-- literal not empty -->
20+
|""".stripMargin
21+
// the xml snippet is not indented because indentation affects pretty printing
22+
// results
23+
val actual: NodeSeq =
24+
<xml:group>
25+
<hi/> <!-- literal short -->
26+
<there></there> <!-- literal long -->
27+
<guys who="you all"></guys> <!-- literal long with attribute-->
28+
<hows it="going"/> <!-- literal short with attribute -->
29+
<this>is pretty cool</this> <!-- literal not empty -->
30+
</xml:group>
31+
assertEquals(expected, actual)
32+
}
33+
34+
@Test
35+
def programmaticLong: Unit = {
36+
assertEquals("<emptiness></emptiness> <!--programmatic long-->",
37+
Elem(null, "emptiness", Null, TopScope, false) ++ Text(" ") ++ Comment("programmatic long"))
38+
}
39+
40+
@Test
41+
def programmaticShort: Unit = {
42+
assertEquals("<vide/> <!--programmatic short-->",
43+
Elem(null, "vide", Null, TopScope, true) ++ Text(" ") ++ Comment("programmatic short"))
44+
}
45+
46+
@Test
47+
def programmaticShortWithAttribute: Unit = {
48+
assertEquals("""<elem attr="value"/> <!--programmatic short with attribute-->""",
49+
Elem(null, "elem", Attribute("attr", Text("value"), Null), TopScope, true) ++ Text(" ") ++ Comment ("programmatic short with attribute"))
50+
}
51+
52+
@Test
53+
def programmaticLongWithAttribute: Unit = {
54+
assertEquals("""<elem2 attr2="value2"></elem2> <!--programmatic long with attribute-->""",
55+
Elem(null, "elem2", Attribute("attr2", Text("value2"), Null), TopScope, false) ++ Text(" ") ++ Comment ("programmatic long with attribute"))
56+
}
57+
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package scala.xml
2+
3+
import org.junit.Test
4+
import org.junit.Ignore
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Assert.assertFalse
9+
import org.junit.Assert.assertEquals
10+
11+
class UtilityTest {
12+
13+
@Test
14+
def isNameStart: Unit = {
15+
assertTrue(Utility.isNameStart('b'))
16+
assertFalse(Utility.isNameStart(':'))
17+
}
18+
19+
@Test
20+
def trim: Unit = {
21+
val x = <foo>
22+
<toomuchws/>
23+
</foo>
24+
val y = xml.Utility.trim(x)
25+
assertEquals(1, y match { case <foo><toomuchws/></foo> => 1 })
26+
27+
val x2 = <foo>
28+
<toomuchws> a b b a </toomuchws>
29+
</foo>
30+
val y2 = xml.Utility.trim(x2)
31+
assertEquals(2, y2 match { case <foo><toomuchws>a b b a</toomuchws></foo> => 2 })
32+
}
33+
34+
@Test
35+
def aposEscaping: Unit = {
36+
val z = <bar>''</bar>
37+
val z1 = z.toString
38+
assertEquals("<bar>''</bar>", z1)
39+
}
40+
41+
@Test
42+
def sort: Unit = {
43+
val q = xml.Utility.sort(<a g='3' j='2' oo='2' a='2'/>)
44+
assertEquals(" a=\"2\" g=\"3\" j=\"2\" oo=\"2\"", xml.Utility.sort(q.attributes).toString)
45+
val pp = new xml.PrettyPrinter(80,5)
46+
assertEquals("<a a=\"2\" g=\"3\" j=\"2\" oo=\"2\"/>", pp.format(q))
47+
}
48+
49+
@Test
50+
def issue777: Unit = {
51+
<hi>
52+
<there/>
53+
<guys/>
54+
</hi>.hashCode // Bug #777
55+
}
56+
57+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package scala.xml
2+
3+
import org.junit.Test
4+
import org.junit.Ignore
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Assert.assertFalse
9+
import org.junit.Assert.assertEquals
10+
11+
class XMLEmbeddingTest {
12+
13+
@Test
14+
def basic: Unit = {
15+
val ya = <x>{{</x>
16+
assertEquals("{", ya.text)
17+
val ua = <x>}}</x>
18+
assertEquals("}", ua.text)
19+
val za = <x>{{}}{{}}{{}}</x>
20+
assertEquals("{}{}{}", za.text)
21+
}
22+
23+
}

0 commit comments

Comments
 (0)