diff --git a/tests/language/enum_shorthands/constructor/constructor_error_test.dart b/tests/language/enum_shorthands/constructor/constructor_error_test.dart new file mode 100644 index 00000000000..6bc8a0c9ecb --- /dev/null +++ b/tests/language/enum_shorthands/constructor/constructor_error_test.dart @@ -0,0 +1,45 @@ +// Copyright (c) 2024, 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. + +// Errors involving enum shorthands of constructors. + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../enum_shorthand_helper.dart'; + +void main() { + // Using a constructor shorthand without any context. + + var ctorNew = .new(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + const ctorConstNew = .new(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + var ctorNamed = .regular(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + const ctorConstNamed = .regular(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + // `.new()` and `.new` are a compile-time error. + + UnnamedConstructorTypeParameters typeParameters = .new(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + UnnamedConstructorTypeParameters Function() tearOff = .new; + // ^ + // [analyzer] unspecified + // [cfe] unspecified +} diff --git a/tests/language/enum_shorthands/constructor/constructor_test.dart b/tests/language/enum_shorthands/constructor/constructor_test.dart new file mode 100644 index 00000000000..7255d6bddde --- /dev/null +++ b/tests/language/enum_shorthands/constructor/constructor_test.dart @@ -0,0 +1,77 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Testing shorthands for constructor calls. + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../enum_shorthand_helper.dart'; + +class ConstructorClassContext { + final ConstructorClass? x; + ConstructorClassContext(this.x); + ConstructorClassContext.named({this.x}); + ConstructorClassContext.optional([this.x]); +} + +class ConstructorExtContext { + final ConstructorExt? x; + ConstructorExtContext(this.x); + ConstructorExtContext.named({this.x}); + ConstructorExtContext.optional([this.x]); +} + +void main() { + int x = 1; + + ConstructorClass ctor = .new(x); + ConstructorClass ctor1 = .regular(x); + ConstructorClass ctor2 = .named(x: x); + ConstructorClass ctor3 = .optional(x); + ConstructorClass ctor4 = .constRegular(x); + ConstructorClass ctor5 = .constNamed(x: x); + ConstructorClass ctor6 = .constOptional(x); + + ConstructorExt ctorExt = .new(x); + ConstructorExt ctorExt1 = .regular(x); + ConstructorExt ctorExt2 = .named(x: x); + ConstructorExt ctorExt3 = .optional(x); + ConstructorExt ctorExt4 = .constRegular(x); + ConstructorExt ctorExt5 = .constNamed(x: x); + ConstructorExt ctorExt6 = .constOptional(x); + + ConstructorClass? ctorNullable = .new(x); + ConstructorClass? ctorNullable1 = .regular(x); + ConstructorClass? ctorNullable2 = .named(x: x); + ConstructorClass? ctorNullable3 = .optional(x); + ConstructorClass? ctorNullable4 = .constRegular(x); + ConstructorClass? ctorNullable5 = .constNamed(x: x); + ConstructorClass? ctorNullable6 = .constOptional(x); + + ConstructorExt? ctorExtNullable = .new(x); + ConstructorExt? ctorExtNullable1 = .regular(x); + ConstructorExt? ctorExtNullable2= .named(x: x); + ConstructorExt? ctorExtNullable3 = .optional(x); + ConstructorExt? ctorExtNullable4 = .constRegular(x); + ConstructorExt? ctorExtNullable5 = .constNamed(x: x); + ConstructorExt? ctorExtNullable6 = .constOptional(x); + + UnnamedConstructor Function() ctorTearoff = .new; + + // Parameter context type. + ConstructorClassContext(.new(1)); + ConstructorClassContext.named(x: .optional(1)); + ConstructorClassContext.optional(.optional(1)); + + ConstructorExtContext(.new(1)); + ConstructorExtContext.named(x: .optional(1)); + ConstructorExtContext.optional(.optional(1)); + + // Collection + [.new(x), .regular(x), .constRegular(x)]; + [.new(x), .regular(x), .constRegular(x)]; + [.new(x), .regular(x), .constRegular(x)]; + [.new(x), .regular(x), .constRegular(x)]; +} + diff --git a/tests/language/enum_shorthands/enum_shorthand_helper.dart b/tests/language/enum_shorthands/enum_shorthand_helper.dart index 094e116d855..308e41a7a64 100644 --- a/tests/language/enum_shorthands/enum_shorthand_helper.dart +++ b/tests/language/enum_shorthands/enum_shorthand_helper.dart @@ -37,3 +37,44 @@ class _IntegerWithMixin extends Integer with IntegerMixin { const _IntegerWithMixin(int integer) : this._(integer); const _IntegerWithMixin._(super.integer) : super._(); } + +// Selector chain test declarations. + +class ConstructorClass { + final int? x; + ConstructorClass(this.x); + ConstructorClass.regular(this.x); + ConstructorClass.named({this.x}); + ConstructorClass.optional([this.x]); + + const ConstructorClass.constRegular(this.x); + const ConstructorClass.constNamed({this.x}); + const ConstructorClass.constOptional([this.x]); +} + +class UnnamedConstructor {} + +class UnnamedConstructorTypeParameters {} + +extension type ConstructorExt(int? x) { + ConstructorExt.regular(this.x); + ConstructorExt.named({this.x}); + ConstructorExt.optional([this.x]); + + const ConstructorExt.constRegular(this.x); + const ConstructorExt.constNamed({this.x}); + const ConstructorExt.constOptional([this.x]); +} + +class StaticMember { + static StaticMember member() => StaticMember(1); + static StaticMember memberType(U u) => StaticMember(u); + + final T t; + StaticMember(this.t); +} + +extension type StaticMemberExt(T x) { + static StaticMemberExt member() => StaticMemberExt(1); + static StaticMemberExt memberType(U u) => StaticMemberExt(u); +} diff --git a/tests/language/enum_shorthands/member/static_method_test.dart b/tests/language/enum_shorthands/member/static_method_test.dart new file mode 100644 index 00000000000..94135affa46 --- /dev/null +++ b/tests/language/enum_shorthands/member/static_method_test.dart @@ -0,0 +1,51 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +// Basic usages of enum shorthands with static members in classes and extension +// types. + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../enum_shorthand_helper.dart'; + +class StaticMemberContext { + final StaticMember? clas; + StaticMemberContext(this.clas); + StaticMemberContext.named({this.clas}); + StaticMemberContext.optional([this.clas]); +} + +class StaticMemberExtContext { + final StaticMemberExt? ext; + StaticMemberExtContext(this.ext); + StaticMemberExtContext.named({this.ext}); + StaticMemberExtContext.optional([this.ext]); +} + +void main() { + StaticMember s = .member(); + StaticMemberContext(.member()); + StaticMemberContext.named(clas: .member()); + StaticMemberContext.optional(.member()); + + StaticMember sTypeParameters = .memberType("s"); + StaticMemberContext(.memberType("s")); + StaticMemberContext.named(clas: .memberType("s")); + StaticMemberContext.optional(.memberType("s")); + + StaticMemberExt sExt = .member(); + StaticMemberExtContext(.member()); + StaticMemberExtContext.named(ext: .member()); + StaticMemberExtContext.optional(.member()); + + StaticMemberExt sTypeParametersExt = .memberType("s"); + StaticMemberExtContext(.memberType("s")); + StaticMemberExtContext.named(ext: .memberType("s")); + StaticMemberExtContext.optional(.memberType("s")); + + [.member(), .memberType('s')]; + [.member(), .memberType('s')]; + [.member(), .memberType('s')]; + [.member(), .memberType('s')]; +} diff --git a/tests/language/enum_shorthands/member/static_method_type_parameter_error_test.dart b/tests/language/enum_shorthands/member/static_method_type_parameter_error_test.dart new file mode 100644 index 00000000000..3d344dc5e2b --- /dev/null +++ b/tests/language/enum_shorthands/member/static_method_type_parameter_error_test.dart @@ -0,0 +1,32 @@ +// Copyright (c) 2024, 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. + +// Errors when the type parameters of the shorthand methods don't match the +// context type. + +// SharedOptions=--enable-experiment=enum-shorthands + +import '../enum_shorthand_helper.dart'; + +void main() { + StaticMember s = .member(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + StaticMember sTypeParameters = .memberType("s"); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + StaticMemberExt sExt = .member(); + // ^ + // [analyzer] unspecified + // [cfe] unspecified + + StaticMemberExt sTypeParametersExt = .memberType("s"); + // ^ + // [analyzer] unspecified + // [cfe] unspecified +} diff --git a/tests/language/enum_shorthands/multiple/type_alias_test.dart b/tests/language/enum_shorthands/multiple/type_alias_test.dart new file mode 100644 index 00000000000..08d1b256eaa --- /dev/null +++ b/tests/language/enum_shorthands/multiple/type_alias_test.dart @@ -0,0 +1,53 @@ +// Copyright (c) 2024, 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. + +// Type aliases with enum shorthands. + +// SharedOptions=--enable-experiment=enum-shorthands + +typedef ClassAlias = A; +typedef ExtensionAlias = AExt; + +class A { + static ClassAlias get alias => A(1); + + static const ClassAlias constAlias = const A._(1); + + static ClassAlias method() => A(1); + + final T t; + + A(this.t); + + const A._(this.t); + + ClassAlias get aliasGetter => A(1); +} + +extension type AExt(T t) { + static ExtensionAlias get alias => AExt(1); + + static const ExtensionAlias constAlias = const AExt._(1); + + static ExtensionAlias method() => AExt(1); + + const AExt._(this.t); + + ExtensionAlias get aliasGetter => AExt(1); +} + + +void main() { + // Class + ClassAlias classAlias = .alias; + ClassAlias classAlias2 = .new('s').aliasGetter; + ClassAlias classAliasMethod = .method(); + const ClassAlias constClassAlias = .constAlias; + + // Extension type + ExtensionAlias extensionAlias = .alias; + ExtensionAlias extensionAliasCtor = .new('s').aliasGetter; + ExtensionAlias extensionAliasMethod = .method(); + const ExtensionAlias constExtensionAlias = .constAlias; +}