summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Tisue <seth@tisue.net>2016-01-13 16:37:21 -0500
committerSeth Tisue <seth@tisue.net>2016-01-13 16:37:21 -0500
commit14d0931053f94963b1f734ad0a6cd22196b8474f (patch)
tree415439490e3723f71582252f220d06fdd0a69f6b
parent334cbc720fd3020b59271bf060747bc2350e1f89 (diff)
parentc47a60b2cd34a2d66b2908e0a3931cb5a624b9a6 (diff)
downloadscala-14d0931053f94963b1f734ad0a6cd22196b8474f.tar.gz
scala-14d0931053f94963b1f734ad0a6cd22196b8474f.tar.bz2
scala-14d0931053f94963b1f734ad0a6cd22196b8474f.zip
Merge pull request #4900 from mpociecha/correct-decimal-marks-in-tests2
Fix problems with a locale-dependent decimal mark in StringContextTest
-rw-r--r--test/junit/scala/StringContextTest.scala52
1 files changed, 36 insertions, 16 deletions
diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala
index b93cd978f4..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)
// -------------------------------
@@ -236,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"
)