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 | |
5 // Tests all statement types. Not an exhaustive test of all statement semantics. | |
6 class StatementTest { | |
7 | |
8 StatementTest() {} | |
9 | |
10 static testMain() { | |
11 var test = new StatementTest(); | |
12 test.testIfStatement(); | |
13 test.testForLoop(); | |
14 test.testWhileLoops(); | |
15 test.testSwitch(); | |
16 test.testExceptions(); | |
17 test.testBreak(); | |
18 test.testContinue(); | |
19 test.testFunction(); | |
20 test.testReturn(); | |
21 } | |
22 | |
23 testIfStatement() { | |
24 // Basic if statements. | |
25 if (true) { | |
26 Expect.equals(true, true); | |
27 } else { | |
28 Expect.equals(false, true); | |
29 } | |
30 | |
31 if (false) { | |
32 Expect.equals(false, true); | |
33 } else { | |
34 Expect.equals(true, true); | |
35 } | |
36 } | |
37 | |
38 testForLoop() { | |
39 int count = 0, count2; | |
40 | |
41 // Basic for loop. | |
42 for (int i = 0; i < 10; ++i) { | |
43 ++count; | |
44 } | |
45 Expect.equals(10, count); | |
46 | |
47 // For loop with no 'var'. | |
48 count2 = 0; | |
49 for (count = 0; count < 5; ++count) { | |
50 ++count2; | |
51 } | |
52 Expect.equals(5, count); | |
53 Expect.equals(5, count2); | |
54 | |
55 // For loop with no initializer. | |
56 count = count2 = 0; | |
57 for (; count < 10; ++count) { | |
58 ++count2; | |
59 } | |
60 Expect.equals(10, count); | |
61 Expect.equals(10, count2); | |
62 | |
63 // For loop with no increment. | |
64 for (count = 0; count < 5; ) { | |
65 ++count; | |
66 } | |
67 Expect.equals(5, count); | |
68 | |
69 // For loop with no test. | |
70 for (count = 0; ; ++count) { | |
71 if (count == 10) { | |
72 break; | |
73 } | |
74 } | |
75 Expect.equals(10, count); | |
76 | |
77 // For loop with no nothing. | |
78 count = 0; | |
79 for (;;) { | |
80 if (count == 5) { | |
81 break; | |
82 } | |
83 ++count; | |
84 } | |
85 Expect.equals(5, count); | |
86 } | |
87 | |
88 testWhileLoops() { | |
89 // Basic while loop. | |
90 int count = 0; | |
91 while (count < 10) { | |
92 ++count; | |
93 } | |
94 Expect.equals(10, count); | |
95 | |
96 // Basic do loop. | |
97 count = 0; | |
98 do { | |
99 ++count; | |
100 } while (count < 5); | |
101 Expect.equals(5, count); | |
102 } | |
103 | |
104 testSwitch() { | |
105 // Int switch. | |
106 bool hit0, hit1, hitDefault; | |
107 for (int x = 0; x < 3; ++x) { | |
108 switch (x) { | |
109 case 0: hit0 = true; break; | |
110 case 1: hit1 = true; break; | |
111 default: hitDefault = true; break; | |
112 } | |
113 } | |
114 Expect.equals(true, hit0); | |
115 Expect.equals(true, hit1); | |
116 Expect.equals(true, hitDefault); | |
117 | |
118 // String switch. | |
119 var strings = ['a', 'b', 'c']; | |
120 bool hitA, hitB; | |
121 hitDefault = false; | |
122 for (int x = 0; x < 3; ++x) { | |
123 switch (strings[x]) { | |
124 case 'a': hitA = true; break; | |
125 case 'b': hitB = true; break; | |
126 default: hitDefault = true; break; | |
127 } | |
128 } | |
129 Expect.equals(true, hitA); | |
130 Expect.equals(true, hitB); | |
131 Expect.equals(true, hitDefault); | |
132 } | |
133 | |
134 testExceptions() { | |
135 // TODO(jgw): Better tests once all the exception semantics are worked out. | |
136 bool hitCatch, hitFinally; | |
137 try { | |
138 throw "foo"; | |
139 } catch (var e) { | |
140 Expect.equals(true, e == "foo"); | |
141 hitCatch = true; | |
142 } finally { | |
143 hitFinally = true; | |
144 } | |
145 | |
146 Expect.equals(true, hitCatch); | |
147 Expect.equals(true, hitFinally); | |
148 } | |
149 | |
150 testBreak() { | |
151 var ints = [ | |
152 [ 32, 87, 3, 589 ], | |
153 [ 12, 1076, 2000, 8 ], | |
154 [ 622, 127, 77, 955 ] | |
155 ]; | |
156 int i, j = 0; | |
157 bool foundIt = false; | |
158 | |
159 search: | |
160 for (i = 0; i < ints.length; i++) { | |
161 for (j = 0; j < ints[i].length; j++) { | |
162 if (ints[i][j] == 12) { | |
163 foundIt = true; | |
164 break search; | |
165 } | |
166 } | |
167 } | |
168 Expect.equals(true, foundIt); | |
169 } | |
170 | |
171 testContinue() { | |
172 String searchMe = "Look for a substring in me"; | |
173 String substring = "sub"; | |
174 bool foundIt = false; | |
175 int max = searchMe.length - substring.length; | |
176 | |
177 test: | |
178 for (int i = 0; i <= max; i++) { | |
179 int n = substring.length; | |
180 int j = i; | |
181 int k = 0; | |
182 while (n-- != 0) { | |
183 if (searchMe[j++] != substring[k++]) { | |
184 continue test; | |
185 } | |
186 } | |
187 foundIt = true; | |
188 break test; | |
189 } | |
190 } | |
191 | |
192 testFunction() { | |
193 int foo() { | |
194 return 42; | |
195 } | |
196 Expect.equals(42, foo()); | |
197 } | |
198 | |
199 void testReturn() { | |
200 if (true) { | |
201 return; | |
202 } | |
203 Expect.equals(true, false); | |
204 } | |
205 } | |
206 | |
207 main() { | |
208 StatementTest.testMain(); | |
209 } | |
OLD | NEW |