Skip to content

Commit

Permalink
#2145. Add more tests for Variable (#2146)
Browse files Browse the repository at this point in the history
Add more tests for variables. Note that Language/Classes/Instance_Variables/constant_t01.dart, Language/Variables/covariant_t01.dart, Language/Variables/static_variable_t02.dart have syntax errors intentionally.
  • Loading branch information
sgrekhov authored Jul 26, 2023
1 parent f500a37 commit a4d7265
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 19 deletions.
14 changes: 12 additions & 2 deletions Language/Classes/Instance_Variables/constant_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@
class C {
const v1 = 1;
//^
//^^^^^
// [analyzer] unspecified
// [cfe] unspecified

const var v2 = 2;
//^^^^^
// [analyzer] unspecified
// [cfe] unspecified

const int v3 = 3;
//^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
new C().v1;
print(C);
}
44 changes: 44 additions & 0 deletions Language/Variables/covariant_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 It is a compile-time error for the declaration of a variable
/// which is not an instance variable to include the modifier covariant.
///
/// @description Checks that a compile-time error if not-instance variable has a
/// modifier `covariant`
/// @author [email protected]
/**/covariant var c1 = 1;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

/**/covariant int c2 = 2;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

class C {
static covariant var c3 = 3;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

static covariant int c4 = 4;
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}

main() {
covariant var c5 = 5;
//^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

covariant int c6 = 6;
//^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
}
75 changes: 75 additions & 0 deletions Language/Variables/multiple_t01.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 An ⟨initializedVariableDeclaration⟩ that declares two or more
/// variables is equivalent to multiple variable declarations declaring the same
/// set of variable names, in the same order, with the same initialization,
/// type, and modifiers
///
/// @description Checks that two or more variables is equivalent to multiple
/// variable declarations declaring the same set of variable names, in the same
/// order, with the same initialization, type, and modifiers
/// @author [email protected]
import "../../Utils/expect.dart";

var v1 = 1, v2, v3 = "3";
int? i1 = 1, i2, i3 = i1;
final int fi1 = 1, fi2 = fi1, fi3 = fi2;

class C {
static var s1 = 1, s2, s3 = "3";
static int? si1 = 1, si2, si3 = si1;
static final int sfi1 = 1, sfi2 = sfi1, sfi3 = sfi2;

var _v1 = 1, _v2, _v3 = "3";
int? _i1 = 1, _i2, _i3 = 3;
final int _fi1 = 1, _fi2 = 2;
}

main() {
var lv1 = 1, lv2, lv3 = "3";
int? li1 = 1, li2, li3 = li1;
final int lfi1 = 1, lfi2 = lfi1, lfi3 = lfi2;

Expect.equals(1, v1);
Expect.isNull(v2);
Expect.equals("3", v3);
Expect.equals(1, i1);
Expect.isNull(i2);
Expect.equals(1, i3);
Expect.equals(1, fi1);
Expect.equals(1, fi1);
Expect.equals(1, fi3);

Expect.equals(1, C.s1);
Expect.isNull(C.s2);
Expect.equals("3", C.s3);
Expect.equals(1, C.si1);
Expect.isNull(C.si2);
Expect.equals(1, C.si3);
Expect.equals(1, C.sfi1);
Expect.equals(1, C.sfi1);
Expect.equals(1, C.sfi3);

C c = C();
Expect.equals(1, c._v1);
Expect.isNull(c._v2);
Expect.equals("3", c._v3);
Expect.equals(1, c._i1);
Expect.isNull(c._i2);
Expect.equals(3, c._i3);
Expect.equals(1, c._fi1);
Expect.equals(1, c._fi1);

Expect.equals(1, lv1);
Expect.isNull(lv2);
Expect.equals("3", lv3);
Expect.equals(1, li1);
Expect.isNull(li2);
Expect.equals(1, li3);
Expect.equals(1, lfi1);
Expect.equals(1, lfi1);
Expect.equals(1, lfi3);
}
77 changes: 77 additions & 0 deletions Language/Variables/multiple_t02.dart
Original file line number Diff line number Diff line change
@@ -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.

