Migrate remaining corelib_2/ tests to NNBD.

Change-Id: I3a31632ce28fb87a410b759d092c7ebc9393574d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/128306
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Robert Nystrom 2019-12-17 23:06:24 +00:00 committed by commit-bot@chromium.org
parent 2655f82326
commit b998b10f3e
90 changed files with 7752 additions and 0 deletions

View file

@ -0,0 +1,94 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test for testing out of range exceptions on arrays, and the content
// of range_error toString().
void main() {
testRead();
testWrite();
testToString();
}
void testRead() {
testListRead([], 0);
testListRead([], -1);
testListRead([], 1);
var list = <int?>[1];
testListRead(list, -1);
testListRead(list, 1);
list = new List(1);
testListRead(list, -1);
testListRead(list, 1);
list = new List();
testListRead(list, -1);
testListRead(list, 0);
testListRead(list, 1);
}
void testWrite() {
testListWrite([], 0);
testListWrite([], -1);
testListWrite([], 1);
var list = <int?>[1];
testListWrite(list, -1);
testListWrite(list, 1);
list = new List(1);
testListWrite(list, -1);
testListWrite(list, 1);
list = new List();
testListWrite(list, -1);
testListWrite(list, 0);
testListWrite(list, 1);
}
void testToString() {
for (var name in [null, "THENAME"]) {
for (var message in [null, "THEMESSAGE"]) {
var value = 37;
for (var re in [
new ArgumentError.value(value, name, message),
new RangeError.value(value, name, message),
new RangeError.index(value, [], name, message),
new RangeError.range(value, 0, 24, name, message)
]) {
var str = re.toString();
if (name != null) Expect.isTrue(str.contains(name), "$name in $str");
if (message != null)
Expect.isTrue(str.contains(message), "$message in $str");
Expect.isTrue(str.contains("$value"), "$value in $str");
// No empty ':' separated parts - in that case the colon is omitted too.
Expect.isFalse(str.contains(new RegExp(":\s*:")));
}
}
}
}
void testListRead(list, index) {
var exception = null;
try {
var e = list[index];
} on RangeError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
}
void testListWrite(list, index) {
var exception = null;
try {
list[index] = null;
} on RangeError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
}

View file

@ -0,0 +1,31 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for testing regular expressions in Dart.
import "package:expect/expect.dart";
class RegExp1Test {
static testMain() {
RegExp exp1 = new RegExp("bar|foo");
Expect.equals(true, exp1.hasMatch("foo"));
Expect.equals(true, exp1.hasMatch("bar"));
Expect.equals(false, exp1.hasMatch("gim"));
Expect.equals(true, exp1.hasMatch("just foo"));
Expect.equals("bar|foo", exp1.pattern);
Expect.equals(false, exp1.isMultiLine);
Expect.equals(true, exp1.isCaseSensitive);
RegExp exp2 = new RegExp("o+", caseSensitive: false);
Expect.equals(true, exp2.hasMatch("this looks good"));
Expect.equals(true, exp2.hasMatch("fOO"));
Expect.equals(false, exp2.hasMatch("bar"));
Expect.equals("o+", exp2.pattern);
Expect.equals(false, exp2.isCaseSensitive);
Expect.equals(false, exp2.isMultiLine);
}
}
main() {
RegExp1Test.testMain();
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for testing regular expressions in Dart.
import "package:expect/expect.dart";
main() {
String str = "";
try {
RegExp ex = new RegExp(str);
} catch (e) {
if (!(e is ArgumentError)) {
Expect.fail("Expected: ArgumentError got: ${e}");
}
}
Expect.isFalse(new RegExp(r"^\w+$").hasMatch(str));
Match? fm = new RegExp(r"^\w+$").firstMatch(str);
Expect.equals(null, fm);
Iterable<Match> am = new RegExp(r"^\w+$").allMatches(str);
Expect.isFalse(am.iterator.moveNext());
Expect.equals(null, new RegExp(r"^\w+$").stringMatch(str));
}

View file

@ -0,0 +1,118 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for RegExp.allMatches.
class RegExpAllMatchesTest {
static testIterator() {
var matches = new RegExp("foo").allMatches("foo foo");
Iterator it = matches.iterator;
Expect.isTrue(it.moveNext());
Expect.equals('foo', it.current.group(0));
Expect.isTrue(it.moveNext());
Expect.equals('foo', it.current.group(0));
Expect.isFalse(it.moveNext());
// Run two iterators over the same results.
it = matches.iterator;
Iterator it2 = matches.iterator;
Expect.isTrue(it.moveNext());
Expect.isTrue(it2.moveNext());
Expect.equals('foo', it.current.group(0));
Expect.equals('foo', it2.current.group(0));
Expect.isTrue(it.moveNext());
Expect.isTrue(it2.moveNext());
Expect.equals('foo', it.current.group(0));
Expect.equals('foo', it2.current.group(0));
Expect.equals(false, it.moveNext());
Expect.equals(false, it2.moveNext());
}
static testForEach() {
var matches = new RegExp("foo").allMatches("foo foo");
var strbuf = new StringBuffer();
matches.forEach((Match m) {
strbuf.write(m.group(0));
});
Expect.equals("foofoo", strbuf.toString());
}
static testMap() {
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
var mapped = matches.map((Match m) => "${m.group(0)}bar");
Expect.equals(4, mapped.length);
var strbuf = new StringBuffer();
for (String s in mapped) {
strbuf.write(s);
}
Expect.equals("foobarfobarfoobarfobar", strbuf.toString());
}
static testFilter() {
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
var filtered = matches.where((Match m) {
return m.group(0) == 'foo';
});
Expect.equals(2, filtered.length);
var strbuf = new StringBuffer();
for (Match m in filtered) {
strbuf.write(m.group(0));
}
Expect.equals("foofoo", strbuf.toString());
}
static testEvery() {
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
Expect.equals(true, matches.every((Match m) {
return m.group(0)!.startsWith("fo");
}));
Expect.equals(false, matches.every((Match m) {
return m.group(0)!.startsWith("foo");
}));
}
static testSome() {
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
Expect.equals(true, matches.any((Match m) {
return m.group(0)!.startsWith("fo");
}));
Expect.equals(true, matches.any((Match m) {
return m.group(0)!.startsWith("foo");
}));
Expect.equals(false, matches.any((Match m) {
return m.group(0)!.startsWith("fooo");
}));
}
static testIsEmpty() {
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
Expect.equals(false, matches.isEmpty);
matches = new RegExp("fooo").allMatches("foo fo foo fo");
Expect.equals(true, matches.isEmpty);
}
static testGetCount() {
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
Expect.equals(4, matches.length);
matches = new RegExp("fooo").allMatches("foo fo foo fo");
Expect.equals(0, matches.length);
}
static testMain() {
testIterator();
testForEach();
testMap();
testFilter();
testEvery();
testSome();
testIsEmpty();
testGetCount();
}
}
main() {
RegExpAllMatchesTest.testMain();
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Runs several similar regexps in a loop to see if an internal cache works (at
// least in easy conditions).
import "package:expect/expect.dart";
void main() {
for (int j = 1; j < 50; j += 4) {
for (int i = 0; i < 15 * j; i++) {
var regExp = new RegExp("foo$i");
var match = regExp.firstMatch("foo$i");
Expect.isNotNull(match);
Expect.equals("foo$i", match![0]);
}
}
}

View file

@ -0,0 +1,18 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for RegExp.firstMatch.
class RegExpFirstMatchTest {
static testMain() {
Expect.equals('cat', new RegExp("(\\w+)").firstMatch("cat dog")![0]);
Expect.equals(null, new RegExp("foo").firstMatch("bar"));
}
}
main() {
RegExpFirstMatchTest.testMain();
}

View file

@ -0,0 +1,22 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for RegExp.group.
class RegExpGroupTest {
static testMain() {
var match = new RegExp("(a(b)((c|de)+))").firstMatch("abcde")!;
Expect.equals('abcde', match.group(0));
Expect.equals('abcde', match.group(1));
Expect.equals('b', match.group(2));
Expect.equals('cde', match[3]);
Expect.equals('de', match[4]);
}
}
main() {
RegExpGroupTest.testMain();
}

View file

@ -0,0 +1,22 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for RegExp.groups.
class RegExpGroupsTest {
static testMain() {
var match = new RegExp("(a(b)((c|de)+))").firstMatch("abcde")!;
var groups = match.groups([0, 4, 2, 3]);
Expect.equals('abcde', groups[0]);
Expect.equals('de', groups[1]);
Expect.equals('b', groups[2]);
Expect.equals('cde', groups[3]);
}
}
main() {
RegExpGroupsTest.testMain();
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for RegExp.hasMatch.
class RegExpHasMatchTest {
static testMain() {
Expect.equals(false, new RegExp("bar").hasMatch("foo"));
Expect.equals(true, new RegExp("bar|foo").hasMatch("foo"));
Expect.equals(true, new RegExp("o+").hasMatch("foo"));
}
}
main() {
RegExpHasMatchTest.testMain();
}

View file

@ -0,0 +1,27 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for testing regular expressions in Dart.
import "package:expect/expect.dart";
// Regression test for http://dartbug.com/17998
main() {
for (var s in [
r"a",
r"a|b",
r"(?:)",
r"^",
r"$",
r"^$",
r"$^",
r"",
r"\\",
r"/",
r"[^]",
"\x00",
]) {
Expect.equals(s, new RegExp(s).pattern);
}
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
var matches = new RegExp("(a(b)((c|de)+))").allMatches("abcde abcde abcde");
var it = matches.iterator;
int start = 0;
int end = 5;
while (it.moveNext()) {
Match match = it.current;
Expect.equals(start, match.start);
Expect.equals(end, match.end);
start += 6;
end += 6;
}
}

View file

@ -0,0 +1,18 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for RegExp.stringMatch.
class RegExpStringMatchTest {
static testMain() {
Expect.equals('cat', new RegExp("(\\w+)").stringMatch("cat dog"));
Expect.equals(null, new RegExp("foo").stringMatch("bar"));
}
}
main() {
RegExpStringMatchTest.testMain();
}

View file

@ -0,0 +1,16 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
void main() {
var l = [new MyTest(1), new MyTest(5), new MyTest(3)];
l.sort();
if (l.toString() != "[d{1}, d{3}, d{5}]") throw 'Wrong result!';
}
class MyTest implements Comparable<MyTest> {
final int a;
MyTest(this.a);
int compareTo(MyTest b) => this.a - b.a;
String toString() => "d{$a}";
}

View file

@ -0,0 +1,32 @@
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import "package:expect/expect.dart";
// Regression test for https://github.com/dart-lang/sdk/issues/33166
void main() async {
// Check that a `null` data handler (like the one passe by `drain`)
// doesn't crash.
{
var stream = new Stream<Object>.fromIterable([1, 2, 3]);
Expect.equals(await stream.cast<int>().drain().then((_) => 'Done'), 'Done');
}
// Check that type errors go into stream error channel.
{
var stream = new Stream<Object>.fromIterable([1, 2, 3]);
var errors = [];
var done = new Completer();
var subscription = stream.cast<String>().listen((value) {
Expect.fail("Unexpected value: $value");
}, onError: (e, s) {
errors.add(e);
}, onDone: () {
done.complete(null);
});
await done.future;
Expect.equals(3, errors.length);
}
}

View file

@ -0,0 +1,17 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=--optimization_counter_threshold=5 --no-background_compilation
import "package:expect/expect.dart";
sll(x, shift) => x << shift;
main() {
for (int i = 0; i < 10; i++) {
var x = 0x50000000;
var shift = 34;
Expect.equals(sll(x, shift), 0x4000000000000000);
}
}

View file

@ -0,0 +1,35 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
Expect.stringEquals('1', Error.safeToString(1));
Expect.stringEquals('0.5', Error.safeToString(0.5));
Expect.stringEquals('"1"', Error.safeToString("1"));
Expect.stringEquals('"\'"', Error.safeToString("'"));
Expect.stringEquals('"\'\'"', Error.safeToString("''"));
Expect.stringEquals(r'"\""', Error.safeToString('"'));
Expect.stringEquals(r'"\"\""', Error.safeToString('""'));
Expect.stringEquals(r'"\\\"\n\r"', Error.safeToString('\\"\n\r'));
Expect.stringEquals(r'"\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007"',
Error.safeToString('\x00\x01\x02\x03\x04\x05\x06\x07'));
Expect.stringEquals(r'"\b\t\n\u000b\f\r\u000e\u000f"',
Error.safeToString('\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'));
Expect.stringEquals(r'"\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017"',
Error.safeToString('\x10\x11\x12\x13\x14\x15\x16\x17'));
Expect.stringEquals(r'"\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f"',
Error.safeToString('\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'));
Expect.stringEquals('" "', Error.safeToString(" "));
Expect.stringEquals('null', Error.safeToString(null));
Expect.stringEquals('true', Error.safeToString(true));
Expect.stringEquals('false', Error.safeToString(false));
// The class name may be minified.
String className = "$Object";
Expect.stringEquals(
"Instance of '$className'", Error.safeToString(new Object()));
}

View file

@ -0,0 +1,26 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class A {
const A();
}
class B extends A {
const B();
}
main() {
var set1 = new Set<B>();
set1.add(const B());
var set2 = new Set<B>();
var list = <B>[const B()];
var set3 = list.toSet();
var sets = [set1, set2, set3];
for (var setToTest in sets) {
Expect.isFalse(setToTest.containsAll(<A>[new A()]));
}
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class A {
const A();
}
class B extends A {
const B();
}
main() {
var set1 = new Set<B>();
set1.add(const B());
var set2 = new Set<B>();
var list = <B>[const B()];
var set3 = list.toSet();
var sets = [set1, set2, set3];
for (var setToTest in sets) {
// Test that the set accepts a value that is not of the same type
// Set<B>.contains(A)
Expect.isFalse(setToTest.contains(new A()));
}
}

View file

@ -0,0 +1,31 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class A {
const A();
}
class B extends A {
const B();
}
main() {
var set1 = new Set<B>();
set1.add(const B());
var set2 = new Set<B>();
var list = <B>[const B()];
var set3 = list.toSet();
var setOther = new Set<A>();
setOther.add(new A());
var sets = [set1, set2, set3];
for (var setToTest in sets) {
// Test that the set accepts another set that is not of the same type:
// Set<B>.intersection(Set<A>)
Set result = setToTest.intersection(setOther);
Expect.isTrue(result.isEmpty);
}
}

View file

@ -0,0 +1,146 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class FixedHashCode {
final int _hashCode;
const FixedHashCode(this._hashCode);
int get hashCode {
return _hashCode;
}
}
class SetIteratorTest {
static testMain() {
testSmallSet();
testLargeSet();
testEmptySet();
testSetWithDeletedEntries();
testBug5116829();
testDifferentSizes();
testDifferentHashCodes();
}
static int sum(int expected, Iterator<int> it) {
int count = 0;
while (it.moveNext()) {
count += it.current;
}
Expect.equals(expected, count);
}
static void testSmallSet() {
Set<int> set = new Set<int>();
set.add(1);
set.add(2);
set.add(3);
Iterator<int> it = set.iterator;
sum(6, it);
Expect.isFalse(it.moveNext());
Expect.isNull(it.current);
}
static void testLargeSet() {
Set<int> set = new Set<int>();
int count = 0;
for (int i = 0; i < 100; i++) {
count += i;
set.add(i);
}
Iterator<int> it = set.iterator;
sum(count, it);
Expect.isFalse(it.moveNext());
Expect.isNull(it.current);
}
static void testEmptySet() {
Set<int> set = new Set<int>();
Iterator<int> it = set.iterator;
sum(0, it);
Expect.isFalse(it.moveNext());
Expect.isNull(it.current);
}
static void testSetWithDeletedEntries() {
Set<int> set = new Set<int>();
for (int i = 0; i < 100; i++) {
set.add(i);
}
for (int i = 0; i < 100; i++) {
set.remove(i);
}
Iterator<int> it = set.iterator;
Expect.isFalse(it.moveNext());
it = set.iterator;
sum(0, it);
Expect.isFalse(it.moveNext());
Expect.isNull(it.current);
int count = 0;
for (int i = 0; i < 100; i++) {
set.add(i);
if (i % 2 == 0)
set.remove(i);
else
count += i;
}
it = set.iterator;
sum(count, it);
Expect.isFalse(it.moveNext());
Expect.isNull(it.current);
}
static void testBug5116829() {
// During iteration we skipped slot 0 of the hashset's key list. "A" was
// hashed to slot 0 and therefore triggered the bug.
Set<String> mystrs = new Set<String>();
mystrs.add("A");
int seen = 0;
for (String elt in mystrs) {
seen++;
Expect.equals("A", elt);
}
Expect.equals(1, seen);
}
static void testDifferentSizes() {
for (int i = 1; i < 20; i++) {
Set set = new Set();
int sum = 0;
for (int j = 0; j < i; j++) {
set.add(j);
sum += j;
}
int count = 0;
int controlSum = 0;
for (int x in set) {
controlSum += x;
count++;
}
Expect.equals(i, count);
Expect.equals(sum, controlSum);
}
}
static void testDifferentHashCodes() {
for (int i = -20; i < 20; i++) {
Set set = new Set();
var element = new FixedHashCode(i);
set.add(element);
Expect.equals(1, set.length);
bool foundIt = false;
for (var x in set) {
foundIt = true;
Expect.equals(true, identical(x, element));
}
Expect.equals(true, foundIt);
}
}
}
main() {
SetIteratorTest.testMain();
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class A {
const A();
}
class B extends A {
const B();
}
main() {
var set1 = new Set<B>();
set1.add(const B());
var set2 = new Set<B>();
var list = <B>[const B()];
var set3 = list.toSet();
var sets = [set1, set2, set3];
for (var setToTest in sets) {
// Test that the set accepts a list that is not of the same type:
// Set<B>.removeAll(List<A>)
Expect.isNull(setToTest.removeAll(<A>[new A()]) as dynamic);
}
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class A {
const A();
}
class B extends A {
const B();
}
main() {
var set1 = new Set<B>();
set1.add(const B());
var set2 = new Set<B>();
var list = <B>[const B()];
var set3 = list.toSet();
var sets = [set1, set2, set3];
for (var setToTest in sets) {
// Test that the set accepts a value that is not of the same type
// Set<B>.remove(A)
Expect.isFalse(setToTest.remove(new A()));
}
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class A {
const A();
}
class B extends A {
const B();
}
main() {
var set1 = new Set<B>();
set1.add(const B());
var set2 = new Set<B>();
var list = <B>[const B()];
var set3 = list.toSet();
var sets = [set1, set2, set3];
for (var setToTest in sets) {
// Test that the set accepts a list that is not of the same type:
// Set<B>.retainAll(List<A>)
setToTest.retainAll(<A>[new A()]);
}
}

556
tests/corelib/set_test.dart Normal file
View file

@ -0,0 +1,556 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library set_test;
import 'package:expect/expect.dart';
import "dart:collection";
void testMain(Set create()) {
testInts(create);
testStrings(create);
testInts(() => create().toSet());
testStrings(() => create().toSet());
}
void testInts(Set create()) {
Set set = create();
testLength(0, set);
Expect.isTrue(set.add(1));
testLength(1, set);
Expect.isTrue(set.contains(1));
Expect.isFalse(set.add(1));
testLength(1, set);
Expect.isTrue(set.contains(1));
Expect.isTrue(set.remove(1));
testLength(0, set);
Expect.isFalse(set.contains(1));
Expect.isFalse(set.remove(1));
testLength(0, set);
Expect.isFalse(set.contains(1));
for (int i = 0; i < 10; i++) {
set.add(i);
}
testLength(10, set);
for (int i = 0; i < 10; i++) {
Expect.isTrue(set.contains(i));
}
testLength(10, set);
for (int i = 10; i < 20; i++) {
Expect.isFalse(set.contains(i));
}
// Test Set.forEach.
int sum = 0;
void testForEach(dynamic val) {
sum += ((val as int) + 1);
}
set.forEach(testForEach);
Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum);
Expect.isTrue(set.containsAll(set));
// Test Set.map.
bool testMap(dynamic val) {
return (val as int) * val;
}
Set mapped = set.map(testMap).toSet();
Expect.equals(10, mapped.length);
Expect.isTrue(mapped.contains(0));
Expect.isTrue(mapped.contains(1));
Expect.isTrue(mapped.contains(4));
Expect.isTrue(mapped.contains(9));
Expect.isTrue(mapped.contains(16));
Expect.isTrue(mapped.contains(25));
Expect.isTrue(mapped.contains(36));
Expect.isTrue(mapped.contains(49));
Expect.isTrue(mapped.contains(64));
Expect.isTrue(mapped.contains(81));
sum = 0;
set.forEach(testForEach);
Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum);
sum = 0;
mapped.forEach(testForEach);
Expect.equals(1 + 2 + 5 + 10 + 17 + 26 + 37 + 50 + 65 + 82, sum);
// Test Set.filter.
bool testFilter(dynamic val) {
return (val as int).isEven;
}
Set filtered = set.where(testFilter).toSet();
Expect.equals(5, filtered.length);
Expect.isTrue(filtered.contains(0));
Expect.isTrue(filtered.contains(2));
Expect.isTrue(filtered.contains(4));
Expect.isTrue(filtered.contains(6));
Expect.isTrue(filtered.contains(8));
sum = 0;
filtered.forEach(testForEach);
Expect.equals(1 + 3 + 5 + 7 + 9, sum);
Expect.isTrue(set.containsAll(filtered));
// Test Set.every.
bool testEvery(dynamic val) {
return ((val as int) < 10);
}
Expect.isTrue(set.every(testEvery));
Expect.isTrue(filtered.every(testEvery));
filtered.add(10);
Expect.isFalse(filtered.every(testEvery));
// Test Set.some.
bool testSome(dynamic val) {
return (val == 4);
}
Expect.isTrue(set.any(testSome));
Expect.isTrue(filtered.any(testSome));
filtered.remove(4);
Expect.isFalse(filtered.any(testSome));
// Test Set.intersection.
Set intersection = set.intersection(filtered);
Expect.isTrue(set.contains(0));
Expect.isTrue(set.contains(2));
Expect.isTrue(set.contains(6));
Expect.isTrue(set.contains(8));
Expect.isFalse(intersection.contains(1));
Expect.isFalse(intersection.contains(3));
Expect.isFalse(intersection.contains(4));
Expect.isFalse(intersection.contains(5));
Expect.isFalse(intersection.contains(7));
Expect.isFalse(intersection.contains(9));
Expect.isFalse(intersection.contains(10));
Expect.equals(4, intersection.length);
Expect.isTrue(set.containsAll(intersection));
Expect.isTrue(filtered.containsAll(intersection));
// Test Set.union.
Set twice = create()..addAll([0, 2, 4, 6, 8, 10, 12, 14]);
Set thrice = create()..addAll([0, 3, 6, 9, 12, 15]);
Set union = twice.union(thrice);
Expect.equals(11, union.length);
for (int i = 0; i < 16; i++) {
Expect.equals(i.isEven || (i % 3) == 0, union.contains(i));
}
// Test Set.difference.
Set difference = twice.difference(thrice);
Expect.equals(5, difference.length);
for (int i = 0; i < 16; i++) {
Expect.equals(i.isEven && (i % 3) != 0, difference.contains(i));
}
Expect.isTrue(twice.difference(thrice).difference(twice).isEmpty);
// Test Set.difference with non-element type.
Set diffSet = create()..addAll([0, 1, 2, 499, 999]);
Set<Object> objectSet = new Set<Object>();
objectSet.add("foo");
objectSet.add(499);
Set diffResult = diffSet.difference(objectSet);
Expect.equals(4, diffResult.length);
for (int value in [0, 1, 2, 999]) {
Expect.isTrue(diffResult.contains(value));
}
// Test Set.addAll.
List list = new List(10);
for (int i = 0; i < 10; i++) {
list[i] = i + 10;
}
set.addAll(list);
testLength(20, set);
for (int i = 0; i < 20; i++) {
Expect.isTrue(set.contains(i));
}
// Test Set.removeAll
set.removeAll(list);
testLength(10, set);
for (int i = 0; i < 10; i++) {
Expect.isTrue(set.contains(i));
}
for (int i = 10; i < 20; i++) {
Expect.isFalse(set.contains(i));
}
// Test Set.clear.
set.clear();
testLength(0, set);
Expect.isTrue(set.add(11));
testLength(1, set);
// Test Set.toSet.
set.add(1);
set.add(21);
testLength(3, set);
var set2 = set.toSet();
testLength(3, set2);
Expect.listEquals(set.toList(), set2.toList());
set.add(31);
testLength(4, set);
testLength(3, set2);
set2 = set.toSet()..clear();
testLength(0, set2);
Expect.isTrue(set2.add(11));
Expect.isTrue(set2.add(1));
Expect.isTrue(set2.add(21));
Expect.isTrue(set2.add(31));
testLength(4, set2);
Expect.listEquals(set.toList(), set2.toList());
set2 = (set.toSet()..clear()).toSet(); // Cloning empty set shouldn't fail.
testLength(0, set2);
}
void testLength(int length, Set set) {
Expect.equals(length, set.length);
(length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty);
(length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty);
if (length == 0) {
for (var e in set) {
Expect.fail("contains element when iterated: $e");
}
}
(length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext());
}
void testStrings(Set create()) {
var set = create();
var strings = ["foo", "bar", "baz", "qux", "fisk", "hest", "svin", "pigvar"];
set.addAll(strings);
testLength(8, set);
set.removeAll(strings.where((x) => x.length == 3));
testLength(4, set);
Expect.isTrue(set.add("bar"));
Expect.isTrue(set.add("qux"));
testLength(6, set);
set.addAll(strings);
testLength(8, set);
set.removeWhere((x) => x.length != 3);
testLength(4, set);
set.retainWhere((x) => x[1] == "a");
testLength(2, set);
Expect.isTrue(set.containsAll(["baz", "bar"]));
set = set.union(strings.where((x) => x.length != 3).toSet());
testLength(6, set);
set = set.intersection(["qux", "baz", "fisk", "egern"].toSet());
testLength(2, set);
Expect.isTrue(set.containsAll(["baz", "fisk"]));
}
void testTypeAnnotations(Set<int> set) {
set.add(0);
set.add(999);
set.add(0x800000000);
set.add(0x20000000000000);
Expect.isFalse(set.contains("not an it"));
Expect.isFalse(set.remove("not an it"));
Expect.isFalse(set.containsAll(["Not an int", "Also no an int"]));
testLength(4, set);
set.removeAll(["Not an int", 999, "Also no an int"]);
testLength(3, set);
set.retainAll(["Not an int", 0, "Also no an int"]);
testLength(1, set);
}
void testRetainWhere(
Set<CE> create(
[CEEq? equals, CEHash? hashCode, ValidKey? validKey, CECompare? compare])) {
// The retainWhere method must not collapse the argument Iterable
// in a way that doesn't match the equality of the set.
// It must not throw away equal elements that are different in the
// equality of the set.
// It must not consider objects to be not there if they are equal
// in the equality of the set.
// If set equality is natural equality, using different but equal objects
// must work. Can't use an identity set internally (as was done at some point
// during development).
var set = create();
set.addAll([new CE(0), new CE(1), new CE(2)]);
Expect.equals(3, set.length); // All different.
set.retainAll([new CE(0), new CE(2)]);
Expect.equals(2, set.length);
Expect.isTrue(set.contains(new CE(0)));
Expect.isTrue(set.contains(new CE(2)));
// If equality of set is identity, we can't internally use a non-identity
// based set because it might throw away equal objects that are not identical.
var elems = [new CE(0), new CE(1), new CE(2), new CE(0)];
set = create(identical, null, null, identityCompare);
set.addAll(elems);
Expect.equals(4, set.length);
set.retainAll([elems[0], elems[2], elems[3]]);
Expect.equals(3, set.length);
Expect.isTrue(set.contains(elems[0]));
Expect.isTrue(set.contains(elems[2]));
Expect.isTrue(set.contains(elems[3]));
// If set equality is less precise than equality, we must not use equality
// internally to see if the element is there:
set = create(customEq(3), customHash(3), validKey, customCompare(3));
set.addAll([new CE(0), new CE(1), new CE(2)]);
Expect.equals(3, set.length);
set.retainAll([new CE(3), new CE(5)]);
Expect.equals(2, set.length);
Expect.isTrue(set.contains(new CE(6)));
Expect.isTrue(set.contains(new CE(8)));
// It shouldn't matter if the input is a set.
set.clear();
set.addAll([new CE(0), new CE(1), new CE(2)]);
Expect.equals(3, set.length);
set.retainAll(new Set.from([new CE(3), new CE(5)]));
Expect.equals(2, set.length);
Expect.isTrue(set.contains(new CE(6)));
Expect.isTrue(set.contains(new CE(8)));
}
void testDifferenceIntersection(create([equals, hashCode, validKey, compare])) {
// Test that elements of intersection comes from receiver set.
CE ce1a = new CE(1);
CE ce1b = new CE(1);
CE ce2 = new CE(2);
CE ce3 = new CE(3);
Expect.equals(ce1a, ce1b); // Sanity check.
var set1 = create();
var set2 = create();
set1.add(ce1a);
set1.add(ce2);
set2.add(ce1b);
set2.add(ce3);
var difference = set1.difference(set2);
testLength(1, difference);
Expect.identical(ce2, difference.lookup(ce2));
difference = set2.difference(set1);
testLength(1, difference);
Expect.identical(ce3, difference.lookup(ce3));
// Difference uses other.contains to check for equality.
var set3 = create(identical, identityHashCode, null, identityCompare);
set3.add(ce1b);
difference = set1.difference(set3);
testLength(2, difference); // ce1a is not identical to element in set3.
Expect.identical(ce1a, difference.lookup(ce1a));
Expect.identical(ce2, difference.lookup(ce2));
// Intersection always takes elements from receiver set.
var intersection = set1.intersection(set2);
testLength(1, intersection);
Expect.identical(ce1a, intersection.lookup(ce1a));
intersection = set1.intersection(set3);
testLength(0, intersection);
}
// Objects that are equal based on data.
class CE implements Comparable<CE> {
final int id;
const CE(this.id);
int get hashCode => id;
bool operator ==(Object other) => other is CE && id == other.id;
int compareTo(CE other) => id - other.id;
String toString() => "CE($id)";
}
typedef int CECompare(CE e1, CE e2);
typedef int CEHash(CE e1);
typedef bool CEEq(CE e1, CE e2);
typedef bool ValidKey(dynamic o);
// Equality of Id objects based on id modulo value.
CEEq customEq(int mod) => (CE e1, CE e2) => ((e1.id - e2.id) % mod) == 0;
CEHash customHash(int mod) => (CE e) => e.id % mod;
CECompare customCompare(int mod) =>
(CE e1, CE e2) => (e1.id % mod) - (e2.id % mod);
bool validKey(dynamic o) => o is CE;
final customId = new Map<dynamic, dynamic>.identity();
int counter = 0;
int identityCompare(e1, e2) {
if (identical(e1, e2)) return 0;
int i1 = customId.putIfAbsent(e1, () => ++counter);
int i2 = customId.putIfAbsent(e2, () => ++counter);
return i1 - i2;
}
void testIdentity(Set create()) {
Set set = create();
var e1 = new CE(0);
var e2 = new CE(0);
Expect.equals(e1, e2);
Expect.isFalse(identical(e1, e2));
testLength(0, set);
set.add(e1);
testLength(1, set);
Expect.isTrue(set.contains(e1));
Expect.isFalse(set.contains(e2));
set.add(e2);
testLength(2, set);
Expect.isTrue(set.contains(e1));
Expect.isTrue(set.contains(e2));
var set2 = set.toSet();
testLength(2, set2);
Expect.isTrue(set2.contains(e1));
Expect.isTrue(set2.contains(e2));
}
void testIntSetFrom(setFrom) {
List<num> numList = [2, 3, 5, 7, 11, 13];
Set<int> set1 = setFrom(numList);
Expect.listEquals(numList, set1.toList()..sort());
Set<num> numSet = numList.toSet();
Set<int> set2 = setFrom(numSet);
Expect.listEquals(numList, set2.toList()..sort());
Iterable<num> numIter = numList.where((x) => true);
Set<int> set3 = setFrom(numIter);
Expect.listEquals(numList, set3.toList()..sort());
Set<int> set4 = setFrom(new Iterable.generate(0));
Expect.isTrue(set4.isEmpty);
}
void testCESetFrom(setFrom) {
var ceList = [
new CE(2),
new CE(3),
new CE(5),
new CE(7),
new CE(11),
new CE(13)
];
Set<CE> set1 = setFrom(ceList);
Expect.listEquals(ceList, set1.toList()..sort());
Set<CE> ceSet = ceList.toSet();
Set<CE> set2 = setFrom(ceSet);
Expect.listEquals(ceList, set2.toList()..sort());
Iterable<CE> ceIter = ceList.where((x) => true);
Set<CE> set3 = setFrom(ceIter);
Expect.listEquals(ceList, set3.toList()..sort());
Set<CE> set4 = setFrom(new Iterable.generate(0));
Expect.isTrue(set4.isEmpty);
}
class A {}
class B {}
class C implements A, B {}
void testASetFrom(setFrom) {
List<B> bList = <B>[new C()];
// Set.from allows to cast elements.
Set<A> aSet = setFrom(bList);
Expect.isTrue(aSet.length == 1);
}
main() {
testMain(() => new HashSet());
testMain(() => new LinkedHashSet());
testMain(() => new HashSet.identity());
testMain(() => new LinkedHashSet.identity());
testMain(() => new HashSet(equals: identical));
testMain(() => new LinkedHashSet(equals: identical));
testMain(() => new HashSet(
equals: (a, b) => a == b,
hashCode: (a) => -a.hashCode,
isValidKey: (a) => true));
testMain(() => new LinkedHashSet(
equals: (a, b) => a == b,
hashCode: (a) => -a.hashCode,
isValidKey: (a) => true));
testMain(() => new SplayTreeSet());
testIdentity(() => new HashSet.identity());
testIdentity(() => new LinkedHashSet.identity());
testIdentity(() => new HashSet(equals: identical));
testIdentity(() => new LinkedHashSet(equals: identical));
testIdentity(() => new SplayTreeSet(identityCompare));
testTypeAnnotations(new HashSet<int>());
testTypeAnnotations(new LinkedHashSet<int>());
testTypeAnnotations(new HashSet<int>(equals: identical));
testTypeAnnotations(new LinkedHashSet<int>(equals: identical));
testTypeAnnotations(new HashSet<int>(
equals: (int a, int b) => a == b,
hashCode: (int a) => a.hashCode,
isValidKey: (a) => a is int));
testTypeAnnotations(new LinkedHashSet<int>(
equals: (int a, int b) => a == b,
hashCode: (int a) => a.hashCode,
isValidKey: (a) => a is int));
testTypeAnnotations(new SplayTreeSet<int>());
testRetainWhere(([equals, hashCode, validKey, comparator]) =>
new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey));
testRetainWhere(([equals, hashCode, validKey, comparator]) =>
new LinkedHashSet(
equals: equals, hashCode: hashCode, isValidKey: validKey));
testRetainWhere(([equals, hashCode, validKey, comparator]) =>
new SplayTreeSet(comparator, validKey));
testDifferenceIntersection(([equals, hashCode, validKey, comparator]) =>
new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey));
testDifferenceIntersection(([equals, hashCode, validKey, comparator]) =>
new LinkedHashSet(
equals: equals, hashCode: hashCode, isValidKey: validKey));
testDifferenceIntersection(([equals, hashCode, validKey, comparator]) =>
new SplayTreeSet(comparator, validKey));
testIntSetFrom((x) => new Set<int>.from(x));
testIntSetFrom((x) => new HashSet<int>.from(x));
testIntSetFrom((x) => new LinkedHashSet<int>.from(x));
testIntSetFrom((x) => new SplayTreeSet<int>.from(x));
testCESetFrom((x) => new Set<CE>.from(x));
testCESetFrom((x) => new HashSet<CE>.from(x));
testCESetFrom((x) => new LinkedHashSet<CE>.from(x));
testCESetFrom((x) => new SplayTreeSet<CE>.from(x));
testCESetFrom(
(x) => new SplayTreeSet<CE>.from(x, customCompare(20), validKey));
testCESetFrom((x) => new SplayTreeSet<CE>.from(x, identityCompare));
testASetFrom((x) => new Set<A>.from(x));
testASetFrom((x) => new HashSet<A>.from(x));
testASetFrom((x) => new LinkedHashSet<A>.from(x));
testASetFrom((x) => new SplayTreeSet<A>.from(x, identityCompare));
}

View file

@ -0,0 +1,36 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import "dart:collection";
void main() {
Set s = new HashSet();
s.add(1);
Expect.equals("{1}", s.toString());
s.remove(1);
s.add(s);
Expect.equals("{{...}}", s.toString());
Queue q = new ListQueue(4);
q.add(1);
q.add(2);
q.add(q);
q.add(s);
Expect.equals("{1, 2, {...}, {{...}}}", q.toString());
// Throwing in the middle of a toString does not leave the
// set as being visited
q.addLast(new ThrowOnToString());
Expect.throws(q.toString, (e) => e == "Bad!");
q.removeLast();
Expect.equals("{1, 2, {...}, {{...}}}", q.toString());
}
class ThrowOnToString {
String toString() {
throw "Bad!";
}
}

View file

@ -0,0 +1,127 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for List.shuffle.
library shuffle_test;
import "dart:typed_data";
import "dart:math" show Random;
import "package:expect/expect.dart";
main() {
for (int size in [0, 1, 2, 3, 7, 15, 99, 1023]) {
var numbers = new List<int>.generate(size, (x) => x);
testShuffle(numbers.toList(growable: true));
testShuffle(numbers.toList(growable: false));
testShuffle(new Uint32List(size)..setAll(0, numbers));
testShuffle(new Int32List(size)..setAll(0, numbers));
testShuffle(new Uint16List(size)..setAll(0, numbers));
testShuffle(new Int16List(size)..setAll(0, numbers));
// Some numbers will be truncated in the following two.
testShuffle(new Uint8List(size)..setAll(0, numbers));
testShuffle(new Int8List(size)..setAll(0, numbers));
//testShuffle(numbers.map((x) => "$x").toList());
}
// Check that it actually can keep the same list (regression test).
List l = [1, 2];
success:
{
for (int i = 0; i < 266; i++) {
int first = l.first;
l.shuffle();
if (l.first == first) break success; // List didn't change.
}
// Chance of changing 266 times in a row should be < 1:1e80.
Expect.fail("List changes every time.");
}
testRandom();
}
void testShuffle(list) {
List copy = list.toList();
list.shuffle();
if (list.length < 2) {
Expect.listEquals(copy, list);
return;
}
// Test that the list after shuffling has the same elements as before,
// without considering order.
Map seen = {};
for (var e in list) {
seen[e] = seen.putIfAbsent(e, () => 0) + 1;
}
for (var e in copy) {
int remaining = seen[e];
remaining -= 1; // Throws if e was not in map at all.
if (remaining == 0) {
seen.remove(e);
} else {
seen[e] = remaining;
}
}
Expect.isTrue(seen.isEmpty);
// Test that shuffle actually does make a change. Repeat until the probability
// of a proper shuffling hitting the same list again is less than 10^80
// (arbitrary bignum - approx. number of atoms in the universe).
//
// The probablility of shuffling a list of length n into the same list is
// 1/n!. If one shuffle didn't change the list, repeat shuffling until
// probability of randomly hitting the same list every time is less than
// 1/1e80.
bool listsDifferent() {
for (int i = 0; i < list.length; i++) {
if (list[i] != copy[i]) return true;
}
return false;
}
if (list.length < 59) {
// 59! > 1e80.
double limit = 1e80;
double fact = 1.0;
for (int i = 2; i < list.length; i++) fact *= i;
double combos = fact;
while (!listsDifferent() && combos < limit) {
list.shuffle();
combos *= fact;
}
}
if (!listsDifferent()) {
Expect.fail("Didn't shuffle at all, p < 1:1e80: $list");
}
}
// Checks that the "random" argument to shuffle is used.
testRandom() {
List<int> randomNums = [37, 87, 42, 157, 252, 17];
List numbers = new List.generate(25, (x) => x);
List l1 = numbers.toList()..shuffle(new MockRandom(randomNums));
for (int i = 0; i < 50; i++) {
// With same random sequence, we get the same shuffling each time.
List l2 = numbers.toList()..shuffle(new MockRandom(randomNums));
Expect.listEquals(l1, l2);
}
}
class MockRandom implements Random {
final List<int> _values;
int index = 0;
MockRandom(this._values);
int get _next {
int next = _values[index];
index = (index + 1) % _values.length;
return next;
}
int nextInt(int limit) => _next % limit;
double nextDouble() => _next / 256.0;
bool nextBool() => _next.isEven;
}

View file

@ -0,0 +1,155 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library sort_helper;
import "package:expect/expect.dart";
typedef Sorter = void Function(List<num>);
typedef Comparer = int Function(num, num);
class SortHelper {
SortHelper(this.sortFunction, this.compareFunction) {}
void run() {
testSortIntLists();
testSortDoubleLists();
}
bool isSorted(List<num> a) {
for (int i = 1; i < a.length; i++) {
if (compareFunction(a[i - 1], a[i]) > 0) {
return false;
}
}
return true;
}
void testSortIntLists() {
var a = new List<int>(40);
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
testSort(a);
for (int i = 0; i < a.length; i++) {
a[a.length - i - 1] = i;
}
testSort(a);
for (int i = 0; i < 21; i++) {
a[i] = 1;
}
for (int i = 21; i < a.length; i++) {
a[i] = 2;
}
testSort(a);
// Same with bad pivot-choices.
for (int i = 0; i < 21; i++) {
a[i] = 1;
}
for (int i = 21; i < a.length; i++) {
a[i] = 2;
}
a[6] = 1;
a[13] = 1;
a[19] = 1;
a[25] = 1;
a[33] = 2;
testSort(a);
for (int i = 0; i < 21; i++) {
a[i] = 2;
}
for (int i = 21; i < a.length; i++) {
a[i] = 1;
}
testSort(a);
// Same with bad pivot-choices.
for (int i = 0; i < 21; i++) {
a[i] = 2;
}
for (int i = 21; i < a.length; i++) {
a[i] = 1;
}
a[6] = 2;
a[13] = 2;
a[19] = 2;
a[25] = 2;
a[33] = 1;
testSort(a);
var a2 = new List<int>(0);
testSort(a2);
var a3 = new List<int>(1);
a3[0] = 1;
testSort(a3);
// --------
// Test insertion sort.
testInsertionSort(0, 1, 2, 3);
testInsertionSort(0, 1, 3, 2);
testInsertionSort(0, 3, 2, 1);
testInsertionSort(0, 3, 1, 2);
testInsertionSort(0, 2, 1, 3);
testInsertionSort(0, 2, 3, 1);
testInsertionSort(1, 0, 2, 3);
testInsertionSort(1, 0, 3, 2);
testInsertionSort(1, 2, 3, 0);
testInsertionSort(1, 2, 0, 3);
testInsertionSort(1, 3, 2, 0);
testInsertionSort(1, 3, 0, 2);
testInsertionSort(2, 0, 1, 3);
testInsertionSort(2, 0, 3, 1);
testInsertionSort(2, 1, 3, 0);
testInsertionSort(2, 1, 0, 3);
testInsertionSort(2, 3, 1, 0);
testInsertionSort(2, 3, 0, 1);
testInsertionSort(3, 0, 1, 2);
testInsertionSort(3, 0, 2, 1);
testInsertionSort(3, 1, 2, 0);
testInsertionSort(3, 1, 0, 2);
testInsertionSort(3, 2, 1, 0);
testInsertionSort(3, 2, 0, 1);
}
void testSort(List<num> a) {
sortFunction(a);
Expect.isTrue(isSorted(a));
}
void testInsertionSort(int i1, int i2, int i3, int i4) {
var a = new List<int>(4);
a[0] = i1;
a[1] = i2;
a[2] = i3;
a[3] = i4;
testSort(a);
}
void testSortDoubleLists() {
var a = new List<double>(40);
for (int i = 0; i < a.length; i++) {
a[i] = 1.0 * i + 0.5;
}
testSort(a);
for (int i = 0; i < a.length; i++) {
a[i] = 1.0 * (a.length - i) + 0.5;
}
testSort(a);
for (int i = 0; i < a.length; i++) {
a[i] = 1.5;
}
testSort(a);
}
Sorter sortFunction;
Comparer compareFunction;
}

View file

@ -0,0 +1,313 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for sort routines.
library sort_test;
import "package:expect/expect.dart";
import 'sort_helper.dart';
main() {
var compare = (num a, num b) => a.compareTo(b);
var sort = (List<num> list) => list.sort(compare);
new SortHelper(sort, compare).run();
compare = (num a, num b) => -a.compareTo(b);
new SortHelper(sort, compare).run();
var intCompare = (int a, int b) => a.compareTo(b);
// Pivot-candidate indices: 7, 15, 22, 29, 37
// Test Dutch flag partitioning (candidates 2 and 4 are the same).
var list = [
0,
0,
0,
0,
0,
0,
0,
0 /**/,
0,
0,
0,
0,
0,
0,
0,
1 /**/,
1,
1,
1,
1,
1,
1,
1 /**/,
1,
1,
1,
1,
1,
1,
1 /**/,
2,
2,
2,
2,
2,
2,
2,
2 /**/,
2,
2,
2,
2,
2,
2,
2
];
list.sort(intCompare);
Expect.listEquals(list, <int>[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2
]);
list = [
0,
0,
0,
0,
0,
0,
0,
1 /**/,
0,
0,
0,
0,
0,
0,
0,
0 /**/,
1,
1,
1,
1,
1,
1,
0 /**/,
1,
1,
1,
1,
1,
1,
0 /**/,
2 /**/,
2,
2,
2,
2,
2,
2,
2 /**/,
2,
2,
2,
2,
2,
2,
2
];
list.sort(intCompare);
Expect.listEquals(list, <int>[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2
]);
// Pivots: 1 and 8.
// The second partition will be big (more than 2/3 of the list), and an
// optimization kicks in that removes the pivots from the partition.
list = [
0,
9,
0,
9,
3,
9,
0,
1 /**/,
1,
0,
1,
9,
8,
2,
1,
1 /**/,
4,
5,
2,
5,
0,
1,
8 /**/,
8,
8,
5,
2,
2,
9,
8 /**/,
8,
4,
4,
1,
5,
3,
2,
8 /**/,
5,
1,
2,
8,
5,
6,
8
];
list.sort(intCompare);
Expect.listEquals(list, <int>[
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
1,
1,
2,
2,
2,
2,
2,
2,
3,
3,
4,
4,
4,
5,
5,
5,
5,
5,
5,
6,
8,
8,
8,
8,
8,
8,
8,
8,
8,
9,
9,
9,
9,
9
]);
}

View file

@ -0,0 +1,168 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import 'dart:collection';
main() {
defaultFunctionValuesTest();
defaultKeyFunctionTest();
defaultValueFunctionTest();
noDefaultValuesTest();
emptyIterableTest();
equalElementsTest();
genericTypeTest();
typedTest();
}
void defaultFunctionValuesTest() {
var map = new SplayTreeMap.fromIterable([1, 2, 3]);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(3, map.length);
Expect.equals(3, map.keys.length);
Expect.equals(3, map.values.length);
Expect.equals(1, map[1]);
Expect.equals(2, map[2]);
Expect.equals(3, map[3]);
}
void defaultKeyFunctionTest() {
var map = new SplayTreeMap.fromIterable([1, 2, 3], value: (x) => x + 1);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(3, map.length);
Expect.equals(3, map.keys.length);
Expect.equals(3, map.values.length);
Expect.equals(2, map[1]);
Expect.equals(3, map[2]);
Expect.equals(4, map[3]);
}
void defaultValueFunctionTest() {
var map = new SplayTreeMap.fromIterable([1, 2, 3], key: (x) => x + 1);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(3, map.length);
Expect.equals(3, map.keys.length);
Expect.equals(3, map.values.length);
Expect.equals(1, map[2]);
Expect.equals(2, map[3]);
Expect.equals(3, map[4]);
}
void noDefaultValuesTest() {
var map = new SplayTreeMap.fromIterable([1, 2, 3],
key: (x) => x + 1, value: (x) => x - 1);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(3, map.length);
Expect.equals(3, map.keys.length);
Expect.equals(3, map.values.length);
Expect.equals(0, map[2]);
Expect.equals(1, map[3]);
Expect.equals(2, map[4]);
}
void emptyIterableTest() {
var map = new SplayTreeMap.fromIterable([]);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(0, map.length);
Expect.equals(0, map.keys.length);
Expect.equals(0, map.values.length);
}
void equalElementsTest() {
var map = new SplayTreeMap.fromIterable([1, 2, 2], key: (x) => x + 1);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(2, map.length);
Expect.equals(2, map.keys.length);
Expect.equals(2, map.values.length);
Expect.equals(1, map[2]);
Expect.equals(2, map[3]);
}
void genericTypeTest() {
var map =
new SplayTreeMap<int, String>.fromIterable([1, 2, 3], value: (x) => '$x');
Expect.isTrue(map is Map<int, String>);
Expect.isTrue(map is SplayTreeMap<int, String>);
// Make sure it is not just SplayTreeMap<dynamic, dynamic>.
Expect.isFalse(map is SplayTreeMap<String, dynamic>);
Expect.isFalse(map is SplayTreeMap<dynamic, int>);
}
typedef String DynamicToString(dynamic v);
typedef bool DynamicToBool(dynamic v);
// Test in checked mode with explicitly given types.
void typedTest() {
// Assign functions to typed function variables.
DynamicToString key = (v) => "$v";
DynamicToBool value = (v) => (v as int).isOdd;
Function id = (int i) => i;
Expect.throws(() {
new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3], key: key
// No "value" map, defaults to identity, which returns int, not bool.
);
});
Expect.throws(() {
new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3],
// No "key" map, defaults to identity, which returns int, not String.
value: value);
});
Expect.throws(() {
new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3],
key: id as dynamic, // wrong type.
value: value);
});
Expect.throws(() {
new SplayTreeMap<String, bool>.fromIterable(<int>[1, 2, 3],
key: key, value: id as dynamic // wrong type.
);
});
// But it works with explicit types when used correctly.
SplayTreeMap<String, bool> map = new SplayTreeMap<String, bool>.fromIterable(
<int>[1, 2, 3],
key: key, value: value);
Iterable<String> keys = map.keys;
Iterable<bool> values = map.values;
List<String> keyList = keys.toList();
List<bool> valueList = values.toList();
Expect.equals(3, keyList.length);
Expect.equals(3, valueList.length);
Expect.equals(keys.first, map.firstKey());
Expect.equals(keys.last, map.lastKey());
}

