OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 for testing super operator calls | 4 // Dart test for testing super operator calls |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 | |
9 class A { | 8 class A { |
10 String val = ""; | 9 String val = ""; |
11 List things; | 10 List things; |
12 | 11 |
13 A() : things = ['D', 'a', 'r', 't', 42]; | 12 A() : things = ['D', 'a', 'r', 't', 42]; |
14 | 13 |
15 operator + (String s) { | 14 operator +(String s) { |
16 val = "${val}${s}"; | 15 val = "${val}${s}"; |
17 return this; | 16 return this; |
18 } | 17 } |
19 | 18 |
20 operator [] (i) { | 19 operator [](i) { |
21 return things[i]; | 20 return things[i]; |
22 } | 21 } |
23 | 22 |
24 operator []= (i, val) { | 23 operator []=(i, val) { |
25 return things[i] = val; | 24 return things[i] = val; |
26 } | 25 } |
27 } | 26 } |
28 | 27 |
29 | |
30 class B extends A { | 28 class B extends A { |
31 operator + (String s) { | 29 operator +(String s) { |
32 super + ("${s}${s}"); // Call A.operator+(this, "${s}${s}"). | 30 super + ("${s}${s}"); // Call A.operator+(this, "${s}${s}"). |
33 return this; | 31 return this; |
34 } | 32 } |
35 | 33 |
36 operator [] (i) { | 34 operator [](i) { |
37 var temp = super[i]; | 35 var temp = super[i]; |
38 if (temp is String) { | 36 if (temp is String) { |
39 return "$temp$temp"; | 37 return "$temp$temp"; |
40 } | 38 } |
41 return temp + temp; | 39 return temp + temp; |
42 } | 40 } |
43 | 41 |
44 operator []= (i, val) { | 42 operator []=(i, val) { |
45 // Make sure the index expression is only evaluated | 43 // Make sure the index expression is only evaluated |
46 // once in the presence of a compound assignment. | 44 // once in the presence of a compound assignment. |
47 return super[i++] += val; | 45 return super[i++] += val; |
48 } | 46 } |
49 | |
50 } | 47 } |
51 | 48 |
52 | |
53 class Autobianchi { | 49 class Autobianchi { |
54 g() => super[0]; | 50 g() => super[0]; |
55 } | 51 } |
56 | 52 |
57 | |
58 testRegression6403() { | 53 testRegression6403() { |
59 // Do not crash, throw exception instead | 54 // Do not crash, throw exception instead |
60 new Autobianchi().g(); | 55 new Autobianchi().g(); |
61 } | 56 } |
62 | 57 |
63 | 58 main() { |
64 main () { | |
65 var a = new A(); | 59 var a = new A(); |
66 a = a + "William"; // operator + of class A. | 60 a = a + "William"; // operator + of class A. |
67 Expect.equals("William", a.val); | 61 Expect.equals("William", a.val); |
68 Expect.equals("r", a[2]); // operator [] of class A. | 62 Expect.equals("r", a[2]); // operator [] of class A. |
69 | 63 |
70 a = new B(); | 64 a = new B(); |
71 a += "Tell"; // operator + of class B. | 65 a += "Tell"; // operator + of class B. |
72 Expect.equals("TellTell", a.val); | 66 Expect.equals("TellTell", a.val); |
73 Expect.equals("rr", a[2]); // operator [] of class B. | 67 Expect.equals("rr", a[2]); // operator [] of class B. |
74 | 68 |
75 a[4] = 1; // operator []= of class B. | 69 a[4] = 1; // operator []= of class B. |
76 Expect.equals(43, a.things[4]); | 70 Expect.equals(43, a.things[4]); |
77 Expect.equals(86, a[4]); | 71 Expect.equals(86, a[4]); |
78 | 72 |
79 Expect.throws(testRegression6403); | 73 Expect.throws(testRegression6403); |
80 } | 74 } |
OLD | NEW |