Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 419891f

Browse files
Dupliate Scala.js linkable tests from jvm/test to js/test
1 parent 806500d commit 419891f

File tree

6 files changed

+756
-0
lines changed

6 files changed

+756
-0
lines changed
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 scala.xml.transform._
4+
import org.junit.Test
5+
import org.junit.Assert.assertTrue
6+
import org.junit.Assert.assertEquals
7+
import org.junit.runner.RunWith
8+
/**
9+
* This test verify that after the tranform, the resultant xml node
10+
* uses as many old nodes as possible.
11+
*
12+
* Three transformers class for case -
13+
* One for orginal, one for modified, and one proposed which shows
14+
* all are equivalent when it comes to reusing as many nodes as possible
15+
*/
16+
object ReuseNodesTest {
17+
18+
class OriginalTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
19+
override def transform(ns: Seq[Node]): Seq[Node] = {
20+
val xs = ns.toStream map transform
21+
val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) }
22+
23+
if (xs2.isEmpty) ns
24+
else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1))
25+
}
26+
override def transform(n:Node): Seq[Node] = super.transform(n)
27+
}
28+
29+
class ModifiedTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
30+
override def transform(ns: Seq[Node]): Seq[Node] = {
31+
val changed = ns flatMap transform
32+
33+
if (changed.length != ns.length || (changed, ns).zipped.exists(_ != _)) changed
34+
else ns
35+
}
36+
override def transform(n:Node): Seq[Node] = super.transform(n)
37+
}
38+
39+
class AlternateTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) {
40+
override def transform(ns: Seq[Node]): Seq[Node] = {
41+
val xs = ns.toStream map transform
42+
val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) }
43+
44+
if (xs2.isEmpty) ns
45+
else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1))
46+
}
47+
override def transform(n:Node): Seq[Node] = super.transform(n)
48+
}
49+
50+
def rewriteRule = new RewriteRule {
51+
override def transform(n: Node): NodeSeq = n match {
52+
case n if n.label == "change" => Elem(
53+
n.prefix, "changed", n.attributes, n.scope, n.child.isEmpty, n.child : _*)
54+
case _ => n
55+
}
56+
}
57+
58+
}
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 XMLSyntaxTest {
12+
13+
private def handle[A](x: Node): A = {
14+
x.child(0).asInstanceOf[Atom[A]].data
15+
}
16+
17+
@Test
18+
def test1(): Unit = {
19+
val xNull = <hello>{null}</hello> // these used to be Atom(unit), changed to empty children
20+
assertTrue(xNull.child sameElements Nil)
21+
22+
val x0 = <hello>{}</hello> // these used to be Atom(unit), changed to empty children
23+
val x00 = <hello>{ }</hello> // dto.
24+
val xa = <hello>{ "world" }</hello>
25+
26+
assertTrue(x0.child sameElements Nil)
27+
assertTrue(x00.child sameElements Nil)
28+
assertEquals("world", handle[String](xa))
29+
30+
val xb = <hello>{ 1.5 }</hello>
31+
assertEquals(1.5, handle[Double](xb), 0.0)
32+
33+
val xc = <hello>{ 5 }</hello>
34+
assertEquals(5, handle[Int](xc))
35+
36+
val xd = <hello>{ true }</hello>
37+
assertEquals(true, handle[Boolean](xd))
38+
39+
val xe = <hello>{ 5:Short }</hello>
40+
assertEquals((5:Short), handle[Short](xe))
41+
42+
val xf = <hello>{ val x = 27; x }</hello>
43+
assertEquals(27, handle[Int](xf))
44+
45+
val xg = <hello>{ List(1,2,3,4) }</hello>
46+
assertEquals("<hello>1 2 3 4</hello>", xg.toString)
47+
assertFalse(xg.child.map(_.isInstanceOf[Text]).exists(identity))
48+
49+
val xh = <hello>{ for(x <- List(1,2,3,4) if x % 2 == 0) yield x }</hello>
50+
assertEquals("<hello>2 4</hello>", xh.toString)
51+
assertFalse(xh.child.map(_.isInstanceOf[Text]).exists(identity))
52+
}
53+
54+
/** see SVN r13821 (emir): support for <elem key={x:Option[Seq[Node]]} />,
55+
* so that Options can be used for optional attributes.
56+
*/
57+
@Test
58+
def test2(): Unit = {
59+
val x1: Option[Seq[Node]] = Some(<b>hello</b>)
60+
val n1 = <elem key={x1} />;
61+
assertEquals(x1, n1.attribute("key"))
62+
63+
val x2: Option[Seq[Node]] = None
64+
val n2 = <elem key={x2} />;
65+
assertEquals(x2, n2.attribute("key"))
66+
}
67+
68+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /