Skip to content

Commit

Permalink
dart-lang#1400. Tests for constructors of inline classes added (dart-…
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov authored May 31, 2023
1 parent e4211d8 commit 5e4181a
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 Constructor tear-off of inline classes happens in the same way as
/// for regular classes
/// @note Inline classes specification doesn't contain this statement but
/// assumes it
///
/// @description Checks static type of an inline class constructor tear-off
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

import "../../Utils/static_type_helper.dart";

inline class IC1 {
final int id;
IC1(this.id);
IC1.n1(this.id, [String s = ""]);
IC1.n2(this.id, {String s = ""});
IC1.n3(this.id, {required String s});
}

inline class IC2<T extends num> {
final T id;
IC2(this.id);
IC2.n1(this.id, [String s = ""]);
IC2.n2(this.id, {String s = ""});
IC2.n3(this.id, {required String s});
}

main() {
IC1.new.expectStaticType<Exactly<IC1 Function(int)>>();
IC1.n1.expectStaticType<Exactly<IC1 Function(int, [String])>>();
IC1.n2.expectStaticType<Exactly<IC1 Function(int, {String s})>>();
IC1.n3.expectStaticType<Exactly<IC1 Function(int, {required String s})>>();

IC2<num>.new.expectStaticType<Exactly<IC2 Function(num)>>();
IC2<int>.n1.expectStaticType<Exactly<IC2<int> Function(int, [String])>>();
IC2<int>.n2.expectStaticType<Exactly<IC2<int> Function(int, {String s})>>();
IC2<int>
.n3
.expectStaticType<Exactly<IC2<int> Function(int, {required String s})>>();

IC2.new.expectStaticType<Exactly<IC2<T> Function<T extends num>(T)>>();
IC2.n1
.expectStaticType<Exactly<IC2<T> Function<T extends num>(T, [String])>>();
IC2.n2.expectStaticType<
Exactly<IC2<T> Function<T extends num>(T, {String s})>>();
IC2.n3.expectStaticType<
Exactly<IC2<T> Function<T extends num>(T, {required String s})>>();
}
57 changes: 57 additions & 0 deletions LanguageFeatures/Inline-classes/syntax_A07_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 A rule for <inlineClassDeclaration> is added to the grammar,
/// along with some rules for elements used in inline class declarations:
///
/// <inlineClassDeclaration> ::=
/// 'final'? 'inline' 'class' <typeIdentifier> <typeParameters>? <interfaces>?
/// '{'
/// (<metadata> <inlineMemberDeclaration>)*
/// '}'
///
/// <inlineMemberDeclaration> ::= <classMemberDefinition>
/// ...
///
/// @description Checks that inline classes may have constant constructors
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

inline class IC1 {
final int id;
const IC1(this.id);
}

inline class IC2<T> {
final T id;
const IC2(this.id);
}

inline class IC3<T extends Object> {
final T id;
const IC3(this.id);
}

inline class IC4<T extends num> {
final T id;
const IC4(this.id);
}

const v1 = const IC1(42);
const v2 = const IC2<bool>(true);
const v3 = const IC3<String>("42");
const v4 = const IC4<int>(42);

void foo1([IC1 v = v1]) {}
void foo2([IC2 v = v2]) {}
void foo3([IC3 v = v3]) {}
void foo4([IC4 v = v4]) {}

main() {
foo1();
foo2();
foo3();
foo4();
}
76 changes: 76 additions & 0 deletions LanguageFeatures/Inline-classes/syntax_A07_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 A rule for <inlineClassDeclaration> is added to the grammar,
/// along with some rules for elements used in inline class declarations:
///
/// <inlineClassDeclaration> ::=
/// 'final'? 'inline' 'class' <typeIdentifier> <typeParameters>? <interfaces>?
/// '{'
/// (<metadata> <inlineMemberDeclaration>)*
/// '}'
///
/// <inlineMemberDeclaration> ::= <classMemberDefinition>
/// ...
///
/// @description Checks that inline classes may have factory constructors
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

import "../../Utils/expect.dart";

inline class IC1 {
final int id;
IC1(this.id);

factory IC1.f1(int id) = IC1;
factory IC1.f2(int id) => IC1(id);
}

inline class IC2<T> {
final T id;
IC2(this.id);

factory IC2.f1(T id) = IC2;
factory IC2.f2(T id) => IC2(id);
}

inline class IC3<T extends Object> {
final T id;
IC3(this.id);

factory IC3.f1(T id) = IC3;
factory IC3.f2(T id) => IC3(id);
}

inline class IC4<T extends num> {
final T id;
const IC4(this.id);

factory IC4.f1(T id) = IC4;
factory IC4.f2(T id) => IC4(id);
}

main() {
var ic1_1 = IC1.f1(1);
Expect.equals(1, ic1_1.id);
var ic1_2 = IC1.f2(1);
Expect.equals(1, ic1_2.id);

var ic2_1 = IC2<int>.f1(2);
Expect.equals(2, ic2_1.id);
var ic2_2 = IC2.f2(2);
Expect.equals(2, ic2_2.id);

var ic3_1 = IC3<int>.f1(3);
Expect.equals(3, ic3_1.id);
var ic3_2 = IC3.f2(3);
Expect.equals(3, ic3_2.id);

var ic4_1 = IC4<int>.f1(4);
Expect.equals(4, ic4_1.id);
var ic4_2 = IC4.f2(4);
Expect.equals(4, ic4_2.id);
}
64 changes: 64 additions & 0 deletions LanguageFeatures/Inline-classes/syntax_A07_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 A rule for <inlineClassDeclaration> is added to the grammar,
/// along with some rules for elements used in inline class declarations:
///
/// <inlineClassDeclaration> ::=
/// 'final'? 'inline' 'class' <typeIdentifier> <typeParameters>? <interfaces>?
/// '{'
/// (<metadata> <inlineMemberDeclaration>)*
/// '}'
///
/// <inlineMemberDeclaration> ::= <classMemberDefinition>
/// ...
///
/// @description Checks that inline classes may have redirecting constructors
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=inline-class

import "../../Utils/expect.dart";

inline class IC1 {
final int id;
IC1(this.id);

IC1.r(int id) : this(id);
}

inline class IC2<T> {
final T id;
IC2(this.id);

IC2.r(T id) : this(id);
}

inline class IC3<T extends Object> {
final T id;
IC3(this.id);

IC3.r(T id) : this(id);
}

inline class IC4<T extends num> {
final T id;
const IC4(this.id);

IC4.r(T id) : this(id);
}

main() {
var ic1 = IC1.r(1);
Expect.equals(1, ic1.id);

var ic2 = IC2<int>.r(2);
Expect.equals(2, ic2.id);

var ic3 = IC3<int>.r(3);
Expect.equals(3, ic3.id);

var ic4 = IC4<int>.r(4);
Expect.equals(4, ic4.id);
}

0 comments on commit 5e4181a

Please sign in to comment.