Skip to content

Commit

Permalink
#2350. Update existing factory constructor tests. Part 2 (#2357)
Browse files Browse the repository at this point in the history
Update existing factory constructor tests. Part 2
  • Loading branch information
sgrekhov authored Nov 7, 2023
1 parent 7ccb121 commit d3d4eb6
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 30 deletions.
26 changes: 19 additions & 7 deletions Language/Classes/Constructors/Factories/arguments_type_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@
// 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 It is a compile error warning if any of the type arguments to k'
/// are not subtypes of the bounds of the corresponding formal type parameters
/// of type.
/// @assertion Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// Let Ts be the static argument list type (T1 . . . , Tn+k) when k takes no
/// named arguments, and (T1 . . . , Tn, Tn+1 xn+1, . . . , Tn+k xn+k) when k
/// takes some named arguments. It is a compile-time error if Ts is not a
/// subtype match for the formal parameter list of the redirectee.
///
/// @description Checks that a compile error is produced if factory constructor
/// type parameters are not subtypes of bounds of corresponding type parameters
/// of redirected class constructor.
/// @author ilya

class F <T extends String> {
class F<T extends String> {
factory F([T x]) = C;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

class C <T extends num> implements F<T> {
class C<T extends num> implements F<T> {
// ^
// [analyzer] unspecified
// [cfe] unspecified
Expand All @@ -30,5 +42,5 @@ class C <T extends num> implements F<T> {
}

main() {
new F<String>();
print(F<String>);
}
22 changes: 18 additions & 4 deletions Language/Classes/Constructors/Factories/const_modifier_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@
// 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 It is a compile-time error if k is prefixed with the const
/// modifier but k' is not a constant constructor.
/// @assertion Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// The redirectee constructor for this declaration is the constructor k′
/// denoted by R.
/// ...
/// It is a compile-time error if k is prefixed with the const modifier but k'
/// is not a constant constructor.
///
/// @description Checks that it is a compile-error if const redirecting
/// factory constructor redirects to non-const generative constructor
/// @author ilya

class F {
const factory F() = C;
// ^
Expand All @@ -20,5 +34,5 @@ class C implements F {
}

main() {
new F();
print(F);
}
38 changes: 32 additions & 6 deletions Language/Classes/Constructors/Factories/const_modifier_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,53 @@
// 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 It is a compile-time error if k is prefixed with the const
/// modifier but k' is not a constant constructor.
/// @assertion Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// The redirectee constructor for this declaration is the constructor k′
/// denoted by R.
/// ...
/// It is a compile-time error if k is prefixed with the const modifier but k'
/// is not a constant constructor.
///
/// @description Checks that it is a compile-error if const redirecting
/// factory constructor redirects to non-const redirecting factory constructor
/// factory constructor redirects to non-const factory constructor
/// @author ilya

class F1 {
const factory F1() = F2;
// ^^
// [analyzer] unspecified
// [cfe] unspecified
}

class F2 {
class F2 extends F1 {
factory F2() = C;
}

class C implements F2 {
}

enum E {
e1, e2;
const E();

factory E.f1() => E.e1;
const factory E.f2() = E.f1;
// ^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
new F1();
print(F1);
print(E);
}
32 changes: 27 additions & 5 deletions Language/Classes/Constructors/Factories/default_value_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
// 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 It is a compile-time error if k explicitly specifies a default
/// value for an optional parameter.
/// @description Checks that it is a compile-error if redirecting factory
/// @assertion Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// It is a compile-time error if k explicitly specifies a default value for an
/// optional parameter.
///
/// @description Checks that it is a compile-error if a redirecting factory
/// constructor specifies a default value for its optional positional parameter.
/// @author ilya

class F {
factory F(int x, [int y = 0]) = C;
// ^
Expand All @@ -20,6 +31,17 @@ class C implements F {
C(int x, [int y = 0]);
}

enum E {
e1, e2;
const E();
factory E.f1([int x = 0]) => E.e1;
factory E.f2([int x = 0]) = E.f1;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
new F(1);
print(F);
print(E);
}
32 changes: 27 additions & 5 deletions Language/Classes/Constructors/Factories/default_value_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
// 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 It is a compile-time error if k explicitly specifies a default
/// value for an optional parameter.
/// @description Checks that it is a compile-error if redirecting factory
/// @assertion Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// It is a compile-time error if k explicitly specifies a default value for an
/// optional parameter.
///
/// @description Checks that it is a compile-error if a redirecting factory
/// constructor specifies a default value for its optional named parameter.
/// @author ilya

class F {
factory F(int x, {int y: 0}) = C;
factory F(int x, {int y = 0}) = C;
// ^
// [analyzer] unspecified
// [cfe] unspecified
Expand All @@ -20,6 +31,17 @@ class C implements F {
C(int x, {int y = 0});
}

enum E {
e1, e2;
const E();
factory E.f1({int x = 0}) => E.e1;
factory E.f2({int x = 0}) = E.f1;
// ^
// [analyzer] unspecified
// [cfe] unspecified
}


main() {
new F(1);
}
24 changes: 21 additions & 3 deletions Language/Classes/Constructors/Factories/default_value_t03.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@
// 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 It is a compile-time error if k explicitly specifies a default
/// value for an optional parameter.
/// @description Checks that it is not an error if redirecting factory
/// @assertion Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// It is a compile-time error if k explicitly specifies a default value for an
/// optional parameter.
///
/// @description Checks that it is not an error if a redirecting factory
/// constructor has optional parameters with no defaults and that actual
/// arguments are passed as expected.
/// @author ilya
Expand All @@ -21,6 +33,7 @@ class F {
factory F(int x, [int y]) = C;
factory F.foo(int x, [int y]) = C.foo;
factory F.bar(int x, {int y}) = C.bar;
factory F.baz(int x, {required int y}) = C.baz;
}

class C implements F {
Expand All @@ -33,6 +46,9 @@ class C implements F {
C.bar(int x, {int y = 0}) {
test(x, y);
}
C.baz(int x, {required int y}) {
test(x, y);
}
}

main() {
Expand All @@ -48,4 +64,6 @@ main() {
new F.bar(1);
expect = [1, 1];
new F.bar(1, y: 1);
expect = [2, 2];
new F.baz(2, y: 2);
}
67 changes: 67 additions & 0 deletions Language/Classes/Constructors/Factories/default_value_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2023, 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 Assume that C<X1 extends B1 . . . , Xm extends Bm> is the name
/// and formal type parameters of the enclosing class, const? is const or empty,
/// N is C or C.id0 for some identifier id0, and id is an identifier, then
/// consider a declaration of a redirecting factory constructor k of one of the
/// forms
/// const? factory
/// N(T1 x1 . . . , Tn xn, [Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk]) = R;
/// const? factory
/// N(T1 x1 . . . , Tn xn, {Tn+1 xn+1=d1, . . . , Tn+k xn+k=dk}) = R;
/// where R is of one of the forms T<S1 . . . , Sp> or T<S1 . . . , Sp>.id.
/// ...
/// It is a compile-time error if k explicitly specifies a default value for an
/// optional parameter.
///
/// @description Checks that it is not an error if redirecting factory
/// constructor has optional parameters with no defaults and that actual
/// arguments are passed as expected.
/// @author sgrekhov22@gmail.com
import "../../../../Utils/expect.dart";

List expect = [];

test(x, y) {
Expect.listEquals(expect, [x, y]);
}

enum E {
e1, e2, e3;
const E();

factory E.f1(int x, [int y = 0]) {
test(x, y);
return E.e1;
}
factory E.f2(int x, {int y = 0}) {
test(x, y);
return E.e2;
}
factory E.f3(int x, {required int y}) {
test(x, y);
return E.e3;
}

factory E.fr1(int x, [int y]) = E.f1;
factory E.fr2(int x, {int y}) = E.f2;
factory E.fr3(int x, {required int y}) = E.f3;
}

main() {
expect = [1, 0];
E.fr1(1);
expect = [1, 2];
E.fr1(1, 2);

expect = [1, 0];
E.fr2(1);
expect = [1, 2];
E.fr2(1, y: 2);

expect = [1, 2];
E.fr3(1, y: 2);
}

0 comments on commit d3d4eb6

Please sign in to comment.