View file

@ -0,0 +1,85 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import 'dart:collection';
main() {
positiveTest();
emptyMapTest();
fewerKeysIterableTest();
fewerValuesIterableTest();
equalElementsTest();
genericTypeTest();
}
void positiveTest() {
var map = new SplayTreeMap.fromIterables([1, 2, 3], ["one", "two", "three"]);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(3, map.length);
Expect.equals(3, map.keys.length);
Expect.equals(3, map.values.length);
Expect.equals("one", map[1]);
Expect.equals("two", map[2]);
Expect.equals("three", map[3]);
}
void emptyMapTest() {
var map = new SplayTreeMap.fromIterables([], []);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(0, map.length);
Expect.equals(0, map.keys.length);
Expect.equals(0, map.values.length);
}
void fewerValuesIterableTest() {
Expect.throws(() => new SplayTreeMap.fromIterables([1, 2], [0]));
}
void fewerKeysIterableTest() {
Expect.throws(() => new SplayTreeMap.fromIterables([1], [0, 2]));
}
void equalElementsTest() {
var map = new SplayTreeMap.fromIterables([1, 2, 2], ["one", "two", "three"]);
Expect.isTrue(map is Map);
Expect.isTrue(map is SplayTreeMap);
Expect.isFalse(map is HashMap);
Expect.equals(2, map.length);
Expect.equals(2, map.keys.length);
Expect.equals(2, map.values.length);
Expect.equals("one", map[1]);
Expect.equals("three", map[2]);
}
void genericTypeTest() {
var map = new SplayTreeMap<int, String>.fromIterables(
[1, 2, 3], ["one", "two", "three"]);
Expect.isTrue(map is Map<int, String>);
Expect.isTrue(map is SplayTreeMap<int, String>);
// Make sure it is not just SplayTreeMap<dynamic, dynamic>.
Expect.isFalse(map is SplayTreeMap<String, dynamic>);
Expect.isFalse(map is SplayTreeMap<dynamic, int>);
Expect.equals(3, map.length);
Expect.equals(3, map.keys.length);
Expect.equals(3, map.values.length);
Expect.equals("one", map[1]);
Expect.equals("two", map[2]);
Expect.equals("three", map[3]);
}

View file

@ -0,0 +1,138 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for Splaytrees.
library splay_tree_test;
import "package:expect/expect.dart";
import 'dart:collection';
main() {
// Simple tests.
SplayTreeMap tree = new SplayTreeMap();
tree[1] = "first";
tree[3] = "third";
tree[5] = "fifth";
tree[2] = "second";
tree[4] = "fourth";
var correctSolution = ["first", "second", "third", "fourth", "fifth"];
tree.forEach((key, value) {
Expect.equals(true, key >= 1);
Expect.equals(true, key <= 5);
Expect.equals(value, correctSolution[key - 1]);
});
for (var v in ["first", "second", "third", "fourth", "fifth"]) {
Expect.isTrue(tree.containsValue(v));
}
;
Expect.isFalse(tree.containsValue("sixth"));
tree[7] = "seventh";
Expect.equals(1, tree.firstKey());
Expect.equals(7, tree.lastKey());
Expect.equals(2, tree.lastKeyBefore(3));
Expect.equals(4, tree.firstKeyAfter(3));
Expect.equals(null, tree.lastKeyBefore(1));
Expect.equals(2, tree.firstKeyAfter(1));
Expect.equals(4, tree.lastKeyBefore(5));
Expect.equals(7, tree.firstKeyAfter(5));
Expect.equals(5, tree.lastKeyBefore(7));
Expect.equals(null, tree.firstKeyAfter(7));
Expect.equals(5, tree.lastKeyBefore(6));
Expect.equals(7, tree.firstKeyAfter(6));
testSetFrom();
regressRemoveWhere();
regressRemoveWhere2();
regressFromCompare();
}
void regressRemoveWhere() {
// Regression test. Fix in https://codereview.chromium.org/148523006/
var t = new SplayTreeSet();
t.addAll([1, 3, 5, 7, 2, 4, 6, 8, 0]);
var seen = new List<bool>.filled(9, false);
t.removeWhere((x) {
// Called only once per element.
Expect.isFalse(seen[x], "seen $x");
seen[x] = true;
return x.isOdd;
});
}
void regressRemoveWhere2() {
// Regression test for http://dartbug.com/18676
// Removing all elements with removeWhere causes error.
var t = new SplayTreeSet();
t.addAll([1, 2, 3, 4, 5]);
t.removeWhere((_) => true); // Should not throw.
Expect.isTrue(t.isEmpty);
t.addAll([1, 2, 3, 4, 5]);
t.retainWhere((_) => false); // Should not throw.
Expect.isTrue(t.isEmpty);
}
void testSetFrom() {
var set1 = new SplayTreeSet<num>()..addAll([1, 2, 3, 4, 5]);
var set2 = new SplayTreeSet<int>.from(set1);
Expect.equals(5, set2.length);
for (int i = 1; i <= 5; i++) {
Expect.isTrue(set2.contains(i));
}
set1 = new SplayTreeSet<num>()..addAll([0, 1, 2.4, 3.14, 5]);
set2 = new SplayTreeSet<int>.from(set1.where((x) => x is int));
Expect.equals(3, set2.length);
}
void regressFromCompare() {
// Regression test for http://dartbug.com/23387.
// The compare and isValidKey arguments to SplayTreeMap.from were ignored.
int compare(a, b) {
if (a is IncomparableKey && b is IncomparableKey) {
return b.id - a.id;
}
throw "isValidKey failure";
}
bool isValidKey(o) => o is IncomparableKey;
IncomparableKey key(int n) => new IncomparableKey(n);
var entries = {key(0): 0, key(1): 1, key(2): 2, key(0): 0};
Expect.equals(4, entries.length);
var map =
new SplayTreeMap<IncomparableKey, int>.from(entries, compare, isValidKey);
Expect.equals(3, map.length);
for (int i = 0; i < 3; i++) {
Expect.isTrue(map.containsKey(key(i)));
Expect.equals(i, map[key(i)]);
}
Expect.isFalse(map.containsKey(key(5)));
Expect.isFalse(map.containsKey(1));
Expect.isFalse(map.containsKey("string"));
Expect.equals(null, map[key(5)]);
Expect.equals(null, map[1]);
Expect.equals(null, map["string"]);
map[1] = 42; //# 01: compile-time error
map["string"] = 42; //# 02: compile-time error
map[key(5)] = 42;
Expect.equals(4, map.length);
Expect.equals(42, map[key(5)]);
}
class IncomparableKey {
final int id;
IncomparableKey(this.id);
}

