summaryrefslogtreecommitdiff
path: root/test/junit/scala/collection
diff options
context:
space:
mode:
authorMike Pheasant <mike@insilico.io>2016-06-01 08:51:41 +1000
committerJason Zaugg <jzaugg@gmail.com>2016-06-01 08:51:41 +1000
commit608387529b790c2a0fc87b4cf5b5324d68d02648 (patch)
treea5d66e9c0fd0dce03bf9cc0ca9e9eb6694c4090b /test/junit/scala/collection
parent8f567bc9c01e29624f5b7fcce40a0ee7fe261c08 (diff)
downloadscala-608387529b790c2a0fc87b4cf5b5324d68d02648.tar.gz
scala-608387529b790c2a0fc87b4cf5b5324d68d02648.tar.bz2
scala-608387529b790c2a0fc87b4cf5b5324d68d02648.zip
SI-9767 document and test behaviour of String->integer/float conversions
We delegate `String`'s extension methods `toInt`, `toFloat`, etc to corresponding methods in the Java standard library. These differ in the way they handle whitespace in the original string. This commit documents and tests the current behaviour.
Diffstat (limited to 'test/junit/scala/collection')
-rw-r--r--test/junit/scala/collection/immutable/StringLikeTest.scala31
1 files changed, 31 insertions, 0 deletions
diff --git a/test/junit/scala/collection/immutable/StringLikeTest.scala b/test/junit/scala/collection/immutable/StringLikeTest.scala
index 50be638b89..44bade860e 100644
--- a/test/junit/scala/collection/immutable/StringLikeTest.scala
+++ b/test/junit/scala/collection/immutable/StringLikeTest.scala
@@ -1,5 +1,6 @@
package scala.collection.immutable
+import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
@@ -40,4 +41,34 @@ class StringLikeTest {
AssertUtil.assertSameElements("--ch--omp--".split("-"), Array("", "", "ch", "", "omp")) // All the cases!
AssertUtil.assertSameElements(twopairs.split(high), Array(twopairs)) //don't split on characters that are half a surrogate pair
}
+
+ /* Test for SI-9767 */
+ @Test
+ def testNumericConversion: Unit = {
+ val sOne = " \t\n 1 \n\r\t "
+ val sOk = "2"
+ val sNull:String = null
+
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toInt)
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toLong)
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toShort)
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toByte)
+ assertTrue("trim toDouble", sOne.toDouble == 1.0d)
+ assertTrue("trim toFloat", sOne.toFloat == 1.0f)
+
+ assertTrue("no trim toInt", sOk.toInt == 2)
+ assertTrue("no trim toLong", sOk.toLong == 2L)
+ assertTrue("no trim toShort", sOk.toShort == 2.toShort)
+ assertTrue("no trim toByte", sOk.toByte == 2.toByte)
+ assertTrue("no trim toDouble", sOk.toDouble == 2.0d)
+ assertTrue("no trim toFloat", sOk.toFloat == 2.0f)
+
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toInt, {s => s == "null"})
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toLong, {s => s == "null"})
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toShort, {s => s == "null"})
+ AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toByte, {s => s == "null"})
+
+ AssertUtil.assertThrows[java.lang.NullPointerException](sNull.toDouble)
+ AssertUtil.assertThrows[java.lang.NullPointerException](sNull.toFloat)
+ }
}