/// @assertion An ⟨initializedVariableDeclaration⟩ that declares two or more
/// variables is equivalent to multiple variable declarations declaring the same
/// set of variable names, in the same order, with the same initialization,
/// type, and modifiers
///
/// @description Checks that two or more variables is equivalent to multiple
/// variable declarations declaring the same set of variable names, in the same
/// order, with the same initialization, type, and modifiers. Test late
/// variables.
/// @author [email protected]
import "../../Utils/expect.dart";

class Log {
static String log = "";
static void clearLog() {
log = "";
}

int val;
Log(this.val) {
log += "$val;";
}
}

late var v1 = Log(1), v2 = Log(2);
late final Log v3 = Log(3), v4 = v3;

class C {
static late var v5 = Log(5), v6 = Log(6);
static late final Log v7 = Log(7), v8 = v7;

late var v9 = Log(9), v10 = Log(10);
late final Log v11 = Log(11), v12 = v11;
}

main() {
late var v13 = Log(13), v14 = Log(14);
late final Log v15 = Log(15), v16 = v15;

Expect.equals("", Log.log);
print(v1);
Expect.equals("1;", Log.log);
print(v2);
Expect.equals("1;2;", Log.log);
print(v4);
Expect.equals("1;2;3;", Log.log);

Log.clearLog();
print(C.v5);
Expect.equals("5;", Log.log);
print(C.v6);
Expect.equals("5;6;", Log.log);
print(C.v8);
Expect.equals("5;6;7;", Log.log);

Log.clearLog();
C c = C();
print(c.v9);
Expect.equals("9;", Log.log);
print(c.v10);
Expect.equals("9;10;", Log.log);
print(c.v12);
Expect.equals("9;10;11;", Log.log);

Log.clearLog();
print(v13);
Expect.equals("13;", Log.log);
print(v14);
Expect.equals("13;14;", Log.log);
print(v16);
Expect.equals("13;14;15;", Log.log);
}
11 changes: 4 additions & 7 deletions Language/Variables/static_variable_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
// 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 static variable is a variable that is not associated with
/// a particular instance, but rather with an entire library or class. Static
/// variables include library variables and class variables. Class variables
/// are variables whose declaration is immediately nested inside a class
/// declaration and includes the modifer static.
/// A library variable is implicitly static. It is a compile-time error to
/// preface a top-level variable declaration with the built-in identier static.
/// @assertion A static variable is a variable whose declaration is immediately
/// nested inside a class, mixin, enum, or extension declaration and includes
/// the modifier `static`
///
/// @description Checks that a static variable is not associated with a
/// particular instance.
/// @author kaigorodov
Expand Down
35 changes: 25 additions & 10 deletions Language/Variables/static_variable_t02.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@
// 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 static variable is a variable that is not associated with
/// a particular instance, but rather with an entire library or class. Static
/// variables include library variables and class variables. Class variables
/// are variables whose declaration is immediately nested inside a class
/// declaration and includes the modifer static.
/// A library variable is implicitly static. It is a compile-time error to
/// preface a top-level variable declaration with the built-in identier static.
/// @assertion It is a compile-time error if a library variable declaration has
/// the modifier `static`.
///
/// @description Checks that it is a compile-time error to preface a top-level
/// variable declaration with the built-in identifier static.
/// variable declaration with the built-in identifier `static`.
/// @author kaigorodov
/**/static var v1 = 1;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

/**/static int v2 = 1;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

/**/static final v3 = 1;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

/**/late static final v4 = 1;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

static var foo = 1;
//^
/**/static late final v5 = 1;
// ^^^^^^
// [analyzer] unspecified
// [cfe] unspecified

Expand Down

0 comments on commit a4d7265

Please sign in to comment.