diff options
author | Lukas Rytz <lukas.rytz@gmail.com> | 2016-01-24 13:03:22 +0100 |
---|---|---|
committer | Lukas Rytz <lukas.rytz@gmail.com> | 2016-01-24 13:03:22 +0100 |
commit | b0831e774e290148ce28cb7957794542dffe0366 (patch) | |
tree | 1380b224b4fe31598b07b3ddee9a46ff2e3eaf10 /test/junit | |
parent | 1a9e649e34cda4306537d74ab425c33d5f6a77db (diff) | |
parent | 1081e718f8f8e174dbf615e42b157e187d3d3886 (diff) | |
download | scala-b0831e774e290148ce28cb7957794542dffe0366.tar.gz scala-b0831e774e290148ce28cb7957794542dffe0366.tar.bz2 scala-b0831e774e290148ce28cb7957794542dffe0366.zip |
Merge remote-tracking branch 'upstream/2.12.x' into opt/elimBoxes
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/PartialFunctionSerializationTest.scala | 36 | ||||
-rw-r--r-- | test/junit/scala/StringContextTest.scala | 57 | ||||
-rw-r--r-- | test/junit/scala/collection/mutable/SetLikeTest.scala | 26 | ||||
-rw-r--r-- | test/junit/scala/sys/process/t7350.scala | 1 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/transform/delambdafy/DelambdafyTest.scala | 73 | ||||
-rw-r--r-- | test/junit/scala/util/SystemPropertiesTest.scala | 27 |
6 files changed, 201 insertions, 19 deletions
diff --git a/test/junit/scala/PartialFunctionSerializationTest.scala b/test/junit/scala/PartialFunctionSerializationTest.scala new file mode 100644 index 0000000000..d525b045cd --- /dev/null +++ b/test/junit/scala/PartialFunctionSerializationTest.scala @@ -0,0 +1,36 @@ +package scala + +import org.junit.Test +import org.junit.Assert._ +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class PartialFunctionSerializationTest { + val pf1: PartialFunction[Int, Int] = { + case n if n > 0 => 1 + } + + val pf2: PartialFunction[Int, Int] = { + case n if n <= 0 => 2 + } + + + private def assertSerializable[A,B](fn: A => B) = { + import java.io._ + + new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(fn) + } + + @Test def canSerializeLiteral= assertSerializable(pf1) + + @Test def canSerializeLifted= assertSerializable(pf1.lift) + + @Test def canSerializeOrElse = assertSerializable(pf1 orElse pf2) + + @Test def canSerializeUnlifted = assertSerializable(Function.unlift((x: Int) => Some(x))) + + @Test def canSerializeAndThen = assertSerializable(pf1.andThen((x: Int) => x)) + + @Test def canSerializeEmpty = assertSerializable(PartialFunction.empty) +} diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala index 900852fcc6..b5af6de7eb 100644 --- a/test/junit/scala/StringContextTest.scala +++ b/test/junit/scala/StringContextTest.scala @@ -1,6 +1,8 @@ package scala +import java.text.DecimalFormat + import language.implicitConversions import org.junit.Test @@ -10,10 +12,32 @@ import org.junit.runners.JUnit4 import scala.tools.testing.AssertUtil._ +object StringContextTestUtils { + private val decimalSeparator: Char = new DecimalFormat().getDecimalFormatSymbols().getDecimalSeparator() + private val numberPattern = """(\d+)\.(\d+.*)""".r + + implicit class StringContextOps(val sc: StringContext) extends AnyVal { + // Use this String interpolator to avoid problems with a locale-dependent decimal mark. + def locally(numbers: String*): String = { + val numbersWithCorrectLocale = numbers.map(applyProperLocale) + sc.s(numbersWithCorrectLocale: _*) + } + + // Handles cases like locally"3.14" - it's prettier than locally"${"3.14"}". + def locally(): String = sc.parts.map(applyProperLocale).mkString + + private def applyProperLocale(number: String): String = { + val numberPattern(intPart, fractionalPartAndSuffix) = number + s"$intPart$decimalSeparator$fractionalPartAndSuffix" + } + } +} + @RunWith(classOf[JUnit4]) class StringContextTest { import StringContext._ + import StringContextTestUtils.StringContextOps @Test def noEscape() = { val s = "string" @@ -67,26 +91,22 @@ class StringContextTest { @Test def fIf() = { val res = f"${if (true) 2.5 else 2.5}%.2f" - val expected = formatUsingCurrentLocale(2.50) + val expected = locally"2.50" assertEquals(expected, res) } @Test def fIfNot() = { val res = f"${if (false) 2.5 else 3.5}%.2f" - val expected = formatUsingCurrentLocale(3.50) + val expected = locally"3.50" assertEquals(expected, res) } @Test def fHeteroArgs() = { val res = f"${3.14}%.2f rounds to ${3}%d" - val expected = formatUsingCurrentLocale(3.14) + " rounds to 3" + val expected = locally"${"3.14"} rounds to 3" assertEquals(expected, res) } - // Use this method to avoid problems with a locale-dependent decimal mark. - // The string interpolation is not used here intentionally as this method is used to test string interpolation. - private def formatUsingCurrentLocale(number: Double, decimalPlaces: Int = 2) = ("%." + decimalPlaces + "f").format(number) - @Test def `f interpolator baseline`(): Unit = { implicit def stringToBoolean(s: String): Boolean = java.lang.Boolean.parseBoolean(s) @@ -203,17 +223,17 @@ class StringContextTest { // 'e' | 'E' | 'g' | 'G' | 'f' | 'a' | 'A' (category: floating point) // ------------------------------------------------------------------ - f"${3.4f}%e" -> "3.400000e+00", - f"${3.4}%e" -> "3.400000e+00", - f"${3.4f : java.lang.Float}%e" -> "3.400000e+00", - f"${3.4 : java.lang.Double}%e" -> "3.400000e+00", + f"${3.4f}%e" -> locally"3.400000e+00", + f"${3.4}%e" -> locally"3.400000e+00", + f"${3.4f : java.lang.Float}%e" -> locally"3.400000e+00", + f"${3.4 : java.lang.Double}%e" -> locally"3.400000e+00", - f"${BigDecimal(3.4)}%e" -> "3.400000e+00", + f"${BigDecimal(3.4)}%e" -> locally"3.400000e+00", - f"${new java.math.BigDecimal(3.4)}%e" -> "3.400000e+00", + f"${new java.math.BigDecimal(3.4)}%e" -> locally"3.400000e+00", - f"${3}%e" -> "3.000000e+00", - f"${3L}%e" -> "3.000000e+00", + f"${3}%e" -> locally"3.000000e+00", + f"${3L}%e" -> locally"3.000000e+00", // 't' | 'T' (category: date/time) // ------------------------------- @@ -224,11 +244,10 @@ class StringContextTest { // literals and arg indexes f"%%" -> "%", - f" mind%n------%nmatter%n" -> + f" mind%n------%nmatter" -> """| mind |------ - |matter - |""".stripMargin, + |matter""".stripMargin.lines.mkString(compat.Platform.EOL), f"${i}%d %<d ${9}%d" -> "42 42 9", f"${7}%d %<d ${9}%d" -> "7 7 9", f"${7}%d %2$$d ${9}%d" -> "7 9 9", @@ -237,7 +256,7 @@ class StringContextTest { f"${5: Any}" -> "5", f"${5}%s%<d" -> "55", - f"${3.14}%s,%<f" -> "3.14,3.140000", + f"${3.14}%s,%<f" -> locally"3.14,${"3.140000"}", f"z" -> "z" ) diff --git a/test/junit/scala/collection/mutable/SetLikeTest.scala b/test/junit/scala/collection/mutable/SetLikeTest.scala new file mode 100644 index 0000000000..c819024558 --- /dev/null +++ b/test/junit/scala/collection/mutable/SetLikeTest.scala @@ -0,0 +1,26 @@ +package scala.collection.mutable + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class SetLikeTest { + + class MySet(self: Set[String]) extends Set[String] with SetLike[String, MySet] { + override def -=(elem: String) = { self -= elem; this } + override def +=(elem: String) = { self += elem; this } + + override def empty = new MySet(self.empty) + override def iterator = self.iterator + override def contains(elem: String) = self.contains(elem) + } + + @Test + def hasCorrectClear() { + val s = new MySet(Set("EXPOSEDNODE", "CONNECTABLE")) + s.clear() + assertEquals(new MySet(Set()), s) + } +} diff --git a/test/junit/scala/sys/process/t7350.scala b/test/junit/scala/sys/process/t7350.scala index 7f3e8897f2..3c0e5145e6 100644 --- a/test/junit/scala/sys/process/t7350.scala +++ b/test/junit/scala/sys/process/t7350.scala @@ -18,6 +18,7 @@ import scala.util.control.Exception.ignoring class PipedProcessTest { class ProcessMock(error: Boolean) extends Process { var destroyCount = 0 + def isAlive() = false def exitValue(): Int = { if (error) { throw new InterruptedException() diff --git a/test/junit/scala/tools/nsc/transform/delambdafy/DelambdafyTest.scala b/test/junit/scala/tools/nsc/transform/delambdafy/DelambdafyTest.scala new file mode 100644 index 0000000000..010078e28a --- /dev/null +++ b/test/junit/scala/tools/nsc/transform/delambdafy/DelambdafyTest.scala @@ -0,0 +1,73 @@ +package scala.tools.nsc.transform.delambdafy + +import scala.reflect.io.Path.jfile2path +import scala.tools.nsc.backend.jvm.CodeGenTools.getGeneratedClassfiles +import scala.tools.nsc.backend.jvm.CodeGenTools.makeSourceFile +import scala.tools.nsc.backend.jvm.CodeGenTools.newCompilerWithoutVirtualOutdir +import scala.tools.nsc.io.AbstractFile +import scala.tools.testing.TempDir + +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class DelambdafyTest { + def compileToMultipleOutputWithDelamdbafyMethod(): List[(String, Array[Byte])] = { + val codeForMultiOutput = """ +object Delambdafy { + type -->[D, I] = PartialFunction[D, I] + + def main(args: Array[String]): Unit = { + val result = List(1, 2, 4).map { a => + val list = List("1", "2", "3").map { _ + "test" } + list.find { _ == a.toString + "test" } + } + println(result) + lazy val _foo = foo(result) { + case x :: xs if x isDefined => x.get.length + case _ => 0 + } + println(_foo) + lazy val bar: Int => Int = { + case 2 => 23 + case _ => + val v = List(1).map { _ + 42 }.head + v + 31 + } + bar(3) + lazy val _baz = baz { + case 1 => + val local = List(1).map(_ + 1) + local.head + } + } + + def baz[T](f: Any --> Any): Any => Any = f + + def foo(b: List[Option[String]])(a: List[Option[String]] => Int): Int = a(b) +} +""" + val srcFile = makeSourceFile(codeForMultiOutput, "delambdafyTest.scala") + val outDir = AbstractFile.getDirectory(TempDir.createTempDir()) + val outDirPath = outDir.canonicalPath + val extraArgs = "-Ybackend:GenBCode -Ydelambdafy:method" + val argsWithOutDir = extraArgs + s" -d $outDirPath -cp $outDirPath" + val compiler = newCompilerWithoutVirtualOutdir(extraArgs = argsWithOutDir) + compiler.settings.outputDirs.add(srcFile.file, outDir) + + new compiler.Run().compileSources(List(srcFile)) + + val classfiles = getGeneratedClassfiles(outDir) + outDir.delete() + classfiles + } + + @Test + def shouldFindOutputFoldersForAllPromotedLambdasAsMethod(): Unit = { + val actual = compileToMultipleOutputWithDelamdbafyMethod() + + assertTrue(actual.length > 0) + } +} diff --git a/test/junit/scala/util/SystemPropertiesTest.scala b/test/junit/scala/util/SystemPropertiesTest.scala new file mode 100644 index 0000000000..38e830eb88 --- /dev/null +++ b/test/junit/scala/util/SystemPropertiesTest.scala @@ -0,0 +1,27 @@ +package scala.util + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test +import org.junit.Assert._ + +@RunWith(classOf[JUnit4]) +class SystemPropertiesTest { + @Test + def filterAll(): Unit = { + val isEmpty = sys.props.filter(_ => false).size == 0 + assertTrue("A filter matching nothing should produce an empty result", isEmpty) + } + + @Test + def filterNone(): Unit = { + val isUnchanged = sys.props.filter(_ => true) == sys.props + assertTrue("A filter matching everything should not change the result", isUnchanged) + } + + @Test + def empty(): Unit = { + val hasSize0 = sys.props.empty.size == 0 + assertTrue("SystemProperties.empty should have size of 0", hasSize0) + } +} |