OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // Test switch statement using labels. | |
5 | |
6 class Switcher { | |
7 | |
8 Switcher() { } | |
9 | |
10 say1 (sound) { | |
11 var x = 0; | |
12 switch (sound) { | |
13 MOO: | |
14 case "moo": | |
15 x = 100; | |
16 break; | |
17 case "woof": | |
18 x = 200; | |
19 continue MOO; | |
20 default: | |
21 x = 300; | |
22 break; | |
23 } | |
24 return x; | |
25 } | |
26 | |
27 say2 (sound) { | |
28 var x = 0; | |
29 switch (sound) { | |
30 WOOF: | |
31 case "woof": | |
32 x = 200; | |
33 break; | |
34 case "moo": | |
35 x = 100; | |
36 continue WOOF; | |
37 default: | |
38 x = 300; | |
39 break; | |
40 } | |
41 return x; | |
42 } | |
43 | |
44 // forward label to outer switch | |
45 say3 (animal, sound) { | |
46 var x = 0; | |
47 switch (animal) { | |
48 case "cow": | |
49 switch (sound) { | |
50 case "moo": | |
51 x = 100; break; | |
52 case "muh": | |
53 x = 200; break; | |
54 default: | |
55 continue NIX_UNDERSTAND; | |
56 } | |
57 break; | |
58 case "dog": | |
59 if (sound == "woof") { | |
60 x = 300; | |
61 } else { | |
62 continue NIX_UNDERSTAND; | |
63 } | |
64 break; | |
65 NIX_UNDERSTAND: | |
66 case "unicorn": | |
67 x = 400; | |
68 break; | |
69 default: | |
70 x = 500; | |
71 break; | |
72 } | |
73 return x; | |
74 } | |
75 } | |
76 | |
77 class SwitchLabelTest { | |
78 static testMain() { | |
79 Switcher s = new Switcher(); | |
80 Expect.equals(100, s.say1("moo")); | |
81 Expect.equals(100, s.say1("woof")); | |
82 Expect.equals(300, s.say1("cockadoodledoo")); | |
83 | |
84 Expect.equals(200, s.say2("moo")); | |
85 Expect.equals(200, s.say2("woof")); | |
86 Expect.equals(300, s.say2("")); // Dead unicorn says nothing. | |
87 | |
88 Expect.equals(100, s.say3("cow", "moo")); | |
89 Expect.equals(200, s.say3("cow", "muh")); | |
90 Expect.equals(400, s.say3("cow", "boeh")); // Don't ask. | |
91 Expect.equals(300, s.say3("dog", "woof")); | |
92 Expect.equals(400, s.say3("dog", "boj")); // Ĉu vi parolas Esperanton? | |
93 Expect.equals(400, s.say3("unicorn", "")); // Still dead. | |
94 Expect.equals(500, s.say3("angry bird", "whoooo")); | |
95 } | |
96 } | |
97 | |
98 main() { | |
99 SwitchLabelTest.testMain(); | |
100 } | |
OLD | NEW |