View file

@ -0,0 +1,36 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:convert" show LineSplitter;
import "package:expect/expect.dart";
void main() {
var st0;
var st1;
// Primitive way to get stack trace,.
try {
throw 0;
} catch (_, s) {
st0 = s;
}
st1 = StackTrace.current;
var st0s = findMain(st0);
var st1s = findMain(st1);
// Stack traces are not equal (contains at least a different line number,
// and possible different frame numbers).
// They are *similar*, so check that they agree on everything but numbers.
var digits = new RegExp(r"\d+");
Expect.equals(st0s.replaceAll(digits, "0"), st1s.replaceAll(digits, "0"));
}
String findMain(StackTrace stack) {
var string = "$stack";
var lines = LineSplitter.split(string).toList();
while (lines.isNotEmpty && !lines.first.contains("main")) {
lines.removeAt(0);
}
return lines.join("\n");
}

View file

@ -0,0 +1,45 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import "package:async_helper/async_helper.dart";
import "dart:async";
void main() {
StackTrace stack;
try {
throw 0;
} catch (e, s) {
stack = s;
}
var string = "$stack";
StackTrace stringTrace = new StackTrace.fromString(string);
Expect.isTrue(stringTrace is StackTrace);
Expect.equals(stack.toString(), stringTrace.toString());
string = "some random string, nothing like a StackTrace";
stringTrace = new StackTrace.fromString(string);
Expect.isTrue(stringTrace is StackTrace);
Expect.equals(string, stringTrace.toString());
// Use stacktrace asynchronously.
asyncStart();
var c = new Completer();
c.completeError(0, stringTrace);
c.future.then((v) {
throw "Unexpected value: $v";
}, onError: (e, s) {
Expect.equals(string, s.toString());
}).then((_) {
var c = new StreamController();
c.stream.listen((v) {
throw "Unexpected value: $v";
}, onError: (e, s) {
Expect.equals(string, s.toString());
asyncEnd();
});
c.addError(0, stringTrace);
c.close();
});
}

View file

@ -0,0 +1,17 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test program for elapsed getters in stopwatch support.
import "package:expect/expect.dart";
main() {
Stopwatch sw = new Stopwatch()..start();
while (sw.elapsedMilliseconds < 2) {
/* do nothing. */
}
sw.stop();
Expect.equals(sw.elapsedMicroseconds, sw.elapsed.inMicroseconds);
Expect.equals(sw.elapsedMilliseconds, sw.elapsed.inMilliseconds);
}

View file

@ -0,0 +1,128 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test program for testing stopwatch support.
library stopwatch_test;
import "package:expect/expect.dart";
class StopwatchTest {
static bool checkTicking(Stopwatch sw) {
Expect.isFalse(sw.isRunning);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 1000000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
break;
}
}
return sw.elapsedTicks > 0;
}
static bool checkStopping(Stopwatch sw) {
sw.stop();
Expect.isFalse(sw.isRunning);
int v1 = sw.elapsedTicks;
Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time.
Stopwatch sw2 = new Stopwatch(); // Used for verification.
sw2.start();
Expect.isTrue(sw2.isRunning);
int sw2LastElapsed = 0;
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
int v2 = sw.elapsedTicks;
if (v1 != v2) {
return false;
}
// If sw2 elapsed twice then sw must have advanced too if it wasn't
// stopped.
if (sw2LastElapsed > 0 && sw2.elapsedTicks > sw2LastElapsed) {
break;
}
sw2LastElapsed = sw2.elapsedTicks;
}
// The test only makes sense if measureable time elapsed and elapsed time
// on the stopped Stopwatch did not increase.
Expect.isTrue(sw2.elapsedTicks > 0);
return true;
}
static checkRestart() {
Stopwatch sw = new Stopwatch();
Expect.isFalse(sw.isRunning);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
break;
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
int initial = sw.elapsedTicks;
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > initial) {
break;
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
Expect.isTrue(sw.elapsedTicks > initial);
}
static checkReset() {
Stopwatch sw = new Stopwatch();
Expect.isFalse(sw.isRunning);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
break;
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
sw.reset();
Expect.isFalse(sw.isRunning);
Expect.equals(0, sw.elapsedTicks);
sw.start();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
break;
}
}
sw.reset();
Expect.isTrue(sw.isRunning);
for (int i = 0; i < 100000; i++) {
int.parse(i.toString());
if (sw.elapsedTicks > 0) {
break;
}
}
sw.stop();
Expect.isFalse(sw.isRunning);
Expect.isTrue(sw.elapsedTicks > 0);
}
static testMain() {
Stopwatch sw = new Stopwatch();
Expect.isTrue(checkTicking(sw));
Expect.isTrue(checkStopping(sw));
checkRestart();
checkReset();
}
}
main() {
StopwatchTest.testMain();
}

View file

@ -0,0 +1,10 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test program for testing class 'StringBase' (currently VM specific).
import "package:expect/expect.dart";
void main() {
String s4 = new String.fromCharCodes([/*@compile-error=unspecified*/ 0.0]);
}

View file

@ -0,0 +1,74 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test program for testing class 'StringBase' (currently VM specific).
library string_base_test;
import "package:expect/expect.dart";
class StringBaseTest {
StringBaseTest() {}
toString() {
return "StringBase Tester";
}
static testInterpolation() {
var answer = 40 + 2;
var s = "The answer is $answer.";
Expect.equals("The answer is 42.", s);
int numBottles = 33;
String wall = "wall";
s = "${numBottles*3} bottles of beer on the $wall.";
Expect.equals("99 bottles of beer on the wall.", s);
}
static testCreation() {
String s = "Hello";
List<int> a = new List.filled(s.length, -1);
List<int> ga = new List();
bool exception_caught = false;
for (int i = 0; i < a.length; i++) {
a[i] = s.codeUnitAt(i);
ga.add(s.codeUnitAt(i));
}
try {
String s4 = new String.fromCharCodes([-1]);
} on ArgumentError catch (ex) {
exception_caught = true;
}
Expect.equals(true, exception_caught);
}
static testSubstring() {
String s = "Hello World";
Expect.equals("World", s.substring(6, s.length));
Expect.equals("", s.substring(8, 8));
bool exception_caught = false;
try {
s.substring(5, 12);
} on RangeError catch (ex) {
exception_caught = true;
}
Expect.equals(true, exception_caught);
exception_caught = false;
try {
s.substring(5, 4);
} on RangeError catch (ex) {
exception_caught = true;
}
Expect.equals(true, exception_caught);
}
static void testMain() {
testInterpolation();
testCreation();
testSubstring();
}
}
main() {
StringBaseTest.testMain();
}

View file

@ -0,0 +1,224 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// TODO(srdjan): Move StringBuffer to visible names.
void testConstructor() {
StringBuffer bf = new StringBuffer("");
testBufferLength(0, bf);
bf = new StringBuffer("abc");
testBufferLength(3, bf);
Expect.equals("abc", bf.toString());
bf = new StringBuffer("\x00");
}
void testWrite() {
StringBuffer bf = new StringBuffer("");
Expect.equals(true, bf.isEmpty);
bf.write("a");
testBufferLength(1, bf);
Expect.equals("a", bf.toString());
bf = new StringBuffer("");
bf.write("a");
bf.write("b");
Expect.equals("ab", bf.toString());
bf = new StringBuffer("abc");
bf.write("d");
bf.write("e");
bf.write("f");
bf.write("g");
bf.write("h");
bf.write("i");
bf.write("j");
bf.write("k");
bf.write("l");
bf.write("m");
bf.write("n");
bf.write("o");
bf.write("p");
bf.write("q");
bf.write("r");
bf.write("s");
bf.write("t");
bf.write("u");
bf.write("v");
bf.write("w");
bf.write("x");
bf.write("y");
bf.write("z");
bf.write("\n");
bf.write("thequickbrownfoxjumpsoverthelazydog");
Expect.equals(
"abcdefghijklmnopqrstuvwxyz\n"
"thequickbrownfoxjumpsoverthelazydog",
bf.toString());
bf = new StringBuffer("");
for (int i = 0; i < 100000; i++) {
bf.write('');
bf.write("");
}
Expect.equals("", bf.toString());
}
void testLength() {
StringBuffer bf = new StringBuffer("");
testBufferLength(0, bf);
bf.write("foo");
testBufferLength(3, bf);
bf.write("bar");
testBufferLength(6, bf);
bf.write("");
testBufferLength(6, bf);
}
void testIsEmpty() {
StringBuffer bf = new StringBuffer("");
Expect.equals(true, bf.isEmpty);
bf.write("foo");
Expect.equals(false, bf.isEmpty);
}
void testWriteAll() {
StringBuffer bf = new StringBuffer("");
bf.writeAll(["foo", "bar", "a", "b", "c"]);
Expect.equals("foobarabc", bf.toString());
bf.writeAll([]);
Expect.equals("foobarabc", bf.toString());
bf.writeAll(["", "", ""]);
Expect.equals("foobarabc", bf.toString());
bf.writeAll(["", "", ""], "");
Expect.equals("foobarabc", bf.toString());
StringBuffer bf2 = new StringBuffer("");
bf2.writeAll([], "s");
Expect.equals("", bf2.toString());
StringBuffer bf3 = new StringBuffer("");
bf3.writeAll(["a"], "s");
Expect.equals("a", bf3.toString());
StringBuffer bf4 = new StringBuffer("");
bf4.writeAll(["a", "b"], "s");
Expect.equals("asb", bf4.toString());
}
void testWriteln() {
StringBuffer bf1 = new StringBuffer("");
bf1.writeln("Hello");
Expect.equals("Hello\n", bf1.toString());
StringBuffer bf2 = new StringBuffer("");
bf2.writeln();
Expect.equals("\n", bf2.toString());
StringBuffer bf3 = new StringBuffer("");
bf3.writeln("\n");
bf3.writeln(null);
bf3.writeln(1);
Expect.equals("\n\nnull\n1\n", bf3.toString());
}
void testClear() {
StringBuffer bf = new StringBuffer("");
bf.write("foo");
bf.clear();
Expect.equals("", bf.toString());
testBufferLength(0, bf);
bf.write("bar");
Expect.equals("bar", bf.toString());
testBufferLength(3, bf);
bf.clear();
Expect.equals("", bf.toString());
testBufferLength(0, bf);
}
void testToString() {
StringBuffer bf = new StringBuffer("");
Expect.equals("", bf.toString());
bf = new StringBuffer("foo");
Expect.equals("foo", bf.toString());
bf = new StringBuffer("foo");
bf.write("bar");
Expect.equals("foobar", bf.toString());
}
void testChaining() {
StringBuffer bf = new StringBuffer("");
StringBuffer bf2 = new StringBuffer("");
bf2.write("bf2");
bf..write("foo")..write("bar")..write(bf2)..write(bf2)..write("toto");
Expect.equals("foobarbf2bf2toto", bf.toString());
}
void testWriteCharCode() {
StringBuffer bf1 = new StringBuffer();
StringBuffer bf2 = new StringBuffer();
bf1.write("a");
bf2.writeCharCode(0x61); // a
bf1.write("b");
bf2.writeCharCode(0x62); // b
bf1.write("c");
bf2.writeCharCode(0x63); // c
bf1.write(new String.fromCharCode(0xD823));
bf2.writeCharCode(0xD823);
bf1.write(new String.fromCharCode(0xDC23));
bf2.writeCharCode(0xDC23);
bf1.write("\u{1d49e}");
bf2.writeCharCode(0x1d49e);
bf1.write("\x00");
bf2.writeCharCode(0);
Expect.equals(bf1.toString(), bf2.toString());
Expect.equals("abc\u{18c23}\u{1d49e}\x00", bf2.toString());
// Mixing strings and char-codes.
bf1.clear();
bf1.write("abcde");
bf1.writeCharCode(0x61);
bf1.writeCharCode(0x62);
bf1.writeCharCode(0x63);
bf1.write("d");
bf1.writeCharCode(0x65);
Expect.equals("abcdeabcde", bf1.toString());
// Out-of-range character codes are not allowed.
Expect.throws(() {
bf2.writeCharCode(-1);
});
Expect.throws(() {
bf2.writeCharCode(0x110000);
});
}
void testBufferLength(int length, StringBuffer bf) {
Expect.equals(length, bf.length);
(length == 0 ? Expect.isTrue : Expect.isFalse)(bf.isEmpty);
(length != 0 ? Expect.isTrue : Expect.isFalse)(bf.isNotEmpty);
}
void main() {
testToString();
testConstructor();
testLength();
testIsEmpty();
testWrite();
testWriteCharCode();
testWriteAll();
testWriteln();
testClear();
testChaining();
}

View file

@ -0,0 +1,55 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
testLowerUpper();
testSpecialCases();
}
void testLowerUpper() {
var a = "Stop! Smell the Roses.";
var allLower = "stop! smell the roses.";
var allUpper = "STOP! SMELL THE ROSES.";
Expect.equals(allUpper, a.toUpperCase());
Expect.equals(allLower, a.toLowerCase());
}
void testSpecialCases() {
// Letters in Latin-1 where the upper case is not in Latin-1.
// German sharp s. Upper case variant is "SS".
Expect.equals("SS", "\xdf".toUpperCase()); // //# 01: ok
Expect.equals("\xdf", "\xdf".toLowerCase());
Expect.equals("ss", "\xdf".toUpperCase().toLowerCase()); // //# 01: continued
// Micro sign (not same as lower-case Greek letter mu, U+03BC).
Expect.equals("\u039c", "\xb5".toUpperCase()); // //# 02: ok
Expect.equals("\xb5", "\xb5".toLowerCase());
Expect.equals("\u03Bc", // //# 02: continued
"\xb5".toUpperCase().toLowerCase()); // //# 02: continued
// Small letter y diaresis.
Expect.equals("\u0178", "\xff".toUpperCase()); // //# 03: ok
Expect.equals("\xff", "\xff".toLowerCase());
Expect.equals("\xff", "\xff".toUpperCase().toLowerCase()); // //# 03: continued
// Zero.
Expect.equals("\x00", "\x00".toLowerCase());
Expect.equals("\x00", "\x00".toUpperCase());
// Test all combinations of ordering of lower-case, upper-case and
// special-when-upper-cased characters.
Expect.equals("AA\u0178", "Aa\xff".toUpperCase()); // //# 03: continued
Expect.equals("AA\u0178", "aA\xff".toUpperCase()); // //# 03: continued
Expect.equals("A\u0178A", "A\xffa".toUpperCase()); // //# 03: continued
Expect.equals("A\u0178A", "a\xffA".toUpperCase()); // //# 03: continued
Expect.equals("\u0178AA", "\xffAa".toUpperCase()); // //# 03: continued
Expect.equals("\u0178AA", "\xffaA".toUpperCase()); // //# 03: continued
Expect.equals("aa\xff", "Aa\xff".toLowerCase());
Expect.equals("aa\xff", "aA\xff".toLowerCase());
Expect.equals("a\xffa", "A\xffa".toLowerCase());
Expect.equals("a\xffa", "a\xffA".toLowerCase());
Expect.equals("\xffaa", "\xffAa".toLowerCase());
Expect.equals("\xffaa", "\xffaA".toLowerCase());
}

View file

@ -0,0 +1,78 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
test(String s) {
List<int> units = s.codeUnits;
List<int> expectedUnits = <int>[];
for (int i = 0; i < s.length; i++) {
expectedUnits.add(s.codeUnitAt(i));
}
Expect.equals(s.length, units.length);
for (int i = 0; i < s.length; i++) {
Expect.equals(s.codeUnitAt(i), units.elementAt(i));
}
// for-in
var res = [];
for (int unit in units) {
res.add(unit);
}
Expect.listEquals(expectedUnits, res);
// .map
Expect.listEquals(expectedUnits.map((x) => x.toRadixString(16)).toList(),
units.map((x) => x.toRadixString(16)).toList());
if (s == "") {
Expect.throwsStateError(() => units.first);
Expect.throwsStateError(() => units.last);
Expect.throwsRangeError(() => units[0]);
Expect.throwsUnsupportedError(() => units[0] = 499);
Expect.listEquals([], units.sublist(0, 0));
Expect.equals(-1, units.indexOf(42));
Expect.equals(-1, units.lastIndexOf(499));
} else {
Expect.equals(s.codeUnitAt(0), units.first);
Expect.equals(s.codeUnitAt(s.length - 1), units.last);
Expect.equals(s.codeUnitAt(0), units[0]);
Expect.throwsUnsupportedError(() => units[0] = 499);
List<int> sub = units.sublist(1);
Expect.listEquals(s.substring(1, s.length).codeUnits, sub);
Expect.equals(-1, units.indexOf(-1));
Expect.equals(0, units.indexOf(units[0]));
Expect.equals(-1, units.lastIndexOf(-1));
Expect.equals(
units.length - 1, units.lastIndexOf(units[units.length - 1]));
}
Iterable reversed = units.reversed;
int i = units.length - 1;
for (int codeUnit in reversed) {
Expect.equals(units[i--], codeUnit);
}
}
test("");
test("abc");
test("\x00\u0000\u{000000}");
test("\u{ffff}\u{10000}\u{10ffff}");
String string = new String.fromCharCodes(
[0xdc00, 0xd800, 61, 0xd9ab, 0xd9ab, 0xddef, 0xddef, 62, 0xdc00, 0xd800]);
test(string);
string = "\x00\x7f\xff\u0100\ufeff\uffef\uffff"
"\u{10000}\u{12345}\u{1d800}\u{1dc00}\u{1ffef}\u{1ffff}";
test(string);
// Reading each unit of a surrogate pair works.
var r = "\u{10000}".codeUnits;
var it = r.iterator;
Expect.isTrue(it.moveNext());
Expect.equals(0xD800, it.current);
Expect.isTrue(it.moveNext());
Expect.equals(0xDC00, it.current);
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// SharedOptions=-Da=a -Da=bb -Db=bb -Dc=ccc -Da=ccc -Db=ccc
import "package:expect/expect.dart";
main() {
Expect.equals('ccc', const String.fromEnvironment('a'));
Expect.equals('ccc', const String.fromEnvironment('b'));
Expect.equals('ccc', const String.fromEnvironment('c'));
}

View file

@ -0,0 +1,11 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
main() {
const String.fromEnvironment('NOT_FOUND', defaultValue: 1); // //# 01: compile-time error
const String.fromEnvironment('NOT_FOUND', defaultValue: true); // //# 02: compile-time error
const String.fromEnvironment(null); // //# 03: compile-time error
const String.fromEnvironment(1); // //# 04: compile-time error
const String.fromEnvironment([]); // //# 05: compile-time error
}

View file

@ -0,0 +1,13 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// SharedOptions=-Da=a -Db=bb -Dc=ccc -Dd=
import "package:expect/expect.dart";
main() {
Expect.equals('a', const String.fromEnvironment('a'));
Expect.equals('bb', const String.fromEnvironment('b'));
Expect.equals('ccc', const String.fromEnvironment('c'));
Expect.equals('', const String.fromEnvironment('d'));
}

View file

@ -0,0 +1,84 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void main() {
Expect.equals("", new String.fromCharCodes(new List.empty()));
Expect.equals("", new String.fromCharCodes([]));
Expect.equals("", new String.fromCharCodes(const []));
Expect.equals("AB", new String.fromCharCodes([65, 66]));
Expect.equals("AB", new String.fromCharCodes(const [65, 66]));
Expect.equals("Ærø", new String.fromCharCodes(const [0xc6, 0x72, 0xf8]));
Expect.equals("\u{1234}", new String.fromCharCodes([0x1234]));
Expect.equals("\u{12345}*", new String.fromCharCodes([0x12345, 42]));
Expect.equals("", new String.fromCharCodes(new List()));
{
var a = <int>[];
a.add(65);
a.add(66);
Expect.equals("AB", new String.fromCharCodes(a));
}
// Long list (bug 6919).
for (int len in [499, 500, 501, 999, 100000]) {
List<int> list = new List.filled(len, -1);
for (int i = 0; i < len; i++) {
list[i] = 65 + (i % 26);
}
for (int i = len - 9; i < len; i++) {
list[i] = 48 + (len - i);
}
// We should not throw a stack overflow here.
String long = new String.fromCharCodes(list);
// Minimal sanity checking on the string.
Expect.isTrue(long.startsWith('ABCDE'));
Expect.isTrue(long.endsWith('987654321'));
int middle = len ~/ 2;
middle -= middle % 26;
Expect.equals('XYZABC', long.substring(middle - 3, middle + 3));
Expect.equals(len, long.length);
}
// Should work with iterables and non-default-lists (http://dartbug.com/8922)
Expect.equals("CBA", new String.fromCharCodes([65, 66, 67].reversed));
Expect.equals(
"BCD", new String.fromCharCodes([65, 66, 67].map((x) => x + 1)));
Expect.equals(
"AC", new String.fromCharCodes([0x41, 0x42, 0x43].where((x) => x.isOdd)));
Expect.equals(
"CE",
new String.fromCharCodes(
[0x41, 0x42, 0x43].where((x) => x.isOdd).map((x) => x + 2)));
Expect.equals(
"ABC", new String.fromCharCodes(new Iterable.generate(3, (x) => 65 + x)));
Expect.equals("ABC", new String.fromCharCodes("ABC".codeUnits));
Expect.equals(
"BCD", new String.fromCharCodes("ABC".codeUnits.map((x) => x + 1)));
Expect.equals("BCD", new String.fromCharCodes("ABC".runes.map((x) => x + 1)));
var nonBmpCharCodes = [0, 0xD812, 0xDC34, 0x14834, 0xDC34, 0xD812];
var nonBmp = new String.fromCharCodes(nonBmpCharCodes);
Expect.equals(7, nonBmp.length);
Expect.equals(0, nonBmp.codeUnitAt(0));
Expect.equals(0xD812, nonBmp.codeUnitAt(1)); // Separated surrogate pair
Expect.equals(0xDC34, nonBmp.codeUnitAt(2));
Expect.equals(0xD812, nonBmp.codeUnitAt(3)); // Single non-BMP code point.
Expect.equals(0xDC34, nonBmp.codeUnitAt(4));
Expect.equals(0xDC34, nonBmp.codeUnitAt(5)); // Unmatched surrogate.
Expect.equals(0xD812, nonBmp.codeUnitAt(6)); // Unmatched surrogate.
var reversedNonBmp = new String.fromCharCodes(nonBmpCharCodes.reversed);
Expect.equals(7, reversedNonBmp.length);
Expect.equals(0, reversedNonBmp.codeUnitAt(6));
Expect.equals(0xD812, reversedNonBmp.codeUnitAt(5));
Expect.equals(0xDC34, reversedNonBmp.codeUnitAt(4));
Expect.equals(0xDC34, reversedNonBmp.codeUnitAt(3));
Expect.equals(0xD812, reversedNonBmp.codeUnitAt(2));
Expect.equals(0xDC34, reversedNonBmp.codeUnitAt(1));
Expect.equals(0xD812, reversedNonBmp.codeUnitAt(0));
Expect.equals(nonBmp, new String.fromCharCodes(nonBmp.codeUnits));
Expect.equals(nonBmp, new String.fromCharCodes(nonBmp.runes));
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
Expect.equals("A", new String.fromCharCode(65));
Expect.equals("B", new String.fromCharCode(66));
var gClef = new String.fromCharCode(0x1D11E);
Expect.equals(2, gClef.length);
Expect.equals(0xD834, gClef.codeUnitAt(0));
Expect.equals(0xDD1E, gClef.codeUnitAt(1));
// Unmatched surrogates.
var unmatched = new String.fromCharCode(0xD800);
Expect.equals(1, unmatched.length);
Expect.equals(0xD800, unmatched.codeUnitAt(0));
unmatched = new String.fromCharCode(0xDC00);
Expect.equals(1, unmatched.length);
Expect.equals(0xDC00, unmatched.codeUnitAt(0));
Expect.throwsArgumentError(() => new String.fromCharCode(-1));
// Invalid code point.
Expect.throwsArgumentError(() => new String.fromCharCode(0x110000));
Expect.throwsArgumentError(() => new String.fromCharCode(0x110001));
}

View file

