Get started on some augmentation library tests.

Bug: https://github.com/dart-lang/sdk/issues/53629
Change-Id: I4f86088b87796a094d49dd5941b167660fab3bab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328600
Commit-Queue: Jake Macdonald <jakemac@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Jake Macdonald 2024-02-12 20:28:00 +00:00 committed by Commit Queue
parent 716d8ccbf8
commit 05aa6b4c16
3 changed files with 172 additions and 0 deletions

View file

@ -0,0 +1,3 @@
analyzer:
enable-experiment:
- macros

View file

@ -0,0 +1,77 @@
// 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.
library augment 'class_augmentation_test.dart';
String get foo => 'a';
augment String get foo => '$augmented + b';
augment String get foo => '$augmented + c';
augment class A extends B implements I {
augment List<int> get ints => augmented..add(2);
augment List<int> get ints => augmented..add(3);
String str = 'hello';
augment String get str => '$augmented world';
augment set str(String value) => augmented = '2$value'
augment int needsInitialization = 1;
augment String fieldWithInitializer = '${augmented}b';
augment String fieldWithInitializer = '${augmented}c';
augment bool _privateField = false;
augment String funcWithoutBody() => 'a';
augment String funcWithBody() => 'b${augmented()}';
augment String funcWithBody() => '${augmented()}c';
augment String get getterWithoutBody => _underlyingString;
augment set setterWithoutBody(String value) => _underlyingString = value;
augment set setterWithBody(String value) =>
augmented = '$_underlyingString$value';
augment operator==(Object other) => false;
String newFunction() => 'a';
augment String newFunction() => '${augmented()}b';
String get newGetter => 'a';
augment String get newGetter => '${augmented}b';
set newSetter(String value) => _underlyingString = value;
augment set newSetter(String value) => augmented='$value 1';
// Override of `S.superX`.
String get superX => '${super.superX}c';
augment String get superX => '${augmented}d';
augment A() : augmentationInitializerInitialized = true {
augmented();
augmentationConstructorInitialized = true;
}
}
augment class A with M {
augment List<int> get ints => augmented..add(4);
augment String get str => '$augmented!';
augment set str(String value) => augmented = '4$value';
// Override of `M.mixinX`.
String get mixinX => '${super.mixinX}c';
augment String get mixinX => '${augmented}d';
}
augment base class B {
augment String get superX => '${augmented}b';
}
augment mixin M {
augment String get mixinX => '${augmented}b';
}

View file

@ -0,0 +1,92 @@
// 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.
// SharedOptions=--enable-experiment=macros
import "package:expect/expect.dart";
import augment "class_augmentation.dart";
main() {
Expect.equals(foo, 'abc');
Expect.listEquals(A().ints, [1, 2, 3, 4]);
Expect.equals(A().str, 'hello world!');
{
var a = A()..str = 'test';
Expect.equals(a.str, '42test world!');
}
Expect.equals(A().needsInitialization, 1);
Expect.equals(A().fieldWithInitializer, 'abc');
Expect.equals(A()._privateField, false);
Expect.equals(A().funcWithBody(), 'bac');
Expect.equals(A().funcWithoutBody(), 'a');
{
var a = A();
Expect.equals(a.getterWithoutBody, 'a');
a.setterWithoutBody = 'b';
Expect.equals(a.getterWithoutBody, 'b');
a.setterWithBody = 'c';
Expect.equals(a.getterWithoutBody, 'bc');
}
var a = A();
Expect(a == a, false);
Expect(A().newFunction(), 'ab');
Expect(A().newGetter, 'ab');
{
var a = A()..newSetter = 'a';
Expect(a._underlyingString, 'a 1');
}
Expect(A() is B, true);
Expect(A() is M, true);
Expect(A() is I, true);
Expect(A().superX, 'abcd');
Expect(A().mixinX, 'abcd');
Expect(A().augmentationConstructorInitialized, true);
Expect(A().augmentationInitializerInitialized, true);
Expect(A().constructorInitialized, true);
Expect(A().initializerInitialized, true);
}
class A {
List<int> get ints => [1];
int needsInitialization;
String fieldWithInitializer = 'a';
bool _privateField = true; // augmented to `false`
String funcWithBody() => 'a';
String funcWithoutBody();
String _underlyingString = 'a';
String get getterWithoutBody;
set setterWithoutBody(String value);
set setterWithBody(String value) => _underlyingString = value;
operator==(Object other) => identical(true);
late final bool augmentationConstructorInitialized;
late final bool constructorInitialized;
final bool initializerInitialized;
final bool augmentationInitializerInitialized;
A() : initializerInitialized = true {
constructorInitialized = true;
}
}
base class B {
String get superX => 'a';
}
mixin M {
String get mixin => 'a';
}
abstract interface class I {
String get str;
}