mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 12:48:10 +00:00
Migrate language_2/function to NNBD.
Change-Id: I0726fa252736a83a2d80345d71c05aacf8ed0649 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142941 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Leaf Petersen <leafp@google.com>
This commit is contained in:
parent
c99da6857f
commit
5ed88471ec
|
@ -639,7 +639,7 @@ class StringBuffer {
|
|||
return string;
|
||||
}
|
||||
|
||||
static String _writeOne(String string, Object obj) {
|
||||
static String _writeOne(String string, Object? obj) {
|
||||
return Primitives.stringConcatUnchecked(string, '$obj');
|
||||
}
|
||||
}
|
||||
|
|
36
tests/language/dynamic_type_helper.dart
Normal file
36
tests/language/dynamic_type_helper.dart
Normal 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.
|
||||
|
||||
library dynamic_type_helper;
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
/// Checks that a dynamic type error is thrown when [f] is executed
|
||||
/// and [expectTypeError] is `true`.
|
||||
void testDynamicTypeError(bool expectTypeError, f(), [String? message]) {
|
||||
if (expectTypeError) {
|
||||
checkDynamicTypeError(f, message);
|
||||
} else {
|
||||
checkNoDynamicTypeError(f, message);
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that a dynamic type error is thrown when f is executed.
|
||||
void checkDynamicTypeError(f(), [String? message]) {
|
||||
message = message != null ? ': $message' : '';
|
||||
try {
|
||||
f();
|
||||
Expect.fail('Missing type error$message.');
|
||||
} on TypeError catch (e) {}
|
||||
}
|
||||
|
||||
/// Checks that no dynamic type error is thrown when [f] is executed.
|
||||
void checkNoDynamicTypeError(f(), [String? message]) {
|
||||
message = message != null ? ': $message' : '';
|
||||
try {
|
||||
f();
|
||||
} on TypeError catch (e) {
|
||||
Expect.fail('Unexpected type error$message.');
|
||||
}
|
||||
}
|
47
tests/language/function/apply_generic2_test.dart
Normal file
47
tests/language/function/apply_generic2_test.dart
Normal file
|
@ -0,0 +1,47 @@
|
|||
// 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 "package:expect/expect.dart";
|
||||
|
||||
makeFn() {
|
||||
return <T extends num>({T? a1, T? a2, T? a3, T? a4, T? a5}) {
|
||||
return <T?>[a1, a2, a3, a4, a5];
|
||||
};
|
||||
}
|
||||
|
||||
staticFn<T extends num>({T? a1, T? a2, T? a3, T? a4, T? a5, T? xx}) {
|
||||
return <T?>[a1, a2, a3, a4, a5, xx];
|
||||
}
|
||||
|
||||
class CCC {
|
||||
memberFn<T extends num>({T? a1, T? a2, T? a3, T? a4, T? a5, T? yy}) {
|
||||
return <T?>[a1, a2, a3, a4, a5, yy];
|
||||
}
|
||||
}
|
||||
|
||||
check(a, b) {
|
||||
print('a: $a\nb: $b');
|
||||
Expect.equals(a.toString(), b.toString());
|
||||
}
|
||||
|
||||
main() {
|
||||
check('[null, 33, null, 11, 22, null]',
|
||||
Function.apply(new CCC().memberFn, [], {#a4: 11, #a5: 22, #a2: 33}));
|
||||
|
||||
Expect.throwsTypeError(
|
||||
() => Function.apply(new CCC().memberFn, [], {#a3: 'hi'}));
|
||||
|
||||
check('[11, 22, 33, null, null]',
|
||||
Function.apply(makeFn(), [], {#a1: 11, #a2: 22, #a3: 33}));
|
||||
|
||||
check('[null, 33, null, 11, 22]',
|
||||
Function.apply(makeFn(), [], {#a4: 11, #a5: 22, #a2: 33}));
|
||||
|
||||
Expect.throwsTypeError(() => Function.apply(makeFn(), [], {#a3: 'hi'}));
|
||||
|
||||
check('[null, 33, null, 11, 22, null]',
|
||||
Function.apply(staticFn, [], {#a4: 11, #a5: 22, #a2: 33}));
|
||||
|
||||
Expect.throwsTypeError(() => Function.apply(staticFn, [], {#a3: 'hi'}));
|
||||
}
|
64
tests/language/function/apply_generic_test.dart
Normal file
64
tests/language/function/apply_generic_test.dart
Normal file
|
@ -0,0 +1,64 @@
|
|||
// 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 "package:expect/expect.dart";
|
||||
|
||||
List<T?> staticFn<T>(
|
||||
[T? a1, T? a2, T? a3, T? a4, T? a5, T? a6, T? a7, T? a8, T? a9, T? a10]) {
|
||||
return <T?>[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
|
||||
}
|
||||
|
||||
class C<CT> {
|
||||
List<T?> memberFn<T>(
|
||||
[T? a1, T? a2, T? a3, T? a4, T? a5, T? a6, T? a7, T? a8, T? a9, T? a10]) {
|
||||
return <T?>[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
|
||||
}
|
||||
|
||||
// Intercepted, e.g. on JSArray.
|
||||
List<T?> map<T>(
|
||||
[T? a1, T? a2, T? a3, T? a4, T? a5, T? a6, T? a7, T? a8, T? a9, T? a10]) {
|
||||
return <T?>[a1, a2, a3, a4, a5, a6, a7, a8, a9, a10];
|
||||
}
|
||||
}
|
||||
|
||||
check(a, b) {
|
||||
print('a: $a\nb: $b');
|
||||
Expect.equals(a.toString(), b.toString());
|
||||
}
|
||||
|
||||
main() {
|
||||
check('[1, 2, 3, null, null, null, null, null, null, null]',
|
||||
Function.apply(staticFn, [1, 2, 3]));
|
||||
|
||||
check('[1, 2, 3, 4, null, null, null, null, null, null]',
|
||||
Function.apply(staticFn, [1, 2, 3, 4]));
|
||||
|
||||
check('[1, 2, 3, 4, 5, 6, 7, null, null, null]',
|
||||
Function.apply(staticFn, [1, 2, 3, 4, 5, 6, 7]));
|
||||
|
||||
var o = new C<num>();
|
||||
dynamic memberFn1 = o.map;
|
||||
|
||||
check('[1, 2, 3, null, null, null, null, null, null, null]',
|
||||
Function.apply(memberFn1, [1, 2, 3]));
|
||||
|
||||
check('[1, 2, 3, 4, null, null, null, null, null, null]',
|
||||
Function.apply(memberFn1, [1, 2, 3, 4]));
|
||||
|
||||
check('[1, 2, 3, 4, 5, 6, 7, null, null, null]',
|
||||
Function.apply(memberFn1, [1, 2, 3, 4, 5, 6, 7]));
|
||||
|
||||
dynamic memberFn2 = o.memberFn;
|
||||
|
||||
check('[1, 2, 3, null, null, null, null, null, null, null]',
|
||||
Function.apply(memberFn2, [1, 2, 3]));
|
||||
|
||||
check('[1, 2, 3, 4, null, null, null, null, null, null]',
|
||||
Function.apply(memberFn2, [1, 2, 3, 4]));
|
||||
|
||||
check('[1, 2, 3, 4, 5, 6, 7, null, null, null]',
|
||||
Function.apply(memberFn2, [1, 2, 3, 4, 5, 6, 7]));
|
||||
|
||||
// TODO(sra): Apply of instantiations
|
||||
}
|
22
tests/language/function/argument_test.dart
Normal file
22
tests/language/function/argument_test.dart
Normal 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.
|
||||
// Dart test for function passing.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class FunctionArgumentTest {
|
||||
static testMe(Function f) {
|
||||
return f();
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
Expect.equals(42, testMe(() {
|
||||
return 42;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
FunctionArgumentTest.testMain();
|
||||
}
|
83
tests/language/function/call_generic_test.dart
Normal file
83
tests/language/function/call_generic_test.dart
Normal file
|
@ -0,0 +1,83 @@
|
|||
// 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.
|
||||
// dart2jsOptions=-Ddart.isdart2js=true
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
@pragma('dart2js:noInline')
|
||||
List staticFn<T>([T? a1, T? a2, T? a3, T? a4, T? a5]) => [T, a1, a2, a3, a4, a5];
|
||||
|
||||
class C {
|
||||
@pragma('dart2js:noInline')
|
||||
List memberFn<T>([T? a1, T? a2, T? a3, T? a4, T? a5]) => [T, a1, a2, a3, a4, a5];
|
||||
|
||||
@pragma('dart2js:noInline')
|
||||
// 'map' is implemented by native iterables. On dart2js, 'map' has interceptor
|
||||
// calling convention.
|
||||
List map<T>([T? a1, T? a2, T? a3, T? a4, T? a5]) => [T, a1, a2, a3, a4, a5];
|
||||
}
|
||||
|
||||
check(expected, actual) {
|
||||
print('a: $expected');
|
||||
print('b: $actual');
|
||||
if (((actual[0] == Object && expected[0] == dynamic) ||
|
||||
(actual[0] == dynamic && expected[0] == Object)) &&
|
||||
!const bool.fromEnvironment('dart.isdart2js')) {
|
||||
// TODO(32483): dartdevk sometimes defaults type to 'Object' when 'dynamic'
|
||||
// is required. Remove this hack when fixed.
|
||||
// TODO(31581): dart2js needs instantiate-to-bound to generic 'dynamic'
|
||||
// instead of 'Object'.
|
||||
actual = actual.toList()..[0] = expected[0];
|
||||
print('b*: $actual');
|
||||
}
|
||||
Expect.equals(expected.toString(), actual.toString());
|
||||
}
|
||||
|
||||
main() {
|
||||
check([dynamic, 1, 2, 3, null, null], staticFn(1 as dynamic, 2, 3));
|
||||
|
||||
check([Object, 'Z', 2, 4, null, null], staticFn('Z', 2, 4));
|
||||
|
||||
check([int, 3, 2, 1, null, null], staticFn(3, 2, 1));
|
||||
|
||||
dynamic f1 = staticFn;
|
||||
|
||||
check([dynamic, 4, 2, 3, null, null], f1(4 as dynamic, 2, 3));
|
||||
|
||||
check([dynamic, 'Q', 2, 3, null, null], f1('Q', 2, 3));
|
||||
|
||||
check([dynamic, 6, 2, 3, null, null], f1(6, 2, 3));
|
||||
|
||||
check([int, 7, 2, null, null, null], f1<int>(7, 2));
|
||||
|
||||
var c = new C();
|
||||
|
||||
check([dynamic, 8, 2, 3, null, null], c.memberFn(8 as dynamic, 2, 3));
|
||||
|
||||
check([Object, 'A', 2, 3, null, null], c.memberFn('A', 2, 3));
|
||||
|
||||
check([int, 9, 2, 3, null, null], c.memberFn<int>(9, 2, 3));
|
||||
|
||||
check([dynamic, 10, 2, 3, null, null], c.map(10 as dynamic, 2, 3));
|
||||
|
||||
check([Object, 'B', 2, 3, null, null], c.map('B', 2, 3));
|
||||
|
||||
check([int, 11, 2, 3, null, null], c.map(11, 2, 3));
|
||||
|
||||
dynamic o = new C();
|
||||
|
||||
check([dynamic, 12, 2, 3, null, null], o.memberFn(12 as dynamic, 2, 3));
|
||||
|
||||
check([dynamic, 'C', 2, 3, null, null], o.memberFn('C', 2, 3));
|
||||
|
||||
check([int, 13, 2, null, null, null], o.memberFn<int>(13, 2));
|
||||
|
||||
check([dynamic, 14, 2, 3, null, null], o.map(14 as dynamic, 2, 3));
|
||||
|
||||
check([dynamic, 'D', 2, 3, null, null], o.map('D', 2, 3));
|
||||
|
||||
check([int, 15, null, null, null, null], o.map<int>(15));
|
||||
|
||||
check([int, 16, 2, 3, 4, null], o.map<int>(16, 2, 3, 4));
|
||||
}
|
20
tests/language/function/field_test.dart
Normal file
20
tests/language/function/field_test.dart
Normal file
|
@ -0,0 +1,20 @@
|
|||
// 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.
|
||||
// VMOptions=--fatal-type-errors --enable_type_checks
|
||||
//
|
||||
// Test of calling Function, which is field of some class.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class Wrapper {
|
||||
late Function f;
|
||||
}
|
||||
|
||||
main() {
|
||||
Wrapper w = new Wrapper();
|
||||
w.f = () {
|
||||
return 42;
|
||||
};
|
||||
Expect.equals(42, w.f());
|
||||
}
|
378
tests/language/function/function_test.dart
Normal file
378
tests/language/function/function_test.dart
Normal file
|
@ -0,0 +1,378 @@
|
|||
// 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";
|
||||
|
||||
// Tests function statements and expressions.
|
||||
|
||||
class Bug4089219 {
|
||||
int x;
|
||||
var f;
|
||||
|
||||
Bug4089219(int i) : this.x = i {
|
||||
f = () => x;
|
||||
}
|
||||
}
|
||||
|
||||
class Bug4342163 {
|
||||
final m;
|
||||
Bug4342163(int a) : this.m = (() => a) {}
|
||||
}
|
||||
|
||||
class StaticFunctionDef {
|
||||
static const int one = 1;
|
||||
static var fn1;
|
||||
static var fn2;
|
||||
static var fn3;
|
||||
|
||||
static init() {
|
||||
fn1 = () {
|
||||
return one;
|
||||
};
|
||||
fn2 = () {
|
||||
return (() {
|
||||
return one;
|
||||
})();
|
||||
};
|
||||
fn3 = () {
|
||||
final local = 1;
|
||||
return (() {
|
||||
return local;
|
||||
})();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class A {
|
||||
var ma;
|
||||
A(a) {
|
||||
ma = a;
|
||||
}
|
||||
}
|
||||
|
||||
class B1 extends A {
|
||||
final mfn;
|
||||
B1(int a)
|
||||
: this.mfn = (() {
|
||||
return a;
|
||||
}),
|
||||
super(a);
|
||||
}
|
||||
|
||||
class B2 extends A {
|
||||
final mfn;
|
||||
B2(int a)
|
||||
: this.mfn = (() {
|
||||
return a;
|
||||
}),
|
||||
super(2);
|
||||
}
|
||||
|
||||
class B3 extends A {
|
||||
final mfn;
|
||||
B3(int a)
|
||||
: this.mfn = (() {
|
||||
return a;
|
||||
}),
|
||||
super(() {
|
||||
return a;
|
||||
});
|
||||
}
|
||||
|
||||
typedef void Fisk();
|
||||
|
||||
class FunctionTest {
|
||||
FunctionTest() {}
|
||||
|
||||
static void testMain() {
|
||||
var test = new FunctionTest();
|
||||
test.testForEach();
|
||||
test.testVarOrder1();
|
||||
test.testVarOrder2();
|
||||
test.testLexicalClosureRef1();
|
||||
test.testLexicalClosureRef2();
|
||||
test.testLexicalClosureRef3();
|
||||
test.testLexicalClosureRef4();
|
||||
test.testLexicalClosureRef5();
|
||||
test.testDefaultParametersOrder();
|
||||
test.testParametersOrder();
|
||||
test.testFunctionDefaults1();
|
||||
test.testFunctionDefaults2();
|
||||
test.testEscapingFunctions();
|
||||
test.testThisBinding();
|
||||
test.testFnBindingInStatics();
|
||||
test.testFnBindingInInitLists();
|
||||
test.testSubclassConstructorScopeAlias();
|
||||
}
|
||||
|
||||
void testSubclassConstructorScopeAlias() {
|
||||
var b1 = new B1(10);
|
||||
Expect.equals(10, (b1.mfn)());
|
||||
Expect.equals(10, b1.ma);
|
||||
|
||||
var b2 = new B2(11);
|
||||
Expect.equals(11, (b2.mfn)());
|
||||
Expect.equals(2, b2.ma);
|
||||
|
||||
var b3 = new B3(12);
|
||||
Expect.equals(12, (b3.mfn)());
|
||||
Expect.equals(12, (b3.ma)());
|
||||
}
|
||||
|
||||
void testFnBindingInInitLists() {
|
||||
Expect.equals(1, (new Bug4342163(1).m)());
|
||||
}
|
||||
|
||||
void testFnBindingInStatics() {
|
||||
StaticFunctionDef.init();
|
||||
Expect.equals(1, ((StaticFunctionDef.fn1)()));
|
||||
Expect.equals(1, ((StaticFunctionDef.fn2)()));
|
||||
Expect.equals(1, ((StaticFunctionDef.fn3)()));
|
||||
}
|
||||
|
||||
Fisk testReturnVoidFunction() {
|
||||
void f() {}
|
||||
Fisk x = f;
|
||||
return f;
|
||||
}
|
||||
|
||||
void testVarOrder1() {
|
||||
var a = 0, b = a++, c = a++;
|
||||
|
||||
Expect.equals(a, 2);
|
||||
Expect.equals(b, 0);
|
||||
Expect.equals(c, 1);
|
||||
}
|
||||
|
||||
void testVarOrder2() {
|
||||
var a = 0;
|
||||
f() {
|
||||
return a++;
|
||||
}
|
||||
|
||||
;
|
||||
var b = f(), c = f();
|
||||
|
||||
Expect.equals(a, 2);
|
||||
Expect.equals(b, 0);
|
||||
Expect.equals(c, 1);
|
||||
}
|
||||
|
||||
void testLexicalClosureRef1() {
|
||||
var a = 1;
|
||||
var f, g;
|
||||
{
|
||||
var b = 2;
|
||||
f = () {
|
||||
return b - a;
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
var b = 3;
|
||||
g = () {
|
||||
return b - a;
|
||||
};
|
||||
}
|
||||
Expect.equals(1, f());
|
||||
Expect.equals(2, g());
|
||||
}
|
||||
|
||||
void testLexicalClosureRef2() {
|
||||
var a = 1;
|
||||
var f, g;
|
||||
{
|
||||
var b = 2;
|
||||
f = () {
|
||||
return (() {
|
||||
return b - a;
|
||||
})();
|
||||
};
|
||||
}
|
||||
|
||||
{
|
||||
var b = 3;
|
||||
g = () {
|
||||
return (() {
|
||||
return b - a;
|
||||
})();
|
||||
};
|
||||
}
|
||||
Expect.equals(1, f());
|
||||
Expect.equals(2, g());
|
||||
}
|
||||
|
||||
void testLexicalClosureRef3() {
|
||||
var a = [];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
var x = i;
|
||||
a.add(() {
|
||||
return x;
|
||||
});
|
||||
}
|
||||
|
||||
var sum = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
sum += (a[i])() as int;
|
||||
}
|
||||
|
||||
Expect.equals(45, sum);
|
||||
}
|
||||
|
||||
void testLexicalClosureRef5() {
|
||||
{
|
||||
var a;
|
||||
Expect.equals(null, a);
|
||||
a = 1;
|
||||
Expect.equals(1, a);
|
||||
}
|
||||
|
||||
{
|
||||
var a;
|
||||
Expect.equals(null, a);
|
||||
a = 1;
|
||||
Expect.equals(1, a);
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure labels are preserved, and a second 'i' does influence the first.
|
||||
void testLexicalClosureRef4() {
|
||||
var a = [];
|
||||
x:
|
||||
for (int i = 0; i < 10; i++) {
|
||||
a.add(() {
|
||||
return i;
|
||||
});
|
||||
continue x;
|
||||
}
|
||||
|
||||
var sum = 0;
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
sum += (a[i])() as int;
|
||||
}
|
||||
|
||||
Expect.equals(45, sum);
|
||||
}
|
||||
|
||||
int tempField = -1;
|
||||
|
||||
void testForEach() {
|
||||
List<int> vals = [1, 2, 3];
|
||||
int total = 0;
|
||||
vals.forEach((int v) {
|
||||
total += v;
|
||||
});
|
||||
Expect.equals(6, total);
|
||||
}
|
||||
|
||||
void testDefaultParametersOrder() {
|
||||
f([a = 1, b = 3]) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
Expect.equals(-2, f());
|
||||
}
|
||||
|
||||
void testParametersOrder() {
|
||||
f(a, b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
Expect.equals(-2, f(1, 3));
|
||||
}
|
||||
|
||||
void testFunctionDefaults1() {
|
||||
// TODO(jimhug): This return null shouldn't be necessary.
|
||||
f() {
|
||||
return null;
|
||||
}
|
||||
|
||||
;
|
||||
(([a = 10]) {
|
||||
Expect.equals(10, a);
|
||||
})();
|
||||
((a, [b = 10]) {
|
||||
Expect.equals(10, b);
|
||||
})(1);
|
||||
(([a = 10]) {
|
||||
Expect.equals(null, a);
|
||||
})(f());
|
||||
// FAILS: (([a = 10]) { Expect.equals(null ,a); })( f() );
|
||||
}
|
||||
|
||||
void testFunctionDefaults2() {
|
||||
Expect.equals(10, helperFunctionDefaults2());
|
||||
Expect.equals(1, helperFunctionDefaults2(1));
|
||||
}
|
||||
|
||||
num helperFunctionDefaults2([a = 10]) {
|
||||
return (() {
|
||||
return a;
|
||||
})();
|
||||
}
|
||||
|
||||
void testEscapingFunctions() {
|
||||
f() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
;
|
||||
(() {
|
||||
Expect.equals(42, f());
|
||||
})();
|
||||
var o = new Bug4089219(42);
|
||||
Expect.equals(42, (o.f)());
|
||||
}
|
||||
|
||||
void testThisBinding() {
|
||||
Expect.equals(this, () {
|
||||
return this;
|
||||
}());
|
||||
}
|
||||
}
|
||||
|
||||
typedef void Foo<A, B>(A a, B b);
|
||||
|
||||
class Bar<A, B> {
|
||||
Foo<A, B> field;
|
||||
Bar(A a, B b) : this.field = ((A a1, B b2) {}) {
|
||||
field(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
typedef UntypedFunction(arg);
|
||||
typedef UntypedFunction2(arg);
|
||||
|
||||
class UseFunctionTypes {
|
||||
void test() {
|
||||
Function? f = null;
|
||||
UntypedFunction? uf = null;
|
||||
UntypedFunction2? uf2 = null;
|
||||
Foo? foo = null;
|
||||
Foo<int, String>? fooIntString = null;
|
||||
Foo<Object, Object>? fooObjectObject = null;
|
||||
|
||||
f = uf;
|
||||
f = uf2;
|
||||
f = foo;
|
||||
f = fooIntString;
|
||||
|
||||
uf = f as UntypedFunction?;
|
||||
uf2 = f;
|
||||
foo = f as Foo?;
|
||||
fooIntString = f as Foo<int, String>?;
|
||||
|
||||
fooIntString = foo as Foo<int, String>?;
|
||||
|
||||
foo = fooObjectObject as Foo?;
|
||||
fooObjectObject = foo as Foo<Object, Object>?;
|
||||
|
||||
uf = uf2;
|
||||
uf2 = uf;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
FunctionTest.testMain();
|
||||
}
|
13
tests/language/function/getter_test.dart
Normal file
13
tests/language/function/getter_test.dart
Normal file
|
@ -0,0 +1,13 @@
|
|||
// 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";
|
||||
|
||||
class A {
|
||||
a() => 42;
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.equals(new A().a(), (new A().a)());
|
||||
}
|
113
tests/language/function/literals2_test.dart
Normal file
113
tests/language/function/literals2_test.dart
Normal file
|
@ -0,0 +1,113 @@
|
|||
// 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 new function type alias.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class FunctionLiteralsTest {
|
||||
static void testMain() {
|
||||
f(x) {
|
||||
return x * 2;
|
||||
}
|
||||
|
||||
f(42); // make sure it is parsed as a function call
|
||||
Expect.equals(20, f(10));
|
||||
|
||||
int g(x) {
|
||||
return x * 2;
|
||||
}
|
||||
|
||||
g(42); // make sure it is parsed as a function call
|
||||
Expect.equals(20, g(10));
|
||||
|
||||
h(x) {
|
||||
return x * 2;
|
||||
}
|
||||
|
||||
h(42); // make sure it is parsed as a function call
|
||||
Expect.equals(20, h(10));
|
||||
|
||||
dynamic a = (x) {
|
||||
return x + 2;
|
||||
};
|
||||
Expect.equals(7, a(5));
|
||||
|
||||
Expect.equals(
|
||||
10,
|
||||
apply((k) {
|
||||
return k << 1;
|
||||
}, 5));
|
||||
Expect.equals(20, apply((k) => k << 1, 10));
|
||||
|
||||
a = new A(3);
|
||||
Expect.equals(-1, a.f);
|
||||
Expect.equals(-3, a.f2);
|
||||
|
||||
a = new A.n(5);
|
||||
Expect.equals(-2, a.f);
|
||||
Expect.equals(2, a.f2);
|
||||
|
||||
Expect.equals(true, isOdd(5));
|
||||
Expect.equals(false, isOdd(8));
|
||||
|
||||
var b = new B(10);
|
||||
Expect.equals(10, b.n);
|
||||
Expect.equals(100, (b.f)(10));
|
||||
|
||||
b = new B.withZ(10);
|
||||
Expect.equals(10, b.n);
|
||||
Expect.equals(101, (b.f)(10));
|
||||
|
||||
var c = new C(5);
|
||||
Expect.equals("2*x is 10", c.s);
|
||||
|
||||
int x = 0;
|
||||
int y = 1;
|
||||
// make sure this isn't parsed as a generic type
|
||||
Expect.isTrue(x < y, "foo");
|
||||
}
|
||||
}
|
||||
|
||||
int apply(f, n) {
|
||||
return f(n) as int;
|
||||
}
|
||||
|
||||
bool isOdd(b) => b % 2 == 1;
|
||||
|
||||
class A {
|
||||
int f = -1;
|
||||
int f2 = -1;
|
||||
A(p) : f = apply((j) => 2 - j, p) {
|
||||
/* constr. body */
|
||||
f2 = -p;
|
||||
}
|
||||
A.n(p) : f = 1 + apply((j) => 2 - j, p) {
|
||||
/* constr. body */
|
||||
f2 = -f;
|
||||
}
|
||||
}
|
||||
|
||||
class B {
|
||||
var f;
|
||||
int n = -1;
|
||||
B(z) : f = ((x) => x * x) {
|
||||
n = z;
|
||||
}
|
||||
B.withZ(z)
|
||||
: f = ((x) {
|
||||
return x * x + 1;
|
||||
}) {
|
||||
n = z;
|
||||
}
|
||||
}
|
||||
|
||||
class C {
|
||||
String s;
|
||||
C(x) : s = "2*x is ${() { return 2*x; }()}";
|
||||
}
|
||||
|
||||
main() {
|
||||
FunctionLiteralsTest.testMain();
|
||||
}
|
112
tests/language/function/literals_test.dart
Normal file
112
tests/language/function/literals_test.dart
Normal file
|
@ -0,0 +1,112 @@
|
|||
// 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";
|
||||
|
||||
/**
|
||||
* Test various forms of function literals.
|
||||
*/
|
||||
typedef int IntFunc(int);
|
||||
|
||||
class FunctionLiteralsTest {
|
||||
static void checkIntFunction<T>(expected, int f(T x), arg) {
|
||||
Expect.equals(expected, f(arg));
|
||||
}
|
||||
|
||||
static void checkIntFuncFunction<T>(expected, IntFunc f(T x), arg) {
|
||||
Expect.equals(expected, f(arg)(arg));
|
||||
}
|
||||
|
||||
int func1(int x) => x;
|
||||
|
||||
int func2(x) => x;
|
||||
|
||||
int func3(int x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
int func4(x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
FunctionLiteralsTest() {}
|
||||
|
||||
static void testMain() {
|
||||
var test = new FunctionLiteralsTest();
|
||||
test.testArrow();
|
||||
test.testArrowArrow();
|
||||
test.testArrowBlock();
|
||||
test.testBlock();
|
||||
test.testBlockArrow();
|
||||
test.testBlockBlock();
|
||||
test.testFunctionRef();
|
||||
}
|
||||
|
||||
void testArrow() {
|
||||
checkIntFunction(42, (x) => x as int, 42);
|
||||
checkIntFunction(42, (dynamic x) => x, 42);
|
||||
}
|
||||
|
||||
void testArrowArrow() {
|
||||
checkIntFuncFunction(84, (x) => (y) => (x as int) + (y as int), 42);
|
||||
checkIntFuncFunction(84, (dynamic x) => (y) => x + y, 42);
|
||||
}
|
||||
|
||||
void testArrowBlock() {
|
||||
checkIntFuncFunction(
|
||||
84,
|
||||
(x) => (y) {
|
||||
return (x as int) + (y as int);
|
||||
},
|
||||
42);
|
||||
checkIntFuncFunction(
|
||||
84,
|
||||
(int x) => (y) {
|
||||
return (x + y) as int;
|
||||
},
|
||||
42);
|
||||
}
|
||||
|
||||
void testBlock() {
|
||||
checkIntFunction(42, (x) {
|
||||
return x as int;
|
||||
}, 42);
|
||||
checkIntFunction(42, (int x) {
|
||||
return x;
|
||||
}, 42);
|
||||
}
|
||||
|
||||
void testBlockArrow() {
|
||||
checkIntFuncFunction(84, (x) {
|
||||
return (y) => (x as int) + (y as int);
|
||||
}, 42);
|
||||
checkIntFuncFunction(84, (int x) {
|
||||
return (y) => (x + y) as int;
|
||||
}, 42);
|
||||
}
|
||||
|
||||
void testBlockBlock() {
|
||||
checkIntFuncFunction(84, (x) {
|
||||
return (y) {
|
||||
return (x as int) + (y as int);
|
||||
};
|
||||
}, 42);
|
||||
checkIntFuncFunction(84, (int x) {
|
||||
return (y) {
|
||||
return (x + y) as int;
|
||||
};
|
||||
}, 42);
|
||||
}
|
||||
|
||||
void testFunctionRef() {
|
||||
checkIntFunction(42, func1, 42);
|
||||
checkIntFunction(42, func2, 42);
|
||||
checkIntFunction(42, func3, 42);
|
||||
checkIntFunction(42, func4, 42);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
FunctionLiteralsTest.testMain();
|
||||
}
|
45
tests/language/function/local2_test.dart
Normal file
45
tests/language/function/local2_test.dart
Normal file
|
@ -0,0 +1,45 @@
|
|||
// 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 program testing closures.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef T F<T>(T t);
|
||||
|
||||
class Parameterized<T> {
|
||||
Parameterized() {}
|
||||
T mul3(F<T> f, T t) {
|
||||
return (3 as dynamic) * f(t);
|
||||
}
|
||||
|
||||
T test(T t) {
|
||||
return mul3((T t) {
|
||||
return (3 as dynamic) * t;
|
||||
}, t);
|
||||
}
|
||||
}
|
||||
|
||||
class LocalFunction2Test {
|
||||
static int f(int n) {
|
||||
int a = 0;
|
||||
var g = (int n) {
|
||||
a += n;
|
||||
return a;
|
||||
};
|
||||
var h = (int n) {
|
||||
a += 10 * n;
|
||||
return a;
|
||||
};
|
||||
return g(n) + h(n);
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
Expect.equals(3 + 33, f(3));
|
||||
Expect.equals(9.0, new Parameterized<double>().test(1.0));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
LocalFunction2Test.testMain();
|
||||
}
|
39
tests/language/function/local3_test.dart
Normal file
39
tests/language/function/local3_test.dart
Normal file
|
@ -0,0 +1,39 @@
|
|||
// 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 program testing closures.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class LocalFunction3Test {
|
||||
static testExceptions() {
|
||||
dynamic f = (int n) {
|
||||
return n + 1;
|
||||
};
|
||||
Expect.equals(true, f is Object);
|
||||
bool exception_caught = false;
|
||||
try {
|
||||
f.xyz(0);
|
||||
} on NoSuchMethodError {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
exception_caught = false;
|
||||
String f_string = "";
|
||||
try {
|
||||
f_string = f.toString();
|
||||
} on NoSuchMethodError {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(false, exception_caught);
|
||||
Expect.equals(true, f_string.startsWith("Closure"));
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
testExceptions();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
LocalFunction3Test.testMain();
|
||||
}
|
227
tests/language/function/local_function_test.dart
Normal file
227
tests/language/function/local_function_test.dart
Normal file
|
@ -0,0 +1,227 @@
|
|||
// 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 program testing closures.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class LocalFunctionTest {
|
||||
LocalFunctionTest()
|
||||
: field1 = 100,
|
||||
field2_ = 200 {}
|
||||
static int f(int n) {
|
||||
int a = 0;
|
||||
g(int m) {
|
||||
a = 3 * n + m + 1; // Capture parameter n and local a.
|
||||
return a;
|
||||
}
|
||||
|
||||
var b = g(n);
|
||||
return a + b;
|
||||
}
|
||||
|
||||
static int h(int n) {
|
||||
k(int n) {
|
||||
var a = new List<dynamic>.filled(n, null);
|
||||
var b = new List<dynamic>.filled(n, null);
|
||||
int i;
|
||||
for (i = 0; i < n; i++) {
|
||||
var j = i;
|
||||
a[i] = () => i; // Captured i is always n.
|
||||
b[i] = () => j; // Captured j varies from 0 to n-1.
|
||||
}
|
||||
var a_sum = 0;
|
||||
var b_sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
a_sum += a[i]() as int;
|
||||
b_sum += b[i]() as int;
|
||||
}
|
||||
return a_sum + b_sum;
|
||||
}
|
||||
|
||||
return k(n);
|
||||
}
|
||||
|
||||
static int h2(int n) {
|
||||
k(int n) {
|
||||
var a = new List<dynamic>.filled(n, null);
|
||||
var b = new List<dynamic>.filled(n, null);
|
||||
for (int i = 0; i < n; i++) {
|
||||
var j = i;
|
||||
a[i] = () => i; // Captured i varies from 0 to n-1.
|
||||
b[i] = () => j; // Captured j varies from 0 to n-1.
|
||||
}
|
||||
var a_sum = 0;
|
||||
var b_sum = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
a_sum += a[i]() as int;
|
||||
b_sum += b[i]() as int;
|
||||
}
|
||||
return a_sum + b_sum;
|
||||
}
|
||||
|
||||
return k(n);
|
||||
}
|
||||
|
||||
int field1;
|
||||
int field2_;
|
||||
int get field2 {
|
||||
return field2_;
|
||||
}
|
||||
|
||||
void set field2(int value) {
|
||||
field2_ = value;
|
||||
}
|
||||
|
||||
int method(int n) {
|
||||
incField1() {
|
||||
field1++;
|
||||
}
|
||||
|
||||
incField2() {
|
||||
field2++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
incField1();
|
||||
incField2();
|
||||
}
|
||||
return field1 + field2;
|
||||
}
|
||||
|
||||
int execute(int times, apply(int x)) {
|
||||
for (int i = 0; i < times; i++) {
|
||||
apply(i);
|
||||
}
|
||||
return field1;
|
||||
}
|
||||
|
||||
int testExecute(int n) {
|
||||
execute(n, (int x) {
|
||||
field1 += x;
|
||||
});
|
||||
return field1;
|
||||
}
|
||||
|
||||
static int foo(int n) {
|
||||
return -100; // Wrong foo.
|
||||
}
|
||||
|
||||
static testSelfReference1(int n) {
|
||||
int foo(int n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1 + foo(n - 1); // Local foo, not static foo.
|
||||
}
|
||||
}
|
||||
|
||||
;
|
||||
return foo(n); // Local foo, not static foo.
|
||||
}
|
||||
|
||||
static void hep(Function f) {
|
||||
f();
|
||||
}
|
||||
|
||||
static testNesting(int n) {
|
||||
var a = new List<dynamic>.filled(n * n, null);
|
||||
f0() {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int vi = i;
|
||||
f1() {
|
||||
for (int j = 0; j < n; j++) {
|
||||
int vj = j;
|
||||
a[i * n + j] = () => vi * n + vj;
|
||||
}
|
||||
}
|
||||
|
||||
f1();
|
||||
}
|
||||
}
|
||||
|
||||
f0();
|
||||
int result = 0;
|
||||
for (int k = 0; k < n * n; k++) {
|
||||
Expect.equals(k, a[k]());
|
||||
result += a[k]() as int;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static var field5;
|
||||
static var set_field5_func;
|
||||
static testClosureCallStatement(int x) {
|
||||
LocalFunctionTest.set_field5_func = (int n) {
|
||||
field5 = n * n;
|
||||
};
|
||||
(LocalFunctionTest.set_field5_func)(x);
|
||||
Expect.equals(x * x, LocalFunctionTest.field5);
|
||||
return true;
|
||||
}
|
||||
|
||||
static testExceptions() {
|
||||
dynamic f = (int n) => n + 1;
|
||||
Expect.equals(2, f(1));
|
||||
Expect.equals(true, f is Function);
|
||||
Expect.equals(true, f is Object);
|
||||
Expect.equals(true, f.toString().startsWith("Closure"));
|
||||
bool exception_caught = false;
|
||||
try {
|
||||
f(1, 2);
|
||||
} on NoSuchMethodError catch (e) {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
exception_caught = false;
|
||||
try {
|
||||
f();
|
||||
} on NoSuchMethodError catch (e) {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
exception_caught = false;
|
||||
try {
|
||||
f.xyz(0);
|
||||
} on NoSuchMethodError catch (e) {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
|
||||
// Overwrite closure value.
|
||||
f = 3;
|
||||
exception_caught = false;
|
||||
try {
|
||||
f(1);
|
||||
} on NoSuchMethodError catch (e) {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
|
||||
// Do not expect any exceptions to be thrown.
|
||||
var g = ([int n = 1]) => n + 1;
|
||||
Expect.equals(2, g());
|
||||
Expect.equals(3, g(2));
|
||||
}
|
||||
|
||||
static int doThis(int n, int f(int n)) {
|
||||
return f(n);
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
Expect.equals(2 * (3 * 2 + 2 + 1), f(2));
|
||||
Expect.equals(10 * 10 + 10 * 9 / 2, h(10));
|
||||
Expect.equals(90, h2(10));
|
||||
Expect.equals(320, new LocalFunctionTest().method(10));
|
||||
Expect.equals(145, new LocalFunctionTest().testExecute(10));
|
||||
Expect.equals(5, testSelfReference1(5));
|
||||
Expect.equals(24 * 25 / 2, testNesting(5));
|
||||
Expect.equals(true, testClosureCallStatement(7));
|
||||
Expect.equals(99, doThis(10, (n) => n * n - 1));
|
||||
testExceptions();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
LocalFunctionTest.testMain();
|
||||
}
|
33
tests/language/function/local_non_equal_test.dart
Normal file
33
tests/language/function/local_non_equal_test.dart
Normal file
|
@ -0,0 +1,33 @@
|
|||
// 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";
|
||||
|
||||
foo() => () => 42;
|
||||
bar() {
|
||||
var c = () => 54;
|
||||
return c;
|
||||
}
|
||||
|
||||
baz() {
|
||||
c() => 68;
|
||||
return c;
|
||||
}
|
||||
|
||||
main() {
|
||||
var first = foo();
|
||||
var second = foo();
|
||||
Expect.isFalse(identical(first, second));
|
||||
Expect.notEquals(first, second);
|
||||
|
||||
first = bar();
|
||||
second = bar();
|
||||
Expect.isFalse(identical(first, second));
|
||||
Expect.notEquals(first, second);
|
||||
|
||||
first = baz();
|
||||
second = baz();
|
||||
Expect.isFalse(identical(first, second));
|
||||
Expect.notEquals(first, second);
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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 for a function with a malformed result type.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class C<T, U> {}
|
||||
|
||||
main() {
|
||||
|
||||
}
|
16
tests/language/function/malformed_result_type_test.dart
Normal file
16
tests/language/function/malformed_result_type_test.dart
Normal file
|
@ -0,0 +1,16 @@
|
|||
// 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 for a function with a malformed result type.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class C<T, U> {}
|
||||
|
||||
main() {
|
||||
C<int> f() => throw "uncalled";
|
||||
//^^^^^^
|
||||
// [analyzer] STATIC_TYPE_WARNING.WRONG_NUMBER_OF_TYPE_ARGUMENTS
|
||||
// [cfe] Expected 2 type arguments.
|
||||
}
|
23
tests/language/function/propagation_test.dart
Normal file
23
tests/language/function/propagation_test.dart
Normal file
|
@ -0,0 +1,23 @@
|
|||
// 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";
|
||||
|
||||
class A {
|
||||
int call(String str) => 499;
|
||||
}
|
||||
|
||||
typedef int F(String str);
|
||||
|
||||
main() {
|
||||
var a = new A();
|
||||
Expect.type<A>(a);
|
||||
Expect.notType<F>(a);
|
||||
|
||||
Function a3 = new A();
|
||||
Expect.notType<A>(a3);
|
||||
|
||||
F a4 = new A();
|
||||
Expect.notType<A>(a4);
|
||||
}
|
733
tests/language/function/syntax_test.dart
Normal file
733
tests/language/function/syntax_test.dart
Normal file
|
@ -0,0 +1,733 @@
|
|||
// 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";
|
||||
|
||||
// Tests function statement and expression syntax.
|
||||
|
||||
class FunctionSyntaxTest {
|
||||
static void testMain
|
||||
/* //# 00: syntax error
|
||||
()
|
||||
*/ //# 00: continued
|
||||
{
|
||||
testNestedFunctions();
|
||||
testFunctionExpressions();
|
||||
testPrecedence();
|
||||
testInitializers();
|
||||
testFunctionParameter();
|
||||
testFunctionIdentifierExpression();
|
||||
testFunctionIdentifierStatement();
|
||||
}
|
||||
|
||||
static void testNestedFunctions
|
||||
/* //# 01: syntax error
|
||||
()
|
||||
*/ //# 01: continued
|
||||
{
|
||||
// No types - braces.
|
||||
nb0
|
||||
/* //# 02: syntax error
|
||||
()
|
||||
*/ //# 02: continued
|
||||
{
|
||||
return 42;
|
||||
}
|
||||
|
||||
nb1
|
||||
/* //# 03: syntax error
|
||||
(a)
|
||||
*/ //# 03: continued
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
nb2
|
||||
/* //# 04: syntax error
|
||||
(a, b)
|
||||
*/ //# 04: continued
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
Expect.equals(42, nb0());
|
||||
Expect.equals(87, nb1(87));
|
||||
Expect.equals(1 + 2, nb2(1, 2));
|
||||
|
||||
// No types - arrows.
|
||||
na0
|
||||
/* //# 05: syntax error
|
||||
()
|
||||
*/ //# 05: continued
|
||||
=>
|
||||
42;
|
||||
na1
|
||||
/* //# 06: syntax error
|
||||
(a)
|
||||
*/ //# 06: continued
|
||||
=>
|
||||
a;
|
||||
na2
|
||||
/* //# 07: syntax error
|
||||
(a, b)
|
||||
*/ //# 07: continued
|
||||
=>
|
||||
a + b;
|
||||
Expect.equals(42, na0());
|
||||
Expect.equals(87, na1(87));
|
||||
Expect.equals(1 + 2, na2(1, 2));
|
||||
|
||||
// Return type - braces.
|
||||
int rb0
|
||||
/* //# 08: syntax error
|
||||
()
|
||||
*/ //# 08: continued
|
||||
{
|
||||
return 42;
|
||||
}
|
||||
|
||||
int rb1
|
||||
/* //# 09: syntax error
|
||||
(a)
|
||||
*/ //# 09: continued
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
int rb2
|
||||
/* //# 10: syntax error
|
||||
(a, b)
|
||||
*/ //# 10: continued
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
Expect.equals(42, rb0());
|
||||
Expect.equals(87, rb1(87));
|
||||
Expect.equals(1 + 2, rb2(1, 2));
|
||||
|
||||
// Return type - arrows.
|
||||
int ra0
|
||||
/* //# 11: syntax error
|
||||
()
|
||||
*/ //# 11: continued
|
||||
=>
|
||||
42;
|
||||
int ra1
|
||||
/* //# 12: syntax error
|
||||
(a)
|
||||
*/ //# 12: continued
|
||||
=>
|
||||
a;
|
||||
int ra2
|
||||
/* //# 13: syntax error
|
||||
(a, b)
|
||||
*/ //# 13: continued
|
||||
=>
|
||||
a + b;
|
||||
Expect.equals(42, ra0());
|
||||
Expect.equals(87, ra1(87));
|
||||
Expect.equals(1 + 2, ra2(1, 2));
|
||||
|
||||
// Fully typed - braces.
|
||||
int fb1
|
||||
/* //# 14: syntax error
|
||||
(int a)
|
||||
*/ //# 14: continued
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
int fb2
|
||||
/* //# 15: syntax error
|
||||
(int a, int b)
|
||||
*/ //# 15: continued
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
Expect.equals(42, rb0());
|
||||
Expect.equals(87, rb1(87));
|
||||
Expect.equals(1 + 2, rb2(1, 2));
|
||||
|
||||
// Fully typed - arrows.
|
||||
int fa1
|
||||
/* //# 16: syntax error
|
||||
(int a)
|
||||
*/ //# 16: continued
|
||||
=>
|
||||
a;
|
||||
int fa2
|
||||
/* //# 17: syntax error
|
||||
(int a, int b)
|
||||
*/ //# 17: continued
|
||||
=>
|
||||
a + b;
|
||||
Expect.equals(42, ra0());
|
||||
Expect.equals(87, ra1(87));
|
||||
Expect.equals(1 + 2, ra2(1, 2));
|
||||
|
||||
// Generic types - braces.
|
||||
List<int> gb0
|
||||
/* //# 18: syntax error
|
||||
()
|
||||
*/ //# 18: continued
|
||||
{
|
||||
return [42];
|
||||
}
|
||||
|
||||
List<int> gb1
|
||||
/* //# 19: syntax error
|
||||
(List<int> a)
|
||||
*/ //# 19: continued
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
Expect.equals(42, gb0()[0]);
|
||||
Expect.equals(87, gb1([87])[0]);
|
||||
|
||||
// Generic types - arrows.
|
||||
List<int> ga0
|
||||
/* //# 20: syntax error
|
||||
()
|
||||
*/ //# 20: continued
|
||||
=>
|
||||
[42];
|
||||
List<int> ga1
|
||||
/* //# 21: syntax error
|
||||
(List<int> a)
|
||||
*/ //# 21: continued
|
||||
=>
|
||||
a;
|
||||
Expect.equals(42, ga0()[0]);
|
||||
Expect.equals(87, ga1([87])[0]);
|
||||
}
|
||||
|
||||
static void testFunctionExpressions
|
||||
/* //# 22: syntax error
|
||||
()
|
||||
*/ //# 22: continued
|
||||
{
|
||||
eval0
|
||||
/* //# 23: syntax error
|
||||
(fn)
|
||||
*/ //# 23: continued
|
||||
=>
|
||||
fn();
|
||||
eval1
|
||||
/* //# 24: syntax error
|
||||
(fn, a)
|
||||
*/ //# 24: continued
|
||||
=>
|
||||
fn(a);
|
||||
eval2
|
||||
/* //# 25: syntax error
|
||||
(fn, a, b)
|
||||
*/ //# 25: continued
|
||||
=>
|
||||
fn(a, b);
|
||||
|
||||
// No types - braces.
|
||||
Expect.equals(42, eval0(
|
||||
/* //# 26: syntax error
|
||||
()
|
||||
*/ //# 26: continued
|
||||
{
|
||||
return 42;
|
||||
}));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 27: syntax error
|
||||
(a)
|
||||
*/ //# 27: continued
|
||||
{
|
||||
return a;
|
||||
}, 87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 28: syntax error
|
||||
(a, b)
|
||||
*/ //# 28: continued
|
||||
{
|
||||
return a + b;
|
||||
}, 1, 2));
|
||||
Expect.equals(42, eval0(
|
||||
/* //# 29: syntax error
|
||||
()
|
||||
*/ //# 29: continued
|
||||
{
|
||||
return 42;
|
||||
}));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 30: syntax error
|
||||
(a)
|
||||
*/ //# 30: continued
|
||||
{
|
||||
return a;
|
||||
}, 87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 31: syntax error
|
||||
(a, b)
|
||||
*/ //# 31: continued
|
||||
{
|
||||
return a + b;
|
||||
}, 1, 2));
|
||||
|
||||
// No types - arrows.
|
||||
Expect.equals(
|
||||
42,
|
||||
eval0(
|
||||
/* //# 32: syntax error
|
||||
()
|
||||
*/ //# 32: continued
|
||||
=>
|
||||
42));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 33: syntax error
|
||||
(a)
|
||||
*/ //# 33: continued
|
||||
=>
|
||||
a,
|
||||
87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 34: syntax error
|
||||
(a, b)
|
||||
*/ //# 34: continued
|
||||
=>
|
||||
a + b,
|
||||
1,
|
||||
2));
|
||||
Expect.equals(
|
||||
42,
|
||||
eval0(
|
||||
/* //# 35: syntax error
|
||||
()
|
||||
*/ //# 35: continued
|
||||
=>
|
||||
42));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 36: syntax error
|
||||
(a)
|
||||
*/ //# 36: continued
|
||||
=>
|
||||
a,
|
||||
87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 37: syntax error
|
||||
(a, b)
|
||||
*/ //# 37: continued
|
||||
=>
|
||||
a + b,
|
||||
1,
|
||||
2));
|
||||
|
||||
// Argument types - braces.
|
||||
Expect.equals(42, eval0(
|
||||
/* //# 44: syntax error
|
||||
()
|
||||
*/ //# 44: continued
|
||||
{
|
||||
return 42;
|
||||
}));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 45: syntax error
|
||||
(int a)
|
||||
*/ //# 45: continued
|
||||
{
|
||||
return a;
|
||||
}, 87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 46: syntax error
|
||||
(int a, int b)
|
||||
*/ //# 46: continued
|
||||
{
|
||||
return a + b;
|
||||
}, 1, 2));
|
||||
Expect.equals(42, eval0(
|
||||
/* //# 47: syntax error
|
||||
()
|
||||
*/ //# 47: continued
|
||||
{
|
||||
return 42;
|
||||
}));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 48: syntax error
|
||||
(int a)
|
||||
*/ //# 48: continued
|
||||
{
|
||||
return a;
|
||||
}, 87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 49: syntax error
|
||||
(int a, int b)
|
||||
*/ //# 49: continued
|
||||
{
|
||||
return a + b;
|
||||
}, 1, 2));
|
||||
|
||||
// Argument types - arrows.
|
||||
Expect.equals(
|
||||
42,
|
||||
eval0(
|
||||
/* //# 50: syntax error
|
||||
()
|
||||
*/ //# 50: continued
|
||||
=>
|
||||
42));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 51: syntax error
|
||||
(int a)
|
||||
*/ //# 51: continued
|
||||
=>
|
||||
a,
|
||||
87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 52: syntax error
|
||||
(int a, int b)
|
||||
*/ //# 52: continued
|
||||
=>
|
||||
a + b,
|
||||
1,
|
||||
2));
|
||||
Expect.equals(
|
||||
42,
|
||||
eval0(
|
||||
/* //# 53: syntax error
|
||||
()
|
||||
*/ //# 53: continued
|
||||
=>
|
||||
42));
|
||||
Expect.equals(
|
||||
87,
|
||||
eval1(
|
||||
/* //# 54: syntax error
|
||||
(int a)
|
||||
*/ //# 54: continued
|
||||
=>
|
||||
a,
|
||||
87));
|
||||
Expect.equals(
|
||||
1 + 2,
|
||||
eval2(
|
||||
/* //# 55: syntax error
|
||||
(int a, int b)
|
||||
*/ //# 55: continued
|
||||
=>
|
||||
a + b,
|
||||
1,
|
||||
2));
|
||||
}
|
||||
|
||||
static void testPrecedence
|
||||
/* //# 64: syntax error
|
||||
()
|
||||
*/ //# 64: continued
|
||||
{
|
||||
expectEvaluatesTo
|
||||
/* //# 65: syntax error
|
||||
(value, fn)
|
||||
*/ //# 65: continued
|
||||
{
|
||||
Expect.equals(value, fn());
|
||||
}
|
||||
|
||||
// Assignment.
|
||||
var x;
|
||||
expectEvaluatesTo(42, () => x = 42);
|
||||
Expect.equals(42, x);
|
||||
x = 1;
|
||||
expectEvaluatesTo(100, () => x += 99);
|
||||
Expect.equals(100, x);
|
||||
x = 1;
|
||||
expectEvaluatesTo(87, () => x *= 87);
|
||||
Expect.equals(87, x);
|
||||
|
||||
// Conditional.
|
||||
expectEvaluatesTo(42, () => true ? 42 : 87);
|
||||
expectEvaluatesTo(87, () => false ? 42 : 87);
|
||||
|
||||
// Logical or.
|
||||
expectEvaluatesTo(true, () => true || true);
|
||||
expectEvaluatesTo(true, () => true || false);
|
||||
expectEvaluatesTo(true, () => false || true);
|
||||
expectEvaluatesTo(false, () => false || false);
|
||||
|
||||
// Logical and.
|
||||
expectEvaluatesTo(true, () => true && true);
|
||||
expectEvaluatesTo(false, () => true && false);
|
||||
expectEvaluatesTo(false, () => false && true);
|
||||
expectEvaluatesTo(false, () => false && false);
|
||||
|
||||
// Bitwise operations.
|
||||
expectEvaluatesTo(3, () => 1 | 2);
|
||||
expectEvaluatesTo(2, () => 3 ^ 1);
|
||||
expectEvaluatesTo(1, () => 3 & 1);
|
||||
|
||||
// Equality.
|
||||
expectEvaluatesTo(true, () => 1 == 1);
|
||||
expectEvaluatesTo(false, () => 1 != 1);
|
||||
expectEvaluatesTo(true, () => identical(1, 1));
|
||||
expectEvaluatesTo(false, () => !identical(1, 1));
|
||||
|
||||
// Relational.
|
||||
expectEvaluatesTo(true, () => 1 <= 1);
|
||||
expectEvaluatesTo(false, () => 1 < 1);
|
||||
expectEvaluatesTo(false, () => 1 > 1);
|
||||
expectEvaluatesTo(true, () => 1 >= 1);
|
||||
|
||||
// Is.
|
||||
expectEvaluatesTo(true, () => 1 is int);
|
||||
expectEvaluatesTo(true, () => 1.0 is double);
|
||||
|
||||
// Shift.
|
||||
expectEvaluatesTo(2, () => 1 << 1);
|
||||
expectEvaluatesTo(1, () => 2 >> 1);
|
||||
|
||||
// Additive.
|
||||
expectEvaluatesTo(2, () => 1 + 1);
|
||||
expectEvaluatesTo(1, () => 2 - 1);
|
||||
|
||||
// Multiplicative.
|
||||
expectEvaluatesTo(2, () => 1 * 2);
|
||||
expectEvaluatesTo(2.0, () => 4 / 2);
|
||||
expectEvaluatesTo(2, () => 4 ~/ 2);
|
||||
expectEvaluatesTo(0, () => 4 % 2);
|
||||
|
||||
// Negate.
|
||||
expectEvaluatesTo(false, () => !true);
|
||||
|
||||
// Postfix / prefix.
|
||||
var y = 0;
|
||||
expectEvaluatesTo(0, () => y++);
|
||||
expectEvaluatesTo(2, () => ++y);
|
||||
expectEvaluatesTo(1, () => --y);
|
||||
expectEvaluatesTo(1, () => y--);
|
||||
Expect.equals(0, y);
|
||||
|
||||
// Selector.
|
||||
fn
|
||||
/* //# 66: syntax error
|
||||
()
|
||||
*/ //# 66: continued
|
||||
=>
|
||||
42;
|
||||
var list = [87];
|
||||
expectEvaluatesTo(42, () => fn());
|
||||
expectEvaluatesTo(1, () => list.length);
|
||||
expectEvaluatesTo(87, () => list[0]);
|
||||
expectEvaluatesTo(87, () => list.removeLast());
|
||||
}
|
||||
|
||||
static void testInitializers
|
||||
/* //# 67: syntax error
|
||||
()
|
||||
*/ //# 67: continued
|
||||
{
|
||||
Expect.equals(42, (new C.cb0().fn)());
|
||||
Expect.equals(43, (new C.ca0().fn)());
|
||||
Expect.equals(44, (new C.cb1().fn)());
|
||||
Expect.equals(45, (new C.ca1().fn)());
|
||||
Expect.equals(46, (new C.cb2().fn)());
|
||||
Expect.equals(47, (new C.ca2().fn)());
|
||||
Expect.equals(48, (new C.cb3().fn)());
|
||||
Expect.equals(49, (new C.ca3().fn)());
|
||||
|
||||
Expect.equals(52, (new C.nb0().fn)());
|
||||
Expect.equals(53, (new C.na0().fn)());
|
||||
Expect.equals(54, (new C.nb1().fn)());
|
||||
Expect.equals(55, (new C.na1().fn)());
|
||||
Expect.equals(56, (new C.nb2().fn)());
|
||||
Expect.equals(57, (new C.na2().fn)());
|
||||
Expect.equals(58, (new C.nb3().fn)());
|
||||
Expect.equals(59, (new C.na3().fn)());
|
||||
|
||||
Expect.equals(62, (new C.rb0().fn)());
|
||||
Expect.equals(63, (new C.ra0().fn)());
|
||||
Expect.equals(64, (new C.rb1().fn)());
|
||||
Expect.equals(65, (new C.ra1().fn)());
|
||||
Expect.equals(66, (new C.rb2().fn)());
|
||||
Expect.equals(67, (new C.ra2().fn)());
|
||||
Expect.equals(68, (new C.rb3().fn)());
|
||||
Expect.equals(69, (new C.ra3().fn)());
|
||||
}
|
||||
|
||||
static void testFunctionParameter
|
||||
/* //# 68: syntax error
|
||||
()
|
||||
*/ //# 68: continued
|
||||
{
|
||||
f0(fn()) => fn();
|
||||
Expect.equals(42, f0(() => 42));
|
||||
|
||||
f1(int fn()) => fn();
|
||||
Expect.equals(87, f1(() => 87));
|
||||
|
||||
f2(fn(a)) => fn(42);
|
||||
Expect.equals(43, f2((a) => a + 1));
|
||||
|
||||
f3(fn(int a)) => fn(42);
|
||||
Expect.equals(44, f3((int a) => a + 2));
|
||||
}
|
||||
|
||||
static void testFunctionIdentifierExpression
|
||||
/* //# 69: syntax error
|
||||
()
|
||||
*/ //# 69: continued
|
||||
{
|
||||
Expect.equals(
|
||||
87,
|
||||
(
|
||||
/* //# 70: syntax error
|
||||
()
|
||||
*/ //# 70: continued
|
||||
=>
|
||||
87)());
|
||||
}
|
||||
|
||||
static void testFunctionIdentifierStatement
|
||||
/* //# 71: syntax error
|
||||
()
|
||||
*/ //# 71: continued
|
||||
{
|
||||
function
|
||||
/* //# 72: syntax error
|
||||
()
|
||||
*/ //# 72: continued
|
||||
=>
|
||||
42;
|
||||
Expect.equals(42, function());
|
||||
Expect.equals(true, function is Function);
|
||||
}
|
||||
}
|
||||
|
||||
class C {
|
||||
C.cb0()
|
||||
: fn = (() {
|
||||
return 42;
|
||||
}) {}
|
||||
C.ca0() : fn = (() => 43) {}
|
||||
|
||||
C.cb1()
|
||||
: fn = wrap(() {
|
||||
return 44;
|
||||
}) {}
|
||||
C.ca1() : fn = wrap(() => 45) {}
|
||||
|
||||
C.cb2()
|
||||
: fn = [
|
||||
() {
|
||||
return 46;
|
||||
}
|
||||
][0] {}
|
||||
C.ca2() : fn = [() => 47][0] {}
|
||||
|
||||
C.cb3()
|
||||
: fn = {
|
||||
'x': () {
|
||||
return 48;
|
||||
}
|
||||
}['x'] {}
|
||||
C.ca3() : fn = {'x': () => 49}['x'] {}
|
||||
|
||||
C.nb0()
|
||||
: fn = (() {
|
||||
return 52;
|
||||
}) {}
|
||||
C.na0() : fn = (() => 53) {}
|
||||
|
||||
C.nb1()
|
||||
: fn = wrap(() {
|
||||
return 54;
|
||||
}) {}
|
||||
C.na1() : fn = wrap(() => 55) {}
|
||||
|
||||
C.nb2()
|
||||
: fn = [
|
||||
() {
|
||||
return 56;
|
||||
}
|
||||
][0] {}
|
||||
C.na2() : fn = [() => 57][0] {}
|
||||
|
||||
C.nb3()
|
||||
: fn = {
|
||||
'x': () {
|
||||
return 58;
|
||||
}
|
||||
}['x'] {}
|
||||
C.na3() : fn = {'x': () => 59}['x'] {}
|
||||
|
||||
C.rb0()
|
||||
: fn = (() {
|
||||
return 62;
|
||||
}) {}
|
||||
C.ra0() : fn = (() => 63) {}
|
||||
|
||||
C.rb1()
|
||||
: fn = wrap(() {
|
||||
return 64;
|
||||
}) {}
|
||||
C.ra1() : fn = wrap(() => 65) {}
|
||||
|
||||
C.rb2()
|
||||
: fn = [
|
||||
() {
|
||||
return 66;
|
||||
}
|
||||
][0] {}
|
||||
C.ra2() : fn = [() => 67][0] {}
|
||||
|
||||
C.rb3()
|
||||
: fn = {
|
||||
'x': () {
|
||||
return 68;
|
||||
}
|
||||
}['x'] {}
|
||||
C.ra3() : fn = {'x': () => 69}['x'] {}
|
||||
|
||||
static wrap
|
||||
/* //# 73: syntax error
|
||||
(fn)
|
||||
*/ //# 73: continued
|
||||
{
|
||||
return fn;
|
||||
}
|
||||
|
||||
final fn;
|
||||
}
|
||||
|
||||
main
|
||||
/* //# 74: syntax error
|
||||
()
|
||||
*/ //# 74: continued
|
||||
{
|
||||
FunctionSyntaxTest.testMain();
|
||||
}
|
20
tests/language/function/type2_test.dart
Normal file
20
tests/language/function/type2_test.dart
Normal 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";
|
||||
|
||||
class A<T> {
|
||||
A(f) {
|
||||
f(42);
|
||||
}
|
||||
}
|
||||
|
||||
class B<T> extends A<T> {
|
||||
B() : super((T param) => 42);
|
||||
}
|
||||
|
||||
main() {
|
||||
var t = new B<int>();
|
||||
Expect.throwsTypeError(() => new B<String>());
|
||||
}
|
28
tests/language/function/type3_test.dart
Normal file
28
tests/language/function/type3_test.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
// 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";
|
||||
|
||||
class A<T> {
|
||||
@pragma('dart2js:noInline')
|
||||
A();
|
||||
|
||||
@pragma('dart2js:noInline')
|
||||
foo() => new B<T>();
|
||||
}
|
||||
|
||||
class B<T> {
|
||||
T bar() => throw "uncalled";
|
||||
}
|
||||
|
||||
typedef F();
|
||||
typedef F2(x);
|
||||
|
||||
// Dart2js realized that the generic type for A was not needed, but then used
|
||||
// it nevertheless when it constructed the closure.
|
||||
main() {
|
||||
var f = new A<int>().foo().bar;
|
||||
Expect.isTrue(f is F);
|
||||
Expect.isFalse(f is F2);
|
||||
}
|
23
tests/language/function/type_alias10_test.dart
Normal file
23
tests/language/function/type_alias10_test.dart
Normal file
|
@ -0,0 +1,23 @@
|
|||
// 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.
|
||||
|
||||
// Regression test for https://github.com/dart-lang/sdk/issues/30912.
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
class Foo {}
|
||||
|
||||
class Bar {}
|
||||
|
||||
typedef Type Func<S extends Foo, T>(T s);
|
||||
|
||||
class Baz<S extends Foo, T extends Bar> {
|
||||
Func<S, Bar>? func;
|
||||
}
|
||||
|
||||
void main() {
|
||||
dynamic baz = new Baz();
|
||||
Expect.isNull(baz.func);
|
||||
baz.func = (Bar b) => b.runtimeType;
|
||||
Expect.equals(baz.func(new Bar()), Bar);
|
||||
}
|
54
tests/language/function/type_alias2_test.dart
Normal file
54
tests/language/function/type_alias2_test.dart
Normal file
|
@ -0,0 +1,54 @@
|
|||
// 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.
|
||||
// VMOptions=--enable_type_checks
|
||||
|
||||
// Dart test for function type alias with optional parameters.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef int f1<T>([int? a, int? b, T? c]);
|
||||
typedef int f2<T>([int? a, int? b, T? d]);
|
||||
typedef int f3<T>({int? a, int? b, T? c});
|
||||
typedef int f4<T>({int? a, int? b, T? d});
|
||||
|
||||
class A<T> {
|
||||
int baz([int? a, int? b, T? c]) => -1;
|
||||
int bar({int? a, int? b, T? c}) => -1;
|
||||
}
|
||||
|
||||
int baz([int? a, int? b, int? c]) => -1;
|
||||
|
||||
int bar({int? a, int? b, int? c}) => -1;
|
||||
|
||||
main() {
|
||||
Expect.isFalse(baz is f1);
|
||||
Expect.isFalse(baz is f3);
|
||||
Expect.isFalse(bar is f1);
|
||||
Expect.isFalse(bar is f3);
|
||||
Expect.isFalse(baz is f1);
|
||||
Expect.isTrue(baz is f1<int>);
|
||||
Expect.isTrue(bar is f3<int>);
|
||||
Expect.isFalse(baz is f1<double>);
|
||||
Expect.isFalse(bar is f3<double>);
|
||||
Expect.isFalse(baz is f2);
|
||||
Expect.isFalse(bar is f4);
|
||||
Expect.isTrue(baz is f2<int>);
|
||||
Expect.isFalse(bar is f2<int>);
|
||||
|
||||
A<int> a = new A<int>();
|
||||
Expect.isTrue(a.baz is f1);
|
||||
Expect.isFalse(a.baz is f3);
|
||||
Expect.isFalse(a.bar is f1);
|
||||
Expect.isTrue(a.bar is f3);
|
||||
Expect.isTrue(a.baz is f1);
|
||||
Expect.isTrue(a.baz is f1<Object>);
|
||||
Expect.isTrue(a.bar is f3<Object>);
|
||||
Expect.isTrue(a.baz is f1<int>);
|
||||
Expect.isTrue(a.bar is f3<int>);
|
||||
Expect.isTrue(a.baz is f1<double>);
|
||||
Expect.isTrue(a.bar is f3<double>);
|
||||
Expect.isTrue(a.baz is f2);
|
||||
Expect.isFalse(a.bar is f4);
|
||||
Expect.isTrue(a.baz is f2<Object>);
|
||||
Expect.isFalse(a.bar is f2<Object>);
|
||||
}
|
26
tests/language/function/type_alias3_test.dart
Normal file
26
tests/language/function/type_alias3_test.dart
Normal 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.
|
||||
|
||||
// Dart test for function type alias with an imported result type that happens
|
||||
// to have the same name as a type parameter.
|
||||
import "package:expect/expect.dart";
|
||||
import "../library11.dart" as lib11;
|
||||
|
||||
typedef lib11.Library111<Library111> F<Library111>(
|
||||
lib11.Library111<Library111> a, Library111 b);
|
||||
|
||||
class A<T> {
|
||||
T foo(T a, bool b) => throw "uncalled";
|
||||
}
|
||||
|
||||
main() {
|
||||
var a = new A<lib11.Library111<bool>>();
|
||||
var b = new A<lib11.Library111<int>>();
|
||||
Expect.isTrue(a.foo is! F);
|
||||
Expect.isTrue(a.foo is F<bool>);
|
||||
Expect.isTrue(a.foo is! F<int>);
|
||||
Expect.isTrue(b.foo is! F);
|
||||
Expect.isTrue(b.foo is! F<bool>);
|
||||
Expect.isTrue(a.foo is! F<int>);
|
||||
}
|
40
tests/language/function/type_alias4_test.dart
Normal file
40
tests/language/function/type_alias4_test.dart
Normal file
|
@ -0,0 +1,40 @@
|
|||
// 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 function type alias with a type parameter as result type.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef bool F<bool>(bool a); // 'bool' is not the boolean type.
|
||||
|
||||
bool bar(bool a) => false;
|
||||
|
||||
int baz(int a) => -1;
|
||||
|
||||
class A<T> {
|
||||
T foo(T a) => throw "uncalled";
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.isTrue(bar is! F);
|
||||
Expect.isTrue(baz is! F);
|
||||
Expect.isTrue(bar is! F<Object>);
|
||||
Expect.isTrue(baz is! F<Object>);
|
||||
Expect.isTrue(bar is F<bool>);
|
||||
Expect.isTrue(baz is F<int>);
|
||||
Expect.isTrue(bar is! F<int>);
|
||||
Expect.isTrue(baz is! F<bool>);
|
||||
|
||||
var b = new A<bool>();
|
||||
var i = new A<int>();
|
||||
Expect.isTrue(b.foo is F, 'runtime type of covaraint parameters is Object');
|
||||
Expect.isTrue(i.foo is F, 'runtime type of covaraint parameters is Object');
|
||||
Expect.isTrue(
|
||||
b.foo is F<Object>, 'runtime type of covaraint parameters is Object');
|
||||
Expect.isTrue(
|
||||
i.foo is F<Object>, 'runtime type of covaraint parameters is Object');
|
||||
Expect.isTrue(b.foo is F<bool>);
|
||||
Expect.isTrue(i.foo is F<int>);
|
||||
Expect.isTrue(b.foo is! F<int>);
|
||||
Expect.isTrue(i.foo is! F<bool>);
|
||||
}
|
17
tests/language/function/type_alias5_test.dart
Normal file
17
tests/language/function/type_alias5_test.dart
Normal file
|
@ -0,0 +1,17 @@
|
|||
// 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 for illegally self referencing function type alias.
|
||||
|
||||
typedef Handle Handle(String command); //# 00: compile-time error
|
||||
|
||||
typedef F(F x); //# 01: compile-time error
|
||||
|
||||
typedef A(B x); //# 02: compile-time error
|
||||
typedef B(A x); //# 02: continued
|
||||
|
||||
main() {
|
||||
Handle h; //# 00: continued
|
||||
F f; //# 01: continued
|
||||
A f; //# 02: continued
|
||||
}
|
31
tests/language/function/type_alias6_runtime_test.dart
Normal file
31
tests/language/function/type_alias6_runtime_test.dart
Normal file
|
@ -0,0 +1,31 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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 for legally self referencing function type alias.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef F(
|
||||
List
|
||||
|
||||
x);
|
||||
|
||||
typedef D C();
|
||||
|
||||
class D {
|
||||
C foo() => throw "uncalled";
|
||||
D bar() => throw "uncalled";
|
||||
}
|
||||
|
||||
main() {
|
||||
var f = (List x) {};
|
||||
Expect.isTrue(f is F);
|
||||
var g = (List<F> x) {};
|
||||
Expect.isFalse(g is F);
|
||||
var d = new D();
|
||||
Expect.isTrue(d.foo is! C);
|
||||
Expect.isTrue(d.bar is C);
|
||||
}
|
29
tests/language/function/type_alias6_test.dart
Normal file
29
tests/language/function/type_alias6_test.dart
Normal file
|
@ -0,0 +1,29 @@
|
|||
// 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 for legally self referencing function type alias.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef F(List<F> x);
|
||||
// [error line 8, column 1, length 21]
|
||||
// [analyzer] COMPILE_TIME_ERROR.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF
|
||||
// ^
|
||||
// [cfe] The typedef 'F' has a reference to itself.
|
||||
|
||||
typedef D C();
|
||||
|
||||
class D {
|
||||
C foo() => throw "uncalled";
|
||||
D bar() => throw "uncalled";
|
||||
}
|
||||
|
||||
main() {
|
||||
var f = (List x) {};
|
||||
Expect.isTrue(f is F);
|
||||
var g = (List<F> x) {};
|
||||
Expect.isFalse(g is F);
|
||||
var d = new D();
|
||||
Expect.isTrue(d.foo is! C);
|
||||
Expect.isTrue(d.bar is C);
|
||||
}
|
19
tests/language/function/type_alias7_test.dart
Normal file
19
tests/language/function/type_alias7_test.dart
Normal file
|
@ -0,0 +1,19 @@
|
|||
// 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.
|
||||
|
||||
typedef void funcType([int arg]);
|
||||
|
||||
typedef void badFuncType([int arg = 0]); //# 00: compile-time error
|
||||
|
||||
typedef void badFuncType({int arg: 0}); //# 02: compile-time error
|
||||
|
||||
class A
|
||||
extends funcType // //# 01: compile-time error
|
||||
{}
|
||||
|
||||
main() {
|
||||
new A();
|
||||
badFuncType f; //# 00: continued
|
||||
badFuncType f; //# 02: continued
|
||||
}
|
19
tests/language/function/type_alias8_test.dart
Normal file
19
tests/language/function/type_alias8_test.dart
Normal file
|
@ -0,0 +1,19 @@
|
|||
// 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.
|
||||
|
||||
// Regression test for issue 9442.
|
||||
|
||||
typedef dynamic GetFromThing<T extends Thing>(T target);
|
||||
|
||||
typedef GetFromThing<T>? DefGetFromThing<T extends Thing>(dynamic def);
|
||||
|
||||
class Thing {}
|
||||
|
||||
class Test {
|
||||
static final DefGetFromThing<Thing> fromThing = (dynamic def) {};
|
||||
}
|
||||
|
||||
main() {
|
||||
Test.fromThing(10);
|
||||
}
|
18
tests/language/function/type_alias9_runtime_test.dart
Normal file
18
tests/language/function/type_alias9_runtime_test.dart
Normal file
|
@ -0,0 +1,18 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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 for legally self referencing function type alias.
|
||||
|
||||
typedef void F(
|
||||
List
|
||||
|
||||
l);
|
||||
typedef void G(List<F> l);
|
||||
|
||||
main() {
|
||||
F? foo(G? g) => g as F?;
|
||||
foo(null);
|
||||
}
|
18
tests/language/function/type_alias9_test.dart
Normal file
18
tests/language/function/type_alias9_test.dart
Normal file
|
@ -0,0 +1,18 @@
|
|||
// 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 for legally self referencing function type alias.
|
||||
|
||||
typedef void F(List<G> l);
|
||||
// [error line 6, column 1, length 26]
|
||||
// [analyzer] COMPILE_TIME_ERROR.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF
|
||||
// ^
|
||||
// [cfe] The typedef 'F' has a reference to itself.
|
||||
typedef void G(List<F> l);
|
||||
// [error line 11, column 1, length 26]
|
||||
// [analyzer] COMPILE_TIME_ERROR.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF
|
||||
|
||||
main() {
|
||||
F? foo(G? g) => g as F?;
|
||||
foo(null);
|
||||
}
|
125
tests/language/function/type_alias_test.dart
Normal file
125
tests/language/function/type_alias_test.dart
Normal file
|
@ -0,0 +1,125 @@
|
|||
// 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.
|
||||
// VMOptions=--enable_type_checks
|
||||
//
|
||||
// Dart test for function type alias.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef Fun(Never a, Never b);
|
||||
|
||||
typedef int IntFun(Never a, Never b);
|
||||
|
||||
typedef bool BoolFun(Never a, Never b);
|
||||
|
||||
typedef int CompareObj(Object a, Object b);
|
||||
|
||||
typedef int CompareInt(int a, int b);
|
||||
|
||||
typedef int CompareString(String a, String b, [bool swap]);
|
||||
|
||||
typedef void Test();
|
||||
|
||||
typedef ParameterizedFun1<T, U extends bool, V>(T t, U u);
|
||||
|
||||
typedef List<T> ParameterizedFun2<T, U, V extends Map<T, int>>(
|
||||
Map<T, int> t, U u);
|
||||
|
||||
typedef void BoundsCheck<T extends num>(T arg);
|
||||
|
||||
class FunctionTypeAliasTest {
|
||||
FunctionTypeAliasTest() {}
|
||||
static int test<T>(int compare(T a, T b), T a, T b) {
|
||||
return compare(a, b);
|
||||
}
|
||||
|
||||
foo(Test arg) {}
|
||||
static bar() {
|
||||
FunctionTypeAliasTest a = new FunctionTypeAliasTest();
|
||||
a.foo(() {});
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
int compareStrLen(String a, String b) {
|
||||
return a.length - b.length;
|
||||
}
|
||||
|
||||
Expect.isTrue(compareStrLen is Fun);
|
||||
Expect.isTrue(compareStrLen is IntFun);
|
||||
Expect.isTrue(compareStrLen is! BoolFun);
|
||||
Expect.isTrue(compareStrLen is! CompareObj);
|
||||
Expect.isTrue(compareStrLen is! CompareInt);
|
||||
Expect.isTrue(compareStrLen is! CompareString);
|
||||
Expect.equals(3, test(compareStrLen, "abcdef", "xyz"));
|
||||
|
||||
int compareStrLenSwap(String a, String b, [bool swap = false]) {
|
||||
return swap ? (a.length - b.length) : (b.length - a.length);
|
||||
}
|
||||
|
||||
Expect.isTrue(compareStrLenSwap is Fun);
|
||||
Expect.isTrue(compareStrLenSwap is IntFun);
|
||||
Expect.isTrue(compareStrLenSwap is! BoolFun);
|
||||
Expect.isTrue(compareStrLenSwap is! CompareObj);
|
||||
Expect.isTrue(compareStrLenSwap is! CompareInt);
|
||||
Expect.isTrue(compareStrLenSwap is CompareString);
|
||||
|
||||
int compareStrLenReverse(String a, String b, [bool reverse = false]) {
|
||||
return reverse ? (a.length - b.length) : (b.length - a.length);
|
||||
}
|
||||
|
||||
Expect.isTrue(compareStrLenReverse is Fun);
|
||||
Expect.isTrue(compareStrLenReverse is IntFun);
|
||||
Expect.isTrue(compareStrLenReverse is! BoolFun);
|
||||
Expect.isTrue(compareStrLenReverse is! CompareObj);
|
||||
Expect.isTrue(compareStrLenReverse is! CompareInt);
|
||||
Expect.isTrue(compareStrLenReverse is CompareString);
|
||||
|
||||
int compareObj(Object a, Object b) {
|
||||
return identical(a, b) ? 0 : -1;
|
||||
}
|
||||
|
||||
Expect.isTrue(compareObj is Fun);
|
||||
Expect.isTrue(compareObj is IntFun);
|
||||
Expect.isTrue(compareObj is! BoolFun);
|
||||
Expect.isTrue(compareObj is CompareObj);
|
||||
Expect.isTrue(compareObj is CompareInt);
|
||||
Expect.isTrue(compareObj is! CompareString);
|
||||
Expect.equals(-1, test(compareObj, "abcdef", "xyz"));
|
||||
|
||||
CompareInt minus = (int a, int b) {
|
||||
return a - b;
|
||||
};
|
||||
Expect.isTrue(minus is Fun);
|
||||
Expect.isTrue(minus is IntFun);
|
||||
Expect.isTrue(minus is! BoolFun);
|
||||
Expect.isTrue(minus is! CompareObj);
|
||||
Expect.isTrue(minus is CompareInt);
|
||||
Expect.isTrue(minus is! CompareString);
|
||||
Expect.equals(99, test(minus, 100, 1));
|
||||
|
||||
int plus(int a, [int b = 1]) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
;
|
||||
Expect.isTrue(plus is Fun);
|
||||
Expect.isTrue(plus is IntFun);
|
||||
Expect.isTrue(plus is! BoolFun);
|
||||
Expect.isTrue(plus is! CompareObj);
|
||||
Expect.isTrue(plus is CompareInt);
|
||||
Expect.isTrue(plus is! CompareString);
|
||||
|
||||
Expect.equals(0, bar());
|
||||
|
||||
Function boundsTrue = (num arg) {};
|
||||
Function boundsFalse = (String arg) {};
|
||||
Expect.isTrue(boundsTrue is BoundsCheck<int>);
|
||||
Expect.isFalse(boundsFalse is BoundsCheck<num>);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
FunctionTypeAliasTest.testMain();
|
||||
}
|
76
tests/language/function/type_call_getter2_runtime_test.dart
Normal file
76
tests/language/function/type_call_getter2_runtime_test.dart
Normal file
|
@ -0,0 +1,76 @@
|
|||
// TODO(multitest): This was automatically migrated from a multitest and may
|
||||
// contain strange or dead code.
|
||||
|
||||
// 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";
|
||||
|
||||
class A {
|
||||
final call = null;
|
||||
}
|
||||
|
||||
class B {
|
||||
get call => null;
|
||||
}
|
||||
|
||||
class C {
|
||||
set call(x) {}
|
||||
}
|
||||
|
||||
typedef int F(String str);
|
||||
|
||||
main() {
|
||||
A a = new A();
|
||||
B b = new B();
|
||||
C c = new C();
|
||||
|
||||
final
|
||||
|
||||
a2 = a;
|
||||
|
||||
final
|
||||
|
||||
a3 = a;
|
||||
|
||||
final
|
||||
|
||||
b2 = b;
|
||||
|
||||
final
|
||||
|
||||
b3 = b;
|
||||
|
||||
final
|
||||
|
||||
c2 = c;
|
||||
|
||||
final
|
||||
|
||||
c3 = c;
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
Function a4 = a as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
F a5 = a as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
Function b4 = b as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
F b5 = b as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
Function c4 = c as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
F c5 = c as dynamic;
|
||||
});
|
||||
}
|
91
tests/language/function/type_call_getter2_test.dart
Normal file
91
tests/language/function/type_call_getter2_test.dart
Normal file
|
@ -0,0 +1,91 @@
|
|||
// 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";
|
||||
|
||||
class A {
|
||||
final call = null;
|
||||
}
|
||||
|
||||
class B {
|
||||
get call => null;
|
||||
}
|
||||
|
||||
class C {
|
||||
set call(x) {}
|
||||
}
|
||||
|
||||
typedef int F(String str);
|
||||
|
||||
main() {
|
||||
A a = new A();
|
||||
B b = new B();
|
||||
C c = new C();
|
||||
|
||||
final
|
||||
Function
|
||||
a2 = a;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
// [cfe] A value of type 'A' can't be assigned to a variable of type 'Function'.
|
||||
|
||||
final
|
||||
F
|
||||
a3 = a;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
// [cfe] A value of type 'A' can't be assigned to a variable of type 'int Function(String)'.
|
||||
|
||||
final
|
||||
Function
|
||||
b2 = b;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
// [cfe] A value of type 'B' can't be assigned to a variable of type 'Function'.
|
||||
|
||||
final
|
||||
F
|
||||
b3 = b;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
// [cfe] A value of type 'B' can't be assigned to a variable of type 'int Function(String)'.
|
||||
|
||||
final
|
||||
Function
|
||||
c2 = c;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
// [cfe] A value of type 'C' can't be assigned to a variable of type 'Function'.
|
||||
|
||||
final
|
||||
F
|
||||
c3 = c;
|
||||
// ^
|
||||
// [analyzer] STATIC_TYPE_WARNING.INVALID_ASSIGNMENT
|
||||
// [cfe] A value of type 'C' can't be assigned to a variable of type 'int Function(String)'.
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
Function a4 = a as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
F a5 = a as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
Function b4 = b as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
F b5 = b as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
Function c4 = c as dynamic;
|
||||
});
|
||||
|
||||
Expect.throwsTypeError(() {
|
||||
F c5 = c as dynamic;
|
||||
});
|
||||
}
|
28
tests/language/function/type_call_getter_test.dart
Normal file
28
tests/language/function/type_call_getter_test.dart
Normal file
|
@ -0,0 +1,28 @@
|
|||
// 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";
|
||||
|
||||
class A {
|
||||
final call = null;
|
||||
}
|
||||
|
||||
class B {
|
||||
get call => null;
|
||||
}
|
||||
|
||||
class C {
|
||||
set call(x) {}
|
||||
}
|
||||
|
||||
typedef int F(String str);
|
||||
|
||||
main() {
|
||||
Expect.isFalse(new A() is Function);
|
||||
Expect.isFalse(new B() is Function);
|
||||
Expect.isFalse(new C() is Function);
|
||||
Expect.isFalse(new A() is F);
|
||||
Expect.isFalse(new B() is F);
|
||||
Expect.isFalse(new C() is F);
|
||||
}
|
23
tests/language/function/type_in_constant_test.dart
Normal file
23
tests/language/function/type_in_constant_test.dart
Normal file
|
@ -0,0 +1,23 @@
|
|||
// 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.
|
||||
|
||||
/// Test that consts can be created with inlined function types as type
|
||||
/// arguments.
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
class A<T> {
|
||||
const A();
|
||||
}
|
||||
|
||||
@pragma('dart2js:noInline')
|
||||
test(a, b) {
|
||||
Expect.notEquals(a, b);
|
||||
}
|
||||
|
||||
main() {
|
||||
test(const A<int Function()>(), const A<String Function()>());
|
||||
test(const A<int>(), const A<String Function()>());
|
||||
test(const A<int Function()>(), const A<String>());
|
||||
}
|
29
tests/language/function/type_parameter2_test.dart
Normal file
29
tests/language/function/type_parameter2_test.dart
Normal file
|
@ -0,0 +1,29 @@
|
|||
// 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.
|
||||
|
||||
// Test to check that we can parse closure type formal parameters with
|
||||
// default value.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class FunctionTypeParameterTest {
|
||||
static var formatter;
|
||||
|
||||
static SetFormatter([String fmt(int i)? = null]) {
|
||||
formatter = fmt;
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
Expect.equals(null, formatter);
|
||||
SetFormatter((i) => "$i");
|
||||
Expect.equals(false, null == formatter);
|
||||
Expect.equals("1234", formatter(1230 + 4));
|
||||
SetFormatter();
|
||||
Expect.equals(null, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
FunctionTypeParameterTest.testMain();
|
||||
}
|
17
tests/language/function/type_parameter3_test.dart
Normal file
17
tests/language/function/type_parameter3_test.dart
Normal file
|
@ -0,0 +1,17 @@
|
|||
// 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.
|
||||
|
||||
/// Test that we detect that a function literal is not
|
||||
/// a compile time constant.
|
||||
|
||||
test([String fmt(int i) = (i) => "$i"]) {}
|
||||
// ^^^^^^^^^^^
|
||||
// [analyzer] COMPILE_TIME_ERROR.NON_CONSTANT_DEFAULT_VALUE
|
||||
// [cfe] Not a constant expression.
|
||||
// ^
|
||||
// [cfe] Not a constant expression.
|
||||
|
||||
main() {
|
||||
test();
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
// 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.
|
||||
|
||||
Q hest<Q>(dynamic x) {
|
||||
if (x is Q) return x;
|
||||
throw "unreached";
|
||||
}
|
||||
|
||||
Q Function<Q>(dynamic) pony = hest;
|
||||
Q Function<Q extends Object?>(dynamic) zebra = hest;
|
||||
|
||||
main() {
|
||||
hest(42).fisk(); //# 01: runtime error
|
||||
pony(42).fisk(); //# 02: runtime error
|
||||
zebra(42).fisk(); //# 03: compile-time error
|
||||
}
|
32
tests/language/function/type_parameter_test.dart
Normal file
32
tests/language/function/type_parameter_test.dart
Normal file
|
@ -0,0 +1,32 @@
|
|||
// 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";
|
||||
|
||||
// Test to check that we can parse closure type formal parameters with
|
||||
// default value.
|
||||
|
||||
class A {
|
||||
final f;
|
||||
A(int this.f());
|
||||
const A.nother(int this.f());
|
||||
|
||||
static Function? func;
|
||||
|
||||
static SetFunc([String fmt(int i)? = null]) {
|
||||
func = fmt;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.equals(null, A.func);
|
||||
A.SetFunc((i) => "$i");
|
||||
Expect.equals(false, null == A.func);
|
||||
Expect.equals("1234", A.func!(1230 + 4));
|
||||
A.SetFunc();
|
||||
Expect.equals(null, A.func);
|
||||
|
||||
Expect.equals(42, new A(() => 42).f());
|
||||
Expect.equals(42, new A.nother(() => 42).f());
|
||||
}
|
17
tests/language/function/type_test.dart
Normal file
17
tests/language/function/type_test.dart
Normal file
|
@ -0,0 +1,17 @@
|
|||
// 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 for a function type test that cannot be eliminated at compile time.
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
typedef FListInt(List<int> l);
|
||||
|
||||
main() {
|
||||
Expect.throwsTypeError(() {
|
||||
// Static result type of f(), i.e. FList, is a subtype of FListInt.
|
||||
// However, run time type of returned function is not a subtype of FListInt.
|
||||
// Run time type check should not be eliminated.
|
||||
FListInt fli = ((List<String> l) => null) as dynamic;
|
||||
});
|
||||
}
|
17
tests/language/function/type_this_parameter_test.dart
Normal file
17
tests/language/function/type_this_parameter_test.dart
Normal file
|
@ -0,0 +1,17 @@
|
|||
// 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.
|
||||
// Check that function types are accepted for constructor arguments that
|
||||
// initialize fields.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class A {
|
||||
Function f;
|
||||
A(int this.f());
|
||||
}
|
||||
|
||||
main() {
|
||||
var a = new A(() => 499);
|
||||
Expect.equals(499, (a.f)());
|
||||
}
|
18
tests/language/library1.dart
Normal file
18
tests/language/library1.dart
Normal 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.
|
||||
//
|
||||
|
||||
library library1.dart;
|
||||
|
||||
var foo;
|
||||
|
||||
bar() => "library1.dart bar()";
|
||||
|
||||
baz() => "library1.dart baz()";
|
||||
|
||||
var bay;
|
||||
|
||||
typedef int bax(int a, int b);
|
||||
|
||||
class baw {}
|
40
tests/language/library10.dart
Normal file
40
tests/language/library10.dart
Normal file
|
@ -0,0 +1,40 @@
|
|||
// 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 library10.dart;
|
||||
|
||||
import "library11.dart" as lib11;
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
class Library10 {
|
||||
Library10(this.fld);
|
||||
int func() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
var fld;
|
||||
static int static_func() {
|
||||
var result = 0;
|
||||
var obj = new lib11.Library11(4);
|
||||
result = obj.fld;
|
||||
Expect.equals(4, result);
|
||||
result += obj.func();
|
||||
Expect.equals(7, result);
|
||||
result += lib11.Library11.static_func();
|
||||
Expect.equals(9, result);
|
||||
result += lib11.Library11.static_fld;
|
||||
Expect.equals(10, result);
|
||||
Expect.equals(100, lib11.top_level11);
|
||||
Expect.equals(200, lib11.top_level_func11());
|
||||
return 3;
|
||||
}
|
||||
|
||||
static var static_fld = 4;
|
||||
}
|
||||
|
||||
const int top_level10 = 10;
|
||||
int top_level_func10() {
|
||||
return 20;
|
||||
}
|
31
tests/language/library11.dart
Normal file
31
tests/language/library11.dart
Normal 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.
|
||||
//
|
||||
|
||||
library library11.dart;
|
||||
|
||||
class Library11 {
|
||||
Library11(this.fld);
|
||||
Library11.namedConstructor(this.fld);
|
||||
int func() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
var fld;
|
||||
static int static_func() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
static var static_fld = 1;
|
||||
}
|
||||
|
||||
class Library111<T> {
|
||||
Library111.namedConstructor(T this.fld);
|
||||
T fld;
|
||||
}
|
||||
|
||||
const int top_level11 = 100;
|
||||
int top_level_func11() {
|
||||
return 200;
|
||||
}
|
48
tests/language/library12.dart
Normal file
48
tests/language/library12.dart
Normal file
|
@ -0,0 +1,48 @@
|
|||
// 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 library12.dart;
|
||||
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
import "library11.dart";
|
||||
|
||||
class Library12 {
|
||||
Library12(this.fld);
|
||||
Library12.other(fld, multiplier) {
|
||||
this.fld = fld * multiplier;
|
||||
}
|
||||
func() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
var fld;
|
||||
static static_func() {
|
||||
var result = 0;
|
||||
var obj = new Library11(4);
|
||||
result = obj.fld;
|
||||
Expect.equals(4, result);
|
||||
result += obj.func();
|
||||
Expect.equals(7, result);
|
||||
result += Library11.static_func();
|
||||
Expect.equals(9, result);
|
||||
result += Library11.static_fld;
|
||||
Expect.equals(10, result);
|
||||
Expect.equals(100, top_level11);
|
||||
Expect.equals(200, top_level_func11());
|
||||
return 3;
|
||||
}
|
||||
|
||||
static var static_fld = 4;
|
||||
}
|
||||
|
||||
abstract class Library12Interface {
|
||||
Library12 addObjects(Library12 value1, Library12 value2);
|
||||
}
|
||||
|
||||
const int top_level12 = 10;
|
||||
top_level_func12() {
|
||||
return 20;
|
||||
}
|
16
tests/language/library2.dart
Normal file
16
tests/language/library2.dart
Normal 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.
|
||||
|
||||
var foo;
|
||||
var foo1 = 0;
|
||||
|
||||
bar() => "library2.dart bar()";
|
||||
|
||||
var baz;
|
||||
|
||||
bay() => "library2.dart bay()";
|
||||
|
||||
typedef double bax(int a, int b);
|
||||
|
||||
var baw;
|
Loading…
Reference in a new issue