@ -0,0 +1,292 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import "dart:typed_data";
main() {
iter(count, [values]) => values is List
? new Iterable<int>.generate(count, (x) => values[x])
: new Iterable<int>.generate(count, (x) => values);
test(expect, iter, [start = 0, end]) {
var actual = new String.fromCharCodes(iter, start, end);
Expect.equals(expect, actual);
}
testThrows(iterable, [start = 0, end]) {
Expect.throws(() {
new String.fromCharCodes(iterable, start, end);
});
}
test("", iter(0));
test("", <int>[]);
test("", const <int>[]);
test("", new List<int>.empty());
test("", new Uint8List(0));
test("", new Uint16List(0));
test("", new Uint32List(0));
test("", "".codeUnits);
test("\x00", iter(1, 0));
test("\x00", [0]);
test("\x00", const [0]);
test("\x00", new List<int>.filled(1, 0));
test("\x00", new Uint8List(1));
test("\x00", new Uint16List(1));
test("\x00", new Uint32List(1));
test("\x00", "\x00".codeUnits);
test("\xff", iter(1, 255));
test("\xFF", [255]);
test("\xFF", const [255]);
test("\xFF", new List<int>.filled(1, 255));
test("\xFF", new Uint8List(1)..[0] = 255);
test("\xFF", new Uint16List(1)..[0] = 255);
test("\xFF", new Uint32List(1)..[0] = 255);
test("\xFF", "\xFF".codeUnits);
test("\u0100", iter(1, 256));
test("\u0100", [256]);
test("\u0100", const [256]);
test("\u0100", new List<int>.filled(1, 256));
test("\u0100", new Uint16List(1)..[0] = 256);
test("\u0100", new Uint32List(1)..[0] = 256);
test("\u0100", "\u0100".codeUnits);
test("\uffff", iter(1, 65535));
test("\uffff", [65535]);
test("\uffff", const [65535]);
test("\uffff", new List<int>.filled(1, 65535));
test("\uffff", new Uint16List(1)..[0] = 65535);
test("\uffff", new Uint32List(1)..[0] = 65535);
test("\uffff", "\uffff".codeUnits);
test("\u{10000}", iter(1, 65536));
test("\u{10000}", [65536]);
test("\u{10000}", const [65536]);
test("\u{10000}", new List<int>.filled(1, 65536));
test("\u{10000}", new Uint32List(1)..[0] = 65536);
test("\u{10000}", "\u{10000}".codeUnits);
test("\u{10FFFF}", iter(1, 0x10FFFF));
test("\u{10FFFF}", [0x10FFFF]);
test("\u{10FFFF}", const [0x10FFFF]);
test("\u{10FFFF}", new List<int>.filled(1, 0x10FFFF));
test("\u{10FFFF}", new Uint32List(1)..[0] = 0x10FFFF);
test("\u{10ffff}", iter(2, [0xDBFF, 0xDFFF]));
test("\u{10ffff}", [0xDBFF, 0xDFFF]);
test("\u{10ffff}", const [0xDBFF, 0xDFFF]);
test(
"\u{10ffff}",
new List<int>.filled(2, -1)
..[0] = 0xDBFF
..[1] = 0xDFFF);
test(
"\u{10ffff}",
new Uint16List(2)
..[0] = 0xDBFF
..[1] = 0xDFFF);
test(
"\u{10ffff}",
new Uint32List(2)
..[0] = 0xDBFF
..[1] = 0xDFFF);
test("\u{10FFFF}", "\u{10FFFF}".codeUnits);
var leadSurrogate = "\u{10ffff}"[0];
test(leadSurrogate, iter(1, 0xDBFF));
test(leadSurrogate, [0xDBFF]);
test(leadSurrogate, const [0xDBFF]);
test(leadSurrogate, new List<int>.filled(1, 0xDBFF));
test(leadSurrogate, new Uint16List(1)..[0] = 0xDBFF);
test(leadSurrogate, new Uint32List(1)..[0] = 0xDBFF);
test(leadSurrogate, leadSurrogate.codeUnits);
var tailSurrogate = "\u{10ffff}"[1];
test(tailSurrogate, iter(1, 0xDFFF));
test(tailSurrogate, [0xDFFF]);
test(tailSurrogate, const [0xDFFF]);
test(tailSurrogate, new List<int>.filled(1, 0xDFFF));
test(tailSurrogate, new Uint16List(1)..[0] = 0xDFFF);
test(tailSurrogate, new Uint32List(1)..[0] = 0xDFFF);
test(tailSurrogate, tailSurrogate.codeUnits);
testThrows(null);
testThrows("not an iterable");
testThrows(42);
testThrows([-1]);
testThrows(new List<int>.filled(1, -1));
testThrows(const [-1]);
testThrows(new Int8List(1)..[0] = -1);
testThrows(new Int16List(1)..[0] = -1);
testThrows(new Int32List(1)..[0] = -1);
testThrows([0x110000]);
testThrows(new List<int>.filled(1, 0x110000));
testThrows(const [0x110000]);
testThrows(new Int32List(1)..[0] = 0x110000);
// Check start/end
var list = const [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48];
for (var iterable in [
iter(list.length, list),
list.toList(growable: true),
list.toList(growable: false),
list,
new Uint8List(8)..setRange(0, 8, list),
new Uint16List(8)..setRange(0, 8, list),
new Uint32List(8)..setRange(0, 8, list),
"ABCDEFGH".codeUnits,
]) {
test("ABCDEFGH", iterable);
// start varies, end is null.
test("ABCDEFGH", iterable, 0);
test("BCDEFGH", iterable, 1);
test("H", iterable, 7);
test("", iterable, 8);
// start = 0, end varies.
test("ABCDEFGH", iterable, 0);
test("A", iterable, 0, 1);
test("AB", iterable, 0, 2);
test("ABCDEFG", iterable, 0, 7);
test("ABCDEFGH", iterable, 0, 8);
test("", iterable, 0, 0);
// Both varying.
test("ABCDEFGH", iterable, 0, 8);
test("AB", iterable, 0, 2);
test("GH", iterable, 6, 8);
test("DE", iterable, 3, 5);
test("", iterable, 3, 3);
}
// Can split surrogates in input, but not a single big code point.
test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1);
test(tailSurrogate, [0xDBFF, 0xDFFF], 1);
test("\u{10FFFF}", [0x10FFFF], 0, 1);
void testThrowsRange(iterable, [start = 0, end]) {
Expect.throwsRangeError(
() => new String.fromCharCodes(iterable, start, end));
}
// Test varying slices of the code units of a string.
testSubstring(string) {
var codes = string.codeUnits;
int length = string.length;
for (var iterable in [
iter(length, codes),
codes.toList(growable: true),
codes.toList(growable: false),
new Uint16List(length)..setRange(0, length, codes),
new Int32List(length)..setRange(0, length, codes),
new Uint32List(length)..setRange(0, length, codes),
codes,
]) {
var newString = new String.fromCharCodes(iterable);
Expect.equals(string, newString);
for (int i = 0; i < length; i = i * 2 + 1) {
test(string.substring(i), iterable, i);
test(string.substring(0, i), iterable, 0, i);
for (int j = 0; i + j < length; j = j * 2 + 1) {
test(string.substring(i, i + j), iterable, i, i + j);
}
}
testThrowsRange(iterable, -1);
testThrowsRange(iterable, 0, -1);
testThrowsRange(iterable, 2, 1);
testThrowsRange(iterable, 0, length + 1);
testThrowsRange(iterable, length + 1);
testThrowsRange(iterable, length + 1, length + 2);
}
}
testSubstring("");
testSubstring("ABCDEFGH");
// length > 128
testSubstring("ABCDEFGH" * 33);
testSubstring("\x00" * 357);
// length > 128 and non-ASCII.
testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37);
// Large List.
var megaList = ("abcde" * 200000).codeUnits.toList();
test("abcde" * 199998, megaList, 5, 999995);
// Large Uint8List.
test("abcde" * 199998, new Uint8List.fromList(megaList), 5, 999995);
const cLatin1 = const [0x00, 0xff];
const cUtf16 = const [0x00, 0xffff, 0xdfff, 0xdbff, 0xdfff, 0xdbff];
const cCodepoints = const [0x00, 0xffff, 0xdfff, 0x10ffff, 0xdbff];
List gLatin1 = cLatin1.toList(growable: true);
List gUtf16 = cUtf16.toList(growable: true);
List gCodepoints = cCodepoints.toList(growable: true);
List fLatin1 = cLatin1.toList(growable: false);
List fUtf16 = cUtf16.toList(growable: false);
List fCodepoints = cCodepoints.toList(growable: false);
Uint8List bLatin1 = new Uint8List(2)..setRange(0, 2, cLatin1);
Uint16List wLatin1 = new Uint16List(2)..setRange(0, 2, cLatin1);
Uint16List wUtf16 = new Uint16List(6)..setRange(0, 6, cUtf16);
Uint32List lLatin1 = new Uint32List(2)..setRange(0, 2, cLatin1);
Uint32List lUtf16 = new Uint32List(6)..setRange(0, 6, cUtf16);
Uint32List lCodepoints = new Uint32List(5)..setRange(0, 5, cCodepoints);
Uint8List bvLatin1 = new Uint8List.view(bLatin1.buffer);
Uint16List wvLatin1 = new Uint16List.view(wLatin1.buffer);
Uint16List wvUtf16 = new Uint16List.view(wUtf16.buffer);
Uint32List lvLatin1 = new Uint32List.view(lLatin1.buffer);
Uint32List lvUtf16 = new Uint32List.view(lUtf16.buffer);
Uint32List lvCodepoints = new Uint32List.view(lCodepoints.buffer);
var buffer = new Uint8List(200).buffer;
Uint8List bbLatin1 = new Uint8List.view(buffer, 3, 2)..setAll(0, bLatin1);
Uint16List wbLatin1 = new Uint16List.view(buffer, 8, 2)..setAll(0, wLatin1);
Uint16List wbUtf16 = new Uint16List.view(buffer, 16, 6)..setAll(0, wUtf16);
Uint32List lbLatin1 = new Uint32List.view(buffer, 32, 2)..setAll(0, lLatin1);
Uint32List lbUtf16 = new Uint32List.view(buffer, 64, 6)..setAll(0, lUtf16);
Uint32List lbCodepoints = new Uint32List.view(buffer, 128, 5)
..setAll(0, lCodepoints);
String sLatin1 = "\x00\xff";
String sUnicode =
"\x00\uffff$tailSurrogate$leadSurrogate$tailSurrogate$leadSurrogate";
for (int i = 0; i < 2; i++) {
for (int j = i + 1; j < 2; j++) {
test(sLatin1.substring(i, j), cLatin1, i, j);
test(sLatin1.substring(i, j), gLatin1, i, j);
test(sLatin1.substring(i, j), fLatin1, i, j);
test(sLatin1.substring(i, j), bLatin1, i, j);
test(sLatin1.substring(i, j), wLatin1, i, j);
test(sLatin1.substring(i, j), lLatin1, i, j);
test(sLatin1.substring(i, j), bvLatin1, i, j);
test(sLatin1.substring(i, j), wvLatin1, i, j);
test(sLatin1.substring(i, j), lvLatin1, i, j);
test(sLatin1.substring(i, j), bbLatin1, i, j);
test(sLatin1.substring(i, j), wbLatin1, i, j);
test(sLatin1.substring(i, j), lbLatin1, i, j);
}
}
for (int i = 0; i < 6; i++) {
for (int j = i + 1; j < 6; j++) {
test(sUnicode.substring(i, j), cUtf16, i, j);
test(sUnicode.substring(i, j), gUtf16, i, j);
test(sUnicode.substring(i, j), fUtf16, i, j);
test(sUnicode.substring(i, j), wUtf16, i, j);
test(sUnicode.substring(i, j), lUtf16, i, j);
test(sUnicode.substring(i, j), wvUtf16, i, j);
test(sUnicode.substring(i, j), lvUtf16, i, j);
test(sUnicode.substring(i, j), wbUtf16, i, j);
test(sUnicode.substring(i, j), lbUtf16, i, j);
}
}
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
int stringEnd = j < 4 ? j : j + 1;
test(sUnicode.substring(i, stringEnd), cCodepoints, i, j);
test(sUnicode.substring(i, stringEnd), gCodepoints, i, j);
test(sUnicode.substring(i, stringEnd), fCodepoints, i, j);
test(sUnicode.substring(i, stringEnd), lCodepoints, i, j);
test(sUnicode.substring(i, stringEnd), lvCodepoints, i, j);
test(sUnicode.substring(i, stringEnd), lbCodepoints, i, j);
}
}
}

View file

@ -0,0 +1,17 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
returnStringOrNull() {
return new DateTime.now().millisecondsSinceEpoch == 0 ? 'foo' : null;
}
main() {
Expect.throwsArgumentError(() => 'foo' + returnStringOrNull());
Expect.throws(() => 'foo'.split(returnStringOrNull()),
(e) => e is ArgumentError || e is NoSuchMethodError);
}

View file

@ -0,0 +1,111 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Dart test for testing String.allMatches.
import "package:expect/expect.dart";
String str = "this is a string with hello here and hello there";
main() {
testNoMatch();
testOneMatch();
testTwoMatches();
testEmptyPattern();
testEmptyString();
testEmptyPatternAndString();
testMatchAsPrefix();
testAllMatchesStart();
}
testNoMatch() {
// Also tests that RegExp groups don't work.
String helloPattern = "with (hello)";
Iterable<Match> matches = helloPattern.allMatches(str);
Expect.isFalse(matches.iterator.moveNext());
}
testOneMatch() {
String helloPattern = "with hello";
Iterable<Match> matches = helloPattern.allMatches(str);
var iterator = matches.iterator;
Expect.isTrue(iterator.moveNext());
Match match = iterator.current;
Expect.isFalse(iterator.moveNext());
Expect.equals(str.indexOf('with', 0), match.start);
Expect.equals(str.indexOf('with', 0) + helloPattern.length, match.end);
Expect.equals(helloPattern, match.pattern);
Expect.equals(str, match.input);
Expect.equals(helloPattern, match[0]);
Expect.equals(0, match.groupCount);
}
testTwoMatches() {
String helloPattern = "hello";
Iterable<Match> matches = helloPattern.allMatches(str);
int count = 0;
int start = 0;
for (var match in matches) {
count++;
Expect.equals(str.indexOf('hello', start), match.start);
Expect.equals(str.indexOf('hello', start) + helloPattern.length, match.end);
Expect.equals(helloPattern, match.pattern);
Expect.equals(str, match.input);
Expect.equals(helloPattern, match[0]);
Expect.equals(0, match.groupCount);
start = match.end;
}
Expect.equals(2, count);
}
testEmptyPattern() {
String pattern = "";
Iterable<Match> matches = pattern.allMatches(str);
Expect.isTrue(matches.iterator.moveNext());
}
testEmptyString() {
String pattern = "foo";
String str = "";
Iterable<Match> matches = pattern.allMatches(str);
Expect.isFalse(matches.iterator.moveNext());
}
testEmptyPatternAndString() {
String pattern = "";
String str = "";
Iterable<Match> matches = pattern.allMatches(str);
Expect.isTrue(matches.iterator.moveNext());
}
testMatchAsPrefix() {
String pattern = "an";
String str = "banana";
Expect.isNull(pattern.matchAsPrefix(str));
Expect.isNull(pattern.matchAsPrefix(str, 0));
var m = pattern.matchAsPrefix(str, 1)!;
Expect.equals("an", m[0]);
Expect.equals(1, m.start);
Expect.isNull(pattern.matchAsPrefix(str, 2));
m = pattern.matchAsPrefix(str, 3)!;
Expect.equals("an", m[0]);
Expect.equals(3, m.start);
Expect.isNull(pattern.matchAsPrefix(str, 4));
Expect.isNull(pattern.matchAsPrefix(str, 5));
Expect.isNull(pattern.matchAsPrefix(str, 6));
Expect.throws(() => pattern.matchAsPrefix(str, -1));
Expect.throws(() => pattern.matchAsPrefix(str, 7));
}
testAllMatchesStart() {
String p = "ass";
String s = "assassin";
Expect.equals(2, p.allMatches(s).length);
Expect.equals(2, p.allMatches(s, 0).length);
Expect.equals(1, p.allMatches(s, 1).length);
Expect.equals(0, p.allMatches(s, 4).length);
Expect.equals(0, p.allMatches(s, s.length).length);
Expect.throws(() => p.allMatches(s, -1));
Expect.throws(() => p.allMatches(s, s.length + 1));
}

View file

@ -0,0 +1,11 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
//
// dart2jsOptions=-Ddart2js.testing.String.replaceAll.force.regexp=true
import "string_replace_all_test.dart" as base;
main() {
base.main();
}

View file

@ -0,0 +1,140 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
testReplaceAll() {
Expect.equals("aXXcaXXdae", "abcabdae".replaceAll("b", "XX"));
// Test with the replaced string at the beginning.
Expect.equals("XXbcXXbdXXe", "abcabdae".replaceAll("a", "XX"));
// Test with the replaced string at the end.
Expect.equals("abcabdaXX", "abcabdae".replaceAll("e", "XX"));
// Test when there are no occurence of the string to replace.
Expect.equals("abcabdae", "abcabdae".replaceAll("f", "XX"));
// 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 the replaced string.
Expect.equals("to", "from".replaceAll("from", "to"));
// Test when matches are adjacent
Expect.equals("toto", "fromfrom".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("bcbde", "abcabdae".replaceAll("a", ""));
Expect.equals("AB", "AfromB".replaceAll("from", ""));
// Test changing the empty string.
Expect.equals("to", "".replaceAll("", "to"));
// Test replacing the empty string.
Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
// Pattern strings containing RegExp metacharacters - these are not
// interpreted as RegExps.
Expect.equals(r"$$", "||".replaceAll("|", r"$"));
Expect.equals(r"$$$$", "||".replaceAll("|", r"$$"));
Expect.equals(r"x$|x", "x|.|x".replaceAll("|.", r"$"));
Expect.equals(r"$$", "..".replaceAll(".", r"$"));
Expect.equals(r"[$$$$]", "[..]".replaceAll(".", r"$$"));
Expect.equals(r"[$]", "[..]".replaceAll("..", r"$"));
Expect.equals(r"$$", r"\\".replaceAll(r"\", r"$"));
}
testReplaceAllMapped() {
String mark(Match m) => "[${m[0]}]";
Expect.equals("a[b]ca[b]dae", "abcabdae".replaceAllMapped("b", mark));
// Test with the replaced string at the beginning.
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped("a", mark));
// Test with the replaced string at the end.
Expect.equals("abcabda[e]", "abcabdae".replaceAllMapped("e", mark));
// Test when there are no occurence of the string to replace.
Expect.equals("abcabdae", "abcabdae".replaceAllMapped("f", mark));
// Test when the string to change is the empty string.
Expect.equals("", "".replaceAllMapped("from", mark));
// Test when the string to change is a substring of the string to
// replace.
Expect.equals("fro", "fro".replaceAllMapped("from", mark));
// Test when matches are adjacent
Expect.equals("[from][from]", "fromfrom".replaceAllMapped("from", mark));
// Test replacing by the empty string.
Expect.equals("bcbde", "abcabdae".replaceAllMapped("a", (m) => ""));
Expect.equals("AB", "AfromB".replaceAllMapped("from", (m) => ""));
// Test changing the empty string.
Expect.equals("[]", "".replaceAllMapped("", mark));
// Test replacing the empty string.
Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped("", mark));
}
testSplitMapJoin() {
String mark(Match m) => "[${m[0]}]";
String wrap(String s) => "<${s}>";
Expect.equals("<a>[b]<ca>[b]<dae>",
"abcabdae".splitMapJoin("b", onMatch: mark, onNonMatch: wrap));
// Test with the replaced string at the beginning.
Expect.equals("<>[a]<bc>[a]<bd>[a]<e>",
"abcabdae".splitMapJoin("a", onMatch: mark, onNonMatch: wrap));
// Test with the replaced string at the end.
Expect.equals("<abcabda>[e]<>",
"abcabdae".splitMapJoin("e", onMatch: mark, onNonMatch: wrap));
// Test when there are no occurence of the string to replace.
Expect.equals("<abcabdae>",
"abcabdae".splitMapJoin("f", onMatch: mark, onNonMatch: wrap));
// Test when the string to change is the empty string.
Expect.equals("<>", "".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
// Test when the string to change is a substring of the string to
// replace.
Expect.equals(
"<fro>", "fro".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
// Test when matches are adjacent
Expect.equals("<>[from]<>[from]<>",
"fromfrom".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
// Test changing the empty string.
Expect.equals("<>[]<>", "".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
// Test replacing the empty string.
Expect.equals("<>[]<A>[]<B>[]<C>[]<>",
"ABC".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
// Test with only onMatch.
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".splitMapJoin("a", onMatch: mark));
// Test with only onNonMatch
Expect.equals(
"<>a<bc>a<bd>a<e>", "abcabdae".splitMapJoin("a", onNonMatch: wrap));
}
main() {
testReplaceAll();
testReplaceAllMapped();
testSplitMapJoin();
}

View file

@ -0,0 +1,27 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
String jsText = r"""'$'
""";
String htmlStr = '%%DART';
String htmlOut = htmlStr.replaceAll("%%DART", jsText);
Expect.equals(jsText, htmlOut);
htmlOut = htmlStr.replaceFirst("%%DART", jsText);
Expect.equals(jsText, htmlOut);
htmlOut = htmlStr.replaceAll(new RegExp("%%DART"), jsText);
Expect.equals(jsText, htmlOut);
htmlOut = htmlStr.replaceFirst(new RegExp("%%DART"), jsText);
Expect.equals(jsText, htmlOut);
// Regression test, http://dartbug.com/17886
String doubleDollar = r"$'$`";
var string = r"flip-flip-flop";
var result = string.replaceFirst("flip", doubleDollar);
Expect.equals(r"$'$`-flip-flop", result);
result = string.replaceAll("flip", doubleDollar);
Expect.equals(r"$'$`-$'$`-flop", result);
}

View file

@ -0,0 +1,26 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void main() {
// Test object startIndex
"hello".replaceFirst("h", "X", new Object()); /*@compile-error=unspecified*/
// Test object startIndex
"hello".replaceFirstMapped(
"h", (_) => "X", new Object()); /*@compile-error=unspecified*/
"foo-bar".replaceFirstMapped("bar", (v) {
return 42;
}); /*@compile-error=unspecified*/
"hello".replaceRange(0, 0, 42); /*@compile-error=unspecified*/
"hello".replaceRange(0, 0, ["x"]); /*@compile-error=unspecified*/
}
// Fails to return a String on toString, throws if converted by "$naughty".
class Naughty {
toString() => this; /*@compile-error=unspecified*/
}

View file

@ -0,0 +1,191 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
// Test replaceFirst.
Expect.equals("AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to"));
// Test with the replaced string at the beginning.
Expect.equals("toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to"));
// Test with the replaced string at the end.
Expect.equals("toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to"));
// Test when there are no occurence of the string to replace.
Expect.equals("ABC", "ABC".replaceFirst("from", "to"));
// Test when the string to change is the empty string.
Expect.equals("", "".replaceFirst("from", "to"));
// Test when the string to change is a substring of the string to
// replace.
Expect.equals("fro", "fro".replaceFirst("from", "to"));
// Test when the string to change is the replaced string.
Expect.equals("to", "from".replaceFirst("from", "to"));
// Test when the string to change is the replacement string.
Expect.equals("to", "to".replaceFirst("from", "to"));
// Test replacing by the empty string.
Expect.equals("", "from".replaceFirst("from", ""));
Expect.equals("AB", "AfromB".replaceFirst("from", ""));
// Test changing the empty string.
Expect.equals("to", "".replaceFirst("", "to"));
// Test replacing the empty string.
Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to"));
// Test startIndex.
Expect.equals(
"foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4));
// Test startIndex skipping one case at the beginning.
Expect.equals(
"foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1));
// Test startIndex skipping one case at the beginning.
Expect.equals(
"foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5));
// Test startIndex replacing with the empty string.
Expect.equals("foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1));
// Test startIndex with a RegExp with carat
Expect.equals("foo-bar-foo-bar",
"foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8));
// Test startIndex with a RegExp
Expect.equals(
"aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1));
// Test startIndex with regexp-looking String
Expect.equals("aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3));
// Test negative startIndex
Expect.throwsRangeError(() => "hello".replaceFirst("h", "X", -1));
// Test startIndex too large
Expect.throwsRangeError(() => "hello".replaceFirst("h", "X", 6));
// Test replaceFirstMapped.
Expect.equals(
"AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to"));
// Test with the replaced string at the beginning.
Expect.equals(
"toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to"));
// Test with the replaced string at the end.
Expect.equals("toABtoCDtoEto",
"fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to"));
// Test when there are no occurence of the string to replace.
Expect.equals("ABC", "ABC".replaceFirstMapped("from", (_) => "to"));
// Test when the string to change is the empty string.
Expect.equals("", "".replaceFirstMapped("from", (_) => "to"));
// Test when the string to change is a substring of the string to
// replace.
Expect.equals("fro", "fro".replaceFirstMapped("from", (_) => "to"));
// Test when the string to change is the replaced string.
Expect.equals("to", "from".replaceFirstMapped("from", (_) => "to"));
// Test when the string to change is the replacement string.
Expect.equals("to", "to".replaceFirstMapped("from", (_) => "to"));
// Test replacing by the empty string.
Expect.equals("", "from".replaceFirstMapped("from", (_) => ""));
Expect.equals("AB", "AfromB".replaceFirstMapped("from", (_) => ""));
// Test changing the empty string.
Expect.equals("to", "".replaceFirstMapped("", (_) => "to"));
// Test replacing the empty string.
Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirstMapped("", (_) => "to"));
// Test startIndex.
Expect.equals("foo-AAA-foo-bar",
"foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 4));
// Test startIndex skipping one case at the beginning.
Expect.equals("foo-bar-AAA-bar",
"foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "AAA", 1));
// Test startIndex skipping one case at the beginning.
Expect.equals("foo-bar-foo-AAA",
"foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 5));
// Test startIndex replacing with the empty string.
Expect.equals("foo-bar--bar",
"foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "", 1));
// Test startIndex with a RegExp with carat
Expect.equals("foo-bar-foo-bar",
"foo-bar-foo-bar".replaceFirstMapped(new RegExp(r"^foo"), (_) => "", 8));
// Test startIndex with a RegExp
Expect.equals("aaa{3}X{3}",
"aaa{3}aaa{3}".replaceFirstMapped(new RegExp(r"a{3}"), (_) => "X", 1));
// Test startIndex with regexp-looking String
Expect.equals(
"aaa{3}aaX", "aaa{3}aaa{3}".replaceFirstMapped("a{3}", (_) => "X", 3));
// Test negative startIndex
Expect.throwsRangeError(
() => "hello".replaceFirstMapped("h", (_) => "X", -1));
// Test startIndex too large
Expect.throwsRangeError(() => "hello".replaceFirstMapped("h", (_) => "X", 6));
// Test replacement depending on argument.
Expect.equals("foo-BAR-foo-bar",
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0]!.toUpperCase()));
Expect.equals("foo-[bar]-foo-bar",
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]"));
Expect.equals("foo-foo-bar-foo-bar-foo-bar",
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input));
// Test replacement throwing.
Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42),
(e) => e == 42);
// Test replacement returning non-String.
var o = new Object();
Expect.equals(
"foo-$o",
"foo-bar".replaceFirstMapped("bar", (v) {
// TODO(jmesserly): in strong mode, this function must return a string.
// If we want to allow any Object, we'll have to fix the API signature.
// See https://github.com/dart-lang/sdk/issues/30248.
return '$o';
}));
for (var string in ["", "x", "foo", "x\u2000z"]) {
for (var replacement in ["", "foo", string]) {
for (int start = 0; start <= string.length; start++) {
var expect;
for (int end = start; end <= string.length; end++) {
expect =
string.substring(0, start) + replacement + string.substring(end);
Expect.equals(expect, string.replaceRange(start, end, replacement),
'"$string"[$start:$end]="$replacement"');
}
// Reuse expect from "end == string.length" case when omitting end.
Expect.equals(expect, string.replaceRange(start, null, replacement),
'"$string"[$start:]="$replacement"');
}
}
Expect.throws(() => string.replaceRange(-1, 0, "x"));
Expect.throws(() => string.replaceRange(0, string.length + 1, "x"));
}
}

View file

@ -0,0 +1,84 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
test(String s, List<int> expectedRunes) {
Runes runes = s.runes;
Expect.identical(s, runes.string);
// for-in
var res = [];
for (int rune in runes) {
res.add(rune);
}
Expect.listEquals(expectedRunes, res);
// manual iteration, backwards.
res = [];
for (var it = runes.iterator..reset(s.length); it.movePrevious();) {
res.add(it.current);
}
Expect.listEquals(expectedRunes.reversed.toList(), res);
// Setting rawIndex.
RuneIterator it = runes.iterator;
it.rawIndex = 1;
Expect.equals(expectedRunes[1], it.current);
it = runes.iterator;
it.moveNext();
Expect.equals(0, it.rawIndex);
it.moveNext();
Expect.equals(1, it.rawIndex);
it.moveNext();
Expect.isTrue(1 < it.rawIndex);
it.rawIndex = 1;
Expect.equals(1, it.rawIndex);
Expect.equals(expectedRunes[1], it.current);
// Reset, moveNext.
it.reset(1);
Expect.equals(null, it.rawIndex);
Expect.equals(null, it.current);
it.moveNext();
Expect.equals(1, it.rawIndex);
Expect.equals(expectedRunes[1], it.current);
// Reset, movePrevious.
it.reset(1);
Expect.equals(null, it.rawIndex);
Expect.equals(null, it.current);
it.movePrevious();
Expect.equals(0, it.rawIndex);
Expect.equals(expectedRunes[0], it.current);
// .map
Expect.listEquals(expectedRunes.map((x) => x.toRadixString(16)).toList(),
runes.map((x) => x.toRadixString(16)).toList());
}
// First character must be single-code-unit for test.
test("abc", [0x61, 0x62, 0x63]);
test("\x00\u0000\u{000000}", [0, 0, 0]);
test("\u{ffff}\u{10000}\u{10ffff}", [0xffff, 0x10000, 0x10ffff]);
String string = new String.fromCharCodes(
[0xdc00, 0xd800, 61, 0xd800, 0xdc00, 62, 0xdc00, 0xd800]);
test(string, [0xdc00, 0xd800, 61, 0x10000, 62, 0xdc00, 0xd800]);
// Setting position in the middle of a surrogate pair is not allowed.
var r = new Runes("\u{10000}");
var it = r.iterator;
it.moveNext();
Expect.equals(0x10000, it.current);
// Setting rawIndex inside surrogate pair.
Expect.throws(() {
it.rawIndex = 1;
}, (e) => e is Error);
Expect.throws(() {
it.reset(1);
}, (e) => e is Error);
}

View file

@ -0,0 +1,35 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Test that different representations of the same string are all equal.
import "dart:convert";
import "package:expect/expect.dart";
main() {
var base = "\u{10412}";
var strings = [
"\u{10412}",
"𐐒",
new String.fromCharCodes([0xd801, 0xdc12]),
base[0] + base[1],
"$base",
"${base[0]}${base[1]}",
"${base[0]}${base.substring(1)}",
new String.fromCharCodes([0x10412]),
("a" + base).substring(1),
(new StringBuffer()..writeCharCode(0xd801)..writeCharCode(0xdc12))
.toString(),
(new StringBuffer()..writeCharCode(0x10412)).toString(),
json.decode('"\u{10412}"'),
(json.decode('{"\u{10412}":[]}') as Map).keys.first
];
for (String string in strings) {
Expect.equals(base.length, string.length);
Expect.equals(base, string);
Expect.equals(base.hashCode, string.hashCode);
Expect.listEquals(base.codeUnits.toList(), string.codeUnits.toList());
}
}

View file

@ -0,0 +1,144 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
testSplitString();
testSplitRegExp();
testSplitPattern();
}
testSplit(List<String> expect, String string, Pattern pattern) {
String patternString;
if (pattern is String) {
patternString = '"$pattern"';
} else if (pattern is RegExp) {
patternString = "/${pattern.pattern}/";
} else {
patternString = pattern.toString();
}
List actual = string.split(pattern);
// Ensure that the correct type is reified.
actual = actual as List<String>;
// Check that store of the wrong type throws. Some platforms don't do this,
// so it's protected by multitest syntax.
Expect.throwsTypeError(() => actual.add(42), // //# checkedstore: ok
'List<String>.add should not accept an int'); // //# checkedstore: ok
Expect.listEquals(expect, actual, '"$string".split($patternString)');
}
/** String patterns. */
void testSplitString() {
// Normal match.
testSplit(["a", "b", "c"], "a b c", " ");
testSplit(["a", "b", "c"], "adbdc", "d");
testSplit(["a", "b", "c"], "addbddc", "dd");
// No match.
testSplit(["abc"], "abc", " ");
testSplit(["a"], "a", "b");
testSplit([""], "", "b");
// Empty match matches everywhere except start/end.
testSplit(["a", "b", "c"], "abc", "");
// All empty parts.
testSplit(["", "", "", "", ""], "aaaa", "a");
testSplit(["", "", "", "", ""], " ", " ");
testSplit(["", ""], "a", "a");
// No overlapping matches. Match as early as possible.
testSplit(["", "", "", "a"], "aaaaaaa", "aa");
// Cannot split the empty string.
testSplit([], "", ""); // Match.
testSplit([""], "", "a"); // No match.
}
/** RegExp patterns. */
void testSplitRegExp() {
testSplitWithRegExp((s) => new RegExp(s));
}
/** Non-String, non-RegExp patterns. */
void testSplitPattern() {
testSplitWithRegExp((s) => new RegExpWrap(s));
}
void testSplitWithRegExp(makePattern) {
testSplit(["a", "b", "c"], "a b c", makePattern(r" "));
testSplit(["a", "b", "c"], "adbdc", makePattern(r"[dz]"));
testSplit(["a", "b", "c"], "addbddc", makePattern(r"dd"));
testSplit(["abc"], "abc", makePattern(r"b$"));
testSplit(["a", "b", "c"], "abc", makePattern(r""));
testSplit(["", "", "", ""], " ", makePattern(r"[ ]"));
// Non-zero-length match at end.
testSplit(["aa", ""], "aaa", makePattern(r"a$"));
// Zero-length match at end.
testSplit(["aaa"], "aaa", makePattern(r"$"));
// Non-zero-length match at start.
testSplit(["", "aa"], "aaa", makePattern(r"^a"));
// Zero-length match at start.
testSplit(["aaa"], "aaa", makePattern(r"^"));
// Picks first match, not longest or shortest.
testSplit(["", "", "", "a"], "aaaaaaa", makePattern(r"aa|aaa"));
testSplit(["", "", "", "a"], "aaaaaaa", makePattern(r"aa|"));
testSplit(["", "", "a"], "aaaaaaa", makePattern(r"aaa|aa"));
// Zero-width match depending on the following.
testSplit(["a", "bc"], "abc", makePattern(r"(?=[ab])"));
testSplit(["a", "b", "c"], "abc", makePattern(r"(?!^)"));
// Cannot split empty string.
testSplit([], "", makePattern(r""));
testSplit([], "", makePattern(r"(?:)"));
testSplit([], "", makePattern(r"$|(?=.)"));
testSplit([""], "", makePattern(r"a"));
// Can split singleton string if it matches.
testSplit(["", ""], "a", makePattern(r"a"));
testSplit(["a"], "a", makePattern(r"b"));
// Do not include captures.
testSplit(["a", "", "a"], "abba", makePattern(r"(b)"));
testSplit(["a", "a"], "abba", makePattern(r"(bb)"));
testSplit(["a", "a"], "abba", makePattern(r"(b*)"));
testSplit(["a", "a"], "aa", makePattern(r"(b*)"));
// But captures are still there, and do work with backreferences.
testSplit(["a", "cba"], "abcba", makePattern(r"([bc])(?=.*\1)"));
}
// A Pattern implementation with the same capabilities as a RegExp, but not
// directly recognizable as a RegExp.
class RegExpWrap implements Pattern {
final regexp;
RegExpWrap(String source) : regexp = new RegExp(source);
Iterable<Match> allMatches(String string, [int start = 0]) =>
regexp.allMatches(string, start);
Match matchAsPrefix(String string, [int start = 0]) =>
regexp.matchAsPrefix(string, start);
String toString() => "Wrap(/${regexp.pattern}/)";
}

View file

@ -0,0 +1,14 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void main() {
testIllegalArgument();
}
void testIllegalArgument() {
String a = "Hello";
var c = a[2.2]; /*@compile-error=unspecified*/
}

View file

@ -0,0 +1,31 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
// Test that not providing an optional argument goes to the end.
Expect.equals("".substring(0), "");
Expect.throwsRangeError(() => "".substring(1));
Expect.throwsRangeError(() => "".substring(-1));
Expect.equals("abc".substring(0), "abc");
Expect.equals("abc".substring(1), "bc");
Expect.equals("abc".substring(2), "c");
Expect.equals("abc".substring(3), "");
Expect.throwsRangeError(() => "abc".substring(4));
Expect.throwsRangeError(() => "abc".substring(-1));
// Test that providing null goes to the end.
Expect.equals("".substring(0, null), "");
Expect.throwsRangeError(() => "".substring(1, null));
Expect.throwsRangeError(() => "".substring(-1, null));
Expect.equals("abc".substring(0, null), "abc");
Expect.equals("abc".substring(1, null), "bc");
Expect.equals("abc".substring(2, null), "c");
Expect.equals("abc".substring(3, null), "");
Expect.throwsRangeError(() => "abc".substring(4, null));
Expect.throwsRangeError(() => "abc".substring(-1, null));
}

