Chromium Code Reviews| Index: tests/corelib/string_test.dart |
| diff --git a/tests/corelib/string_test.dart b/tests/corelib/string_test.dart |
| index 2d34c063f103a7f572c2dfa489bb5b6f4325afcf..b671c8e216e265a61a9a7e217438d33f39d674e6 100644 |
| --- a/tests/corelib/string_test.dart |
| +++ b/tests/corelib/string_test.dart |
| @@ -4,387 +4,387 @@ |
| import "package:expect/expect.dart"; |
| -class StringTest { |
| - |
| - static testMain() { |
| - testOutOfRange(); |
| - testIllegalArgument(); |
| - testConcat(); |
| - testIndex(); |
| - testCodeUnitAt(); |
| - testEquals(); |
| - testEndsWith(); |
| - testStartsWith(); |
| - testIndexOf(); |
| - testLastIndexOf(); |
| - testContains(); |
| - testReplaceAll(); |
| - testCompareTo(); |
| - testCharCodes(); |
| - } |
| +void main() { |
| + testOutOfRange(); |
| + testIllegalArgument(); |
| + testConcat(); |
| + testIndex(); |
| + testCodeUnitAt(); |
| + testEquals(); |
| + testEndsWith(); |
| + testStartsWith(); |
| + testIndexOf(); |
| + testLastIndexOf(); |
| + testContains(); |
| + testReplaceAll(); |
| + testCompareTo(); |
| + testCharCodes(); |
| + testRepeat(); |
| + testPadLeft(); |
| + testPadRight(); |
| +} |
| - static void testLength() { |
| - String str = ""; |
| - for (var i = 0; i < 20; i++) { |
| - testStringLength(i, str); |
| - str += " "; |
| - } |
| +void testLength() { |
| + String str = ""; |
| + for (var i = 0; i < 20; i++) { |
| + testStringLength(i, str); |
| + str += " "; |
| } |
| +} |
| - static void testOutOfRange() { |
| - String a = "Hello"; |
| - bool exception_caught = false; |
| - try { |
| - var c = a[20]; // Throw exception. |
| - } on RangeError catch (e) { |
| - exception_caught = true; |
| - } |
| - Expect.isTrue(exception_caught); |
| +void testOutOfRange() { |
| + String a = "Hello"; |
| + bool exception_caught = false; |
| + try { |
| + var c = a[20]; // Throw exception. |
| + } on RangeError catch (e) { |
| + exception_caught = true; |
| } |
| + Expect.isTrue(exception_caught); |
| +} |
| - static testIllegalArgument() { |
| - String a = "Hello"; |
| - bool exception_caught = false; |
| - try { |
| - var c = a[2.2]; // Throw exception. |
| - Expect.fail("Accepting double as index"); |
| - } on ArgumentError catch (e) { |
| - exception_caught = true; |
| - } on TypeError catch (e) { // Thrown in checked mode only. |
| - exception_caught = true; |
| - } |
| - Expect.isTrue(exception_caught); |
| +void testIllegalArgument() { |
| + String a = "Hello"; |
| + bool exception_caught = false; |
| + try { |
| + var c = a[2.2]; // Throw exception. |
| + Expect.fail("Accepting double as index"); |
| + } on ArgumentError catch (e) { |
| + exception_caught = true; |
| + } on TypeError catch (e) { // Thrown in checked mode only. |
| + exception_caught = true; |
| } |
| + Expect.isTrue(exception_caught); |
| +} |
| - static testIndex() { |
| - String str = "string"; |
| - for (int i = 0; i < str.length; i++) { |
| - Expect.isTrue(str[i] is String); |
| - testStringLength(1, str[i]); |
| - } |
| +void testIndex() { |
| + String str = "string"; |
| + for (int i = 0; i < str.length; i++) { |
| + Expect.isTrue(str[i] is String); |
| + testStringLength(1, str[i]); |
| } |
| +} |
| - static testCodeUnitAt() { |
| - String str = "string"; |
| - for (int i = 0; i < str.length; i++) { |
| - Expect.isTrue(str.codeUnitAt(i) is int); |
| - } |
| +void testCodeUnitAt() { |
| + String str = "string"; |
| + for (int i = 0; i < str.length; i++) { |
| + Expect.isTrue(str.codeUnitAt(i) is int); |
| } |
| +} |
| - static testConcat() { |
| - var a = "One"; |
| - var b = "Four"; |
| - var c = a + b; |
| - testStringLength(7, c); |
| - Expect.equals("OneFour", c); |
| - } |
| +void testConcat() { |
| + var a = "One"; |
| + var b = "Four"; |
| + var c = a + b; |
| + testStringLength(7, c); |
| + Expect.equals("OneFour", c); |
| +} |
| - static testEquals() { |
| - Expect.equals("str", "str"); |
| +void testEquals() { |
| + Expect.equals("str", "str"); |
| - Expect.equals("str", "s" + "t" + "r"); |
| - Expect.equals("s" + "t" + "r", "str"); |
| + Expect.equals("str", "s" + "t" + "r"); |
| + Expect.equals("s" + "t" + "r", "str"); |
| - Expect.isFalse("str" == "s"); |
| - Expect.isFalse("str" == "r"); |
| - Expect.isFalse("str" == "st"); |
| - Expect.isFalse("str" == "tr"); |
| + Expect.isFalse("str" == "s"); |
| + Expect.isFalse("str" == "r"); |
| + Expect.isFalse("str" == "st"); |
| + Expect.isFalse("str" == "tr"); |
| - Expect.isFalse("s" == "str"); |
| - Expect.isFalse("r" == "str"); |
| - Expect.isFalse("st" == "str"); |
| - Expect.isFalse("tr" == "str"); |
| + Expect.isFalse("s" == "str"); |
| + Expect.isFalse("r" == "str"); |
| + Expect.isFalse("st" == "str"); |
| + Expect.isFalse("tr" == "str"); |
| - Expect.isFalse("" == "s"); |
| - Expect.equals("", ""); |
| - } |
| + Expect.isFalse("" == "s"); |
| + Expect.equals("", ""); |
| +} |
| - static testEndsWith() { |
| - Expect.isTrue("str".endsWith("r")); |
| - Expect.isTrue("str".endsWith("tr")); |
| - Expect.isTrue("str".endsWith("str")); |
| +void testEndsWith() { |
| + Expect.isTrue("str".endsWith("r")); |
| + Expect.isTrue("str".endsWith("tr")); |
| + Expect.isTrue("str".endsWith("str")); |
| - Expect.isFalse("str".endsWith("stri")); |
| - Expect.isFalse("str".endsWith("t")); |
| - Expect.isFalse("str".endsWith("st")); |
| - Expect.isFalse("str".endsWith("s")); |
| + Expect.isFalse("str".endsWith("stri")); |
| + Expect.isFalse("str".endsWith("t")); |
| + Expect.isFalse("str".endsWith("st")); |
| + Expect.isFalse("str".endsWith("s")); |
| - Expect.isTrue("".endsWith("")); |
| - Expect.isFalse("".endsWith("s")); |
| - } |
| + Expect.isTrue("".endsWith("")); |
| + Expect.isFalse("".endsWith("s")); |
| +} |
| - static testStartsWith() { |
| - Expect.isTrue("str".startsWith("s")); |
| - Expect.isTrue("str".startsWith("st")); |
| - Expect.isTrue("str".startsWith("str")); |
| - |
| - Expect.isFalse("str".startsWith("stri")); |
| - Expect.isFalse("str".startsWith("r")); |
| - Expect.isFalse("str".startsWith("tr")); |
| - Expect.isFalse("str".startsWith("t")); |
| - |
| - Expect.isTrue("".startsWith("")); |
| - Expect.isFalse("".startsWith("s")); |
| - |
| - Expect.isFalse("strstr".startsWith("s", 1)); |
| - Expect.isFalse("strstr".startsWith("s", 2)); |
| - Expect.isTrue("strstr".startsWith("s", 3)); |
| - Expect.isFalse("strstr".startsWith("s", 4)); |
| - |
| - Expect.isFalse("strstr".startsWith("st", 1)); |
| - Expect.isFalse("strstr".startsWith("st", 2)); |
| - Expect.isTrue("strstr".startsWith("st", 3)); |
| - Expect.isFalse("strstr".startsWith("st", 4)); |
| - |
| - Expect.isFalse("strstr".startsWith("str", 1)); |
| - Expect.isFalse("strstr".startsWith("str", 2)); |
| - Expect.isTrue("strstr".startsWith("str", 3)); |
| - Expect.isFalse("strstr".startsWith("str", 4)); |
| - |
| - Expect.isTrue("str".startsWith("", 0)); |
| - Expect.isTrue("str".startsWith("", 1)); |
| - Expect.isTrue("str".startsWith("", 2)); |
| - Expect.isTrue("str".startsWith("", 3)); |
| - |
| - Expect.throws(() => "str".startsWith("", -1)); |
| - Expect.throws(() => "str".startsWith("", 4)); |
| - |
| - var regexp = new RegExp("s(?:tr?)?"); |
| - Expect.isTrue("s".startsWith(regexp)); |
| - Expect.isTrue("st".startsWith(regexp)); |
| - Expect.isTrue("str".startsWith(regexp)); |
| - Expect.isTrue("sX".startsWith(regexp)); |
| - Expect.isTrue("stX".startsWith(regexp)); |
| - Expect.isTrue("strX".startsWith(regexp)); |
| - |
| - Expect.isFalse("".startsWith(regexp)); |
| - Expect.isFalse("astr".startsWith(regexp)); |
| - |
| - Expect.isTrue("".startsWith(new RegExp(""))); |
| - Expect.isTrue("".startsWith(new RegExp("a?"))); |
| - |
| - Expect.isFalse("strstr".startsWith(regexp, 1)); |
| - Expect.isFalse("strstr".startsWith(regexp, 2)); |
| - Expect.isTrue("strstr".startsWith(regexp, 3)); |
| - Expect.isFalse("strstr".startsWith(regexp, 4)); |
| - |
| - Expect.isTrue("str".startsWith(new RegExp(""), 0)); |
| - Expect.isTrue("str".startsWith(new RegExp(""), 1)); |
| - Expect.isTrue("str".startsWith(new RegExp(""), 2)); |
| - Expect.isTrue("str".startsWith(new RegExp(""), 3)); |
| - Expect.isTrue("str".startsWith(new RegExp("a?"), 0)); |
| - Expect.isTrue("str".startsWith(new RegExp("a?"), 1)); |
| - Expect.isTrue("str".startsWith(new RegExp("a?"), 2)); |
| - Expect.isTrue("str".startsWith(new RegExp("a?"), 3)); |
| - |
| - Expect.throws(() => "str".startsWith(regexp, -1)); |
| - Expect.throws(() => "str".startsWith(regexp, 4)); |
| - |
| - regexp = new RegExp("^str"); |
| - Expect.isTrue("strstr".startsWith(regexp)); |
| - Expect.isTrue("strstr".startsWith(regexp, 0)); |
| - Expect.isFalse("strstr".startsWith(regexp, 1)); |
| - Expect.isFalse("strstr".startsWith(regexp, 2)); |
| - Expect.isFalse("strstr".startsWith(regexp, 3)); // Second "str" isn't at ^. |
| - } |
| +void testStartsWith() { |
| + Expect.isTrue("str".startsWith("s")); |
| + Expect.isTrue("str".startsWith("st")); |
| + Expect.isTrue("str".startsWith("str")); |
| + |
| + Expect.isFalse("str".startsWith("stri")); |
| + Expect.isFalse("str".startsWith("r")); |
| + Expect.isFalse("str".startsWith("tr")); |
| + Expect.isFalse("str".startsWith("t")); |
| + |
| + Expect.isTrue("".startsWith("")); |
| + Expect.isFalse("".startsWith("s")); |
| + |
| + Expect.isFalse("strstr".startsWith("s", 1)); |
| + Expect.isFalse("strstr".startsWith("s", 2)); |
| + Expect.isTrue("strstr".startsWith("s", 3)); |
| + Expect.isFalse("strstr".startsWith("s", 4)); |
| + |
| + Expect.isFalse("strstr".startsWith("st", 1)); |
| + Expect.isFalse("strstr".startsWith("st", 2)); |
| + Expect.isTrue("strstr".startsWith("st", 3)); |
| + Expect.isFalse("strstr".startsWith("st", 4)); |
| + |
| + Expect.isFalse("strstr".startsWith("str", 1)); |
| + Expect.isFalse("strstr".startsWith("str", 2)); |
| + Expect.isTrue("strstr".startsWith("str", 3)); |
| + Expect.isFalse("strstr".startsWith("str", 4)); |
| + |
| + Expect.isTrue("str".startsWith("", 0)); |
| + Expect.isTrue("str".startsWith("", 1)); |
| + Expect.isTrue("str".startsWith("", 2)); |
| + Expect.isTrue("str".startsWith("", 3)); |
| + |
| + Expect.throws(() => "str".startsWith("", -1)); |
| + Expect.throws(() => "str".startsWith("", 4)); |
| + |
| + var regexp = new RegExp("s(?:tr?)?"); |
| + Expect.isTrue("s".startsWith(regexp)); |
| + Expect.isTrue("st".startsWith(regexp)); |
| + Expect.isTrue("str".startsWith(regexp)); |
| + Expect.isTrue("sX".startsWith(regexp)); |
| + Expect.isTrue("stX".startsWith(regexp)); |
| + Expect.isTrue("strX".startsWith(regexp)); |
| + |
| + Expect.isFalse("".startsWith(regexp)); |
| + Expect.isFalse("astr".startsWith(regexp)); |
| + |
| + Expect.isTrue("".startsWith(new RegExp(""))); |
| + Expect.isTrue("".startsWith(new RegExp("a?"))); |
| + |
| + Expect.isFalse("strstr".startsWith(regexp, 1)); |
| + Expect.isFalse("strstr".startsWith(regexp, 2)); |
| + Expect.isTrue("strstr".startsWith(regexp, 3)); |
| + Expect.isFalse("strstr".startsWith(regexp, 4)); |
| + |
| + Expect.isTrue("str".startsWith(new RegExp(""), 0)); |
| + Expect.isTrue("str".startsWith(new RegExp(""), 1)); |
| + Expect.isTrue("str".startsWith(new RegExp(""), 2)); |
| + Expect.isTrue("str".startsWith(new RegExp(""), 3)); |
| + Expect.isTrue("str".startsWith(new RegExp("a?"), 0)); |
| + Expect.isTrue("str".startsWith(new RegExp("a?"), 1)); |
| + Expect.isTrue("str".startsWith(new RegExp("a?"), 2)); |
| + Expect.isTrue("str".startsWith(new RegExp("a?"), 3)); |
| + |
| + Expect.throws(() => "str".startsWith(regexp, -1)); |
| + Expect.throws(() => "str".startsWith(regexp, 4)); |
| + |
| + regexp = new RegExp("^str"); |
| + Expect.isTrue("strstr".startsWith(regexp)); |
| + Expect.isTrue("strstr".startsWith(regexp, 0)); |
| + Expect.isFalse("strstr".startsWith(regexp, 1)); |
| + Expect.isFalse("strstr".startsWith(regexp, 2)); |
| + Expect.isFalse("strstr".startsWith(regexp, 3)); // Second "str" isn't at ^. |
| +} |
| - static testIndexOf() { |
| - Expect.equals(0, "str".indexOf("", 0)); |
| - Expect.equals(0, "".indexOf("", 0)); |
| - Expect.equals(-1, "".indexOf("a", 0)); |
| - |
| - Expect.equals(1, "str".indexOf("t", 0)); |
| - Expect.equals(1, "str".indexOf("tr", 0)); |
| - Expect.equals(0, "str".indexOf("str", 0)); |
| - Expect.equals(0, "str".indexOf("st", 0)); |
| - Expect.equals(0, "str".indexOf("s", 0)); |
| - Expect.equals(2, "str".indexOf("r", 0)); |
| - Expect.equals(-1, "str".indexOf("string", 0)); |
| - |
| - Expect.equals(1, "strstr".indexOf("t", 0)); |
| - Expect.equals(1, "strstr".indexOf("tr", 0)); |
| - Expect.equals(0, "strstr".indexOf("str", 0)); |
| - Expect.equals(0, "strstr".indexOf("st", 0)); |
| - Expect.equals(0, "strstr".indexOf("s", 0)); |
| - Expect.equals(2, "strstr".indexOf("r", 0)); |
| - Expect.equals(-1, "str".indexOf("string", 0)); |
| - |
| - Expect.equals(4, "strstr".indexOf("t", 2)); |
| - Expect.equals(4, "strstr".indexOf("tr", 2)); |
| - Expect.equals(3, "strstr".indexOf("str", 1)); |
| - Expect.equals(3, "strstr".indexOf("str", 2)); |
| - Expect.equals(3, "strstr".indexOf("str", 3)); |
| - Expect.equals(3, "strstr".indexOf("st", 1)); |
| - Expect.equals(3, "strstr".indexOf("s", 3)); |
| - Expect.equals(5, "strstr".indexOf("r", 3)); |
| - Expect.equals(5, "strstr".indexOf("r", 4)); |
| - Expect.equals(5, "strstr".indexOf("r", 5)); |
| - |
| - String str = "hello"; |
| - for (int i = 0; i < 10; i++) { |
| - if (i > str.length) { |
| - Expect.throws(() => str.indexOf("", i)); |
| - } else { |
| - int result = str.indexOf("", i); |
| - Expect.equals(i, result); |
| - } |
| +void testIndexOf() { |
| + Expect.equals(0, "str".indexOf("", 0)); |
| + Expect.equals(0, "".indexOf("", 0)); |
| + Expect.equals(-1, "".indexOf("a", 0)); |
| + |
| + Expect.equals(1, "str".indexOf("t", 0)); |
| + Expect.equals(1, "str".indexOf("tr", 0)); |
| + Expect.equals(0, "str".indexOf("str", 0)); |
| + Expect.equals(0, "str".indexOf("st", 0)); |
| + Expect.equals(0, "str".indexOf("s", 0)); |
| + Expect.equals(2, "str".indexOf("r", 0)); |
| + Expect.equals(-1, "str".indexOf("string", 0)); |
| + |
| + Expect.equals(1, "strstr".indexOf("t", 0)); |
| + Expect.equals(1, "strstr".indexOf("tr", 0)); |
| + Expect.equals(0, "strstr".indexOf("str", 0)); |
| + Expect.equals(0, "strstr".indexOf("st", 0)); |
| + Expect.equals(0, "strstr".indexOf("s", 0)); |
| + Expect.equals(2, "strstr".indexOf("r", 0)); |
| + Expect.equals(-1, "str".indexOf("string", 0)); |
| + |
| + Expect.equals(4, "strstr".indexOf("t", 2)); |
| + Expect.equals(4, "strstr".indexOf("tr", 2)); |
| + Expect.equals(3, "strstr".indexOf("str", 1)); |
| + Expect.equals(3, "strstr".indexOf("str", 2)); |
| + Expect.equals(3, "strstr".indexOf("str", 3)); |
| + Expect.equals(3, "strstr".indexOf("st", 1)); |
| + Expect.equals(3, "strstr".indexOf("s", 3)); |
| + Expect.equals(5, "strstr".indexOf("r", 3)); |
| + Expect.equals(5, "strstr".indexOf("r", 4)); |
| + Expect.equals(5, "strstr".indexOf("r", 5)); |
| + |
| + String str = "hello"; |
| + for (int i = 0; i < 10; i++) { |
| + if (i > str.length) { |
| + Expect.throws(() => str.indexOf("", i)); |
| + } else { |
| + int result = str.indexOf("", i); |
| + Expect.equals(i, result); |
| } |
| + } |
| - var re = new RegExp("an?"); |
| - Expect.equals(1, "banana".indexOf(re)); |
| - Expect.equals(1, "banana".indexOf(re, 0)); |
| - Expect.equals(1, "banana".indexOf(re, 1)); |
| - Expect.equals(3, "banana".indexOf(re, 2)); |
| - Expect.equals(3, "banana".indexOf(re, 3)); |
| - Expect.equals(5, "banana".indexOf(re, 4)); |
| - Expect.equals(5, "banana".indexOf(re, 5)); |
| - Expect.equals(-1, "banana".indexOf(re, 6)); |
| - Expect.throws(() => "banana".indexOf(re, -1)); |
| - Expect.throws(() => "banana".indexOf(re, 7)); |
| - re = new RegExp("x?"); |
| - for (int i = 0; i <= str.length; i++) { |
| - Expect.equals(i, str.indexOf(re, i)); |
| - } |
| + var re = new RegExp("an?"); |
| + Expect.equals(1, "banana".indexOf(re)); |
| + Expect.equals(1, "banana".indexOf(re, 0)); |
| + Expect.equals(1, "banana".indexOf(re, 1)); |
| + Expect.equals(3, "banana".indexOf(re, 2)); |
| + Expect.equals(3, "banana".indexOf(re, 3)); |
| + Expect.equals(5, "banana".indexOf(re, 4)); |
| + Expect.equals(5, "banana".indexOf(re, 5)); |
| + Expect.equals(-1, "banana".indexOf(re, 6)); |
| + Expect.throws(() => "banana".indexOf(re, -1)); |
| + Expect.throws(() => "banana".indexOf(re, 7)); |
| + re = new RegExp("x?"); |
| + for (int i = 0; i <= str.length; i++) { |
| + Expect.equals(i, str.indexOf(re, i)); |
| } |
| +} |
| - static testLastIndexOf() { |
| - Expect.equals(2, "str".lastIndexOf("", 2)); |
| - Expect.equals(0, "".lastIndexOf("", 0)); |
| - Expect.equals(-1, "".lastIndexOf("a", 0)); |
| - |
| - Expect.equals(1, "str".lastIndexOf("t", 2)); |
| - Expect.equals(1, "str".lastIndexOf("tr", 2)); |
| - Expect.equals(0, "str".lastIndexOf("str", 2)); |
| - Expect.equals(0, "str".lastIndexOf("st", 2)); |
| - Expect.equals(0, "str".lastIndexOf("s", 2)); |
| - Expect.equals(2, "str".lastIndexOf("r", 2)); |
| - Expect.equals(-1, "str".lastIndexOf("string", 2)); |
| - |
| - Expect.equals(4, "strstr".lastIndexOf("t", 5)); |
| - Expect.equals(4, "strstr".lastIndexOf("tr", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("st", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("s", 5)); |
| - Expect.equals(5, "strstr".lastIndexOf("r", 5)); |
| - Expect.throws(() { |
| - "str".lastIndexOf("string", 5); |
| - }); |
| - Expect.equals(4, "strstr".lastIndexOf("t", 5)); |
| - Expect.equals(4, "strstr".lastIndexOf("tr", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("st", 5)); |
| - Expect.equals(3, "strstr".lastIndexOf("s", 5)); |
| - Expect.equals(5, "strstr".lastIndexOf("r", 5)); |
| - Expect.equals(2, "strstr".lastIndexOf("r", 4)); |
| - Expect.equals(2, "strstr".lastIndexOf("r", 3)); |
| - Expect.equals(5, "strstr".lastIndexOf("r")); |
| - Expect.equals(5, "strstr".lastIndexOf("r", null)); |
| - |
| - String str = "hello"; |
| - for (int i = 0; i < 10; i++) { |
| - if (i > str.length) { |
| - Expect.throws(() => str.indexOf("", i)); |
| - } else { |
| - int result = str.lastIndexOf("", i); |
| - Expect.equals(i, result); |
| - } |
| +void testLastIndexOf() { |
| + Expect.equals(2, "str".lastIndexOf("", 2)); |
| + Expect.equals(0, "".lastIndexOf("", 0)); |
| + Expect.equals(-1, "".lastIndexOf("a", 0)); |
| + |
| + Expect.equals(1, "str".lastIndexOf("t", 2)); |
| + Expect.equals(1, "str".lastIndexOf("tr", 2)); |
| + Expect.equals(0, "str".lastIndexOf("str", 2)); |
| + Expect.equals(0, "str".lastIndexOf("st", 2)); |
| + Expect.equals(0, "str".lastIndexOf("s", 2)); |
| + Expect.equals(2, "str".lastIndexOf("r", 2)); |
| + Expect.equals(-1, "str".lastIndexOf("string", 2)); |
| + |
| + Expect.equals(4, "strstr".lastIndexOf("t", 5)); |
| + Expect.equals(4, "strstr".lastIndexOf("tr", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("st", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("s", 5)); |
| + Expect.equals(5, "strstr".lastIndexOf("r", 5)); |
| + Expect.throws(() { |
| + "str".lastIndexOf("string", 5); |
| + }); |
| + Expect.equals(4, "strstr".lastIndexOf("t", 5)); |
| + Expect.equals(4, "strstr".lastIndexOf("tr", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("str", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("st", 5)); |
| + Expect.equals(3, "strstr".lastIndexOf("s", 5)); |
| + Expect.equals(5, "strstr".lastIndexOf("r", 5)); |
| + Expect.equals(2, "strstr".lastIndexOf("r", 4)); |
| + Expect.equals(2, "strstr".lastIndexOf("r", 3)); |
| + Expect.equals(5, "strstr".lastIndexOf("r")); |
| + Expect.equals(5, "strstr".lastIndexOf("r", null)); |
| + |
| + String str = "hello"; |
| + for (int i = 0; i < 10; i++) { |
| + if (i > str.length) { |
| + Expect.throws(() => str.indexOf("", i)); |
| + } else { |
| + int result = str.lastIndexOf("", i); |
| + Expect.equals(i, result); |
| } |
| + } |
| - var re = new RegExp("an?"); |
| - Expect.equals(5, "banana".lastIndexOf(re)); |
| - Expect.equals(5, "banana".lastIndexOf(re, 6)); |
| - Expect.equals(5, "banana".lastIndexOf(re, 5)); |
| - Expect.equals(3, "banana".lastIndexOf(re, 4)); |
| - Expect.equals(3, "banana".lastIndexOf(re, 3)); |
| - Expect.equals(1, "banana".lastIndexOf(re, 2)); |
| - Expect.equals(1, "banana".lastIndexOf(re, 1)); |
| - Expect.equals(-1, "banana".lastIndexOf(re, 0)); |
| - Expect.throws(() => "banana".lastIndexOf(re, -1)); |
| - Expect.throws(() => "banana".lastIndexOf(re, 7)); |
| - re = new RegExp("x?"); |
| - for (int i = 0; i <= str.length; i++) { |
| - Expect.equals(i, str.indexOf(re, i)); |
| - } |
| + var re = new RegExp("an?"); |
| + Expect.equals(5, "banana".lastIndexOf(re)); |
| + Expect.equals(5, "banana".lastIndexOf(re, 6)); |
| + Expect.equals(5, "banana".lastIndexOf(re, 5)); |
| + Expect.equals(3, "banana".lastIndexOf(re, 4)); |
| + Expect.equals(3, "banana".lastIndexOf(re, 3)); |
| + Expect.equals(1, "banana".lastIndexOf(re, 2)); |
| + Expect.equals(1, "banana".lastIndexOf(re, 1)); |
| + Expect.equals(-1, "banana".lastIndexOf(re, 0)); |
| + Expect.throws(() => "banana".lastIndexOf(re, -1)); |
| + Expect.throws(() => "banana".lastIndexOf(re, 7)); |
| + re = new RegExp("x?"); |
| + for (int i = 0; i <= str.length; i++) { |
| + Expect.equals(i, str.indexOf(re, i)); |
| } |
| +} |
| - static testContains() { |
| - Expect.isTrue("str".contains("s", 0)); |
| - Expect.isTrue("str".contains("st", 0)); |
| - Expect.isTrue("str".contains("str", 0)); |
| - Expect.isTrue("str".contains("t", 0)); |
| - Expect.isTrue("str".contains("r", 0)); |
| - Expect.isTrue("str".contains("tr", 0)); |
| +void testContains() { |
| + Expect.isTrue("str".contains("s", 0)); |
| + Expect.isTrue("str".contains("st", 0)); |
| + Expect.isTrue("str".contains("str", 0)); |
| + Expect.isTrue("str".contains("t", 0)); |
| + Expect.isTrue("str".contains("r", 0)); |
| + Expect.isTrue("str".contains("tr", 0)); |
| - Expect.isFalse("str".contains("sr", 0)); |
| - Expect.isFalse("str".contains("string", 0)); |
| + Expect.isFalse("str".contains("sr", 0)); |
| + Expect.isFalse("str".contains("string", 0)); |
| - Expect.isTrue("str".contains("", 0)); |
| - Expect.isTrue("".contains("", 0)); |
| - Expect.isFalse("".contains("s", 0)); |
| - } |
| + Expect.isTrue("str".contains("", 0)); |
| + Expect.isTrue("".contains("", 0)); |
| + Expect.isFalse("".contains("s", 0)); |
| +} |
| - static testReplaceAll() { |
| - Expect.equals( |
| - "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to")); |
| +void testReplaceAll() { |
| + Expect.equals( |
| + "AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to")); |
| - // Test with the replaced string at the begining. |
| - Expect.equals( |
| - "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to")); |
| + // Test with the replaced string at the begining. |
| + Expect.equals( |
| + "toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to")); |
| - // Test with the replaced string at the end. |
| - Expect.equals( |
| - "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to")); |
| + // Test with the replaced string at the end. |
| + Expect.equals( |
| + "toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to")); |
| - // Test when there are no occurence of the string to replace. |
| - Expect.equals("ABC", "ABC".replaceAll("from", "to")); |
| + // Test when there are no occurence of the string to replace. |
| + Expect.equals("ABC", "ABC".replaceAll("from", "to")); |
| - // Test when the string to change is the empty string. |
| - Expect.equals("", "".replaceAll("from", "to")); |
| + // Test when the string to change is the empty string. |
| + Expect.equals("", "".replaceAll("from", "to")); |
| - // Test when the string to change is a substring of the string to |
| - // replace. |
| - Expect.equals("fro", "fro".replaceAll("from", "to")); |
| + // Test when the string to change is a substring of the string to |
| + // replace. |
| + Expect.equals("fro", "fro".replaceAll("from", "to")); |
| - // Test when the string to change is the replaced string. |
| - Expect.equals("to", "from".replaceAll("from", "to")); |
| + // Test when the string to change is the replaced string. |
| + Expect.equals("to", "from".replaceAll("from", "to")); |
| - // Test when the string to change is the replacement string. |
| - Expect.equals("to", "to".replaceAll("from", "to")); |
| + // Test when the string to change is the replacement string. |
| + Expect.equals("to", "to".replaceAll("from", "to")); |
| - // Test replacing by the empty string. |
| - Expect.equals("", "from".replaceAll("from", "")); |
| - Expect.equals("AB", "AfromB".replaceAll("from", "")); |
| + // Test replacing by the empty string. |
| + Expect.equals("", "from".replaceAll("from", "")); |
| + Expect.equals("AB", "AfromB".replaceAll("from", "")); |
| - // Test changing the empty string. |
| - Expect.equals("to", "".replaceAll("", "to")); |
| + // Test changing the empty string. |
| + Expect.equals("to", "".replaceAll("", "to")); |
| - // Test replacing the empty string. |
| - Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to")); |
| - } |
| + // Test replacing the empty string. |
| + Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to")); |
| +} |
| - static testCompareTo() { |
| - Expect.equals(0, "".compareTo("")); |
| - Expect.equals(0, "str".compareTo("str")); |
| - Expect.equals(-1, "str".compareTo("string")); |
| - Expect.equals(1, "string".compareTo("str")); |
| - Expect.equals(1, "string".compareTo("")); |
| - Expect.equals(-1, "".compareTo("string")); |
| - } |
| +void testCompareTo() { |
| + Expect.equals(0, "".compareTo("")); |
| + Expect.equals(0, "str".compareTo("str")); |
| + Expect.equals(-1, "str".compareTo("string")); |
| + Expect.equals(1, "string".compareTo("str")); |
| + Expect.equals(1, "string".compareTo("")); |
| + Expect.equals(-1, "".compareTo("string")); |
| +} |
| - static testCharCodes() { |
| - test(str) { |
| - var list = str.codeUnits; |
| - Expect.equals(str.length, list.length); |
| - for (int i = 0; i < str.length; i++) { |
| - Expect.equals(str.codeUnitAt(i), list[i]); |
| - } |
| +void testCharCodes() { |
| + test(str) { |
| + var list = str.codeUnits; |
| + Expect.equals(str.length, list.length); |
| + for (int i = 0; i < str.length; i++) { |
| + Expect.equals(str.codeUnitAt(i), list[i]); |
| } |
| - test("abc"); |
| - test(""); |
| - test(" "); |
| } |
| + test("abc"); |
| + test(""); |
| + test(" "); |
| } |
| void testStringLength(int length, String str) { |
| @@ -393,6 +393,105 @@ void testStringLength(int length, String str) { |
| (length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty); |
| } |
| -main() { |
| - StringTest.testMain(); |
| +void testRepeat() { |
| + List<String> testStrings = [ |
| + "", |
| + "\x00", |
| + "a", |
| + "ab", |
| + "\x80", |
| + "\xff", |
| + "\u2028", |
| + "abcdef\u2028", |
| + "\u{10002}", |
| + "abcdef\u{10002}" |
| + ]; |
| + List<int> counts = [ |
| + 0, |
| + 1, |
| + 2, |
| + 3, |
| + 10, |
| + 100 |
| + ]; |
| + void testRepeat(str, repeat, sep) { |
| + String expect; |
| + if (repeat == 0) { |
| + expect = ""; |
| + } else if (repeat == 1) { |
| + expect = str; |
| + } else { |
| + StringBuffer buf = new StringBuffer(str); |
| + for (int i = 1; i < repeat; i++) { |
| + buf.write(sep); |
| + buf.write(str); |
| + } |
| + expect = buf.toString(); |
| + } |
| + String actual = str.repeat(repeat, sep); |
| + Expect.equals(expect, actual, |
| + "$str#${str.length}*$repeat/$sep#${sep.length}"); |
| + } |
| + for (String str in testStrings) { |
| + for (String sep in testStrings) { |
| + for (int repeat in counts) { |
| + testRepeat(str, repeat, sep); |
| + } |
| + } |
| + } |
| + Expect.throws(() { "a".repeat(-1); }); |
| +} |
| + |
| +void testPadLeft() { |
| + Expect.equals(" 1", "1".padLeft(5, ' ')); |
| + Expect.equals(" 11", "11".padLeft(5, ' ')); |
| + Expect.equals(" 111", "111".padLeft(5, ' ')); |
| + Expect.equals(" 1111", "1111".padLeft(5, ' ')); |
| + Expect.equals("11111", "11111".padLeft(5, ' ')); |
| + Expect.equals("111111", "111111".padLeft(5, ' ')); |
| + Expect.equals(" \u{10002}", "\u{10002}".padLeft(5, ' ')); |
| + Expect.equals(" \u{10002}", "\u{10002}".padLeft(5, ' ', size: 1)); |
| + Expect.equals("\u{10002}\u{10002}\u{10002}\u{10002}a", |
| + "a".padLeft(5, '\u{10002}')); |
| + Expect.equals("xyzxyzxyzxyza", |
| + "a".padLeft(5, 'xyz')); |
| + Expect.equals("xyzxyzxyzxyzabc", |
| + "abc".padLeft(5, 'xyz', size: 1)); |
| + Expect.equals("xyzxyzxyza", |
| + "a".padLeft(5, 'xyz', size: 2)); |
| + Expect.equals('a', 'a'.padLeft(5, '')); // Silly but ok. |
| + Expect.equals('aaaaa', ''.padLeft(5, 'a')); |
| + Expect.equals('a', ''.padLeft(1, 'a')); |
| + Expect.equals('aaaaa', ''.padLeft(5, 'a')); |
|
Søren Gjesse
2014/02/19 10:44:10
Lines 463 and 465 are the same tests - did you int
Lasse Reichstein Nielsen
2014/02/19 11:43:16
No, I just ended up with the same test twice, when
|
| + Expect.equals('', ''.padLeft(0, 'a')); |
| + Expect.equals('', ''.padLeft(-2, 'a')); |
| + |
| + Expect.throws(() { ' '.padLeft(5, ' ', size: -1); }); |
| +} |
| + |
| +void testPadRight() { |
| + Expect.equals("1 ", "1".padRight(5, ' ')); |
| + Expect.equals("11 ", "11".padRight(5, ' ')); |
| + Expect.equals("111 ", "111".padRight(5, ' ')); |
| + Expect.equals("1111 ", "1111".padRight(5, ' ')); |
| + Expect.equals("11111", "11111".padRight(5, ' ')); |
| + Expect.equals("111111", "111111".padRight(5, ' ')); |
| + Expect.equals("\u{10002} ", "\u{10002}".padRight(5, ' ')); |
| + Expect.equals("\u{10002} ", "\u{10002}".padRight(5, ' ', size: 1)); |
| + Expect.equals("a\u{10002}\u{10002}\u{10002}\u{10002}", |
| + "a".padRight(5, '\u{10002}')); |
| + Expect.equals("axyzxyzxyzxyz", |
| + "a".padRight(5, 'xyz')); |
| + Expect.equals("abcxyzxyzxyzxyz", |
| + "abc".padRight(5, 'xyz', size: 1)); |
| + Expect.equals("axyzxyzxyz", |
| + "a".padRight(5, 'xyz', size: 2)); |
| + Expect.equals('a', 'a'.padRight(5, '')); // Silly but ok. |
| + Expect.equals('aaaaa', ''.padRight(5, 'a')); |
| + Expect.equals('a', ''.padRight(1, 'a')); |
| + Expect.equals('aaaaa', ''.padRight(5, 'a')); |
|
Søren Gjesse
2014/02/19 10:44:10
Lines 490 and 492 are the same tests - did you int
Lasse Reichstein Nielsen
2014/02/19 11:43:16
Done.
|
| + Expect.equals('', ''.padRight(0, 'a')); |
| + Expect.equals('', ''.padRight(-2, 'a')); |
| + |
| + Expect.throws(() { ' '.padRight(5, ' ', size: -1); }); |
| } |