added request.getParameter() code for more accurate comparsion - thanks for the heads up Cedric!
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
package language.scala
|
package language.scala
|
||||||
|
|
||||||
import kotlin. *
|
import junit.framework.TestCase
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
import junit.framework.TestCase
|
class Request(val value: String?) {
|
||||||
import kotlin.util.arrayList
|
fun getParameter(name: String): String? {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test case shows how we can use T?, the Kotlin nullable type instead of Option[T] in Scala
|
* This test case shows how we can use T?, the Kotlin nullable type instead of Option[T] in Scala
|
||||||
@@ -21,7 +24,7 @@ import kotlin.util.arrayList
|
|||||||
class OptionTest: TestCase() {
|
class OptionTest: TestCase() {
|
||||||
|
|
||||||
fun testPatternMatching() {
|
fun testPatternMatching() {
|
||||||
fun foo(name: String?): String {
|
fun foo(request: Request): String {
|
||||||
|
|
||||||
/* Scala:
|
/* Scala:
|
||||||
|
|
||||||
@@ -37,6 +40,7 @@ class OptionTest: TestCase() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Kotlin version:
|
// Kotlin version:
|
||||||
|
val name = request.getParameter("name")
|
||||||
return when (name) {
|
return when (name) {
|
||||||
is String -> {
|
is String -> {
|
||||||
name.trim().toUpperCase()
|
name.trim().toUpperCase()
|
||||||
@@ -47,16 +51,16 @@ class OptionTest: TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals("No name value", foo(null))
|
assertEquals("No name value", foo(Request(null)))
|
||||||
assertEquals("BAR", foo("BAR"))
|
assertEquals("BAR", foo(Request("BAR")))
|
||||||
assertEquals("BAR", foo(" bar "))
|
assertEquals("BAR", foo(Request(" bar ")))
|
||||||
|
|
||||||
println("foo(null) = ${foo(null)}")
|
println("foo(null) = ${foo(Request(null))}")
|
||||||
println("foo(\" bar \") = ${foo(" bar ")}")
|
println("foo(\" bar \") = ${foo(Request(" bar "))}")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun testPatternMatchingUsingIf() {
|
fun testPatternMatchingUsingIf() {
|
||||||
fun foo2(name: String?): String {
|
fun foo2(request: Request): String {
|
||||||
|
|
||||||
/* Scala:
|
/* Scala:
|
||||||
|
|
||||||
@@ -72,6 +76,7 @@ class OptionTest: TestCase() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Kotlin version
|
// Kotlin version
|
||||||
|
val name = request.getParameter("name")
|
||||||
return if (name != null) {
|
return if (name != null) {
|
||||||
name.trim().toUpperCase()
|
name.trim().toUpperCase()
|
||||||
} else {
|
} else {
|
||||||
@@ -79,12 +84,12 @@ class OptionTest: TestCase() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals("No name value", foo2(null))
|
assertEquals("No name value", foo2(Request(null)))
|
||||||
assertEquals("BAR", foo2("BAR"))
|
assertEquals("BAR", foo2(Request("BAR")))
|
||||||
assertEquals("BAR", foo2(" bar "))
|
assertEquals("BAR", foo2(Request(" bar ")))
|
||||||
|
|
||||||
println("foo2(null) = ${foo2(null)}")
|
println("foo2(Request(null)) = ${foo2(Request(null))}")
|
||||||
println("foo2(\" bar \") = ${foo2(" bar ")}")
|
println("foo2(Request(\" bar \")) = ${foo2(Request(" bar "))}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -114,20 +119,21 @@ class OptionTest: TestCase() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun testCompositionWithFor() {
|
fun testCompositionWithFor() {
|
||||||
fun foo3(name: String?): String {
|
fun foo3(request: Request): String {
|
||||||
/* Scala:
|
/* Scala:
|
||||||
|
|
||||||
val upper = for {
|
val upper = for {
|
||||||
name <- request.getParameter("name")
|
name <- request.getParameter("name")
|
||||||
trimmed <- Some(name.trim)
|
trimmed <- Some(name.trim)
|
||||||
upper <- Some(trimmed.toUpperCase) if trimmed.length != 0
|
upper <- Some(trimmed.toUpperCase) if trimmed.length != 0
|
||||||
} yield upper
|
} yield upper
|
||||||
println(upper.getOrElse(""))
|
println(upper.getOrElse(""))
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Kotlin version
|
// Kotlin version
|
||||||
// not as clean as we've no way to compose if statements so have
|
// not as clean as we've no way to compose if statements so have
|
||||||
// to cheat and use returns
|
// to cheat and use returns
|
||||||
|
val name = request.getParameter("name")
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
val trimmed = name.trim()
|
val trimmed = name.trim()
|
||||||
if (trimmed.length() != 0) {
|
if (trimmed.length() != 0) {
|
||||||
@@ -136,7 +142,9 @@ class OptionTest: TestCase() {
|
|||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assertEquals("", foo3(Request(null)))
|
||||||
|
assertEquals("", foo3(Request("")))
|
||||||
|
assertEquals("BAR", foo3(Request(" bar ")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user