View file

@ -0,0 +1,478 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void main() {
testOutOfRange();
testConcat();
testIndex();
testCodeUnitAt();
testEquals();
testEndsWith();
testStartsWith();
testIndexOf();
testLastIndexOf();
testContains();
testReplaceAll();
testCompareTo();
testCharCodes();
testRepeat();
testPadLeft();
testPadRight();
}
void testLength() {
String str = "";
for (var i = 0; i < 20; i++) {
testStringLength(i, str);
str += " ";
}
}
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 testIndex() {
String str = "string";
for (int i = 0; i < str.length; i++) {
Expect.isTrue(str[i] is String);
testStringLength(1, str[i]);
}
}
void testCodeUnitAt() {
String str = "string";
for (int i = 0; i < str.length; i++) {
Expect.isTrue(str.codeUnitAt(i) is int);
}
}
void testConcat() {
var a = "One";
var b = "Four";
var c = a + b;
testStringLength(7, c);
Expect.equals("OneFour", c);
}
void testEquals() {
Expect.equals("str", "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("s" == "str");
Expect.isFalse("r" == "str");
Expect.isFalse("st" == "str");
Expect.isFalse("tr" == "str");
Expect.isFalse("" == "s");
Expect.equals("", "");
}
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.isTrue("".endsWith(""));
Expect.isFalse("".endsWith("s"));
}
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 ^.
}
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));
}
}
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));
}
}
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.isTrue("str".contains("", 0));
Expect.isTrue("".contains("", 0));
Expect.isFalse("".contains("s", 0));
}
void testReplaceAll() {
Expect.equals("AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to"));
// Test with the replaced string at the beginning.
Expect.equals("toABtoCDtoE", "fromABfromCDfromE".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 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 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 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 replacing the empty string.
Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
}
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"));
}
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(" ");
}
void testStringLength(int length, String str) {
Expect.equals(length, str.length);
(length == 0 ? Expect.isTrue : Expect.isFalse)(str.isEmpty);
(length != 0 ? Expect.isTrue : Expect.isFalse)(str.isNotEmpty);
}
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,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
127,
128,
129
];
void testRepeat(str, repeat) {
String expect;
if (repeat <= 0) {
expect = "";
} else if (repeat == 1) {
expect = str;
} else {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < repeat; i++) {
buf.write(str);
}
expect = buf.toString();
}
String actual = str * repeat;
Expect.equals(expect, actual, "$str#${str.length} * $repeat");
}
for (String str in testStrings) {
for (int repeat in counts) {
testRepeat(str, repeat);
}
}
}
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('', ''.padLeft(0, 'a'));
Expect.equals('a', ''.padLeft(1, 'a'));
Expect.equals('aaaaa', ''.padLeft(5, 'a'));
Expect.equals('', ''.padLeft(-2, 'a'));
Expect.equals('xyzxyzxyzxyzxyz', ''.padLeft(5, 'xyz'));
Expect.equals('xyzxyzxyzxyza', 'a'.padLeft(5, 'xyz'));
Expect.equals('xyzxyzxyzaa', 'aa'.padLeft(5, 'xyz'));
Expect.equals('\u{10002}\u{10002}\u{10002}aa', 'aa'.padLeft(5, '\u{10002}'));
Expect.equals('a', 'a'.padLeft(10, ''));
}
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('', ''.padRight(0, 'a'));
Expect.equals('a', ''.padRight(1, 'a'));
Expect.equals('aaaaa', ''.padRight(5, 'a'));
Expect.equals('', ''.padRight(-2, 'a'));
Expect.equals('xyzxyzxyzxyzxyz', ''.padRight(5, 'xyz'));
Expect.equals('axyzxyzxyzxyz', 'a'.padRight(5, 'xyz'));
Expect.equals('aaxyzxyzxyz', 'aa'.padRight(5, 'xyz'));
Expect.equals('aa\u{10002}\u{10002}\u{10002}', 'aa'.padRight(5, '\u{10002}'));
Expect.equals('a', 'a'.padRight(10, ''));
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void testOneByteSting() {
// Compare one-byte-string toLowerCase with a two-byte-string toLowerCase.
var oneByteString =
new String.fromCharCodes(new List.generate(256, (i) => i)).toLowerCase();
var twoByteString =
new String.fromCharCodes(new List.generate(512, (i) => i)).toLowerCase();
Expect.isTrue(twoByteString.codeUnits.any((u) => u >= 256));
Expect.equals(oneByteString, twoByteString.substring(0, 256));
}
void main() {
testOneByteSting();
}

View file

@ -0,0 +1,48 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
const WHITESPACE = const [
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x20,
0x85,
0xA0,
0x1680,
0x2000,
0x2001,
0x2002,
0x2003,
0x2004,
0x2005,
0x2006,
0x2007,
0x2008,
0x2009,
0x200A,
0x202F,
0x205F,
0x3000,
0x2028,
0x2029,
0xFEFF,
];
main() {
for (var ws in WHITESPACE) {
var name = ws.toRadixString(16);
var c = new String.fromCharCode(ws);
Expect.equals("", c.trim(), "$name");
Expect.equals("a", ("a" + c).trim(), "a-$name");
Expect.equals("a", (c + "a").trim(), "$name-a");
Expect.equals("a", (c + c + "a" + c + c).trim(), "$name around");
Expect.equals(
"a" + c + "a", (c + c + "a" + c + "a" + c + c).trim(), "$name many");
}
Expect.equals("", new String.fromCharCodes(WHITESPACE).trim(), "ALL");
}

View file

@ -0,0 +1,25 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class StringTrimTest {
static testMain() {
Expect.equals("", " ".trim());
Expect.equals("", " ".trim());
var a = " lots of space on the left";
Expect.equals("lots of space on the left", a.trim());
a = "lots of space on the right ";
Expect.equals("lots of space on the right", a.trim());
a = " lots of space ";
Expect.equals("lots of space", a.trim());
a = " x ";
Expect.equals("x", a.trim());
Expect.equals("", " \t \n \r ".trim());
}
}
main() {
StringTrimTest.testMain();
}

View file

@ -0,0 +1,110 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Characters with Whitespace property (Unicode 6.3).
// 0009..000D ; White_Space # Cc <control-0009>..<control-000D>
// 0020 ; White_Space # Zs SPACE
// 0085 ; White_Space # Cc <control-0085>
// 00A0 ; White_Space # Zs NO-BREAK SPACE
// 1680 ; White_Space # Zs OGHAM SPACE MARK
// 2000..200A ; White_Space # Zs EN QUAD..HAIR SPACE
// 2028 ; White_Space # Zl LINE SEPARATOR
// 2029 ; White_Space # Zp PARAGRAPH SEPARATOR
// 202F ; White_Space # Zs NARROW NO-BREAK SPACE
// 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE
// 3000 ; White_Space # Zs IDEOGRAPHIC SPACE
// And BOM:
// FEFF ; Byte order mark.
const WHITESPACE = const [
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x20,
0x85,
0xA0,
0x1680,
0x2000,
0x2001,
0x2002,
0x2003,
0x2004,
0x2005,
0x2006,
0x2007,
0x2008,
0x2009,
0x200A,
0x2028,
0x2029,
0x202F,
0x205F,
0x3000,
0xFEFF,
];
main() {
// Test the whitespace in different positions.
test(ws) {
// trimLeft
Expect.equals("", ws.trimLeft(), "K1");
Expect.equals("", (ws + ws).trimLeft(), "L2");
Expect.equals("a" + ws, ("a" + ws).trimLeft(), "L3");
Expect.equals("a", (ws + "a").trimLeft(), "L4");
Expect.equals("a" + ws + ws, (ws + ws + "a" + ws + ws).trimLeft(), "L5");
Expect.equals("a" + ws + "a", (ws + ws + "a" + ws + "a").trimLeft(), "L6");
var untrimmable = "a" + ws + "a";
Expect.identical(untrimmable, untrimmable.trimLeft(), "L7");
// trimRight
Expect.equals("", ws.trimRight(), "R1");
Expect.equals("", (ws + ws).trimRight(), "R2");
Expect.equals("a", ("a" + ws).trimRight(), "R3");
Expect.equals(ws + "a", (ws + "a").trimRight(), "R4");
Expect.equals(ws + ws + "a", (ws + ws + "a" + ws + ws).trimRight(), "R5");
Expect.equals("a" + ws + "a", ("a" + ws + "a" + ws + ws).trimRight(), "R6");
Expect.identical(untrimmable, untrimmable.trimRight(), "R7");
}
// Test each whitespace at different locations.
for (var ws in WHITESPACE) {
var c = new String.fromCharCode(ws);
test(c);
}
// Test all whitespaces at once at different locations.
test(new String.fromCharCodes(WHITESPACE));
// Empty strings.
Expect.identical("", "".trimLeft());
Expect.identical("", "".trimRight());
// Test all BMP chars and one surrogate pair.
for (int i = 0, j = 0; i <= 0x10000; i++) {
if (j < WHITESPACE.length && i == WHITESPACE[j]) {
j++;
continue;
}
// See below for these exceptions.
if (i == 0x180E) continue;
if (i == 0x200B) continue;
var s = new String.fromCharCode(i);
Expect.identical(s, s.trimLeft());
Expect.identical(s, s.trimRight());
}
// U+200b is not treated as whitespace, though historically, some JS engines
// did.
var s200B = new String.fromCharCode(0x200B);
Expect.identical(s200B, s200B.trimLeft());
Expect.identical(s200B, s200B.trimRight());
// U+180E ceased to be whitespace in Unicode version 6.3.0
// string_trimlr_test/unicode63 fails on implementations using earlier versions.
var s180E = new String.fromCharCode(0x180E);
Expect.identical(s180E, s180E.trimLeft()); // //# unicode63: ok
Expect.identical(s180E, s180E.trimRight()); // //# unicode63: ok
}

View file

@ -0,0 +1,33 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Dart test program for testing class 'Strings'.
class StringsTest {
StringsTest() {}
toString() {
return "Strings Tester";
}
static testCreation() {
String s = "Hello";
List<int> l = new List.filled(s.length, -1);
for (int i = 0; i < l.length; i++) {
l[i] = s.codeUnitAt(i);
}
String s2 = new String.fromCharCodes(l);
Expect.equals(s, s2);
}
static void testMain() {
testCreation();
}
}
main() {
StringsTest.testMain();
}

View file

@ -0,0 +1,15 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library dart.test.symbol_map_helper;
// TODO(ahe): Update map literals to avoid this method.
Map<Symbol, dynamic> symbolMapToStringMap(Map<String, dynamic> map) {
if (map == null) return null;
Map<Symbol, dynamic> result = new Map<Symbol, dynamic>();
map.forEach((name, value) {
result[new Symbol(name)] = value;
});
return result;
}

View file

