Skip to content

Commit

Permalink
[tests] Enum shorthands - Selector chain tests for cascades, ??, coll…
Browse files Browse the repository at this point in the history
…ections and nested contexts.

Tests for nested context types, such as a constructor with an argument that also uses an enum shorthand.

Tests for selector chains. Multiple members and properties in one shorthand.

Bug: #57038
Change-Id: Ie9609b7594ccbe4347a59026d9e758868015fe64
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402442
Reviewed-by: Lasse Nielsen <[email protected]>
Commit-Queue: Kallen Tu <[email protected]>
  • Loading branch information
kallentu authored and Commit Queue committed Jan 8, 2025
1 parent b9de4ae commit 302287e
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
35 changes: 35 additions & 0 deletions tests/language/enum_shorthands/enum_shorthand_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,36 @@ class _IntegerWithMixin extends Integer with IntegerMixin {

class ConstructorClass {
final int? x;

ConstructorClass(this.x);
ConstructorClass.regular(this.x);
ConstructorClass.named({this.x});
ConstructorClass.optional([this.x]);

ConstructorClass.integer(Integer integer) : x = 1;

ConstructorClass.staticMember(StaticMember member) : x = 1;
ConstructorClass.staticMemberExt(StaticMemberExt member) : x = 1;

ConstructorClass.ctor(ConstructorClass ctor) : x = 1;

const ConstructorClass.constRegular(this.x);
const ConstructorClass.constNamed({this.x});
const ConstructorClass.constOptional([this.x]);
}

class ConstructorWithNonFinal {
final int x;

ConstructorWithNonFinal field = ConstructorWithNonFinal(1);

ConstructorWithNonFinal(this.x);

ConstructorWithNonFinal method() => ConstructorWithNonFinal(1);

ConstructorWithNonFinal? methodNullable() => null;
}

class UnnamedConstructor {}

class UnnamedConstructorTypeParameters<T> {}
Expand All @@ -75,13 +95,28 @@ class StaticMember<T> {
static StaticMember<U> memberType<U, V>(U u) => StaticMember(u);
static StaticMember<U>? memberTypeNullable<U, V>(U u) => null;

static StaticMember<ConstructorClass> ctor(ConstructorClass c) =>
StaticMember(c);

static StaticMember<Integer> property(Integer i) => StaticMember(i);

final T t;
StaticMember field = StaticMember.member();

StaticMember(this.t);

StaticMember method() => StaticMember.member();
StaticMember? methodNullable() => null;
}

extension type StaticMemberExt<T>(T x) {
static StaticMemberExt<int> member() => StaticMemberExt(1);
static StaticMemberExt<int>? memberNullable() => null;
static StaticMemberExt<U> memberType<U, V>(U u) => StaticMemberExt(u);
static StaticMemberExt<U>? memberTypeNullable<U, V>(U u) => null;

static StaticMemberExt<ConstructorClass> ctor(ConstructorClass c) =>
StaticMemberExt(c);

static StaticMemberExt<Integer> property(Integer i) => StaticMemberExt(i);
}
53 changes: 53 additions & 0 deletions tests/language/enum_shorthands/multiple/nested_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2025, 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.

// Nested enum shorthands.

// SharedOptions=--enable-experiment=enum-shorthands

import '../enum_shorthand_helper.dart';

void main() {
StaticMember<StaticMember> memberMember = .memberType(
.memberType(.member()),
);

StaticMemberExt<StaticMemberExt> memberMemberExt = .memberType(
.memberType(.member()),
);

StaticMember<StaticMember> memberCtor = .memberType(.new(.member()));

StaticMemberExt<StaticMemberExt> memberCtorExt = .memberType(
.new(.member()),
);

StaticMember<Integer> memberProperty = .property(.one);

StaticMemberExt<Integer> memberPropertyExt = .property(.one);

StaticMember<ConstructorClass> memberCtorMember = .ctor(
.staticMember(.member()),
);

StaticMemberExt<ConstructorClass> memberCtorMemberExt = .ctor(
.staticMemberExt(.member()),
);

StaticMember<ConstructorClass> memberCtorProperty = .ctor(.integer(.one));

ConstructorClass ctorMember = .staticMember(.member());

ConstructorClass ctorMemberExt = .staticMemberExt(.member());

ConstructorClass ctorProperty = .integer(.one);

ConstructorClass ctorCtor = .ctor(.new(1));

ConstructorClass ctorCtorCtor = .ctor(.ctor(.new(1)));

ConstructorClass ctorMemberCtor = .staticMember(.ctor(.named(x: 1)));

ConstructorClass ctorMemberCtorExt = .staticMemberExt(.ctor(.named(x: 1)));
}
87 changes: 87 additions & 0 deletions tests/language/enum_shorthands/multiple/selector_chain_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) 2025, 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.

// Enum shorthands selector chains in collections, cascades, `??` and other
// contexts.

// SharedOptions=--enable-experiment=enum-shorthands

import 'dart:async';

import '../enum_shorthand_helper.dart';

class Cascade {
late StaticMember member;
late ConstructorWithNonFinal ctor;
}

void main() {
StaticMember memberField = .member().field;
StaticMember memberMethod = .member().method();
StaticMember memberMixed = .member().method().field;
StaticMember memberMixed2 = .member().field.method();

ConstructorWithNonFinal ctorField = .new(1).field;
ConstructorWithNonFinal ctorMethod = .new(1).method();
ConstructorWithNonFinal ctorMixed =
.new(1).method().field;
ConstructorWithNonFinal ctorMixed2 =
.new(1).field.method();

// FutureOr
FutureOr<StaticMember> futureOrMember = .member().field.method();
FutureOr<ConstructorWithNonFinal> futureOrCtor =
.new(1).field.method();

// Collection literals
var memberList = <StaticMember>[
.member().field,
.member().field.method(),
];
var memberSet = <StaticMember>{
.member().field,
.member().field.method(),
};
var memberMap = <StaticMember, StaticMember>{
.member().field: .member().field,
.member().field.method(): .member().field.method(),
};

var ctorList = <ConstructorWithNonFinal>[
.new(1).field,
.new(1).method().field,
];
var ctorSet = <ConstructorWithNonFinal>{
.new(1).field,
.new(1).method().field,
};
var ctorMap = <ConstructorWithNonFinal, ConstructorWithNonFinal>{
.new(1).field: .new(1).field,
.new(1).method().field:
.new(1).method().field,
};

// Cascades
Cascade()
..member = .member().field.method()
..ctor = .new(1).method().field;

StaticMember memberCascade = .member().field.method()..toString();
ConstructorWithNonFinal ctorCascade =
.new(1).method().field..toString();

// If-null
StaticMember? staticMemberNull = null as StaticMember?;
StaticMember ifNullMember =
staticMemberNull ?? .member().field.method();
StaticMember ifNullMember2 =
.member().field.methodNullable() ?? .member();

ConstructorWithNonFinal? ctorNull = null as ConstructorWithNonFinal?;
ConstructorWithNonFinal ifNullCtor =
ctorNull ?? .new(1).method().field;
ConstructorWithNonFinal ifNullCtor2 =
.new(1).method().field.methodNullable() ??
ConstructorWithNonFinal(1);
}

0 comments on commit 302287e

Please sign in to comment.