| 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 | 4 |
| 5 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 // Tests all statement types. Not an exhaustive test of all statement semantics. | 7 // Tests all statement types. Not an exhaustive test of all statement semantics. |
| 8 class StatementTest { | 8 class StatementTest { |
| 9 | |
| 10 StatementTest() {} | 9 StatementTest() {} |
| 11 | 10 |
| 12 static testMain() { | 11 static testMain() { |
| 13 var test = new StatementTest(); | 12 var test = new StatementTest(); |
| 14 test.testIfStatement(); | 13 test.testIfStatement(); |
| 15 test.testForLoop(); | 14 test.testForLoop(); |
| 16 test.testWhileLoops(); | 15 test.testWhileLoops(); |
| 17 test.testSwitch(); | 16 test.testSwitch(); |
| 18 test.testExceptions(); | 17 test.testExceptions(); |
| 19 test.testBreak(); | 18 test.testBreak(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 55 |
| 57 // For loop with no initializer. | 56 // For loop with no initializer. |
| 58 count = count2 = 0; | 57 count = count2 = 0; |
| 59 for (; count < 10; ++count) { | 58 for (; count < 10; ++count) { |
| 60 ++count2; | 59 ++count2; |
| 61 } | 60 } |
| 62 Expect.equals(10, count); | 61 Expect.equals(10, count); |
| 63 Expect.equals(10, count2); | 62 Expect.equals(10, count2); |
| 64 | 63 |
| 65 // For loop with no increment. | 64 // For loop with no increment. |
| 66 for (count = 0; count < 5; ) { | 65 for (count = 0; count < 5;) { |
| 67 ++count; | 66 ++count; |
| 68 } | 67 } |
| 69 Expect.equals(5, count); | 68 Expect.equals(5, count); |
| 70 | 69 |
| 71 // For loop with no test. | 70 // For loop with no test. |
| 72 for (count = 0; ; ++count) { | 71 for (count = 0;; ++count) { |
| 73 if (count == 10) { | 72 if (count == 10) { |
| 74 break; | 73 break; |
| 75 } | 74 } |
| 76 } | 75 } |
| 77 Expect.equals(10, count); | 76 Expect.equals(10, count); |
| 78 | 77 |
| 79 // For loop with no nothing. | 78 // For loop with no nothing. |
| 80 count = 0; | 79 count = 0; |
| 81 for (;;) { | 80 for (;;) { |
| 82 if (count == 5) { | 81 if (count == 5) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 101 ++count; | 100 ++count; |
| 102 } while (count < 5); | 101 } while (count < 5); |
| 103 Expect.equals(5, count); | 102 Expect.equals(5, count); |
| 104 } | 103 } |
| 105 | 104 |
| 106 testSwitch() { | 105 testSwitch() { |
| 107 // Int switch. | 106 // Int switch. |
| 108 bool hit0, hit1, hitDefault; | 107 bool hit0, hit1, hitDefault; |
| 109 for (int x = 0; x < 3; ++x) { | 108 for (int x = 0; x < 3; ++x) { |
| 110 switch (x) { | 109 switch (x) { |
| 111 case 0: hit0 = true; break; | 110 case 0: |
| 112 case 1: hit1 = true; break; | 111 hit0 = true; |
| 113 default: hitDefault = true; break; | 112 break; |
| 113 case 1: |
| 114 hit1 = true; |
| 115 break; |
| 116 default: |
| 117 hitDefault = true; |
| 118 break; |
| 114 } | 119 } |
| 115 } | 120 } |
| 116 Expect.equals(true, hit0); | 121 Expect.equals(true, hit0); |
| 117 Expect.equals(true, hit1); | 122 Expect.equals(true, hit1); |
| 118 Expect.equals(true, hitDefault); | 123 Expect.equals(true, hitDefault); |
| 119 | 124 |
| 120 // String switch. | 125 // String switch. |
| 121 var strings = ['a', 'b', 'c']; | 126 var strings = ['a', 'b', 'c']; |
| 122 bool hitA, hitB; | 127 bool hitA, hitB; |
| 123 hitDefault = false; | 128 hitDefault = false; |
| 124 for (int x = 0; x < 3; ++x) { | 129 for (int x = 0; x < 3; ++x) { |
| 125 switch (strings[x]) { | 130 switch (strings[x]) { |
| 126 case 'a': hitA = true; break; | 131 case 'a': |
| 127 case 'b': hitB = true; break; | 132 hitA = true; |
| 128 default: hitDefault = true; break; | 133 break; |
| 134 case 'b': |
| 135 hitB = true; |
| 136 break; |
| 137 default: |
| 138 hitDefault = true; |
| 139 break; |
| 129 } | 140 } |
| 130 } | 141 } |
| 131 Expect.equals(true, hitA); | 142 Expect.equals(true, hitA); |
| 132 Expect.equals(true, hitB); | 143 Expect.equals(true, hitB); |
| 133 Expect.equals(true, hitDefault); | 144 Expect.equals(true, hitDefault); |
| 134 } | 145 } |
| 135 | 146 |
| 136 testExceptions() { | 147 testExceptions() { |
| 137 // TODO(jgw): Better tests once all the exception semantics are worked out. | 148 // TODO(jgw): Better tests once all the exception semantics are worked out. |
| 138 bool hitCatch, hitFinally; | 149 bool hitCatch, hitFinally; |
| 139 try { | 150 try { |
| 140 throw "foo"; | 151 throw "foo"; |
| 141 } catch (e) { | 152 } catch (e) { |
| 142 Expect.equals(true, e == "foo"); | 153 Expect.equals(true, e == "foo"); |
| 143 hitCatch = true; | 154 hitCatch = true; |
| 144 } finally { | 155 } finally { |
| 145 hitFinally = true; | 156 hitFinally = true; |
| 146 } | 157 } |
| 147 | 158 |
| 148 Expect.equals(true, hitCatch); | 159 Expect.equals(true, hitCatch); |
| 149 Expect.equals(true, hitFinally); | 160 Expect.equals(true, hitFinally); |
| 150 } | 161 } |
| 151 | 162 |
| 152 testBreak() { | 163 testBreak() { |
| 153 var ints = [ | 164 var ints = [ |
| 154 [ 32, 87, 3, 589 ], | 165 [32, 87, 3, 589], |
| 155 [ 12, 1076, 2000, 8 ], | 166 [12, 1076, 2000, 8], |
| 156 [ 622, 127, 77, 955 ] | 167 [622, 127, 77, 955] |
| 157 ]; | 168 ]; |
| 158 int i, j = 0; | 169 int i, j = 0; |
| 159 bool foundIt = false; | 170 bool foundIt = false; |
| 160 | 171 |
| 161 search: | 172 search: |
| 162 for (i = 0; i < ints.length; i++) { | 173 for (i = 0; i < ints.length; i++) { |
| 163 for (j = 0; j < ints[i].length; j++) { | 174 for (j = 0; j < ints[i].length; j++) { |
| 164 if (ints[i][j] == 12) { | 175 if (ints[i][j] == 12) { |
| 165 foundIt = true; | 176 foundIt = true; |
| 166 break search; | 177 break search; |
| 167 } | 178 } |
| 168 } | 179 } |
| 169 } | 180 } |
| 170 Expect.equals(true, foundIt); | 181 Expect.equals(true, foundIt); |
| 171 } | 182 } |
| 172 | 183 |
| 173 testContinue() { | 184 testContinue() { |
| 174 String searchMe = "Look for a substring in me"; | 185 String searchMe = "Look for a substring in me"; |
| 175 String substring = "sub"; | 186 String substring = "sub"; |
| 176 bool foundIt = false; | 187 bool foundIt = false; |
| 177 int max = searchMe.length - substring.length; | 188 int max = searchMe.length - substring.length; |
| 178 | 189 |
| 179 test: | 190 test: |
| 180 for (int i = 0; i <= max; i++) { | 191 for (int i = 0; i <= max; i++) { |
| 181 int n = substring.length; | 192 int n = substring.length; |
| 182 int j = i; | 193 int j = i; |
| 183 int k = 0; | 194 int k = 0; |
| 184 while (n-- != 0) { | 195 while (n-- != 0) { |
| 185 if (searchMe[j++] != substring[k++]) { | 196 if (searchMe[j++] != substring[k++]) { |
| 186 continue test; | 197 continue test; |
| 187 } | 198 } |
| 188 } | 199 } |
| 189 foundIt = true; | 200 foundIt = true; |
| 190 break test; | 201 break test; |
| 191 } | 202 } |
| 192 } | 203 } |
| 193 | 204 |
| 194 testFunction() { | 205 testFunction() { |
| 195 int foo() { | 206 int foo() { |
| 196 return 42; | 207 return 42; |
| 197 } | 208 } |
| 209 |
| 198 Expect.equals(42, foo()); | 210 Expect.equals(42, foo()); |
| 199 } | 211 } |
| 200 | 212 |
| 201 void testReturn() { | 213 void testReturn() { |
| 202 if (true) { | 214 if (true) { |
| 203 return; | 215 return; |
| 204 } | 216 } |
| 205 Expect.equals(true, false); | 217 Expect.equals(true, false); |
| 206 } | 218 } |
| 207 } | 219 } |
| 208 | 220 |
| 209 main() { | 221 main() { |
| 210 StatementTest.testMain(); | 222 StatementTest.testMain(); |
| 211 } | 223 } |
| OLD | NEW |