@ -0,0 +1,81 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Test of Symbol class for operators..
dynamic $ = new Symbolize();
main() {
testSymbol(#+, $ + $, "+");
testSymbol(#-, $ - $, "-");
testSymbol(#*, $ * $, "*");
testSymbol(#/, $ / $, "/");
testSymbol(#~/, $ ~/ $, "~/");
testSymbol(#%, $ % $, "%");
testSymbol(#<<, $ << $, "<<");
testSymbol(#>>, $ >> $, ">>");
testSymbol(#~, ~$, "~");
testSymbol(#|, $ | $, "|");
testSymbol(#&, $ & $, "&");
testSymbol(#^, $ ^ $, "^");
testSymbol(#<, $ < $, "<");
testSymbol(#<=, $ <= $, "<=");
testSymbol(#>, $ > $, ">");
testSymbol(#>=, $ >= $, ">=");
testSymbol(#==, new Symbol("=="), "=="); // Can't hit noSuchMethod.
testSymbol(#[], $[$], "[]");
testSymbol(#[]=, ($[$] = $).lastMember, "[]=");
testSymbol(Symbol.unaryMinus, -$, "unary-");
testSymbolThrows(">>>"); // //# 03: ok
testSymbolThrows("!"); // //# 03: continued
testSymbolThrows("&&"); // //# 03: continued
testSymbolThrows("||"); // //# 03: continued
testSymbolThrows("?"); // //# 03: continued
testSymbolThrows("?:"); // //# 03: continued
testSymbolThrows("#"); // //# 03: continued
testSymbolThrows("//"); // //# 03: continued
}
void testSymbol(Symbol constSymbol, var mirrorSymbol, String name) {
Symbol dynamicSymbol = new Symbol(name);
if (constSymbol != mirrorSymbol) {
throw "Not equal #$name, \$$name: $constSymbol, $mirrorSymbol";
}
if (constSymbol != dynamicSymbol) {
throw "Not equal #$name, new Symbol('$name'): $constSymbol, $dynamicSymbol";
}
if (mirrorSymbol != dynamicSymbol) {
throw "Not equal \$$name, new Symbol('$name'): "
"$mirrorSymbol, $dynamicSymbol";
}
if (constSymbol.hashCode != mirrorSymbol.hashCode) {
throw "HashCode not equal #$name, \$$name: $constSymbol, $mirrorSymbol";
}
if (constSymbol.hashCode != dynamicSymbol.hashCode) {
throw "HashCode not equal #$name, new Symbol('$name'): "
"$constSymbol, $dynamicSymbol";
}
if (mirrorSymbol.hashCode != dynamicSymbol.hashCode) {
throw "HashCode not equal \$$name, new Symbol('$name'): "
"$mirrorSymbol, $dynamicSymbol";
}
}
void testSymbolThrows(name) {
bool fails = false;
try {
new Symbol(name);
} catch (e) {
fails = true;
}
if (!fails) {
throw "Didn't throw: $name";
}
}
class Symbolize {
Symbol lastMember;
noSuchMethod(m) => lastMember = m.memberName;
}

View file

@ -0,0 +1,124 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void checkBadSymbol(String s) {
Expect.throwsArgumentError(() => new Symbol(s));
}
main() {
var x;
// 'void' is allowed as a symbol name.
x = const Symbol('void'); // //# 01: ok
x = #void; // //# 02: ok
x = new Symbol('void'); // //# 03: ok
// However, it is not allowed as a part of a symbol name.
x = const Symbol('void.foo'); // //# 04: compile-time error
x = #void.foo; // //# 05: compile-time error
checkBadSymbol('void.foo'); // //# 06: ok
x = const Symbol('foo.void'); // //# 07: compile-time error
x = #foo.void; // //# 08: compile-time error
checkBadSymbol('foo.void'); // //# 09: ok
// All other reserved words are disallowed.
x = const Symbol('assert'); // //# 10: compile-time error
x = const Symbol('break'); // //# 10: continued
x = const Symbol('case'); // //# 10: continued
x = const Symbol('catch'); // //# 10: continued
x = const Symbol('class'); // //# 10: continued
x = const Symbol('const'); // //# 10: continued
x = const Symbol('continue'); // //# 10: continued
x = const Symbol('default'); // //# 10: continued
x = const Symbol('do'); // //# 10: continued
x = const Symbol('else'); // //# 10: continued
x = const Symbol('enum'); // //# 10: continued
x = const Symbol('extends'); // //# 10: continued
x = #assert; // //# 11: compile-time error
x = const Symbol('false'); // //# 10: continued
x = const Symbol('final'); // //# 10: continued
x = const Symbol('finally'); // //# 10: continued
x = const Symbol('for'); // //# 10: continued
x = const Symbol('if'); // //# 10: continued
x = const Symbol('in'); // //# 10: continued
x = const Symbol('is'); // //# 10: continued
x = const Symbol('new'); // //# 10: continued
x = const Symbol('null'); // //# 10: continued
x = const Symbol('rethrow'); // //# 10: continued
x = const Symbol('return'); // //# 10: continued
x = const Symbol('super'); // //# 10: continued
x = const Symbol('switch'); // //# 10: continued
x = const Symbol('this'); // //# 10: continued
x = const Symbol('throw'); // //# 10: continued
x = const Symbol('true'); // //# 10: continued
x = const Symbol('try'); // //# 10: continued
x = const Symbol('var'); // //# 10: continued
x = const Symbol('while'); // //# 10: continued
x = const Symbol('with'); // //# 10: continued
x = #break; // //# 11: continued
x = #case; // //# 11: continued
x = #catch; // //# 11: continued
x = #class; // //# 11: continued
x = #const; // //# 11: continued
x = #continue; // //# 11: continued
x = #default; // //# 11: continued
x = #do; // //# 11: continued
x = #else; // //# 11: continued
x = #enum; // //# 11: continued
x = #extends; // //# 11: continued
x = #false; // //# 11: continued
x = #final; // //# 11: continued
x = #finally; // //# 11: continued
x = #for; // //# 11: continued
x = #if; // //# 11: continued
x = #in; // //# 11: continued
x = #is; // //# 11: continued
x = #new; // //# 11: continued
x = #null; // //# 11: continued
x = #rethrow; // //# 11: continued
x = #return; // //# 11: continued
x = #super; // //# 11: continued
x = #switch; // //# 11: continued
x = #this; // //# 11: continued
x = #throw; // //# 11: continued
x = #true; // //# 11: continued
x = #try; // //# 11: continued
x = #var; // //# 11: continued
x = #while; // //# 11: continued
x = #with; // //# 11: continued
checkBadSymbol('assert'); // //# 12: ok
checkBadSymbol('break'); // //# 12: continued
checkBadSymbol('case'); // //# 12: continued
checkBadSymbol('catch'); // //# 12: continued
checkBadSymbol('class'); // //# 12: continued
checkBadSymbol('const'); // //# 12: continued
checkBadSymbol('continue'); // //# 12: continued
checkBadSymbol('default'); // //# 12: continued
checkBadSymbol('do'); // //# 12: continued
checkBadSymbol('else'); // //# 12: continued
checkBadSymbol('enum'); // //# 12: continued
checkBadSymbol('extends'); // //# 12: continued
checkBadSymbol('false'); // //# 12: continued
checkBadSymbol('final'); // //# 12: continued
checkBadSymbol('finally'); // //# 12: continued
checkBadSymbol('for'); // //# 12: continued
checkBadSymbol('if'); // //# 12: continued
checkBadSymbol('in'); // //# 12: continued
checkBadSymbol('is'); // //# 12: continued
checkBadSymbol('new'); // //# 12: continued
checkBadSymbol('null'); // //# 12: continued
checkBadSymbol('rethrow'); // //# 12: continued
checkBadSymbol('return'); // //# 12: continued
checkBadSymbol('super'); // //# 12: continued
checkBadSymbol('switch'); // //# 12: continued
checkBadSymbol('this'); // //# 12: continued
checkBadSymbol('throw'); // //# 12: continued
checkBadSymbol('true'); // //# 12: continued
checkBadSymbol('try'); // //# 12: continued
checkBadSymbol('var'); // //# 12: continued
checkBadSymbol('while'); // //# 12: continued
checkBadSymbol('with'); // //# 12: continued
}

View file

@ -0,0 +1,79 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Basic test of Symbol class.
main() {
var x;
print(x = const Symbol('fisk'));
try {
print(const Symbol(0)); //# 01: compile-time error
} on NoSuchMethodError {
print('Caught NoSuchMethodError');
} on TypeError {
print('Caught TypeError');
}
try {
print(const Symbol('0')); //# 02: compile-time error
} on ArgumentError catch (e) {
print('Caught $e');
}
try {
print(const Symbol('_')); //# 03: compile-time error
} on ArgumentError catch (e) {
print('Caught $e');
}
try {
print(new Symbol('0'));
throw 'Expected an ArgumentError';
} on ArgumentError catch (e) {
print('Caught $e');
}
try {
print(new Symbol('_'));
throw 'Expected an ArgumentError';
} on ArgumentError catch (e) {
print('Caught $e');
}
if (!identical(const Symbol('fisk'), x)) {
throw 'Symbol constant is not canonicalized';
}
if (const Symbol('fisk') != x) {
throw 'Symbol constant is not equal to itself';
}
if (const Symbol('fisk') != new Symbol('fisk')) {
throw 'Symbol constant is not equal to its non-const equivalent';
}
if (new Symbol('fisk') != new Symbol('fisk')) {
throw 'new Symbol is not equal to its equivalent';
}
if (new Symbol('fisk') == new Symbol('hest')) {
throw 'unrelated Symbols are equal';
}
if (new Symbol('fisk') == new Object()) {
throw 'unrelated objects are equal';
}
x.hashCode as int;
new Symbol('fisk').hashCode as int;
if (new Symbol('fisk').hashCode != x.hashCode) {
throw "non-const Symbol's hashCode not equal to its const equivalent";
}
if (new Symbol('') != Symbol.empty) {
throw 'empty Symbol not equals to itself';
}
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
main() {
var trebleClef = "\u{1D11E}";
if (trebleClef.length != 2) throw "String should be a surrogate pair";
// These uncaught exceptions should not cause the VM to crash attempting to
// print a malformed string.
throw trebleClef[0]; // //# 01: runtime error
throw trebleClef[1]; // //# 02: runtime error
}

View file

@ -0,0 +1,20 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
Expect.equals(-0x80000001, (-0x80000001).toInt());
Expect.equals(-0x80000000, (-0x80000000 - 0.7).toInt());
Expect.equals(-0x80000000, (-0x80000000 - 0.3).toInt());
Expect.equals(-0x7FFFFFFF, (-0x80000000 + 0.3).toInt());
Expect.equals(-0x7FFFFFFF, (-0x80000000 + 0.7).toInt());
Expect.equals(-0x7FFFFFFF, (-0x7FFFFFFF).toInt());
Expect.equals(0x7FFFFFFE, (0x7FFFFFFE).toInt());
Expect.equals(0x7FFFFFFE, (0x7FFFFFFF - 0.7).toInt());
Expect.equals(0x7FFFFFFE, (0x7FFFFFFF - 0.3).toInt());
Expect.equals(0x7FFFFFFF, (0x7FFFFFFF + 0.3).toInt());
Expect.equals(0x7FFFFFFF, (0x7FFFFFFF + 0.7).toInt());
Expect.equals(0x80000000, 0x80000000.toInt());
}

View file

@ -0,0 +1,11 @@
import "package:expect/expect.dart";
void main() {
var h = "hello", w = "world";
Expect.notEquals(h.hashCode, w.hashCode);
Expect.notEquals((String).hashCode, (int).hashCode);
var c = h.runtimeType.hashCode;
Expect.isTrue(c is int);
Expect.notEquals(c, null);
Expect.notEquals(c, 0);
}

View file

@ -0,0 +1,134 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Testing the behavior of `Type.toString`.
//
// The behavior is *unspecified*, but users may depend on it.
// This test ensures that we do not change the format inadvertently.
// If we decide to change the format, it should be deliberate and consistent.
import "dart:async" show FutureOr;
import "package:expect/expect.dart";
void expectType(Type type, Pattern text) {
var typeString = "$type";
if (typeString.contains("minified:")) {
return; // No checks for minimized types.
}
if (text is String) {
Expect.equals(text, typeString);
return;
}
var match = text.matchAsPrefix(typeString);
if (match != null && match.end == typeString.length) return;
Expect.fail(
"$typeString was not matched by $text${match == null ? "" : ", match: ${match[0]}"}");
}
void expect<T>(Pattern text) {
expectType(T, text);
}
void main() {
// Simple interface types.
expect<int>("int");
expect<Object>("Object");
expect<Null>("Null");
expect<Base>("Base");
expect<Mixin>("Mixin");
// Named mixin applications use their name.
expect<MixinApplication>("MixinApplication");
// Non-class, non-function types.
expect<void>("void");
expect<dynamic>("dynamic");
expect<Function>("Function");
// TODO: Add Never with NNBD.
// Generic interface types.
expect<List<int>>("List<int>");
expect<Iterable<Object>>("Iterable<Object>");
expect<Map<List<String>, Future<void>>>("Map<List<String>, Future<void>>");
expect<GenericMixin<String>>("GenericMixin<String>");
// Generic non-class, non-function type.
expect<FutureOr<int>>("FutureOr<int>");
expect<FutureOr<Object>>("FutureOr<Object>");
expect<FutureOr<FutureOr<Future<Object>>>>(
"FutureOr<FutureOr<Future<Object>>>");
expect<FutureOr<Null>>("FutureOr<Null>");
// TODO: Add nullable types with NNBD.
// Private names may be mangled.
expect<_Private>(re(r'_Private\b.*$'));
// Function types.
expect<void Function()>("() => void");
expect<String Function()>("() => String");
expect<String Function(String)>("(String) => String");
expect<String Function(int, [String])>("(int, [String]) => String");
expect<String Function(int, {String z})>("(int, {String z}) => String");
expect<int Function(void Function(String))>("((String) => void) => int");
expect<int Function(void) Function(String)>("(String) => (void) => int");
// A type alias is expanded to its type.
expect<Typedef>("(dynamic) => dynamic");
expect<Typedef<int>>("(int) => int");
expectType(Typedef, "(dynamic) => dynamic");
// Generic functions do not promise to preserve type variable names,
// but do keep the structure of `<typevars>(params) => result`.
// Cannot pass a generic type as type argument, so passing the Type object
// derived from evaluating the typedef name.
// Format: <T>() => void
expectType(G0, re(r"<\w+>\(\) => void$"));
// Format: <T>() => T
expectType(G1, re(r"<(\w+)>\(\) => \1$"));
// Format: <T>(T) => T
expectType(G2, re(r"<(\w+)>\(\1\) => \1$"));
// Format: <T>(<S>(S, T) => S) => T
expectType(G3, re(r"<(\w+)>\(<(\w+)>\(\2, \1\) => \2\) => \1$"));
// Format: <S>(S) => <T>(S, T) => S
expectType(G4, re(r"<(\w+)>\(\1\) => <(\w+)>\(\1, \2\) => \1$"));
// Format: <S, T>(S, T) => S
expectType(G5, re(r"<(\w+), (\w+)>\(\1, \2\) => \1$"));
// Format: <T>(<S>(S) => S) => T
expectType(Weird, re(r"<(\w+)>\(<(\w+)>\(\2\) => \2\) => \1$"));
// One with everything.
expect<FutureOr<void Function([T Function<S, T>(Map<dynamic, Typedef<S>>)])>>(
// Format: FutureOr<([<S, T>(Map<dynamic, (S) => S>) => T]) => void>
re(r"FutureOr<\(\[<(\w+), (\w+)>\(Map<dynamic, "
r"\(\1\) => \1>\) => \2\]\) => void>$"));
}
// Types to test against.
class _Private {}
class Base {}
mixin Mixin {}
class MixinApplication = Base with Mixin;
mixin GenericMixin<T> implements List<T> {}
typedef Typedef<T> = T Function(T);
// Generic function types.
typedef G0 = void Function<T>();
typedef G1 = T Function<T>();
typedef G2 = T Function<T>(T);
typedef G3 = T Function<T>(S Function<S>(S, T));
typedef G4 = S Function<T>(S, T) Function<S>(S);
typedef G5 = S Function<S, T>(S, T);
typedef Weird = Function Function<Function>(
Function Function<Function>(Function));
RegExp re(String source) => RegExp(source);

View file

@ -0,0 +1,49 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=--optimization_counter_threshold=10 --no-background-compilation
// Test for dart:typed_data (in particular, ByteData.get/setUint64 and
// UInt64List) with limited 64-bit integers.
import 'dart:typed_data';
import "package:expect/expect.dart";
testByteData() {
ByteData data = new ByteData(17);
data.setInt64(5, -0x1122334455667788);
Expect.equals(-0x1122334455667788, data.getUint64(5));
data.setUint32(5, 0x10203040);
Expect.equals(0x10203040aa998878, data.getUint64(5));
data.setUint64(3, -1);
Expect.equals(-1, data.getInt64(3));
data.setUint64(7, 0x7fedcba987654321);
Expect.equals(0x7fedcba987654321, data.getInt64(7));
}
testUint64List() {
Uint64List u64 = new Uint64List(3);
Int64List i64 = new Int64List.view(u64.buffer);
i64[0] = 0x7fffffffffffffff;
i64[1] = -1;
i64[2] = 42;
Expect.equals(0x7fffffffffffffff, u64[0]);
Expect.equals(-1, u64[1]);
Expect.equals(42, u64[2]);
u64[0] = -900000000000000;
Expect.equals(-900000000000000, i64[0]);
}
main() {
for (int i = 0; i < 20; i++) {
testByteData();
testUint64List();
}
}

View file

@ -0,0 +1,77 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:expect/expect.dart';
const String testPhrase = "The quick brown fox jumps over the lazy dog.";
const List<int> testCodepoints = const <int>[
84,
104,
101,
32,
113,
117,
105,
99,
107,
32,
98,
114,
111,
119,
110,
32,
102,
111,
120,
32,
106,
117,
109,
112,
115,
32,
111,
118,
101,
114,
32,
116,
104,
101,
32,
108,
97,
122,
121,
32,
100,
111,
103,
46
];
main() {
testCodepointsToString();
testStringCharCodes();
testEmptyStringFromCharCodes();
testEmptyStringCharCodes();
}
void testStringCharCodes() {
Expect.listEquals(testCodepoints, testPhrase.codeUnits);
}
void testCodepointsToString() {
Expect.stringEquals(testPhrase, new String.fromCharCodes(testCodepoints));
}
void testEmptyStringFromCharCodes() {
Expect.stringEquals("", new String.fromCharCodes(<int>[]));
}
void testEmptyStringCharCodes() {
Expect.listEquals([], "".codeUnits);
}

View file

@ -0,0 +1,16 @@
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
class UnicodeTest {
static testMain() {
var lowerStrasse = new String.fromCharCodes([115, 116, 114, 97, 223, 101]);
Expect.equals("STRASSE", lowerStrasse.toUpperCase());
}
}
main() {
UnicodeTest.testMain();
}

View file

@ -0,0 +1,115 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// SharedOptions=--enable-experiment=triple-shift
import "package:expect/expect.dart";
// The >>> operator is (again) supported by Dart, and used on `int`.
// This test assumes that the JS implementation of `>>>` uses the JS `>>>`
// operator directly (that is, convert the value to Uint32, shift right,)
main() {
testIntegerShifts();
testNonDoubleShifts();
testConstantShifts();
}
void testIntegerShifts() {
for (int i = -1; i <= 65; i++) {
testShift(0, i);
testShift(1, i);
testShift(2, i);
testShift(3, i);
testShift(-1, i);
testShift(-5, i);
// . . .
testShift(0x7fffffff, i);
testShift(0x55555555, i);
testShift(0xaaaaaaaa, i);
testShift(0x80000000, i);
// . . . . .
testShift(0x7fffffffffffffff, i);
testShift(0xffffffffffffffff, i);
}
// JavaScript numbers may consider Infinity as an integer.
// If so, it is zero when converted to a fixed precision.
if (double.infinity is int) {
int number = (double.infinity as int);
Expect.equals(0, number >> 1);
Expect.throws(() => 1 >>> number); // infinity > 64.
}
}
void testNonDoubleShifts() {
double n = 0.0;
n >>> 1; //# 01: compile-time error
for (dynamic number in [0.0, 1.0, 2.4, -2.4, double.infinity, double.nan]) {
if (number is! int) {
Expect.throws(() => number >>> 1);
Expect.throws(() => 1 >>> number);
}
}
}
int testConstantShifts() {
const c = C();
// >>> is a constant operation on integers.
const c1 = 2 >>> 1;
const c2 = (1 >>> 0) >>> 0;
// >>> is a potentially constant operation independent of type.
// The type must still type-check.
const c3 = false ? 1 : c >>> c;
// It's an error if it doesn't type-check.
const c4 = true || c >>> c; //# 02: compile-time error
const c5 = true || "string" >>> 1; //# 03: compile-time error
// Or if the shift isn't on integers and it is evaluated.
const c6 = c >>> c; //# 04: compile-time error
// Or if shifting throws
const c7 = 1 >>> -1; //# 05: compile-time error
const c8 = 1 >>> 65; //# 06: compile-time error
Expect.isNotNull(c1 + c2 + c3); // Avoid "unused variable" warnings.
}
const bool isJSBitOps = (-1 | 0) > 0;
const String jsFlag = isJSBitOps ? " (JS)" : "";
void testShift(int value, int shift) {
var title = "0x${value.toRadixString(16)} >>> $shift$jsFlag";
if (shift < 0) {
// No platform allows shifting a negative.
Expect.throws(() => value >>> shift, "$title: shift < 0");
return;
}
if (!isJSBitOps && shift > 64) {
// Native 64-bit integers do not allow shifts above 64.
Expect.throws(() => value >>> shift, "$title: shift > 64");
return;
}
var expected;
if (isJSBitOps) {
// TODO: Check that this is the desired behavior for JS >>>.
expected = value.toUnsigned(32) >> shift;
} else if (value < 0) {
if (shift > 0) {
expected = (value >> shift).toUnsigned(64 - shift);
} else {
expected = value;
}
} else {
expected = value >> shift;
}
Expect.equals(expected, value >>> shift, title);
}
class C {
const C();
C operator >>>(C other) => other;
}

View file

@ -0,0 +1,14 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
try {
Uri base = Uri.base;
Expect.isTrue(Uri.base.scheme == "file" || Uri.base.scheme == "http");
} on UnsupportedError catch (e) {
Expect.isTrue(e.toString().contains("'Uri.base' is not supported"));
}
}

View file

@ -0,0 +1,293 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
testFileUri() {
final unsupported = new UnsupportedError("");
var tests = [
["", "", ""],
["relative", "relative", "relative"],
["relative/", "relative/", "relative\\"],
["a%20b", "a b", "a b"],
["a%20b/", "a b/", "a b\\"],
["a/b", "a/b", "a\\b"],
["a/b/", "a/b/", "a\\b\\"],
["a%20b/c%20d", "a b/c d", "a b\\c d"],
["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"],
["file:///absolute", "/absolute", "\\absolute"],
["file:///absolute", "/absolute", "\\absolute"],
["file:///a/b", "/a/b", "\\a\\b"],
["file:///a/b", "/a/b", "\\a\\b"],
["file://server/a/b", unsupported, "\\\\server\\a\\b"],
["file://server/a/b/", unsupported, "\\\\server\\a\\b\\"],
["file:///C:/", "/C:/", "C:\\"],
["file:///C:/a/b", "/C:/a/b", "C:\\a\\b"],
["file:///C:/a/b/", "/C:/a/b/", "C:\\a\\b\\"],
["http:/a/b", unsupported, unsupported],
["https:/a/b", unsupported, unsupported],
["urn:a:b", unsupported, unsupported],
];
void check(String s, filePath, bool windows) {
Uri uri = Uri.parse(s);
if (filePath is Error) {
if (filePath is UnsupportedError) {
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: windows));
} else {
Expect.throws(() => uri.toFilePath(windows: windows));
}
} else {
Expect.equals(filePath, uri.toFilePath(windows: windows));
Expect.equals(s, new Uri.file(filePath, windows: windows).toString());
}
}
for (var test in tests) {
check(test[0] as String, test[1], false);
check(test[0] as String, test[2], true);
}
Uri uri;
uri = Uri.parse("file:a");
Expect.equals("/a", uri.toFilePath(windows: false));
Expect.equals("\\a", uri.toFilePath(windows: true));
uri = Uri.parse("file:a/");
Expect.equals("/a/", uri.toFilePath(windows: false));
Expect.equals("\\a\\", uri.toFilePath(windows: true));
}
testFileUriWindowsSlash() {
var tests = [
["", "", ""],
["relative", "relative", "relative"],
["relative/", "relative/", "relative\\"],
["a%20b", "a b", "a b"],
["a%20b/", "a b/", "a b\\"],
["a/b", "a/b", "a\\b"],
["a/b/", "a/b/", "a\\b\\"],
["a%20b/c%20d", "a b/c d", "a b\\c d"],
["a%20b/c%20d/", "a b/c d/", "a b\\c d\\"],
["file:///absolute", "/absolute", "\\absolute"],
["file:///absolute", "/absolute", "\\absolute"],
["file:///a/b", "/a/b", "\\a\\b"],
["file:///a/b", "/a/b", "\\a\\b"],
["file://server/a/b", "//server/a/b", "\\\\server\\a\\b"],
["file://server/a/b/", "//server/a/b/", "\\\\server\\a\\b\\"],
["file:///C:/", "C:/", "C:\\"],
["file:///C:/a/b", "C:/a/b", "C:\\a\\b"],
["file:///C:/a/b/", "C:/a/b/", "C:\\a\\b\\"],
];
for (var test in tests) {
Uri uri = new Uri.file(test[1], windows: true);
Expect.equals(test[0], uri.toString());
Expect.equals(test[2], uri.toFilePath(windows: true));
bool couldBeDir = uri.path.isEmpty || uri.path.endsWith('\\');
Uri dirUri = new Uri.directory(test[1], windows: true);
Expect.isTrue(dirUri.path.isEmpty || dirUri.path.endsWith('/'));
if (couldBeDir) {
Expect.equals(uri, dirUri);
}
}
}
testFileUriWindowsWin32Namespace() {
var tests = [
["\\\\?\\C:\\", "file:///C:/", "C:\\"],
["\\\\?\\C:\\", "file:///C:/", "C:\\"],
[
"\\\\?\\UNC\\server\\share\\file",
"file://server/share/file",
"\\\\server\\share\\file"
],
];
for (var test in tests) {
Uri uri = new Uri.file(test[0], windows: true);
Expect.equals(test[1], uri.toString());
Expect.equals(test[2], uri.toFilePath(windows: true));
}
Expect.throwsArgumentError(() => new Uri.file("\\\\?\\file", windows: true));
Expect.throwsArgumentError(
() => new Uri.file("\\\\?\\UNX\\server\\share\\file", windows: true));
Expect.throwsArgumentError(
() => new Uri.directory("\\\\?\\file", windows: true));
Expect.throwsArgumentError(() =>
new Uri.directory("\\\\?\\UNX\\server\\share\\file", windows: true));
}
testFileUriDriveLetter() {
check(String s, String nonWindows, String? windows) {
Uri uri;
uri = Uri.parse(s);
Expect.equals(nonWindows, uri.toFilePath(windows: false));
if (windows != null) {
Expect.equals(windows, uri.toFilePath(windows: true));
} else {
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: true));
}
}
check("file:///C:", "/C:", "C:\\");
check("file:///C:/", "/C:/", "C:\\");
check("file:///C:a", "/C:a", null);
check("file:///C:a/", "/C:a/", null);
Expect.throwsArgumentError(() => new Uri.file("C:", windows: true));
Expect.throwsArgumentError(() => new Uri.file("C:a", windows: true));
Expect.throwsArgumentError(() => new Uri.file("C:a\b", windows: true));
Expect.throwsArgumentError(() => new Uri.directory("C:", windows: true));
Expect.throwsArgumentError(() => new Uri.directory("C:a", windows: true));
Expect.throwsArgumentError(() => new Uri.directory("C:a\b", windows: true));
}
testFileUriResolve() {
var tests = [
["file:///a", "/a", "", "\\a", ""],
["file:///a/", "/a/", "", "\\a\\", ""],
["file:///b", "/a", "b", "\\a", "b"],
["file:///b/", "/a", "b/", "\\a", "b\\"],
["file:///a/b", "/a/", "b", "\\a\\", "b"],
["file:///a/b/", "/a/", "b/", "\\a\\", "b\\"],
["file:///a/c/d", "/a/b", "c/d", "\\a\\b", "c\\d"],
["file:///a/c/d/", "/a/b", "c/d/", "\\a\\b", "c\\d\\"],
["file:///a/b/c/d", "/a/b/", "c/d", "\\a\\b\\", "c\\d"],
["file:///a/b/c/d/", "/a/b/", "c/d/", "\\a\\b\\", "c\\d\\"],
];
check(String s, String absolute, String relative, bool windows) {
Uri absoluteUri = new Uri.file(absolute, windows: windows);
Uri relativeUri = new Uri.file(relative, windows: windows);
String relativeString = windows ? relative.replaceAll("\\", "/") : relative;
Expect.equals(s, absoluteUri.resolve(relativeString).toString());
Expect.equals(s, absoluteUri.resolveUri(relativeUri).toString());
}
for (var test in tests) {
check(test[0], test[1], test[2], false);
check(test[0], test[1], test[2], true);
check(test[0], test[1], test[4], true);
check(test[0], test[3], test[2], true);
check(test[0], test[3], test[4], true);
}
}
testFileUriIllegalCharacters() {
// Slash is an invalid character in file names on both non-Windows
// and Windows.
Uri uri = Uri.parse("file:///a%2Fb");
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: false));
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: true));
// Illegal characters in windows file names.
var illegalWindowsPaths = [
"a<b",
"a>b",
"a:b",
"a\"b",
"a|b",
"a?b",
"a*b",
"\\\\?\\c:\\a/b"
];
for (var test in illegalWindowsPaths) {
Expect.throwsArgumentError(() => new Uri.file(test, windows: true));
Expect.throwsArgumentError(() => new Uri.file("\\$test", windows: true));
Expect.throwsArgumentError(() => new Uri.directory(test, windows: true));
Expect
.throwsArgumentError(() => new Uri.directory("\\$test", windows: true));
// It is possible to create non-Windows URIs, but not Windows URIs.
Uri uri = new Uri.file(test, windows: false);
Uri absoluteUri = new Uri.file("/$test", windows: false);
Uri dirUri = new Uri.directory(test, windows: false);
Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false);
Expect.throwsArgumentError(() => new Uri.file(test, windows: true));
Expect.throwsArgumentError(() => new Uri.file("\\$test", windows: true));
Expect.throwsArgumentError(() => new Uri.directory(test, windows: true));
Expect
.throwsArgumentError(() => new Uri.directory("\\$test", windows: true));
// It is possible to extract non-Windows file path, but not
// Windows file path.
Expect.equals(test, uri.toFilePath(windows: false));
Expect.equals("/$test", absoluteUri.toFilePath(windows: false));
Expect.equals("$test/", dirUri.toFilePath(windows: false));
Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false));
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: true));
Expect.throwsUnsupportedError(() => absoluteUri.toFilePath(windows: true));
Expect.throwsUnsupportedError(() => dirUri.toFilePath(windows: true));
Expect
.throwsUnsupportedError(() => dirAbsoluteUri.toFilePath(windows: true));
}
// Backslash
illegalWindowsPaths = ["a\\b", "a\\b\\"];
for (var test in illegalWindowsPaths) {
// It is possible to create both non-Windows URIs, and Windows URIs.
Uri uri = new Uri.file(test, windows: false);
Uri absoluteUri = new Uri.file("/$test", windows: false);
Uri dirUri = new Uri.directory(test, windows: false);
Uri dirAbsoluteUri = new Uri.directory("/$test", windows: false);
new Uri.file(test, windows: true);
new Uri.file("\\$test", windows: true);
// It is possible to extract non-Windows file path, but not
// Windows file path from the non-Windows URI (it has a backslash
// in a path segment).
Expect.equals(test, uri.toFilePath(windows: false));
Expect.equals("/$test", absoluteUri.toFilePath(windows: false));
Expect.equals("$test/", dirUri.toFilePath(windows: false));
Expect.equals("/$test/", dirAbsoluteUri.toFilePath(windows: false));
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: true));
Expect.throwsUnsupportedError(() => absoluteUri.toFilePath(windows: true));
Expect.throwsUnsupportedError(() => dirUri.toFilePath(windows: true));
Expect
.throwsUnsupportedError(() => dirAbsoluteUri.toFilePath(windows: true));
}
}
testFileUriIllegalDriveLetter() {
Expect.throwsArgumentError(() => new Uri.file("1:\\", windows: true));
Expect.throwsArgumentError(() => new Uri.directory("1:\\", windows: true));
Uri uri = new Uri.file("1:\\", windows: false);
Uri dirUri = new Uri.directory("1:\\", windows: false);
Expect.equals("1:\\", uri.toFilePath(windows: false));
Expect.equals("1:\\/", dirUri.toFilePath(windows: false));
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: true));
Expect.throwsUnsupportedError(() => dirUri.toFilePath(windows: true));
}
testAdditionalComponents() {
check(String s, {bool windowsOk: false}) {
Uri uri = Uri.parse(s);
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: false));
if (windowsOk) {
Expect.isTrue(uri.toFilePath(windows: true) is String);
} else {
Expect.throwsUnsupportedError(() => uri.toFilePath(windows: true));
}
}
check("file:///path?query");
check("file:///path#fragment");
check("file:///path?query#fragment");
check("file://host/path", windowsOk: true);
check("file://user:password@host/path", windowsOk: true);
}
main() {
testFileUri();
testFileUriWindowsSlash();
testFileUriDriveLetter();
testFileUriWindowsWin32Namespace();
testFileUriResolve();
testFileUriIllegalCharacters();
testFileUriIllegalDriveLetter();
testAdditionalComponents();
}

View file

@ -0,0 +1,87 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
testHttpUri() {
void check(Uri uri, String expected) {
Expect.equals(expected, uri.toString());
}
check(new Uri.http("", ""), "http:");
check(new Uri.http("@:", ""), "http://");
check(new Uri.http("@:8080", ""), "http://:8080");
check(new Uri.http("@host:", ""), "http://host");
check(new Uri.http("@host:", ""), "http://host");
check(new Uri.http("xxx:yyy@host:8080", ""), "http://xxx:yyy@host:8080");
check(new Uri.http("host", "a"), "http://host/a");
check(new Uri.http("host", "/a"), "http://host/a");
check(new Uri.http("host", "a/"), "http://host/a/");
check(new Uri.http("host", "/a/"), "http://host/a/");
check(new Uri.http("host", "a/b"), "http://host/a/b");
check(new Uri.http("host", "/a/b"), "http://host/a/b");
check(new Uri.http("host", "a/b/"), "http://host/a/b/");
check(new Uri.http("host", "/a/b/"), "http://host/a/b/");
check(new Uri.http("host", "a b"), "http://host/a%20b");
check(new Uri.http("host", "/a b"), "http://host/a%20b");
check(new Uri.http("host", "/a b/"), "http://host/a%20b/");
check(new Uri.http("host", "/a%2F"), "http://host/a%252F");
check(new Uri.http("host", "/a%2F/"), "http://host/a%252F/");
check(new Uri.http("host", "/a/b", {"c": "d"}), "http://host/a/b?c=d");
check(
new Uri.http("host", "/a/b", {"c=": "&d"}), "http://host/a/b?c%3D=%26d");
check(new Uri.http("[::]", "a"), "http://[::]/a");
check(new Uri.http("[::127.0.0.1]", "a"), "http://[::127.0.0.1]/a");
check(new Uri.http('[fe80::8eae:4c4d:fee9:8434%rename3]', ''),
'http://[fe80::8eae:4c4d:fee9:8434%25rename3]');
check(new Uri.http('[ff02::1%1%41]', ''), 'http://[ff02::1%251a]');
check(new Uri.http('[ff02::1%321]', ''), 'http://[ff02::1%25321]');
check(new Uri.http('[ff02::1%%321]', ''), 'http://[ff02::1%2521]');
}
testHttpsUri() {
void check(Uri uri, String expected) {
Expect.equals(expected, uri.toString());
}
check(new Uri.https("", ""), "https:");
check(new Uri.https("@:", ""), "https://");
check(new Uri.https("@:8080", ""), "https://:8080");
check(new Uri.https("@host:", ""), "https://host");
check(new Uri.https("@host:", ""), "https://host");
check(new Uri.https("xxx:yyy@host:8080", ""), "https://xxx:yyy@host:8080");
check(new Uri.https("host", "a"), "https://host/a");
check(new Uri.https("host", "/a"), "https://host/a");
check(new Uri.https("host", "a/"), "https://host/a/");
check(new Uri.https("host", "/a/"), "https://host/a/");
check(new Uri.https("host", "a/b"), "https://host/a/b");
check(new Uri.https("host", "/a/b"), "https://host/a/b");
check(new Uri.https("host", "a/b/"), "https://host/a/b/");
check(new Uri.https("host", "/a/b/"), "https://host/a/b/");
check(new Uri.https("host", "a b"), "https://host/a%20b");
check(new Uri.https("host", "/a b"), "https://host/a%20b");
check(new Uri.https("host", "/a b/"), "https://host/a%20b/");
check(new Uri.https("host", "/a%2F"), "https://host/a%252F");
check(new Uri.https("host", "/a%2F/"), "https://host/a%252F/");
check(new Uri.https("host", "/a/b", {"c": "d"}), "https://host/a/b?c=d");
check(new Uri.https("host", "/a/b", {"c=": "&d"}),
"https://host/a/b?c%3D=%26d");
check(new Uri.https("[::]", "a"), "https://[::]/a");
check(new Uri.https("[::127.0.0.1]", "a"), "https://[::127.0.0.1]/a");
}
testResolveHttpScheme() {
String s = "//myserver:1234/path/some/thing";
Uri uri = Uri.parse(s);
Uri http = new Uri(scheme: "http");
Uri https = new Uri(scheme: "https");
Expect.equals("http:$s", http.resolveUri(uri).toString());
Expect.equals("https:$s", https.resolveUri(uri).toString());
}
main() {
testHttpUri();
testHttpsUri();
testResolveHttpScheme();
}

