JS backend: dropped unnecessary intrinsics in StringFIF. Added tests for CharSequence.size, CharSequence.length, CharSequence.length() and CharSequence.isEmpty().

This commit is contained in:
Zalim Bashorov
2014-01-29 14:13:24 +04:00
parent 1f1da542b7
commit 5c0eeb59f6
5 changed files with 39 additions and 16 deletions
+4 -4
View File
@@ -35,14 +35,14 @@ native public fun String.match(regex : String) : Array<String> = js.noImpl
native public fun String.trim() : String = js.noImpl
library
public val String.size: Int = js.noImpl
native("length")
public val CharSequence.size: Int = js.noImpl
library
public fun String.length(): Int = js.noImpl
public fun CharSequence.length(): Int = js.noImpl
library
public fun String.isEmpty(): Boolean = js.noImpl
public fun CharSequence.isEmpty(): Boolean = js.noImpl
/*
@@ -86,6 +86,6 @@ public final class StringTest extends AbstractExpressionTest {
}
public void testExtensionMethods() throws Exception {
fooBoxTest();
checkFooBoxIsOk();
}
}
@@ -27,11 +27,7 @@ public final class StringOperationFIF extends CompositeFIF {
private StringOperationFIF() {
add(pattern("jet", "String", "get"), new BuiltInFunctionIntrinsic("charAt"));
add(pattern("jet", "String", "<get-length>"), LENGTH_PROPERTY_INTRINSIC);
add(pattern("js", "<get-size>").receiverExists(), LENGTH_PROPERTY_INTRINSIC);
add(pattern("js", "length").receiverExists(), LENGTH_PROPERTY_INTRINSIC);
add(pattern("jet", "CharSequence", "<get-length>"), LENGTH_PROPERTY_INTRINSIC);
add(pattern("js", "isEmpty").receiverExists(), IS_EMPTY_INTRINSIC);
}
}
@@ -1,6 +1,36 @@
package foo
fun box(): Boolean {
val s = "bar"
return s.size == 3 && s.length() == 3 && s.length == 3 && s.startsWith("b") && s.endsWith("r") && s.contains("a") && !s.isEmpty()
}
val testString = "foobarbaz"
val testStringSize = 9
val emptyString = ""
val startsWithParam = "foo"
val endsWithParam = "az"
val containsParam = "ar"
fun assertEquals(actual: Any, expected: Any, s: String, whatTested: String) =
if (expected != actual) "String.$whatTested fails on \"$s\", expected: $expected, actual: $actual" else null
fun assertEquals(actual: Any, expected: Any, s: CharSequence, whatTested: CharSequence) =
if (expected != actual) "CharSequence.$whatTested fails on \"$s\", expected: $expected, actual: $actual" else null
fun testString(s: String, expectedSize: Int): String? =
assertEquals(s.size, expectedSize, s, "size") ?:
assertEquals(s.length(), expectedSize, s, "length()") ?:
assertEquals(s.length, expectedSize, s, "length") ?:
assertEquals(s.isEmpty(), expectedSize == 0, s, "isEmpty()") ?:
assertEquals(s.startsWith(startsWithParam), expectedSize != 0, s, "startsWith(\"$startsWithParam\")") ?:
assertEquals(s.endsWith(endsWithParam), expectedSize != 0, s, "endsWith(\"$endsWithParam\")") ?:
assertEquals(s.contains(containsParam), expectedSize != 0, s, "contains(\"$containsParam\")")
fun testCharSequence(s: CharSequence, expectedSize: Int): String? =
assertEquals(s.size, expectedSize, s, "size") ?:
assertEquals(s.length(), expectedSize, s, "length()") ?:
assertEquals(s.length, expectedSize, s, "length") ?:
assertEquals(s.isEmpty(), expectedSize == 0, s, "isEmpty()")
fun box(): String =
testString(testString, testStringSize) ?:
testString(emptyString, 0) ?:
testCharSequence(testString, testStringSize) ?:
testCharSequence(emptyString, 0) ?:
"OK"
@@ -123,9 +123,6 @@ fun printField(s : String, steps : Int) {
fun <T> Array<T>.toList() : List<T> = this.to(ArrayList<T>())
val String?.size : Int
get() = if (this != null) this.length else 0;
fun <T, C: MutableCollection<T>> Array<T>.to(result: C) : C {
for (elem in this)
result.add(elem)