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