| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // Dart test program for testing setting/getting/initializing static fields. | 4 // Dart test program for testing setting/getting/initializing static fields. |
| 5 | 5 |
| 6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 | 7 |
| 8 class First { | 8 class First { |
| 9 First() {} | 9 First() {} |
| 10 static var a; | 10 static var a; |
| 11 static var b; | 11 static var b; |
| 12 static const int c = 1; | 12 static const int c = 1; |
| 13 static setValues() { | 13 static setValues() { |
| 14 a = 24; | 14 a = 24; |
| 15 b = 10; | 15 b = 10; |
| 16 return a + b + c; | 16 return a + b + c; |
| 17 } | 17 } |
| 18 } | 18 } |
| 19 | 19 |
| 20 |
| 20 class InitializerTest { | 21 class InitializerTest { |
| 21 static var one; | 22 static var one; |
| 22 static var two = 2; | 23 static var two = 2; |
| 23 static var three = 2; | 24 static var three = 2; |
| 24 | 25 |
| 25 static checkValueOfThree() { | 26 static checkValueOfThree() { |
| 26 // We need to keep this check separate to prevent three from | 27 // We need to keep this check separate to prevent three from |
| 27 // getting initialized before the += is executed. | 28 // getting initialized before the += is executed. |
| 28 Expect.equals(3, three); | 29 Expect.equals(3, three); |
| 29 } | 30 } |
| 30 | 31 |
| 31 static void testStaticFieldInitialization() { | 32 static void testStaticFieldInitialization() { |
| 32 Expect.equals(null, one); | 33 Expect.equals(null, one); |
| 33 Expect.equals(2, two); | 34 Expect.equals(2, two); |
| 34 one = 11; | 35 one = 11; |
| 35 two = 22; | 36 two = 22; |
| 36 Expect.equals(11, one); | 37 Expect.equals(11, one); |
| 37 Expect.equals(22, two); | 38 Expect.equals(22, two); |
| 38 | 39 |
| 39 // Assignment operators exercise a different code path. Make sure | 40 // Assignment operators exercise a different code path. Make sure |
| 40 // that initialization works here as well. | 41 // that initialization works here as well. |
| 41 three += 1; | 42 three += 1; |
| 42 checkValueOfThree(); | 43 checkValueOfThree(); |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 47 |
| 46 class StaticFieldTest { | 48 class StaticFieldTest { |
| 47 static testMain() { | 49 static testMain() { |
| 48 First.a = 3; | 50 First.a = 3; |
| 49 First.b = First.a; | 51 First.b = First.a; |
| 50 Expect.equals(3, First.a); | 52 Expect.equals(3, First.a); |
| 51 Expect.equals(First.a, First.b); | 53 Expect.equals(First.a, First.b); |
| 52 First.b = (First.a = 10); | 54 First.b = (First.a = 10); |
| 53 Expect.equals(10, First.a); | 55 Expect.equals(10, First.a); |
| 54 Expect.equals(10, First.b); | 56 Expect.equals(10, First.b); |
| 55 First.b = First.a = 15; | 57 First.b = First.a = 15; |
| 56 Expect.equals(15, First.a); | 58 Expect.equals(15, First.a); |
| 57 Expect.equals(15, First.b); | 59 Expect.equals(15, First.b); |
| 58 Expect.equals(35, First.setValues()); | 60 Expect.equals(35, First.setValues()); |
| 59 Expect.equals(24, First.a); | 61 Expect.equals(24, First.a); |
| 60 Expect.equals(10, First.b); | 62 Expect.equals(10, First.b); |
| 61 } | 63 } |
| 62 } | 64 } |
| 63 | 65 |
| 66 |
| 64 class StaticField1RunNegativeTest { | 67 class StaticField1RunNegativeTest { |
| 65 static // //# 01: static type warning, runtime error | 68 static // //# 01: static type warning, runtime error |
| 66 var x; | 69 var x; |
| 67 testMain() { | 70 testMain() { |
| 68 var foo = new StaticField1RunNegativeTest(); | 71 var foo = new StaticField1RunNegativeTest(); |
| 69 print(x); // Used to compile 'x' and force any errors. | 72 print(x); // Used to compile 'x' and force any errors. |
| 70 var result = foo.x; | 73 var result = foo.x; |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 | 76 |
| 74 class StaticField1aRunNegativeTest { | 77 class StaticField1aRunNegativeTest { |
| 75 static // //# 02: static type warning, runtime error | 78 static // //# 02: static type warning, runtime error |
| 76 void m() {} | 79 void m() {} |
| 77 | 80 |
| 78 testMain() { | 81 testMain() { |
| 79 var foo = new StaticField1aRunNegativeTest(); | 82 var foo = new StaticField1aRunNegativeTest(); |
| 80 print(m); // Used to compile 'm' and force any errors. | 83 print(m); // Used to compile 'm' and force any errors. |
| 81 var result = foo.m; | 84 var result = foo.m; |
| 82 } | 85 } |
| 83 } | 86 } |
| 84 | 87 |
| 85 class StaticField2RunNegativeTest { | 88 class StaticField2RunNegativeTest { |
| 86 static //# 03: static type warning, runtime error | 89 static //# 03: static type warning, runtime error |
| 87 var x; | 90 var x; |
| 88 | 91 |
| 89 testMain() { | 92 testMain() { |
| 90 var foo = new StaticField2RunNegativeTest(); | 93 var foo = new StaticField2RunNegativeTest(); |
| 91 print(x); // Used to compile 'x' and force any errors. | 94 print(x); // Used to compile 'x' and force any errors. |
| 92 foo.x = 1; | 95 foo.x = 1; |
| 93 } | 96 } |
| 94 } | 97 } |
| 95 | 98 |
| 96 class StaticField2aRunNegativeTest { | 99 class StaticField2aRunNegativeTest { |
| 97 static // //# 04: static type warning, runtime error | 100 static // //# 04: static type warning, runtime error |
| 98 void m() {} | 101 void m() {} |
| 99 | 102 |
| 100 testMain() { | 103 testMain() { |
| 101 var foo = new StaticField2aRunNegativeTest(); | 104 var foo = new StaticField2aRunNegativeTest(); |
| 102 print(m); // Used to compile 'm' and force any errors. | 105 print(m); // Used to compile 'm' and force any errors. |
| 103 foo.m = 1; //# 04:continued | 106 foo.m = 1; //# 04:continued |
| 104 } | 107 } |
| 105 } | 108 } |
| 106 | 109 |
| 107 main() { | 110 main() { |
| 108 StaticFieldTest.testMain(); | 111 StaticFieldTest.testMain(); |
| 109 InitializerTest.testStaticFieldInitialization(); | 112 InitializerTest.testStaticFieldInitialization(); |
| 110 new StaticField1RunNegativeTest().testMain(); | 113 new StaticField1RunNegativeTest().testMain(); |
| 111 new StaticField1aRunNegativeTest().testMain(); | 114 new StaticField1aRunNegativeTest().testMain(); |
| 112 new StaticField2RunNegativeTest().testMain(); | 115 new StaticField2RunNegativeTest().testMain(); |
| 113 new StaticField2aRunNegativeTest().testMain(); | 116 new StaticField2aRunNegativeTest().testMain(); |
| 114 } | 117 } |
| OLD | NEW |