Skip to content

Commit

Permalink
Issue #1087: New Constructor Tear Offs tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
iarkh committed May 24, 2021
1 parent 8f5c5ea commit 4b1988b
Show file tree
Hide file tree
Showing 19 changed files with 722 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LanguageFeatures/Constructor-tear-offs/goal_t01_A02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ main() {
var v1 = C.constr1;
var v2 = (C.constr1);
var v3 = (C.constr1<dynamic>);
var v4 = (C.constr1)<dynamic>);
var v4 = (C.constr1)<dynamic>;
Expect.equals(v1, v2);
Expect.equals(v1, v3);
Expect.equals(v1, v4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class C<T> {
}

main() {
Expect.isTrue(C.constr is C Function<dynamic>(dynamic));
Expect.isTrue(C.new is C Function<dynamic>(dynamic));
Expect.isTrue(C.constr is C Function<X extends dynamic>(X));
Expect.isTrue(C.new is C Function<X extends dynamic>());

Expect.isTrue(C<int>.constr is C Function<int>(int));
Expect.isTrue(C<int>.new is C Function<int>());
Expect.isTrue(C<int>.constr is C Function<X extends int>(X));
Expect.isTrue(C<int>.new is C Function<X extends int>());

Expect.isTrue(C.constr<int> is C Function<int>(int));
Expect.isTrue(C.new<int> is C Function<int>());
Expect.isTrue(C.constr<int> is C Function<T extends int>(T));
Expect.isTrue(C.new<int> is C Function<T extends int>());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2021, 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.

/// @assertion When tearing off a constructor of a generic class using [C.name],
/// the type arguments may be implicitly instantiated, just as for a normal
/// generic method tear-off of the corresponding static function. The
/// instantiation is based on the context-type at the tear-off position. If the
/// context types allows a generic function, the tear-off is not instantiated
/// and the result is a generic function.
///
/// @description Checks that type arguments can be implicitly instantiated.
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C<T1, T2 extends num, T3 extends String> {
C.constr() {}
C.constr1(T1 t, T2 t2) {}
}

main() {
Expect.isTrue(C.constr is C Function<T1 extends dynamic, T2 extends num, T3 extends String>());
Expect.isTrue(C.constr1 is C Function<T1 extends dynamic, T2 extends num, T3 extends String>(T1, T2));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2021, 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.

/// @assertion When tearing off a constructor of a generic class using [C.name],
/// the type arguments may be implicitly instantiated, just as for a normal
/// generic method tear-off of the corresponding static function. The
/// instantiation is based on the context-type at the tear-off position. If the
/// context types allows a generic function, the tear-off is not instantiated
/// and the result is a generic function.
///
/// @description Checks that type arguments can be implicitly instantiated.
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C<T1, T2 extends num, T3 extends String> {
var t1, t2, t3;
var p1, p2;

C.constr() {
t1 = T1;
t2 = T2;
t3 = T3;
}
C.constr1(T1 par1, T2 par2) {
t1 = T1;
t2 = T2;
t3 = T3;
p1 = par1;
p2 = par2;
}

void check(exp1, exp2, exp3, exp4, exp5) {
Expect.equals(exp1, T1);
Expect.equals(exp2, T2);
Expect.equals(exp3, T3);
Expect.equals(exp4, p1);
Expect.equals(exp5, p2);
}
}

main() {
var v = C.constr;
C c = v();
c.check(dynamic, num, String, null, null);

var v1 = C.constr1;
C c1 = v1(1, 2);
c1.check(dynamic, num, String, 1, 2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2021, 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.

/// @assertion When tearing off a constructor of a generic class using
/// [C<typeArgs>.name, the torn off method is always instantiated to the
/// provided type arguments (which must be valid type arguments for the
/// class/corresponding function). It otherwise behaves as an implicitly
/// instantiated function tear-off.
///
/// @description Checks that type arguments correctly instantiated for
/// [C<typeArgs>.name.
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C<T1, T2 extends num, T3 extends String> {
C.constr() {}
C.constr1(T1 t, T2 t2) {}
}

main() {
var v1 = C<dynamic, num, String>.constr;
Expect.isTrue(v1 is C Function<T1 extends dynamic, T2 extends num, T3 extends String>());

var v2 = C<dynamic, num, String>.constr1;
Expect.isTrue(v2 is C Function<T1 extends dynamic, T2 extends num, T3 extends String>(dynamic, num));

var v3 = C<List<int>, int, String>.constr;
Expect.isTrue(v3 is C Function<T1 extends List<int>, T2 extends int, T3 extends String>());

var v4 = C<List<int>, int, String>.constr1;
Expect.isTrue(v3 is C Function<T1 extends List<int>, T2 extends int, T3 extends String>(List<int>, int));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2021, 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.

/// @assertion When tearing off a constructor of a generic class using [C.name],
/// the type arguments may be implicitly instantiated, just as for a normal
/// generic method tear-off of the corresponding static function. The
/// instantiation is based on the context-type at the tear-off position. If the
/// context types allows a generic function, the tear-off is not instantiated
/// and the result is a generic function.
///
/// @description Checks that type arguments can be instantiated during the
/// constructor tearing off.
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C<T1, T2 extends num, T3 extends String> {
var t1, t2, t3;
var p1, p2;

C.constr() {
t1 = T1;
t2 = T2;
t3 = T3;
}
C.constr1(T1 par1, T2 par2) {
t1 = T1;
t2 = T2;
t3 = T3;
p1 = par1;
p2 = par2;
}

void check(exp1, exp2, exp3, exp4, exp5) {
Expect.equals(exp1, T1);
Expect.equals(exp2, T2);
Expect.equals(exp3, T3);
Expect.equals(exp4, p1);
Expect.equals(exp5, p2);
}
}

main() {
var v1 = C<dynamic, num, String>.constr;
C c1 = v1();
c1.check(dynamic, num, String, null, null);

var v2 = C<dynamic, num, String>.constr1;
C c2 = v2([], 0);
c2.check(dynamic, num, String, [], 0);

var v3 = C<List, int, String>.constr;
C c3 = v3();
c3.check(List, int, String, null, null);

var v4 = C<List, int, String>.constr1;
C c4 = v4([], 0);
c1.check(List, int, String, [], 0);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2021, 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.

/// @assertion The constant-ness, identity and equality of the torn-off
/// constructor functions behave exactly the same as if they were tear-offs of
/// the corresponding static function. This means that a non-generic class
/// constructor always tears off to the same function value, as does an
/// uninstantiated tear off of a generic class constructor.
///
/// @description Checks that a non-generic class constructor always tears off to
/// the same function value: test default constructor
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C {
C() {}
}

main() {
var v1 = C.new;
var v2 = C.new;
var v3 = C.new;
Expect.equals(v1, v2);
Expect.equals(v1, v3);
Expect.equals(v2, v3);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2021, 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.

/// @assertion The constant-ness, identity and equality of the torn-off
/// constructor functions behave exactly the same as if they were tear-offs of
/// the corresponding static function. This means that a non-generic class
/// constructor always tears off to the same function value, as does an
/// uninstantiated tear off of a generic class constructor.
///
/// @description Checks that a non-generic class constructor always tears off to
/// the same function value: test named constructors
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C {
C.name1() {}
C.name2(int i) {}
C.name3(int i, String s, List l) {}
}

main() {
var v1 = C.name1;
var v2 = C.name1;
var v3 = C.name1;
Expect.equals(v1, v2);
Expect.equals(v1, v3);
Expect.equals(v2, v3);

var v4 = C.name2;
var v5 = C.name2;
var v6 = C.name2;
Expect.equals(v4, v5);
Expect.equals(v4, v6);
Expect.equals(v5, v6);

var v7 = C.name3;
var v8 = C.name3;
var v9 = C.name3;
Expect.equals(v7, v8);
Expect.equals(v7, v9);
Expect.equals(v8, v9);

Expect.notEquals(v1, v4);
Expect.notEquals(v2, v7);
Expect.notEquals(v5, v9);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2021, 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.

/// @assertion The constant-ness, identity and equality of the torn-off
/// constructor functions behave exactly the same as if they were tear-offs of
/// the corresponding static function. This means that a non-generic class
/// constructor always tears off to the same function value, as does an
/// uninstantiated tear off of a generic class constructor.
///
/// @description Checks that a non-generic class constructor always tears off to
/// the same function value: test constructors with initializing formals.
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C {
int? i, j;
C(this.i, this.j) {}
C.constr1(this.i, this.j);
C.constr2(i, this.j) {}
}

main() {
var v1 = C.new;
var v2 = C.new;
var v3 = C.new;
Expect.equals(v1, v2);
Expect.equals(v1, v3);
Expect.equals(v2, v3);

var v4 = C.constr1;
var v5 = C.constr1;
var v6 = C.constr1;
Expect.equals(v4, v5);
Expect.equals(v4, v6);
Expect.equals(v5, v6);

var v7 = C.constr1;
var v8 = C.constr1;
var v9 = C.constr1;
Expect.equals(v7, v8);
Expect.equals(v7, v9);
Expect.equals(v8, v9);

Expect.notEquals(v1, v4);
Expect.notEquals(v2, v7);
Expect.notEquals(v5, v9);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2021, 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.

/// @assertion The constant-ness, identity and equality of the torn-off
/// constructor functions behave exactly the same as if they were tear-offs of
/// the corresponding static function. This means that a non-generic class
/// constructor always tears off to the same function value, as does an
/// uninstantiated tear off of a generic class constructor.
///
/// @description Checks that a non-generic class constructor always tears off to
/// the same function value: test constructors with named parameters.
/// @author iarkh@unipro.ru
import "../../Utils/expect.dart";

class C {
C.name1({int i = 1, j = "testme"}) {}
C.name2(int i, {List? l, String check = "check"}) {}
C.name3(int i, {String s = "", required l}) {}
}

main() {
var v1 = C.name1;
var v2 = C.name1;
var v3 = C.name1;
Expect.equals(v1, v2);
Expect.equals(v1, v3);
Expect.equals(v2, v3);

var v4 = C.name2;
var v5 = C.name2;
var v6 = C.name2;
Expect.equals(v4, v5);
Expect.equals(v4, v6);
Expect.equals(v5, v6);

var v7 = C.name3;
var v8 = C.name3;
var v9 = C.name3;
Expect.equals(v7, v8);
Expect.equals(v7, v9);
Expect.equals(v8, v9);

Expect.notEquals(v1, v4);
Expect.notEquals(v2, v7);
Expect.notEquals(v5, v9);
}
Loading

0 comments on commit 4b1988b

Please sign in to comment.