Skip to content

Commit

Permalink
Merge pull request scala#109 from OlivierBlanvillain/scala.js
Browse files Browse the repository at this point in the history
Fix scala#49: Add Scala.js support
  • Loading branch information
SethTisue authored Feb 4, 2017
2 parents 4da6dfb + 834bfab commit 00fa469
Show file tree
Hide file tree
Showing 108 changed files with 788 additions and 33 deletions.
62 changes: 29 additions & 33 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import com.typesafe.tools.mima.plugin.{MimaPlugin, MimaKeys}

scalaModuleSettings

name := "scala-xml"

version := "1.0.6-SNAPSHOT"

scalaVersion := crossScalaVersions.value.head

crossScalaVersions := {
scalaVersion in ThisBuild := crossScalaVersions.value.head
crossScalaVersions in ThisBuild := {
val java = System.getProperty("java.version")
if (java.startsWith("1.6.") || java.startsWith("1.7."))
Seq("2.11.8")
Expand All @@ -18,27 +11,30 @@ crossScalaVersions := {
sys.error(s"don't know what Scala versions to build on $java")
}

//reenable -Xfatal-warnings?
scalacOptions ++= "-deprecation:false -feature -Xlint:-stars-align,-nullary-unit,_".split("\\s+").to[Seq]

scalacOptions in Test += "-Xxml:coalescing"

// important!! must come here (why?)
scalaModuleOsgiSettings

OsgiKeys.exportPackage := Seq(s"scala.xml.*;version=${version.value}")

libraryDependencies += "junit" % "junit" % "4.11" % "test"

libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test"

//// testing:
// used in CompilerErrors test
libraryDependencies += ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").exclude("org.scala-lang.modules", s"scala-xml*")

mimaPreviousVersion := Some("1.0.5")

// You cannot disable JVM test forking when working on scala modules
// that are distributed with the compiler because of an SBT
// classloader leaking issue (scala/scala-xml#20 and #112).
fork in Test := true
lazy val root = project.in(file("."))
.aggregate(xmlJS, xmlJVM)
.settings(publish := {}, publishLocal := {})

lazy val xml = crossProject.in(file("."))
.settings(
name := "scala-xml",
version := "1.0.6-SNAPSHOT",
scalacOptions ++= "-deprecation:false -feature -Xlint:-stars-align,-nullary-unit,_".split("\\s+").to[Seq],
scalacOptions in Test += "-Xxml:coalescing")
.jvmSettings(
scalaModuleSettings ++
scalaModuleOsgiSettings ++
List(
OsgiKeys.exportPackage := Seq(s"scala.xml.*;version=${version.value}"),
libraryDependencies += "junit" % "junit" % "4.11" % "test",
libraryDependencies += "com.novocode" % "junit-interface" % "0.10" % "test",
libraryDependencies += ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").exclude("org.scala-lang.modules", s"scala-xml*"),
mimaPreviousVersion := Some("1.0.5"),
// You cannot disable JVM test forking when working on scala modules
// that are distributed with the compiler because of an SBT
// classloader leaking issue (scala/scala-xml#20 and #112).
fork in Test := true): _*)
.jsConfigure(_.enablePlugins(ScalaJSJUnitPlugin))

lazy val xmlJVM = xml.jvm
lazy val xmlJS = xml.js
58 changes: 58 additions & 0 deletions js/src/test/scala/scala/xml/ReuseNodesTest.scala
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
}
}

}
68 changes: 68 additions & 0 deletions js/src/test/scala/scala/xml/XMLSyntaxTest.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 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"))
}

}
Loading

0 comments on commit 00fa469

Please sign in to comment.