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 | 4 |
5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 switcher(val) { | 7 switcher(val) { |
8 var x = 0; | 8 var x = 0; |
9 switch (val) { | 9 switch (val) { |
10 case 1: | 10 case 1: |
11 x = 100; | 11 x = 100; |
12 break; | 12 break; |
13 case 2: | 13 case 2: |
14 x = 200; | 14 x = 200; |
15 break; | 15 break; |
16 case 3: | 16 case 3: |
17 x = 300; | 17 x = 300; |
18 break; | 18 break; |
19 default: | 19 default: |
20 return 400; | 20 return 400; |
21 break; // Intentional dead code (regression test for crash). | 21 break; // Intentional dead code (regression test for crash). |
22 } | 22 } |
23 return x; | 23 return x; |
24 } | 24 } |
25 | 25 |
26 | |
27 // Check unambiguated grammar allowing multiple lables per case/default. | 26 // Check unambiguated grammar allowing multiple lables per case/default. |
28 switcher2(val) { | 27 switcher2(val) { |
29 var x = 0; | 28 var x = 0; |
30 switch (val) { | 29 switch (val) { |
31 foo: | 30 foo: |
32 bar: | 31 bar: |
33 case 1: | 32 case 1: |
34 baz: | 33 baz: |
35 case 2: | 34 case 2: |
36 fez: { | 35 fez: |
| 36 { |
37 x = 100; | 37 x = 100; |
38 break fez; | 38 break fez; |
39 } | 39 } |
40 break; | 40 break; |
41 hest: | 41 hest: |
42 fisk: | 42 fisk: |
43 case 3: | 43 case 3: |
44 case 4: | 44 case 4: |
45 svin: | 45 svin: |
46 default: | 46 default: |
47 barber: { | 47 barber: |
| 48 { |
48 if (val > 2) { | 49 if (val > 2) { |
49 x = 200; | 50 x = 200; |
50 break; | 51 break; |
51 } else { | 52 } else { |
52 // Enable when continue to switch-case is implemented. | 53 // Enable when continue to switch-case is implemented. |
53 continue hest; | 54 continue hest; |
54 } | 55 } |
55 } | 56 } |
56 } | 57 } |
57 return x; | 58 return x; |
58 } | 59 } |
59 | 60 |
60 | |
61 badswitches(val) { | 61 badswitches(val) { |
62 // Test some badly formed switch bodies. | 62 // Test some badly formed switch bodies. |
63 // 01 - a label/statement without a following case/default. | 63 // 01 - a label/statement without a following case/default. |
64 // 02 - a label without a following case/default or statement. | 64 // 02 - a label without a following case/default or statement. |
65 switch (val) { | 65 switch (val) { |
66 foo: break; // //# 01: compile-time error | 66 foo: break; // //# 01: compile-time error |
67 case 2: // //# 02: compile-time error | 67 case 2: // //# 02: compile-time error |
68 foo: // //# 02: continued | 68 foo: // //# 02: continued |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 main() { | 72 main() { |
73 Expect.equals(100, switcher(1)); | 73 Expect.equals(100, switcher(1)); |
74 Expect.equals(200, switcher(2)); | 74 Expect.equals(200, switcher(2)); |
75 Expect.equals(300, switcher(3)); | 75 Expect.equals(300, switcher(3)); |
76 Expect.equals(400, switcher(4)); | 76 Expect.equals(400, switcher(4)); |
77 | 77 |
78 Expect.equals(100, switcher2(1)); | 78 Expect.equals(100, switcher2(1)); |
79 Expect.equals(100, switcher2(2)); | 79 Expect.equals(100, switcher2(2)); |
80 Expect.equals(200, switcher2(3)); | 80 Expect.equals(200, switcher2(3)); |
81 Expect.equals(200, switcher2(4)); | 81 Expect.equals(200, switcher2(4)); |
82 Expect.equals(200, switcher2(5)); | 82 Expect.equals(200, switcher2(5)); |
83 | 83 |
84 badswitches(42); | 84 badswitches(42); |
85 } | 85 } |
OLD | NEW |