Skip to content

Commit 31ec93e

Browse files
authored
Add smoke tests for function as type parameter and triple-shift (#2603)
* beginning * first test * test * begin extracting annotation * dartfmt, comment * Update skip * Rebuild renderers * Beginning feature refactor * Getting there * obliterate strings * Final cleanups * Bypass @Native crashing us in flutter * Restrict ast import * Expand feature rendering out and get span classes fixed in parameters * dartfmt * Very basic tests * review comments * Add tests for triple-shift * dartfmt * Add skip outside 2.13
1 parent 342d2d7 commit 31ec93e

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

test/end2end/model_special_cases_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,60 @@ void main() {
7878
VersionRange(min: Version.parse('2.13.0-0'), includeMin: true);
7979
final _genericMetadataAllowed =
8080
VersionRange(min: Version.parse('2.13.0-0'), includeMin: true);
81+
final _tripleShiftAllowed =
82+
VersionRange(min: Version.parse('2.13.0-0'), includeMin: true);
8183

8284
// Experimental features not yet enabled by default. Move tests out of this
8385
// block when the feature is enabled by default.
8486
group('Experiments', () {
87+
group('triple-shift', () {
88+
Library tripleShift;
89+
Class C, E, F;
90+
Extension ShiftIt;
91+
Operator classShift, extensionShift;
92+
Field constantTripleShifted;
93+
94+
setUpAll(() async {
95+
tripleShift = (await _testPackageGraphExperiments)
96+
.libraries
97+
.firstWhere((l) => l.name == 'triple_shift');
98+
C = tripleShift.classes.firstWhere((c) => c.name == 'C');
99+
E = tripleShift.classes.firstWhere((c) => c.name == 'E');
100+
F = tripleShift.classes.firstWhere((c) => c.name == 'F');
101+
ShiftIt = tripleShift.extensions.firstWhere((e) => e.name == 'ShiftIt');
102+
classShift =
103+
C.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
104+
extensionShift =
105+
ShiftIt.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
106+
constantTripleShifted = C.constantFields
107+
.firstWhere((f) => f.name == 'constantTripleShifted');
108+
});
109+
110+
test('constants with triple shift render correctly', () {
111+
expect(constantTripleShifted.constantValue, equals('3 >>> 5'));
112+
});
113+
114+
test('operators exist and are named correctly', () {
115+
expect(classShift.name, equals('operator >>>'));
116+
expect(extensionShift.name, equals('operator >>>'));
117+
});
118+
119+
test(
120+
'inheritance and overriding of triple shift operators works correctly',
121+
() {
122+
var tripleShiftE =
123+
E.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
124+
var tripleShiftF =
125+
F.instanceOperators.firstWhere((o) => o.name.contains('>>>'));
126+
127+
expect(tripleShiftE.isInherited, isTrue);
128+
expect(tripleShiftE.canonicalModelElement, equals(classShift));
129+
expect(tripleShiftE.modelType.returnType.name, equals('C'));
130+
expect(tripleShiftF.isInherited, isFalse);
131+
expect(tripleShiftF.modelType.returnType.name, equals('F'));
132+
});
133+
}, skip: !_tripleShiftAllowed.allows(_platformVersion));
134+
85135
group('generic metadata', () {
86136
Library genericMetadata;
87137
TopLevelVariable f;

testing/test_package_experiments/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ analyzer:
33
enable-experiment:
44
- non-nullable
55
- nonfunction-type-aliases
6+
- triple-shift
67
- generic-metadata
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// Tests for verifying generic functions as type arguments.
6+
library generic_function_type_args;
7+
8+
late List<T Function<T>(T)> idFunctions;
9+
late S Function<S extends T Function<T>(T)>(S) ff;
10+
11+
typedef F = T Function<T>(T);
12+
13+
class C<T> {
14+
final T value;
15+
const C(this.value);
16+
}
17+
18+
T f<T>(T value) => value;
19+
20+
extension E<T> on T {
21+
T get extensionValue => this;
22+
}
23+
24+
// A generic function type can be a type parameter bound.
25+
26+
// For a type alias:
27+
typedef FB<T extends F> = S Function<S extends T>(S);
28+
29+
// For a class:
30+
class CB<T extends FB<F>> {
31+
final T function;
32+
const CB(this.function);
33+
}
34+
35+
// For a function:
36+
T fb<T extends F>(T value) => value;
37+
38+
extension EB<T extends F> on T {
39+
T get boundExtensionValue => this;
40+
41+
// Any function type has a `call` of its own type?
42+
T get boundCall => this.call;
43+
}
44+
45+
// Can be used as arguments to metadata too.
46+
@C<F>(f)
47+
@CB<FB<F>>(fb)
48+
void main() {}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// Tests for verifying triple_shift operator, borrowed from the SDK.
6+
library triple_shift;
7+
8+
class C {
9+
static int ctr = 0;
10+
11+
/// Check that constants using a triple shift operator appear correctly.
12+
static const int constantTripleShifted = 3>>>5;
13+
final Object? _text;
14+
C([Object? text]) : _text = text ?? "${++ctr}";
15+
16+
// It's possible to declare a `>>>` operator.
17+
C operator >>>(arg) => C("(${++ctr}:$_text>>>$arg)");
18+
19+
// + binds more strongly than `>>`, `>>>` and `<<`.
20+
C operator +(arg) => C("(${++ctr}:$_text+$arg)");
21+
// Both `>>` and `<<` binds exactly as strongly as `>>>`.
22+
C operator >>(arg) => C("(${++ctr}:$_text>>$arg)");
23+
C operator <<(arg) => C("(${++ctr}:$_text<<$arg)");
24+
// & binds less strongly than `>>`, `>>>` and `<<`.
25+
C operator &(arg) => C("(${++ctr}:$_text&$arg)");
26+
27+
String toString() => "${_text}";
28+
}
29+
30+
class _D extends C {}
31+
32+
class E extends _D {}
33+
34+
class F extends E {
35+
@override
36+
F operator >>>(arg) => F();
37+
}
38+
39+
// Valid in extensions too.
40+
extension ShiftIt<T> on T {
41+
List<T> operator >>>(int count) => List<T>.filled(count, this);
42+
}

0 commit comments

Comments
 (0)