Skip to content

Move tests from partests to junit #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2013
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
56 changes: 0 additions & 56 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -83,62 +83,6 @@ pomExtra := (

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

// default value must be set here
TestKeys.includeTestDependencies := true

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

// 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) {
/**
* Exclude all transitive dependencies of partest that include scala-parser-combinators.
* This way we avoid having two (or more) versions of scala-parser-combinators on a classpath.
* See this comment which describes the same issue for scala-xml
* 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-parser-combinators.
* 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-parser-combinators_2.11.0-M4")
exclude("org.scala-lang.modules", "scala-parser-combinators_2.11.0-M5")
exclude("org.scala-lang.modules", "scala-parser-combinators_2.11.0-M6"))
// (this space intentionally not left blank)
Seq("org.scala-lang.modules" % s"scala-partest-interface_${scalaBinaryVersion.value}" % "0.2" % "test" intransitive,
"org.scala-lang.modules" % s"scala-partest_${scalaBinaryVersion.value}" % TestKeys.partestVersion.value % "test" intransitive,
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0" % "test", // diffutils is needed by partest
"org.scala-lang" % "scala-compiler" % scalaVersion.value % "test").map(excludeScalaXml)
}
else Seq.empty
)

fork in Test := true

javaOptions in Test += "-Xmx1G"

testFrameworks += new TestFramework("scala.tools.partest.Framework")

definedTests in Test += (
new sbt.TestDefinition(
"partest",
// marker fingerprint since there are no test classes
// to be discovered by sbt:
new sbt.testing.AnnotatedFingerprint {
def isModule = true
def annotationName = "partest"
}, true, Array())
)

osgiSettings

val osgiVersion = version(_.replace('-', '.'))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/*
* filter: inliner warning\(s\); re-run with -Yinline-warnings for details
*/
import scala.util.parsing.json._
import scala.collection.immutable.TreeMap

@deprecated("Suppress warnings", since="2.11")
object Test extends App {
import org.junit.Test
import org.junit.Assert.{assertEquals, assertTrue}

class JsonTest {
/* This method converts parsed JSON back into real JSON notation with objects in
* sorted-key order. Not required by the spec, but it allows us to do a stable
* toString comparison. */
Expand All @@ -32,12 +31,8 @@ object Test extends App {
}

// For this one, just parsing should be considered a pass
def printJSON(given : String) {
JSON parseRaw given match {
case None => println("Parse failed for \"%s\"".format(given))
case Some(parsed) => println("Passed parse : " + sortJSON(parsed))
}
}
def printJSON(given : String) : Unit =
assertTrue("Parse failed for \"%s\"".format(given), (JSON parseRaw given).isDefined)

// For this usage, do a raw parse (to JSONObject/JSONArray)
def printJSON(given : String, expected : JSONType) {
Expand All @@ -52,76 +47,94 @@ object Test extends App {
// For this usage, do configurable parsing so that you can do raw if desired
def printJSON[T](given : String, parser : String => T, expected : Any) {
parser(given) match {
case None => println("Parse failed for \"%s\"".format(given))
case Some(parsed) => if (parsed == expected) {
println("Passed compare: " + parsed)
} else {
case None => assertTrue("Parse failed for \"%s\"".format(given), false)
case Some(parsed) => if (parsed != expected) {
val eStr = sortJSON(expected).toString
val pStr = sortJSON(parsed).toString
stringDiff(eStr,pStr)
assertTrue(stringDiff(eStr,pStr), false)
}
}
}

def stringDiff (expected : String, actual : String) {
def stringDiff (expected : String, actual : String): String = {
if (expected != actual) {
// Figure out where the Strings differ and generate a marker
val mismatchPosition = expected.toList.zip(actual.toList).indexWhere({case (x,y) => x != y}) match {
case -1 => Math.min(expected.length, actual.length)
case x => x
}
val reason = (" " * mismatchPosition) + "^"
println("Expected: %s\nGot : %s \n %s".format(expected, actual, reason))
"Expected: %s\nGot : %s \n %s".format(expected, actual, reason)

} else {
println("Passed compare: " + actual)
"Passed compare: " + actual
}
}


// The library should differentiate between lower case "l" and number "1" (ticket #136)
printJSON("{\"name\" : \"value\"}", JSONObject(Map("name" -> "value")))
printJSON("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")))
printJSON("{\"name\" : { \"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\" } }",
JSONObject(Map("name" -> JSONObject(Map("name1" -> "va1ue1", "name2" -> "va1ue2")))))
@Test
def testEllVsOne: Unit = {
printJSON("{\"name\" : \"value\"}", JSONObject(Map("name" -> "value")))
printJSON("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")))
printJSON("{\"name\" : { \"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\" } }",
JSONObject(Map("name" -> JSONObject(Map("name1" -> "va1ue1", "name2" -> "va1ue2")))))
}

// Unicode escapes should be handled properly
printJSON("{\"name\" : \"\\u0022\"}")
@Test
def testEscapes: Unit =
printJSON("{\"name\" : \"\\u0022\"}")

// The library should return a map for JSON objects (ticket #873)
printJSONFull("{\"function\" : \"add_symbol\"}", Map("function" -> "add_symbol"))
@Test
def testMap: Unit =
printJSONFull("{\"function\" : \"add_symbol\"}", Map("function" -> "add_symbol"))

// The library should recurse into arrays to find objects (ticket #2207)
printJSON("[{\"a\" : \"team\"},{\"b\" : 52}]", JSONArray(List(JSONObject(Map("a" -> "team")), JSONObject(Map("b" -> 52.0)))))
@Test
def testObjectsInArrays: Unit =
printJSON("[{\"a\" : \"team\"},{\"b\" : 52}]", JSONArray(List(JSONObject(Map("a" -> "team")), JSONObject(Map("b" -> 52.0)))))


// The library should differentiate between empty maps and lists (ticket #3284)
printJSONFull("{}", Map())
printJSONFull("[]", List())
@Test
def testEmptyMapsVsLists: Unit = {
printJSONFull("{}", Map())
printJSONFull("[]", List())
}

// Lists should be returned in the same order as specified
printJSON("[4,1,3,2,6,5,8,7]", JSONArray(List[Double](4,1,3,2,6,5,8,7)))
@Test
def testListOrder: Unit =
printJSON("[4,1,3,2,6,5,8,7]", JSONArray(List[Double](4,1,3,2,6,5,8,7)))

// Additional tests
@Test
def testAdditional: Unit =
printJSON("{\"age\": 0}")

// The library should do a proper toString representation using default and custom renderers (ticket #3605)
stringDiff("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")).toString)
stringDiff("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString)
@Test
def testDefaultAndCustomRenderers: Unit = {
assertEquals("{\"name\" : \"va1ue\"}", JSONObject(Map("name" -> "va1ue")).toString())
assertEquals("{\"name\" : {\"name1\" : \"va1ue1\", \"name2\" : \"va1ue2\"}}",
JSONObject(Map("name" -> JSONObject(TreeMap("name1" -> "va1ue1", "name2" -> "va1ue2")))).toString())

stringDiff("[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]", JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString)
assertEquals("[4.0, 1.0, 3.0, 2.0, 6.0, 5.0, 8.0, 7.0]", JSONArray(List[Double](4,1,3,2,6,5,8,7)).toString())
}

// A test method that escapes all characters in strings
def escapeEverything (in : Any) : String = in match {
case s : String => "\"" + s.map(c => "\\u%04x".format(c : Int)).mkString + "\""
case jo : JSONObject => jo.toString(escapeEverything)
case ja : JSONArray => ja.toString(escapeEverything)
case jo : JSONObject => jo.toString(escapeEverything _)
case ja : JSONArray => ja.toString(escapeEverything _)
case other => other.toString
}

stringDiff("{\"\\u006e\\u0061\\u006d\\u0065\" : \"\\u0076\\u0061\\u006c\"}", JSONObject(Map("name" -> "val")).toString(escapeEverything))
@Test
def testEscapeEverything: Unit =
assertEquals("{\"\\u006e\\u0061\\u006d\\u0065\" : \"\\u0076\\u0061\\u006c\"}", JSONObject(Map("name" -> "val")).toString(escapeEverything _))

println

// from http://en.wikipedia.org/wiki/JSON
val sample1 = """
Expand Down Expand Up @@ -156,9 +169,9 @@ object Test extends App {
)
)


printJSONFull(sample1, sample1Obj)
println
@Test
def testSample1: Unit =
printJSONFull(sample1, sample1Obj)

// from http://www.developer.com/lang/jscript/article.php/3596836
val sample2 = """
Expand Down Expand Up @@ -186,8 +199,9 @@ object Test extends App {
]
}"""

printJSON(sample2)
println
@Test
def testSampl2: Unit =
printJSON(sample2)

// from http://json.org/example.html
val sample3 = """
Expand Down Expand Up @@ -282,6 +296,7 @@ object Test extends App {
}
}"""

printJSON(sample3)
println
@Test
def testSample3 =
printJSON(sample3)
}
32 changes: 32 additions & 0 deletions src/test/scala/scala/util/parsing/combinator/UnitTestIO.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import org.junit.Test
import org.junit.Assert.assertEquals

class UnitTestIO {

@Test
def testUTF8 {
def decode(ch: Int) = new String(Array(ch), 0, 1).getBytes("UTF-8")

assert(new String( decode(0x004D), "utf8") == new String(Array(0x004D.asInstanceOf[Char])))
assert(new String( decode(0x0430), "utf8") == new String(Array(0x0430.asInstanceOf[Char])))
assert(new String( decode(0x4E8C), "utf8") == new String(Array(0x4E8C.asInstanceOf[Char])))
assert(new String(decode(0x10302), "utf8") == new String(Array(0xD800.asInstanceOf[Char],
0xDF02.asInstanceOf[Char])))
// a client
val test = "{\"a\":\"\\u0022\"}"
val expected = "a" -> "\""

val parsed = scala.util.parsing.json.JSON.parseFull(test)
val result = parsed == Some(Map(expected))
assertEquals(Some(Map(expected)), parsed)
}

@Test
def testSource {
val s = "Here is a test string"
val f = io.Source.fromBytes(s.getBytes("utf-8"))
val b = new collection.mutable.ArrayBuffer[Char]()
f.copyToBuffer(b)
assertEquals(new String(b.toArray), s)
}
}
28 changes: 28 additions & 0 deletions src/test/scala/scala/util/parsing/combinator/t0700.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.{File,StringReader}

import scala.util.parsing.combinator.Parsers
import scala.util.parsing.input.{CharArrayReader, StreamReader}

import org.junit.Test
import org.junit.Assert.assertEquals

class T0700 {
class TestParsers extends Parsers {
type Elem = Char

def p: Parser[List[Int]] = rep(p1 | p2)
def p1: Parser[Int] = 'a' ~ nl ~ 'b' ~ nl ^^^ 1
def p2: Parser[Int] = 'a' ~ nl ^^^ 2
def nl: Parser[Int] = rep(accept('\n') | accept('\r')) ^^^ 0
}

@Test
def test: Unit = {
val tstParsers = new TestParsers
val s = "a\na\na"
val r1 = new CharArrayReader(s.toCharArray())
val r2 = StreamReader(new StringReader(s))
assertEquals("[3.2] parsed: List(2, 2, 2)", tstParsers.p(r1).toString)
assertEquals("[3.2] parsed: List(2, 2, 2)", tstParsers.p(r2).toString)
}
}
31 changes: 31 additions & 0 deletions src/test/scala/scala/util/parsing/combinator/t1100.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import scala.util.parsing.combinator.Parsers
import scala.util.parsing.input.CharSequenceReader

import java.io.{File,StringReader}

import scala.util.parsing.combinator.Parsers
import scala.util.parsing.input.{CharArrayReader, StreamReader}

import org.junit.Test
import org.junit.Assert.assertEquals

class T1100 {
class TestParsers extends Parsers {
type Elem = Char

def p: Parser[List[Char]] = rep1(p1)
def p1: Parser[Char] = accept('a') | err("errors are propagated")
}

val expected = """[1.4] error: errors are propagated

aaab
^"""

@Test
def test(): Unit = {
val tstParsers = new TestParsers
val s = new CharSequenceReader("aaab")
assertEquals(expected, tstParsers.p(s).toString)
}
}
12 changes: 12 additions & 0 deletions src/test/scala/scala/util/parsing/combinator/t4138.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import org.junit.Test
import org.junit.Assert.assertEquals

class T4138 {
object p extends scala.util.parsing.combinator.JavaTokenParsers

@Test
def test: Unit = {
assertEquals("""[1.45] parsed: "lir 'de\' ' \\ \n / upa \"new\" \t parsing"""", p.parse(p.stringLiteral, """"lir 'de\' ' \\ \n / upa \"new\" \t parsing"""").toString)
assertEquals("""[1.5] parsed: "s """", p.parse(p.stringLiteral, """"s " lkjse"""").toString)
}
}
Loading