| 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 // Test switch statement. | 4 // Test switch statement. |
| 5 | 5 |
| 6 class Switcher { | 6 class Switcher { |
| 7 | 7 |
| 8 Switcher() { } | 8 Switcher() { } |
| 9 | 9 |
| 10 test1 (val) { | 10 test1 (val) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 Expect.equals(200, s.test1(2)); | 39 Expect.equals(200, s.test1(2)); |
| 40 Expect.equals(200, s.test1(3)); | 40 Expect.equals(200, s.test1(3)); |
| 41 Expect.equals(400, s.test1(4)); | 41 Expect.equals(400, s.test1(4)); |
| 42 Expect.equals(400, s.test1(5)); | 42 Expect.equals(400, s.test1(5)); |
| 43 | 43 |
| 44 Expect.equals(200, s.test2(1)); | 44 Expect.equals(200, s.test2(1)); |
| 45 Expect.equals(400, s.test2(2)); | 45 Expect.equals(400, s.test2(2)); |
| 46 } | 46 } |
| 47 } | 47 } |
| 48 | 48 |
| 49 |
| 50 class Enum { |
| 51 static const Enum e1 = const Enum(1); |
| 52 static const Enum e2 = const Enum(2); |
| 53 static const Enum e3 = const Enum(3); |
| 54 final int id; |
| 55 const Enum(this.id); |
| 56 } |
| 57 |
| 58 void testSwitchEnum(Enum input, int expect) { |
| 59 int result = null; |
| 60 switch (input) { |
| 61 case Enum.e1: |
| 62 result = 10; |
| 63 break; |
| 64 case Enum.e2: |
| 65 result = 20; |
| 66 break; |
| 67 case Enum.e3: |
| 68 result = 30; |
| 69 break; |
| 70 default: |
| 71 result = 40; |
| 72 } |
| 73 Expect.equals(expect, result); |
| 74 } |
| 75 |
| 76 const int ic1 = 1; |
| 77 const int ic2 = 2; |
| 78 void testSwitchIntExpression(int input, int expect) { |
| 79 int result = null; |
| 80 switch (input) { |
| 81 case 1 + 1: // 2 |
| 82 case ic1 + 2: // 3 |
| 83 result = 11; |
| 84 break; |
| 85 case ic2 * 2: // 4 |
| 86 case 1 * 5: // 5 |
| 87 result = 21; |
| 88 break; |
| 89 case ic1 % ic2 + 5: // 6 |
| 90 result = 31; |
| 91 break; |
| 92 } |
| 93 Expect.equals(expect, result); |
| 94 } |
| 95 |
| 96 void testSwitchBool(bool input, int expect) { |
| 97 int result = null; |
| 98 switch (input) { |
| 99 case true: |
| 100 result = 12; |
| 101 break; |
| 102 case false: |
| 103 result = 22; |
| 104 } |
| 105 Expect.equals(expect, result); |
| 106 } |
| 107 |
| 49 main() { | 108 main() { |
| 50 SwitchTest.testMain(); | 109 SwitchTest.testMain(); |
| 110 |
| 111 testSwitchEnum(Enum.e1, 10); |
| 112 testSwitchEnum(Enum.e2, 20); |
| 113 testSwitchEnum(Enum.e3, 30); |
| 114 testSwitchEnum(null, 40); |
| 115 |
| 116 testSwitchIntExpression(2, 11); |
| 117 testSwitchIntExpression(3, 11); |
| 118 testSwitchIntExpression(4, 21); |
| 119 testSwitchIntExpression(5, 21); |
| 120 testSwitchIntExpression(6, 31); |
| 121 testSwitchIntExpression(7, null); |
| 122 |
| 123 testSwitchBool(true, 12); |
| 124 testSwitchBool(false, 22); |
| 51 } | 125 } |
| OLD | NEW |