Skip to content

Commit

Permalink
dart-lang#2641. Add wildcard initializer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed May 13, 2024
1 parent 14e8d69 commit 5bfdaf3
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
56 changes: 56 additions & 0 deletions LanguageFeatures/Wildcards/binding_A03_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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.

/// @assertion A local declaration whose name is `_` does not bind that name to
/// anything. This means you can have multiple local declarations named `_` in
/// the same namespace without a collision error. The initializer, if there is
/// one, is still executed, but the value is not accessible.
///
/// @description Checks that an initializer is executed for local variables
/// declarations named `_`
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=wildcard-variables

import '../../Utils/expect.dart';

String _log = "";

int init(int val) {
_log += "init($val);";
return val;
}

test1() {
var _ = init(1);
}

test2() {
final _ = init(2);
}

test3() {
int _ = init(3);
}

test4() {
int _ = init(4), _ = init(5);
}

main() {
test1();
Expect.equals("init(1);", _log);
_log = "";

test2();
Expect.equals("init(2);", _log);
_log = "";

test3();
Expect.equals("init(3);", _log);
_log = "";

test4();
Expect.equals("init(4);init(5);", _log);
}
44 changes: 44 additions & 0 deletions LanguageFeatures/Wildcards/binding_A03_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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.

/// @assertion A local declaration whose name is `_` does not bind that name to
/// anything. This means you can have multiple local declarations named `_` in
/// the same namespace without a collision error. The initializer, if there is
/// one, is still executed, but the value is not accessible.
///
/// @description Checks that for late local variables declarations named `_`
/// initializer is not executed
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=wildcard-variables

import '../../Utils/expect.dart';

String _log = "";

int init(int val) {
_log = "init($val);";
return val;
}

test1() {
late var _ = init(1);
}

test2() {
late final _ = init(2);
}

test3() {
late int _ = init(3);
}

main() {
test1();
Expect.equals("", _log);
test2();
Expect.equals("", _log);
test3();
Expect.equals("", _log);
}
36 changes: 36 additions & 0 deletions LanguageFeatures/Wildcards/binding_A03_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

/// @assertion A local declaration whose name is `_` does not bind that name to
/// anything. This means you can have multiple local declarations named `_` in
/// the same namespace without a collision error. The initializer, if there is
/// one, is still executed, but the value is not accessible.
///
/// @description Checks that an initializer is executed for for-loop variables
/// declarations named `_`
/// @author sgrekhov22@gmail.com
// SharedOptions=--enable-experiment=wildcard-variables

import '../../Utils/expect.dart';

String _log = "";

int init(int val) {
_log += "init($val);";
return val;
}

main() {
for (int _ = init(1);;) {
break;
}
Expect.equals("init(1);", _log);
_log = "";

for (int _ = init(2), _ = init(3);;) {
break;
}
Expect.equals("init(2);init(3);", _log);
}

0 comments on commit 5bfdaf3

Please sign in to comment.