forked from scala/scala-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request scala#109 from OlivierBlanvillain/scala.js
Fix scala#49: Add Scala.js support
- Loading branch information
Showing
108 changed files
with
788 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package scala.xml | ||
|
||
import scala.xml.transform._ | ||
import org.junit.Test | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Assert.assertEquals | ||
import org.junit.runner.RunWith | ||
/** | ||
* This test verify that after the tranform, the resultant xml node | ||
* uses as many old nodes as possible. | ||
* | ||
* Three transformers class for case - | ||
* One for orginal, one for modified, and one proposed which shows | ||
* all are equivalent when it comes to reusing as many nodes as possible | ||
*/ | ||
object ReuseNodesTest { | ||
|
||
class OriginalTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) { | ||
override def transform(ns: Seq[Node]): Seq[Node] = { | ||
val xs = ns.toStream map transform | ||
val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) } | ||
|
||
if (xs2.isEmpty) ns | ||
else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1)) | ||
} | ||
override def transform(n:Node): Seq[Node] = super.transform(n) | ||
} | ||
|
||
class ModifiedTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) { | ||
override def transform(ns: Seq[Node]): Seq[Node] = { | ||
val changed = ns flatMap transform | ||
|
||
if (changed.length != ns.length || (changed, ns).zipped.exists(_ != _)) changed | ||
else ns | ||
} | ||
override def transform(n:Node): Seq[Node] = super.transform(n) | ||
} | ||
|
||
class AlternateTranformr(rules: RewriteRule*) extends RuleTransformer(rules:_*) { | ||
override def transform(ns: Seq[Node]): Seq[Node] = { | ||
val xs = ns.toStream map transform | ||
val (xs1, xs2) = xs zip ns span { case (x, n) => unchanged(n, x) } | ||
|
||
if (xs2.isEmpty) ns | ||
else (xs1 map (_._2)) ++ xs2.head._1 ++ transform(ns drop (xs1.length + 1)) | ||
} | ||
override def transform(n:Node): Seq[Node] = super.transform(n) | ||
} | ||
|
||
def rewriteRule = new RewriteRule { | ||
override def transform(n: Node): NodeSeq = n match { | ||
case n if n.label == "change" => Elem( | ||
n.prefix, "changed", n.attributes, n.scope, n.child.isEmpty, n.child : _*) | ||
case _ => n | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 XMLSyntaxTest { | ||
|
||
private def handle[A](x: Node): A = { | ||
x.child(0).asInstanceOf[Atom[A]].data | ||
} | ||
|
||
@Test | ||
def test1(): Unit = { | ||
val xNull = <hello>{null}</hello> // these used to be Atom(unit), changed to empty children | ||
assertTrue(xNull.child sameElements Nil) | ||
|
||
val x0 = <hello>{}</hello> // these used to be Atom(unit), changed to empty children | ||
val x00 = <hello>{ }</hello> // dto. | ||
val xa = <hello>{ "world" }</hello> | ||
|
||
assertTrue(x0.child sameElements Nil) | ||
assertTrue(x00.child sameElements Nil) | ||
assertEquals("world", handle[String](xa)) | ||
|
||
val xb = <hello>{ 1.5 }</hello> | ||
assertEquals(1.5, handle[Double](xb), 0.0) | ||
|
||
val xc = <hello>{ 5 }</hello> | ||
assertEquals(5, handle[Int](xc)) | ||
|
||
val xd = <hello>{ true }</hello> | ||
assertEquals(true, handle[Boolean](xd)) | ||
|
||
val xe = <hello>{ 5:Short }</hello> | ||
assertEquals((5:Short), handle[Short](xe)) | ||
|
||
val xf = <hello>{ val x = 27; x }</hello> | ||
assertEquals(27, handle[Int](xf)) | ||
|
||
val xg = <hello>{ List(1,2,3,4) }</hello> | ||
assertEquals("<hello>1 2 3 4</hello>", xg.toString) | ||
assertFalse(xg.child.map(_.isInstanceOf[Text]).exists(identity)) | ||
|
||
val xh = <hello>{ for(x <- List(1,2,3,4) if x % 2 == 0) yield x }</hello> | ||
assertEquals("<hello>2 4</hello>", xh.toString) | ||
assertFalse(xh.child.map(_.isInstanceOf[Text]).exists(identity)) | ||
} | ||
|
||
/** see SVN r13821 (emir): support for <elem key={x:Option[Seq[Node]]} />, | ||
* so that Options can be used for optional attributes. | ||
*/ | ||
@Test | ||
def test2(): Unit = { | ||
val x1: Option[Seq[Node]] = Some(<b>hello</b>) | ||
val n1 = <elem key={x1} />; | ||
assertEquals(x1, n1.attribute("key")) | ||
|
||
val x2: Option[Seq[Node]] = None | ||
val n2 = <elem key={x2} />; | ||
assertEquals(x2, n2.attribute("key")) | ||
} | ||
|
||
} |
Oops, something went wrong.