View file

@ -0,0 +1,30 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:expect/expect.dart';
void testParseIPv4Address() {
void pass(String host, List<int> out) {
Expect.listEquals(Uri.parseIPv4Address(host), out);
}
void fail(String host) {
Expect.throwsFormatException(() => Uri.parseIPv4Address(host));
}
pass('127.0.0.1', [127, 0, 0, 1]);
pass('128.0.0.1', [128, 0, 0, 1]);
pass('255.255.255.255', [255, 255, 255, 255]);
pass('0.0.0.0', [0, 0, 0, 0]);
fail('127.0.0.-1');
fail('255.255.255.256');
fail('0.0.0.0.');
fail('0.0.0.0.0');
fail('a.0.0.0');
fail('0.0..0');
}
void main() {
testParseIPv4Address();
}

View file

@ -0,0 +1,217 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:expect/expect.dart';
void testValidIpv6Uri() {
var path = 'http://[::1]:1234/path?query=5#now';
var uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('::1', uri.host);
Expect.equals(1234, uri.port);
Expect.equals('/path', uri.path);
Expect.equals('query=5', uri.query);
Expect.equals('now', uri.fragment);
Expect.equals(path, uri.toString());
path = 'http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8080/index.html';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('fedc:ba98:7654:3210:fedc:ba98:7654:3210', uri.host);
Expect.equals(8080, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals(path.toLowerCase(), uri.toString());
path = 'http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('fedc:ba98:7654:3210:fedc:ba98:7654:3210', uri.host);
Expect.equals(80, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('http://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]/index.html',
uri.toString());
path = 'https://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:443/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('fedc:ba98:7654:3210:fedc:ba98:7654:3210', uri.host);
Expect.equals(443, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]/index.html',
uri.toString());
path = 'http://[1080:0:0:0:8:800:200C:417A]/index.html';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('1080:0:0:0:8:800:200c:417a', uri.host);
Expect.equals(80, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals(path.toLowerCase(), uri.toString());
path = 'http://[3ffe:2a00:100:7031::1]';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('3ffe:2a00:100:7031::1', uri.host);
Expect.equals(80, uri.port);
Expect.equals('', uri.path);
Expect.equals(path, uri.toString());
path = 'http://[1080::8:800:200C:417A]/foo';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('1080::8:800:200c:417a', uri.host);
Expect.equals(80, uri.port);
Expect.equals('/foo', uri.path);
Expect.equals(path.toLowerCase(), uri.toString());
path = 'http://[::192.9.5.5]/ipng';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('::192.9.5.5', uri.host);
Expect.equals(80, uri.port);
Expect.equals('/ipng', uri.path);
Expect.equals(path, uri.toString());
path = 'http://[::FFFF:129.144.52.38]:8080/index.html';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('::ffff:129.144.52.38', uri.host);
Expect.equals(8080, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals(path.toLowerCase(), uri.toString());
path = 'http://[::FFFF:129.144.52.38]:80/index.html';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('::ffff:129.144.52.38', uri.host);
Expect.equals(80, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('http://[::ffff:129.144.52.38]/index.html', uri.toString());
path = 'https://[::FFFF:129.144.52.38]:443/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('::ffff:129.144.52.38', uri.host);
Expect.equals(443, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[::ffff:129.144.52.38]/index.html', uri.toString());
path = 'http://[2010:836B:4179::836B:4179]';
uri = Uri.parse(path);
Expect.equals('http', uri.scheme);
Expect.equals('2010:836b:4179::836b:4179', uri.host);
Expect.equals(80, uri.port);
Expect.equals('', uri.path);
Expect.equals(path.toLowerCase(), uri.toString());
// Checks for ZoneID in RFC 6874
path = 'https://[fe80::a%en1]:443/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('fe80::a%25en1', uri.host);
Expect.equals(443, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[fe80::a%25en1]/index.html', uri.toString());
path = 'https://[fe80::a%25eE1]:443/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('fe80::a%25eE1', uri.host);
Expect.equals(443, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[fe80::a%25eE1]/index.html', uri.toString());
// Recognize bare '%' and transform into '%25'
path = 'https://[fe80::a%1]:443/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('fe80::a%251', uri.host);
Expect.equals(443, uri.port);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[fe80::a%251]/index.html', uri.toString());
path = 'https://[ff02::5678%pvc1.3]/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('ff02::5678%25pvc1.3', uri.host);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[ff02::5678%25pvc1.3]/index.html', uri.toString());
// ZoneID contains percent encoded
path = 'https://[ff02::1%%321]/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('ff02::1%2521', uri.host);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[ff02::1%2521]/index.html', uri.toString());
path = 'https://[ff02::1%321]/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('ff02::1%25321', uri.host);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[ff02::1%25321]/index.html', uri.toString());
// Lower cases
path = 'https://[ff02::1%1%41]/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('ff02::1%251a', uri.host);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[ff02::1%251a]/index.html', uri.toString());
path = 'https://[fe80::8eae:4c4d:fee9:8434%rename3]/index.html';
uri = Uri.parse(path);
Expect.equals('https', uri.scheme);
Expect.equals('fe80::8eae:4c4d:fee9:8434%25rename3', uri.host);
Expect.equals('/index.html', uri.path);
Expect.equals('https://[fe80::8eae:4c4d:fee9:8434%25rename3]/index.html',
uri.toString());
// Test construtors with host name
uri = Uri(scheme: 'https', host: '[ff02::5678%pvc1.3]');
uri = Uri(scheme: 'https', host: '[fe80::a%1]');
uri = Uri(scheme: 'https', host: '[fe80::a%25eE1]');
uri = Uri(scheme: 'https', host: '[fe80::a%en1]');
}
void testParseIPv6Address() {
void pass(String host, List<int> expected) {
Expect.listEquals(expected, Uri.parseIPv6Address(host));
}
void fail(String host) {
Expect.throwsFormatException(() => Uri.parseIPv6Address(host));
}
pass('::127.0.0.1', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1]);
pass('0::127.0.0.1', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1]);
pass('::', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
pass('0::', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
fail(':0::127.0.0.1');
fail('0:::');
fail(':::');
fail('::0:');
fail('::0::');
fail('::0::0');
fail('00000::0');
fail('-1::0');
fail('-AAA::0');
fail('0::127.0.0.1:0');
fail('0::127.0.0');
pass('0::1111', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17]);
pass('2010:836B:4179::836B:4179',
[32, 16, 131, 107, 65, 121, 0, 0, 0, 0, 0, 0, 131, 107, 65, 121]);
fail('2010:836B:4179:0000:127.0.0.1');
fail('2010:836B:4179:0000:0000:127.0.0.1');
fail('2010:836B:4179:0000:0000:0000::127.0.0.1');
fail('2010:836B:4179:0000:0000:0000:0000:127.0.0.1');
pass('2010:836B:4179:0000:0000:0000:127.0.0.1',
[32, 16, 131, 107, 65, 121, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1]);
}
void main() {
testValidIpv6Uri();
testParseIPv6Address();
}

View file

@ -0,0 +1,59 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library uriNormalizePathTest;
import "package:expect/expect.dart";
test(String path, String normalizedPath) {
for (var scheme in ["http", "file", "unknown"]) {
for (var auth in [
[null, "hostname", null],
["userinfo", "hostname", 1234],
[null, null, null]
]) {
for (var query in [null, "query"]) {
for (var fragment in [null, "fragment"]) {
var base = new Uri(
scheme: scheme,
userInfo: auth[0] as String?,
host: auth[1] as String?,
port: auth[2] as int?,
path: path,
query: query,
fragment: fragment);
var expected = base.replace(
path: (base.hasAuthority && normalizedPath.isEmpty)
? "/"
: normalizedPath);
var actual = base.normalizePath();
Expect.equals(expected, actual, "$base");
}
}
}
}
}
testNoChange(String path) {
test(path, path);
}
main() {
testNoChange("foo/bar/baz");
testNoChange("/foo/bar/baz");
testNoChange("foo/bar/baz/");
test("foo/bar/..", "foo/");
test("foo/bar/.", "foo/bar/");
test("foo/./bar/../baz", "foo/baz");
test("../../foo", "foo");
test("./../foo", "foo");
test("./../", "");
test("./../.", "");
test("foo/bar/baz/../../../../qux", "/qux");
test("/foo/bar/baz/../../../../qux", "/qux");
test(".", "");
test("..", "");
test("/.", "/");
test("/..", "/");
}

View file

@ -0,0 +1,77 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
testNormalizePath() {
test(String expected, String path, {String? scheme, String? host}) {
var uri = new Uri(scheme: scheme, host: host, path: path);
Expect.equals(expected, uri.toString());
if (scheme == null && host == null) {
Expect.equals(expected, uri.path);
}
}
var unreserved = "-._~0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
test("A", "%41");
test("AB", "%41%42");
test("%40AB", "%40%41%42");
test("a", "%61");
test("ab", "%61%62");
test("%60ab", "%60%61%62");
test(unreserved, unreserved);
var x = new StringBuffer();
for (int i = 32; i < 128; i++) {
if (unreserved.indexOf(new String.fromCharCode(i)) != -1) {
x.writeCharCode(i);
} else {
x.write("%");
x.write(i.toRadixString(16));
}
}
Expect.equals(x.toString().toUpperCase(),
new Uri(path: x.toString()).toString().toUpperCase());
// Normalized paths.
// Full absolute path normalization for absolute paths.
test("/a/b/c/", "/../a/./b/z/../c/d/..");
test("/a/b/c/", "/./a/b/c/");
test("/a/b/c/", "/./../a/b/c/");
test("/a/b/c/", "/./../a/b/c/.");
test("/a/b/c/", "/./../a/b/c/z/./..");
test("/", "/a/..");
// Full absolute path normalization for URIs with scheme.
test("s:a/b/c/", "../a/./b/z/../c/d/..", scheme: "s");
test("s:a/b/c/", "./a/b/c/", scheme: "s");
test("s:a/b/c/", "./../a/b/c/", scheme: "s");
test("s:a/b/c/", "./../a/b/c/.", scheme: "s");
test("s:a/b/c/", "./../a/b/c/z/./..", scheme: "s");
test("s:/", "/a/..", scheme: "s");
test("s:/", "a/..", scheme: "s");
// Full absolute path normalization for URIs with authority.
test("//h/a/b/c/", "../a/./b/z/../c/d/..", host: "h");
test("//h/a/b/c/", "./a/b/c/", host: "h");
test("//h/a/b/c/", "./../a/b/c/", host: "h");
test("//h/a/b/c/", "./../a/b/c/.", host: "h");
test("//h/a/b/c/", "./../a/b/c/z/./..", host: "h");
test("//h/", "/a/..", host: "h");
test("//h/", "a/..", host: "h");
// Partial relative normalization (allowing leading .. or ./ for current dir).
test("../a/b/c/", "../a/./b/z/../c/d/..");
test("a/b/c/", "./a/b/c/");
test("../a/b/c/", "./../a/b/c/");
test("../a/b/c/", "./../a/b/c/.");
test("../a/b/c/", "./../a/b/c/z/./..");
test("/", "/a/..");
test("./", "a/..");
}
main() {
testNormalizePath();
}

View file

@ -0,0 +1,51 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import 'dart:convert';
main() {
testAll(["a", "b", "c"]);
testAll([""]);
testAll(["a"]);
testAll(["", ""]);
testAll(["baz"]);
testParse("z&y&w&z", {
"z": ["", ""],
"y": [""],
"w": [""]
});
testParse("x=42&y=42&x=37&y=37", {
"x": ["42", "37"],
"y": ["42", "37"]
});
testParse("x&x&x&x&x", {
"x": ["", "", "", "", ""]
});
testParse("x=&&y", {
"x": [""],
"y": [""]
});
}
testAll(List values) {
var uri =
new Uri(scheme: "foo", path: "bar", queryParameters: {"baz": values});
var list = uri.queryParametersAll["baz"]!;
Expect.listEquals(values, list);
}
testParse(query, results) {
var uri = new Uri(scheme: "foo", path: "bar", query: query);
var params = uri.queryParametersAll;
for (var k in results.keys) {
Expect.listEquals(results[k], params[k]!);
}
uri = new Uri(scheme: "foo", path: "bar", queryParameters: results);
params = uri.queryParametersAll;
for (var k in results.keys) {
Expect.listEquals(results[k], params[k]!);
}
}

View file

@ -0,0 +1,72 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void testUriCombi() {
var schemes = ["", "file", "ws", "ftp"];
var fragments = ["", "#", "#f", "#fragment", "#l:?/"];
var queries = ["", "?", "?q", "?query", "?q:/"];
var paths = ["/", "/x", "/x/y", "/x/y/", "/x:y", "x", "x/y", "x/y/"];
var userInfos = ["", "x", "xxx", "x:4", "xxx:444", "x:4:x"];
var hosts = ["", "h", "hhh", "h:4", "hhh:444", "[::1.2.3.4]"];
void check(uriString, scheme, fragment, query, path, user, host) {
for (var uri in [
Uri.parse(uriString),
Uri.parse(">\u{10000}>$uriString<\u{10000}<", 4, uriString.length + 4),
Uri.parse(
"http://example.com/$uriString#?:/[]\"", 19, uriString.length + 19),
Uri.parse(uriString * 3, uriString.length, uriString.length * 2)
]) {
String name = "$uriString -> $uri";
Expect.equals(scheme, uri.scheme, name);
var uriFragment = uri.fragment;
if (fragment.startsWith('#')) uriFragment = "#$uriFragment";
Expect.equals(fragment, uriFragment, name);
var uriQuery = uri.query;
if (query.startsWith('?')) uriQuery = "?$uriQuery";
Expect.equals(query, uriQuery, name);
Expect.equals(path, uri.path, name);
Expect.equals(user, uri.userInfo, name);
var uriHost = uri.host;
if (host.startsWith("[")) uriHost = "[$uriHost]";
if (uri.port != 0) uriHost += ":${uri.port}";
Expect.equals(host, uriHost, name);
}
}
for (var scheme in schemes) {
for (var fragment in fragments) {
for (var query in queries) {
for (var path in paths) {
// File scheme URIs always get a leading slash.
if (scheme == "file" && !path.startsWith('/')) continue;
for (var user in userInfos) {
for (var host in hosts) {
var auth = host;
var s = scheme;
if (user.isNotEmpty) auth = "$user@$auth";
if (auth.isNotEmpty) auth = "//$auth";
if (auth.isNotEmpty && !path.startsWith('/')) continue;
check(
"$scheme${scheme.isEmpty ? "" : ":"}"
"$auth$path$query$fragment",
scheme,
fragment,
query,
path,
user,
host);
}
}
}
}
}
}
}
void main() {
testUriCombi();
}

View file

@ -0,0 +1,186 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:collection";
import "package:expect/expect.dart";
void testInvalidArguments() {}
void testPath() {
void test(s, uri) {
Expect.equals(s, uri.toString());
Expect.equals(uri, Uri.parse(s));
}
test("http:", new Uri(scheme: "http"));
test("http://host/xxx", new Uri(scheme: "http", host: "host", path: "xxx"));
test("http://host/xxx", new Uri(scheme: "http", host: "host", path: "/xxx"));
test("http://host/xxx",
new Uri(scheme: "http", host: "host", pathSegments: ["xxx"]));
test("http://host/xxx/yyy",
new Uri(scheme: "http", host: "host", path: "xxx/yyy"));
test("http://host/xxx/yyy",
new Uri(scheme: "http", host: "host", path: "/xxx/yyy"));
test("http://host/xxx/yyy",
new Uri(scheme: "http", host: "host", pathSegments: ["xxx", "yyy"]));
test("urn:", new Uri(scheme: "urn"));
test("urn:xxx", new Uri(scheme: "urn", path: "xxx"));
test("urn:xxx:yyy", new Uri(scheme: "urn", path: "xxx:yyy"));
Expect.equals(3, new Uri(path: "xxx/yyy/zzz").pathSegments.length);
Expect.equals(3, new Uri(path: "/xxx/yyy/zzz").pathSegments.length);
Expect.equals(3, Uri.parse("http://host/xxx/yyy/zzz").pathSegments.length);
Expect.equals(3, Uri.parse("file:///xxx/yyy/zzz").pathSegments.length);
}
void testPathSegments() {
void test(String path, List<String> segments) {
void check(uri) {
Expect.equals(path, uri.path);
Expect.equals(path, uri.toString());
Expect.listEquals(segments, uri.pathSegments);
}
var uri1 = new Uri(pathSegments: segments);
var uri2 = new Uri(path: path);
check(uri1);
check(uri2);
Expect.equals(uri1, uri2);
}
test("", []);
test("%20", [" "]);
test("%20/%20%20", [" ", " "]);
test("A", ["A"]);
test("%C3%B8", ["ø"]);
test("%C3%B8/%C3%A5", ["ø", "å"]);
test("%C8%A4/%E5%B9%B3%E4%BB%AE%E5%90%8D", ["Ȥ", "平仮名"]);
test("A/b", ["A", "b"]);
test("A/%25", ["A", "%"]);
test("%2F/a%2Fb", ["/", "a/b"]);
test("name;v=1.1", ["name;v=1.1"]);
test("name,v=1.1", ["name,v=1.1"]);
test("name;v=1.1/name,v=1.1", ["name;v=1.1", "name,v=1.1"]);
var unreserved = "-._~0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
var subDelimiters = r"!$&'()*+,;=";
var additionalPathChars = ":@";
var pchar = unreserved + subDelimiters + additionalPathChars;
var encoded = new StringBuffer();
var unencoded = new StringBuffer();
for (int i = 32; i < 128; i++) {
if (pchar.indexOf(new String.fromCharCode(i)) != -1) {
encoded.writeCharCode(i);
} else {
encoded.write("%");
encoded.write(i.toRadixString(16).toUpperCase());
}
unencoded.writeCharCode(i);
}
var encodedStr = encoded.toString();
var unencodedStr = unencoded.toString();
test(encodedStr, [unencodedStr]);
test(encodedStr + "/" + encodedStr, [unencodedStr, unencodedStr]);
Uri uri;
var pathSegments = ["xxx", "yyy", "zzz"];
uri = new Uri(pathSegments: pathSegments);
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(pathSegments: pathSegments.where((_) => true));
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(pathSegments: new DoubleLinkedQueue.from(pathSegments));
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(scheme: "http", host: "host", pathSegments: pathSegments);
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(
scheme: "http",
host: "host",
pathSegments: pathSegments.where((_) => true));
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(
scheme: "http",
host: "host",
pathSegments: new DoubleLinkedQueue.from(pathSegments));
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(scheme: "file", pathSegments: pathSegments);
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(scheme: "file", pathSegments: pathSegments.where((_) => true));
Expect.equals(3, uri.pathSegments.length);
uri = new Uri(
scheme: "file", pathSegments: new DoubleLinkedQueue.from(pathSegments));
Expect.equals(3, uri.pathSegments.length);
}
void testPathCompare() {
void test(Uri uri1, Uri uri2) {
Expect.equals(uri1, uri2);
Expect.equals(uri2, uri1);
}
test(new Uri(scheme: "http", host: "host", path: "xxx"),
new Uri(scheme: "http", host: "host", path: "/xxx"));
test(new Uri(scheme: "http", host: "host", pathSegments: ["xxx"]),
new Uri(scheme: "http", host: "host", path: "/xxx"));
test(new Uri(scheme: "http", host: "host", pathSegments: ["xxx"]),
new Uri(scheme: "http", host: "host", path: "xxx"));
test(new Uri(scheme: "file", path: "xxx"),
new Uri(scheme: "file", path: "/xxx"));
test(new Uri(scheme: "file", pathSegments: ["xxx"]),
new Uri(scheme: "file", path: "/xxx"));
test(new Uri(scheme: "file", pathSegments: ["xxx"]),
new Uri(scheme: "file", path: "xxx"));
}
testPathSegmentsUnmodifiableList() {
void test(list) {
Expect.equals("a", list[0]);
Expect.throwsUnsupportedError(() => list[0] = "c");
Expect.equals(2, list.length);
Expect.throwsUnsupportedError(() => list.length = 1);
Expect.throwsUnsupportedError(() => list.add("c"));
Expect.throwsUnsupportedError(() => list.addAll(["c", "d"]));
Expect.listEquals(["b", "a"], list.reversed.toList());
Expect.throws(() => list.sort());
Expect.equals(0, list.indexOf("a"));
Expect.equals(0, list.lastIndexOf("a"));
Expect.throwsUnsupportedError(() => list.clear());
Expect.throwsUnsupportedError(() => list.insert(1, "c"));
Expect.throwsUnsupportedError(() => list.insertAll(1, ["c", "d"]));
Expect.throwsUnsupportedError(() => list.setAll(1, ["c", "d"]));
Expect.throwsUnsupportedError(() => list.remove("a"));
Expect.throwsUnsupportedError(() => list.removeAt(0));
Expect.throwsUnsupportedError(() => list.removeLast());
Expect.throwsUnsupportedError(() => list.removeWhere((e) => true));
Expect.throwsUnsupportedError(() => list.retainWhere((e) => false));
Expect.listEquals(["a"], list.sublist(0, 1));
Expect.listEquals(["a"], list.getRange(0, 1).toList());
Expect.throwsUnsupportedError(() => list.setRange(0, 1, ["c"]));
Expect.throwsUnsupportedError(() => list.removeRange(0, 1));
Expect.throwsUnsupportedError(() => list.fillRange(0, 1, "c"));
Expect.throwsUnsupportedError(() => list.replaceRange(0, 1, ["c"]));
Map map = new Map();
map[0] = "a";
map[1] = "b";
Expect.mapEquals(list.asMap(), map);
}
test(Uri.parse("a/b").pathSegments);
test(new Uri(pathSegments: ["a", "b"]).pathSegments);
}
main() {
testInvalidArguments();
testPath();
testPathSegments();
testPathCompare();
testPathSegmentsUnmodifiableList();
}

View file

@ -0,0 +1,180 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void testInvalidArguments() {}
void testEncodeQueryComponent() {
// This exact data is from posting a form in Chrome 26 with the one
// exception that * is encoded as %30 and ~ is not encoded as %7E.
Expect.equals(
"%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F%"
"3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~",
Uri.encodeQueryComponent("!\"#\$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
Expect.equals("+%2B+", Uri.encodeQueryComponent(" + "));
Expect.equals("%2B+%2B", Uri.encodeQueryComponent("+ +"));
}
void testQueryParameters() {
test(String query, Map<String, String?> parameters, [String? normalizedQuery]) {
if (normalizedQuery == null) normalizedQuery = query;
check(uri) {
Expect.isTrue(uri.hasQuery);
Expect.equals(normalizedQuery, uri.query);
Expect.equals("?$normalizedQuery", uri.toString());
if (parameters.containsValue(null)) {
var map = new Map.from(parameters);
map.forEach((k, v) {
if (v == null) map[k] = "";
});
Expect.mapEquals(map, uri.queryParameters);
} else {
Expect.mapEquals(parameters, uri.queryParameters);
}
}
var uri1 = new Uri(queryParameters: parameters);
var uri2 = new Uri(query: query);
var uri3 = Uri.parse("?$query");
check(uri1);
if (query != "") {
check(uri2);
} else {
Expect.isFalse(uri2.hasQuery);
}
check(uri3);
Expect.equals(uri1, uri3);
if (query != "") Expect.equals(uri2, uri3);
if (parameters.containsValue(null)) {
var map = new Map.from(parameters);
map.forEach((k, v) {
if (v == null) map[k] = "";
});
Expect.mapEquals(map, Uri.splitQueryString(query));
} else {
Expect.mapEquals(parameters, Uri.splitQueryString(query));
}
}
test("", {});
test("A", {"A": null});
test("%25", {"%": null});
test("%41", {"A": null}, "A");
test("%41A", {"AA": null}, "AA");
test("A", {"A": ""});
test("%25", {"%": ""});
test("%41", {"A": ""}, "A");
test("%41A", {"AA": ""}, "AA");
test("A=a", {"A": "a"});
test("%25=a", {"%": "a"});
test("%41=%61", {"A": "a"}, "A=a");
test("A=+", {"A": " "});
test("A=%2B", {"A": "+"});
test("A=a&B", {"A": "a", "B": null});
test("A=a&B", {"A": "a", "B": ""});
test("A=a&B=b", {"A": "a", "B": "b"});
test("%41=%61&%42=%62", {"A": "a", "B": "b"}, "A=a&B=b");
var unreserved = "-._~0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
var encoded = new StringBuffer();
var allEncoded = new StringBuffer();
var unencoded = new StringBuffer();
for (int i = 32; i < 128; i++) {
if (i == 32) {
encoded.write("+");
} else if (unreserved.indexOf(new String.fromCharCode(i)) != -1) {
encoded.writeCharCode(i);
} else {
encoded.write("%");
encoded.write(i.toRadixString(16).toUpperCase());
}
if (i == 32) {
allEncoded.write("+");
} else {
allEncoded.write("%");
allEncoded.write(i.toRadixString(16).toUpperCase());
}
unencoded.writeCharCode(i);
}
var encodedStr = encoded.toString();
var unencodedStr = unencoded.toString();
test("a=$encodedStr", {"a": unencodedStr});
test("a=$encodedStr&b=$encodedStr", {"a": unencodedStr, "b": unencodedStr});
var map = <String, String?>{};
map[unencodedStr] = unencodedStr;
test("$encodedStr=$encodedStr", map);
test("$encodedStr=$allEncoded", map, "$encodedStr=$encodedStr");
test("$allEncoded=$encodedStr", map, "$encodedStr=$encodedStr");
test("$allEncoded=$allEncoded", map, "$encodedStr=$encodedStr");
map[unencodedStr] = null;
test("$encodedStr", map);
map[unencodedStr] = "";
test("$encodedStr", map);
}
testInvalidQueryParameters() {
test(String query, Map<String, String> parameters) {
check(uri) {
Expect.equals(query, uri.query);
if (query.isEmpty) {
Expect.equals(query, uri.toString());
} else {
Expect.equals("?$query", uri.toString());
}
if (parameters.containsValue(null)) {} else {
Expect.mapEquals(parameters, uri.queryParameters);
}
}
var uri1 = new Uri(query: query);
var uri2 = Uri.parse("?$query");
check(uri1);
check(uri2);
Expect.equals(uri1, uri2);
}
test("=", {});
test("=xxx", {});
test("===", {});
test("=xxx=yyy=zzz", {});
test("=&=&=", {});
test("=xxx&=yyy&=zzz", {});
test("&=&=&", {});
test("&=xxx&=xxx&", {});
}
testQueryParametersImmutableMap() {
test(map) {
Expect.isTrue(map.containsValue("b"));
Expect.isTrue(map.containsKey("a"));
Expect.equals("b", map["a"]);
Expect.throwsUnsupportedError(() => map["a"] = "c");
Expect.throwsUnsupportedError(() => map.putIfAbsent("b", () => "e"));
Expect.throwsUnsupportedError(() => map.remove("a"));
Expect.throwsUnsupportedError(() => map.clear());
var count = 0;
map.forEach((key, value) => count++);
Expect.equals(2, count);
Expect.equals(2, map.keys.length);
Expect.equals(2, map.values.length);
Expect.equals(2, map.length);
Expect.isFalse(map.isEmpty);
Expect.isTrue(map.isNotEmpty);
}
test(Uri.parse("?a=b&c=d").queryParameters);
test(new Uri(queryParameters: {"a": "b", "c": "d"}).queryParameters);
}
main() {
testInvalidArguments();
testEncodeQueryComponent();
testQueryParameters();
testInvalidQueryParameters();
testQueryParametersImmutableMap();
}

View file

@ -0,0 +1,36 @@
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
void testInvalidArguments() {
Expect.throwsFormatException(() => new Uri(scheme: "_"));
Expect.throwsFormatException(() => new Uri(scheme: "http_s"));
Expect.throwsFormatException(() => new Uri(scheme: "127.0.0.1:80"));
}
void testScheme() {
test(String expectedScheme, String expectedUri, String scheme) {
var uri = new Uri(scheme: scheme);
Expect.equals(expectedScheme, uri.scheme);
Expect.equals(expectedUri, uri.toString());
uri = Uri.parse("$scheme:");
Expect.equals(expectedScheme, uri.scheme);
Expect.equals(expectedUri, uri.toString());
}
test("http", "http:", "http");
test("http", "http:", "HTTP");
test("http", "http:", "hTTP");
test("http", "http:", "Http");
test("http+ssl", "http+ssl:", "HTTP+ssl");
test("urn", "urn:", "urn");
test("urn", "urn:", "UrN");
test("a123.432", "a123.432:", "a123.432");
}
main() {
testInvalidArguments();
testScheme();
}

639
tests/corelib/uri_test.dart Normal file
View file

@ -0,0 +1,639 @@
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library uriTest;
import "package:expect/expect.dart";
import 'dart:convert';
testUri(String uriText, bool isAbsolute) {
var uri = Uri.parse(uriText);
Expect.equals(isAbsolute, uri.isAbsolute);
Expect.stringEquals(uriText, uri.toString());
// Test equals and hashCode members.
var uri2 = Uri.parse(uriText);
Expect.equals(uri, uri2);
Expect.equals(uri.hashCode, uri2.hashCode);
// Test that removeFragment doesn't change anything else.
if (uri.hasFragment) {
Expect.equals(Uri.parse(uriText.substring(0, uriText.indexOf('#'))),
uri.removeFragment());
} else {
Expect.equals(uri, Uri.parse(uriText + "#fragment").removeFragment());
}
}
testEncodeDecode(String orig, String encoded) {
var e = Uri.encodeFull(orig);
Expect.stringEquals(encoded, e);
var d = Uri.decodeFull(encoded);
Expect.stringEquals(orig, d);
}
testEncodeDecodeComponent(String orig, String encoded) {
var e = Uri.encodeComponent(orig);
Expect.stringEquals(encoded, e);
var d = Uri.decodeComponent(encoded);
Expect.stringEquals(orig, d);
}
testEncodeDecodeQueryComponent(String orig, String encodedUTF8,
String encodedLatin1, String encodedAscii) {
var e, d;
e = Uri.encodeQueryComponent(orig);
Expect.stringEquals(encodedUTF8, e);
d = Uri.decodeQueryComponent(encodedUTF8);
Expect.stringEquals(orig, d);
e = Uri.encodeQueryComponent(orig, encoding: utf8);
Expect.stringEquals(encodedUTF8, e);
d = Uri.decodeQueryComponent(encodedUTF8, encoding: utf8);
Expect.stringEquals(orig, d);
e = Uri.encodeQueryComponent(orig, encoding: latin1);
Expect.stringEquals(encodedLatin1, e);
d = Uri.decodeQueryComponent(encodedLatin1, encoding: latin1);
Expect.stringEquals(orig, d);
if (encodedAscii != null) {
e = Uri.encodeQueryComponent(orig, encoding: ascii);
Expect.stringEquals(encodedAscii, e);
d = Uri.decodeQueryComponent(encodedAscii, encoding: ascii);
Expect.stringEquals(orig, d);
} else {
Expect.throwsArgumentError(
() => Uri.encodeQueryComponent(orig, encoding: ascii));
}
}
testUriPerRFCs() {
final urisSample = "http://a/b/c/d;p?q";
Uri base = Uri.parse(urisSample);
testResolve(expect, relative) {
Expect.stringEquals(expect, base.resolve(relative).toString());
}
// From RFC 3986.
testResolve("g:h", "g:h");
testResolve("http://a/b/c/g", "g");
testResolve("http://a/b/c/g", "./g");
testResolve("http://a/b/c/g/", "g/");
testResolve("http://a/g", "/g");
testResolve("http://g", "//g");
testResolve("http://a/b/c/d;p?y", "?y");
testResolve("http://a/b/c/g?y", "g?y");
testResolve("http://a/b/c/d;p?q#s", "#s");
testResolve("http://a/b/c/g#s", "g#s");
testResolve("http://a/b/c/g?y#s", "g?y#s");
testResolve("http://a/b/c/;x", ";x");
testResolve("http://a/b/c/g;x", "g;x");
testResolve("http://a/b/c/g;x?y#s", "g;x?y#s");
testResolve("http://a/b/c/d;p?q", "");
testResolve("http://a/b/c/", ".");
testResolve("http://a/b/c/", "./");
testResolve("http://a/b/", "..");
testResolve("http://a/b/", "../");
testResolve("http://a/b/g", "../g");
testResolve("http://a/", "../..");
testResolve("http://a/", "../../");
testResolve("http://a/g", "../../g");
testResolve("http://a/g", "../../../g");
testResolve("http://a/g", "../../../../g");
testResolve("http://a/g", "/./g");
testResolve("http://a/g", "/../g");
testResolve("http://a/b/c/g.", "g.");
testResolve("http://a/b/c/.g", ".g");
testResolve("http://a/b/c/g..", "g..");
testResolve("http://a/b/c/..g", "..g");
testResolve("http://a/b/g", "./../g");
testResolve("http://a/b/c/g/", "./g/.");
testResolve("http://a/b/c/g/h", "g/./h");
testResolve("http://a/b/c/h", "g/../h");
testResolve("http://a/b/c/g;x=1/y", "g;x=1/./y");
testResolve("http://a/b/c/y", "g;x=1/../y");
testResolve("http://a/b/c/g?y/./x", "g?y/./x");
testResolve("http://a/b/c/g?y/../x", "g?y/../x");
testResolve("http://a/b/c/g#s/./x", "g#s/./x");
testResolve("http://a/b/c/g#s/../x", "g#s/../x");
testResolve("http:g", "http:g");
// Additional tests (not from RFC 3986).
testResolve("http://a/b/g;p/h;s", "../g;p/h;s");
// Test non-URI base (no scheme, no authority, relative path).
base = Uri.parse("a/b/c?_#_");
testResolve("a/b/g?q#f", "g?q#f");
testResolve("../", "../../..");
testResolve("a/b/", ".");
testResolve("c", "../../c");
base = Uri.parse("s:a/b");
testResolve("s:/c", "../c");
}
void testResolvePath(String expected, String path) {
Expect.equals(
expected, new Uri(path: '/').resolveUri(new Uri(path: path)).path);
Expect.equals("http://localhost$expected",
Uri.parse("http://localhost").resolveUri(new Uri(path: path)).toString());
}
const ALPHA = r"abcdefghijklmnopqrstuvwxuzABCDEFGHIJKLMNOPQRSTUVWXUZ";
const DIGIT = r"0123456789";
const PERCENT_ENCODED = "%00%ff";
const SUBDELIM = r"!$&'()*+,;=";
const SCHEMECHAR = "$ALPHA$DIGIT+-.";
const UNRESERVED = "$ALPHA$DIGIT-._~";
const REGNAMECHAR = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED";
const USERINFOCHAR = "$REGNAMECHAR:";
const PCHAR_NC = "$UNRESERVED$SUBDELIM$PERCENT_ENCODED@";
const PCHAR = "$PCHAR_NC:";
const QUERYCHAR = "$PCHAR/?";
void testValidCharacters() {
// test that all valid characters are accepted.
for (var scheme in ["", "$SCHEMECHAR$SCHEMECHAR:"]) {
for (var userinfo in [
"",
"@",
"$USERINFOCHAR$USERINFOCHAR@",
"$USERINFOCHAR:$DIGIT@"
]) {
for (var host in [
"", "$REGNAMECHAR$REGNAMECHAR",
"255.255.255.256", // valid reg-name.
"[ffff::ffff:ffff]", "[ffff::255.255.255.255]"
]) {
for (var port in ["", ":", ":$DIGIT$DIGIT"]) {
var auth = "$userinfo$host$port";
if (auth.isNotEmpty) auth = "//$auth";
var paths = ["", "/", "/$PCHAR", "/$PCHAR/"]; // Absolute or empty.
if (auth.isNotEmpty) {
// Initial segment may be empty.
paths..add("//$PCHAR");
} else {
// Path may begin with non-slash.
if (scheme.isEmpty) {
// Initial segment must not contain colon.
paths
..add(PCHAR_NC)
..add("$PCHAR_NC/$PCHAR")
..add("$PCHAR_NC/$PCHAR/");
} else {
paths..add(PCHAR)..add("$PCHAR/$PCHAR")..add("$PCHAR/$PCHAR/");
}
}
for (var path in paths) {
for (var query in ["", "?", "?$QUERYCHAR"]) {
for (var fragment in ["", "#", "#$QUERYCHAR"]) {
var uri = "$scheme$auth$path$query$fragment";
// Should not throw.
var result = Uri.parse(uri);
}
}
}
}
}
}
}
}
void testInvalidUrls() {
void checkInvalid(uri) {
try {
var result = Uri.parse(uri);
Expect.fail("Invalid URI `$uri` parsed to $result\n" + dump(result));
} on FormatException {
// Success.
}
}
checkInvalid("s%41://x.x/"); // No escapes in scheme,
// and no colon before slash in path.
checkInvalid("1a://x.x/"); // Scheme must start with letter,
// and no colon before slash in path.
checkInvalid(".a://x.x/"); // Scheme must start with letter,
// and no colon before slash in path.
checkInvalid("_:"); // Character not valid in scheme,
// and no colon before slash in path.
checkInvalid(":"); // Scheme must start with letter,
// and no colon before slash in path.
void checkInvalidReplaced(uri, invalid, replacement) {
var source = uri.replaceAll('{}', invalid);
var expected = uri.replaceAll('{}', replacement);
var result = Uri.parse(source);
Expect.equals(expected, "$result", "Source: $source\n${dump(result)}");
}
// Regression test for http://dartbug.com/16081
checkInvalidReplaced(
"http://www.example.org/red%09ros{}#red)", "\u00e9", "%C3%A9");
checkInvalidReplaced("http://r{}sum\{}.example.org", "\u00E9", "%C3%A9");
// Invalid characters. The characters must be rejected, even if normalizing
// the input would cause them to be valid (normalization happens after
// validation).
var invalidCharsAndReplacements = [
"\xe7", "%C3%A7", // Arbitrary non-ASCII letter
" ", "%20", // Space, not allowed anywhere.
'"', "%22", // Quote, not allowed anywhere
"<>", "%3C%3E", // Less/greater-than, not allowed anywhere.
"\x7f", "%7F", // DEL, not allowed anywhere
"\xdf", "%C3%9F", // German lower-case scharf-S.
// Becomes ASCII when upper-cased.
"\u0130", "%C4%B0", // Latin capital dotted I,
// becomes ASCII lower-case in Turkish.
"%\uFB03", "%25%EF%AC%83", // % + Ligature ffi,
// becomes ASCII when upper-cased,
// should not be read as "%FFI".
"\u212a", "%E2%84%AA", // Kelvin sign. Becomes ASCII when lower-cased.
"%1g", "%251g", // Invalid escape.
"\u{10000}", "%F0%90%80%80", // Non-BMP character as surrogate pair.
];
for (int i = 0; i < invalidCharsAndReplacements.length; i += 2) {
var invalid = invalidCharsAndReplacements[i];
var valid = invalidCharsAndReplacements[i + 1];
checkInvalid("A{}b:///".replaceAll('{}', invalid));
checkInvalid("{}b:///".replaceAll('{}', invalid));
checkInvalidReplaced("s://user{}info@x.x/", invalid, valid);
checkInvalidReplaced("s://reg{}name/", invalid, valid);
checkInvalid("s://regname:12{}45/".replaceAll("{}", invalid));
checkInvalidReplaced("s://regname/p{}ath/", invalid, valid);
checkInvalidReplaced("/p{}ath/", invalid, valid);
checkInvalidReplaced("p{}ath/", invalid, valid);
checkInvalidReplaced("s://regname/path/?x{}x", invalid, valid);
checkInvalidReplaced("s://regname/path/#x{}x", invalid, valid);
checkInvalidReplaced("s://regname/path/??#x{}x", invalid, valid);
}
// At most one @ in userinfo.
checkInvalid("s://x@x@x.x/");
// No colon in host except before a port.
checkInvalid("s://x@x:x/");
// At most one port.
checkInvalid("s://x@x:9:9/");
// @ not allowed in scheme.
checkInvalid("s@://x:9/x?x#x");
// ] not allowed alone in host.
checkInvalid("s://xx]/");
// IPv6 must be enclosed in [ and ] for Uri.parse.
// It is allowed un-enclosed as argument to `Uri(host:...)` because we don't
// need to delimit.
checkInvalid("s://ffff::ffff:1234/");
}
void testNormalization() {
// The Uri constructor and the Uri.parse function performs RFC-3986
// syntax based normalization.
var uri;
// Scheme: Only case normalization. Schemes cannot contain escapes.
uri = Uri.parse("A:");
Expect.equals("a", uri.scheme);
uri = Uri.parse("Z:");
Expect.equals("z", uri.scheme);
uri = Uri.parse("$SCHEMECHAR:");
Expect.equals(SCHEMECHAR.toLowerCase(), uri.scheme);
// Percent escape normalization.
// Escapes of unreserved characters are converted to the character,
// subject to case normalization in reg-name.
for (var i = 0; i < UNRESERVED.length; i++) {
var char = UNRESERVED[i];
var escape = "%" + char.codeUnitAt(0).toRadixString(16); // all > 0xf.
uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ"
"?vV${escape}vV#wW${escape}wW");
Expect.equals("xX${char}xX", uri.userInfo);
Expect.equals("yY${char}yY".toLowerCase(), uri.host);
Expect.equals("/zZ${char}zZ", uri.path);
Expect.equals("vV${char}vV", uri.query);
Expect.equals("wW${char}wW", uri.fragment);
}
// Escapes of reserved characters are kept, but upper-cased.
for (var escape in ["%00", "%1f", "%7F", "%fF"]) {
uri = Uri.parse("s://xX${escape}xX@yY${escape}yY/zZ${escape}zZ"
"?vV${escape}vV#wW${escape}wW");
var normalizedEscape = escape.toUpperCase();
Expect.equals("xX${normalizedEscape}xX", uri.userInfo);
Expect.equals("yy${normalizedEscape}yy", uri.host);
Expect.equals("/zZ${normalizedEscape}zZ", uri.path);
Expect.equals("vV${normalizedEscape}vV", uri.query);
Expect.equals("wW${normalizedEscape}wW", uri.fragment);
}
// Some host normalization edge cases.
uri = Uri.parse("x://x%61X%41x%41X%61x/");
Expect.equals("xaxaxaxax", uri.host);
uri = Uri.parse("x://Xxxxxxxx/");
Expect.equals("xxxxxxxx", uri.host);
uri = Uri.parse("x://xxxxxxxX/");
Expect.equals("xxxxxxxx", uri.host);
uri = Uri.parse("x://xxxxxxxx%61/");
Expect.equals("xxxxxxxxa", uri.host);
uri = Uri.parse("x://%61xxxxxxxx/");
Expect.equals("axxxxxxxx", uri.host);
uri = Uri.parse("x://X/");
Expect.equals("x", uri.host);
uri = Uri.parse("x://%61/");
Expect.equals("a", uri.host);
uri = new Uri(scheme: "x", path: "//y");
Expect.equals("//y", uri.path);
Expect.equals("x:////y", uri.toString());
uri = new Uri(scheme: "file", path: "//y");
Expect.equals("//y", uri.path);
Expect.equals("file:////y", uri.toString());
// File scheme noralizes to always showing authority, even if empty.
uri = new Uri(scheme: "file", path: "/y");
Expect.equals("file:///y", uri.toString());
uri = new Uri(scheme: "file", path: "y");
Expect.equals("file:///y", uri.toString());
// Empty host/query/fragment ensures the delimiter is there.
// Different from not being there.
Expect.equals("scheme:/", Uri.parse("scheme:/").toString());
Expect.equals("scheme:/", new Uri(scheme: "scheme", path: "/").toString());
Expect.equals("scheme:///?#", Uri.parse("scheme:///?#").toString());
Expect.equals(
"scheme:///#",
new Uri(scheme: "scheme", host: "", path: "/", query: "", fragment: "")
.toString());
// We allow, and escape, general delimiters in paths, queries and fragments.
// Allow `[` and `]` in path:
Expect.equals("s:/%5B%5D", Uri.parse("s:/[]").toString());
Expect.equals("s:%5B%5D", Uri.parse("s:[]").toString());
Expect.equals("%5B%5D", Uri.parse("[]").toString());
// Allow `[`, `]` and `?` in query (anything after *first* `?`).
// The `?` is not escaped.
Expect.equals("s://xx/?%5B%5D?", Uri.parse("s://xx/?[]?").toString());
// Allow `[`, `]`, `?` and `#` in fragment (anything after *first* `#`).
// The `?` is not escaped.
Expect.equals("s://xx/#%5B%5D%23?", Uri.parse("s://xx/#[]#?").toString());
}
void testReplace() {
var uris = [
Uri.parse(""),
Uri.parse("a://@:/?#"),
Uri.parse("a://b@c:4/e/f?g#h"),
Uri.parse("$SCHEMECHAR://$USERINFOCHAR@$REGNAMECHAR:$DIGIT/$PCHAR/$PCHAR"
"?$QUERYCHAR#$QUERYCHAR"),
];
for (var uri1 in uris) {
for (var uri2 in uris) {
if (identical(uri1, uri2)) continue;
var scheme = uri1.scheme;
var userInfo = uri1.hasAuthority ? uri1.userInfo : "";
var host = uri1.hasAuthority ? uri1.host : null;
var port = uri1.hasAuthority ? uri1.port : 0;
var path = uri1.path;
var query = uri1.hasQuery ? uri1.query : null;
var fragment = uri1.hasFragment ? uri1.fragment : null;
var tmp1 = uri1;
void test() {
var tmp2 = new Uri(
scheme: scheme,
userInfo: userInfo,
host: host,
port: port,
path: path,
query: query == "" ? null : query,
queryParameters: query == "" ? {} : null,
fragment: fragment);
Expect.equals(tmp1, tmp2);
}
test();
scheme = uri2.scheme;
tmp1 = tmp1.replace(scheme: scheme);
test();
if (uri2.hasAuthority) {
userInfo = uri2.userInfo;
host = uri2.host;
port = uri2.port;
tmp1 = tmp1.replace(userInfo: userInfo, host: host, port: port);
test();
}
path = uri2.path;
tmp1 = tmp1.replace(path: path);
test();
if (uri2.hasQuery) {
query = uri2.query;
tmp1 = tmp1.replace(query: query);
test();
}
if (uri2.hasFragment) {
fragment = uri2.fragment;
tmp1 = tmp1.replace(fragment: fragment);
test();
}
}
}
// Regression test, http://dartbug.com/20814
var uri = Uri.parse("/no-authorty/");
uri = uri.replace(fragment: "fragment");
Expect.isFalse(uri.hasAuthority);
uri = new Uri(scheme: "foo", path: "bar");
uri = uri.replace(queryParameters: {
"x": ["42", "37"],
"y": ["43", "38"]
});
var params = uri.queryParametersAll;
Expect.equals(2, params.length);
Expect.listEquals(["42", "37"], params["x"]);
Expect.listEquals(["43", "38"], params["y"]);
}
main() {
testUri("http:", true);
testUri("file:///", true);
testUri("file", false);
testUri("http://user@example.com:8080/fisk?query=89&hest=silas", true);
testUri(
"http://user@example.com:8080/fisk?query=89&hest=silas#fragment", false);
Expect.stringEquals(
"http://user@example.com/a/b/c?query#fragment",
new Uri(
scheme: "http",
userInfo: "user",
host: "example.com",
port: 80,
path: "/a/b/c",
query: "query",
fragment: "fragment")
.toString());
Expect.stringEquals(
"/a/b/c/",
new Uri(
scheme: null,
userInfo: null,
host: null,
port: 0,
path: "/a/b/c/",
query: null,
fragment: null)
.toString());
Expect.stringEquals("file:///", Uri.parse("file:").toString());
testResolvePath("/a/g", "/a/b/c/./../../g");
testResolvePath("/a/g", "/a/b/c/./../../g");
testResolvePath("/mid/6", "mid/content=5/../6");
testResolvePath("/a/b/e", "a/b/c/d/../../e");
testResolvePath("/a/b/e", "../a/b/c/d/../../e");
testResolvePath("/a/b/e", "./a/b/c/d/../../e");
testResolvePath("/a/b/e", "../a/b/./c/d/../../e");
testResolvePath("/a/b/e", "./a/b/./c/d/../../e");
testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/.");
testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/./.");
testResolvePath("/a/b/e/", "./a/b/./c/d/../../e/././.");
testUriPerRFCs();
Expect.stringEquals(
"http://example.com", Uri.parse("http://example.com/a/b/c").origin);
Expect.stringEquals(
"https://example.com", Uri.parse("https://example.com/a/b/c").origin);
Expect.stringEquals("http://example.com:1234",
Uri.parse("http://example.com:1234/a/b/c").origin);
Expect.stringEquals("https://example.com:1234",
Uri.parse("https://example.com:1234/a/b/c").origin);
Expect.throwsStateError(() => Uri.parse("http:").origin,
"origin for uri with empty host should fail");
Expect.throwsStateError(
() => new Uri(
scheme: "http",
userInfo: null,
host: "",
port: 80,
path: "/a/b/c",
query: "query",
fragment: "fragment")
.origin,
"origin for uri with empty host should fail");
Expect.throwsStateError(
() => new Uri(
scheme: null,
userInfo: null,
host: "",
port: 80,
path: "/a/b/c",
query: "query",
fragment: "fragment")
.origin,
"origin for uri with empty scheme should fail");
Expect.throwsStateError(
() => new Uri(
scheme: "http",
userInfo: null,
host: null,
port: 80,
path: "/a/b/c",
query: "query",
fragment: "fragment")
.origin,
"origin for uri with empty host should fail");
Expect.throwsStateError(() => Uri.parse("http://:80").origin,
"origin for uri with empty host should fail");
Expect.throwsStateError(() => Uri.parse("file://localhost/test.txt").origin,
"origin for non-http/https uri should fail");
// URI encode tests
// Create a string with code point 0x10000 encoded as a surrogate pair.
var s = utf8.decode([0xf0, 0x90, 0x80, 0x80]);
Expect.stringEquals("\u{10000}", s);
testEncodeDecode("A + B", "A%20+%20B");
testEncodeDecode("\uFFFE", "%EF%BF%BE");
testEncodeDecode("\uFFFF", "%EF%BF%BF");
testEncodeDecode("\uFFFE", "%EF%BF%BE");
testEncodeDecode("\uFFFF", "%EF%BF%BF");
testEncodeDecode("\x7f", "%7F");
testEncodeDecode("\x80", "%C2%80");
testEncodeDecode("\u0800", "%E0%A0%80");
// All characters not escaped by encodeFull.
var unescapedFull = r"abcdefghijklmnopqrstuvwxyz"
r"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
r"0123456789!#$&'()*+,-./:;=?@_~";
// ASCII characters escaped by encodeFull:
var escapedFull =
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
r' "%<>[\]^`{|}'
"\x7f";
var escapedTo = "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"
"%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"
"%20%22%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F";
testEncodeDecode(unescapedFull, unescapedFull);
testEncodeDecode(escapedFull, escapedTo);
var nonAscii =
"\x80-\xff-\u{100}-\u{7ff}-\u{800}-\u{ffff}-\u{10000}-\u{10ffff}";
var nonAsciiEncoding = "%C2%80-%C3%BF-%C4%80-%DF%BF-%E0%A0%80-%EF%BF%BF-"
"%F0%90%80%80-%F4%8F%BF%BF";
testEncodeDecode(nonAscii, nonAsciiEncoding);
testEncodeDecode(s, "%F0%90%80%80");
testEncodeDecodeComponent("A + B", "A%20%2B%20B");
testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE");
testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF");
testEncodeDecodeComponent("\uFFFE", "%EF%BF%BE");
testEncodeDecodeComponent("\uFFFF", "%EF%BF%BF");
testEncodeDecodeComponent("\x7f", "%7F");
testEncodeDecodeComponent("\x80", "%C2%80");
testEncodeDecodeComponent("\u0800", "%E0%A0%80");
testEncodeDecodeComponent(":/@',;?&=+\$", "%3A%2F%40'%2C%3B%3F%26%3D%2B%24");
testEncodeDecodeComponent(s, "%F0%90%80%80");
testEncodeDecodeQueryComponent("A + B", "A+%2B+B", "A+%2B+B", "A+%2B+B");
testEncodeDecodeQueryComponent(
"æ ø å", "%C3%A6+%C3%B8+%C3%A5", "%E6+%F8+%E5", null);
testEncodeDecodeComponent(nonAscii, nonAsciiEncoding);
// Invalid URI - : and @ is swapped, port ("host") should be numeric.
Expect.throwsFormatException(
() => Uri.parse("file://user@password:host/path"));
testValidCharacters();
testInvalidUrls();
testNormalization();
testReplace();
}
String dump(Uri uri) {
return "URI: $uri\n"
" Scheme: ${uri.scheme} #${uri.scheme.length}\n"
" User-info: ${uri.userInfo} #${uri.userInfo.length}\n"
" Host: ${uri.host} #${uri.host.length}\n"
" Port: ${uri.port}\n"
" Path: ${uri.path} #${uri.path.length}\n"
" Query: ${uri.query} #${uri.query.length}\n"
" Fragment: ${uri.fragment} #${uri.fragment.length}\n";
}

View file

@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.
// Dart test for testing regular expressions in Dart.
// Note: This test deliberately has no NNBD version in corelib/.
import "package:expect/expect.dart";
main() {