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 // Dart test program for testing the instanceof operation. | 4 // Dart test program for testing the instanceof operation. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 // In the type test 'e is T', it is a run-time error if T does not denote a type | 8 // In the type test 'e is T', if T does not denote a type available in the |
9 // available in the current lexical scope. | 9 // current lexical scope, then T is mapped to dynamic and the test succeeds. |
10 | 10 |
11 isCheckedMode() { | 11 isCheckedMode() { |
12 try { | 12 try { |
13 var i = 1; | 13 var i = 1; |
14 String s = i; | 14 String s = i; |
15 return false; | 15 return false; |
16 } catch (e) { | 16 } catch (e) { |
17 return true; | 17 return true; |
18 } | 18 } |
19 } | 19 } |
20 | 20 |
21 testAll() { | 21 testAll() { |
22 { | 22 { |
23 bool got_type_error = false; | 23 bool got_type_error = false; |
24 var x = null; | 24 var x = null; |
25 try { | 25 try { |
26 Expect.isFalse(x is UndeclaredType); // x is null. | 26 Expect.isTrue(x is UndeclaredType); // x is null. |
27 } on TypeError catch (error) { | 27 } on TypeError catch (error) { |
28 got_type_error = true; | 28 got_type_error = true; |
29 } | 29 } |
30 // Type error in production mode and in checked mode. | 30 // No type error. |
31 Expect.isTrue(got_type_error); | 31 Expect.isFalse(got_type_error); |
32 } | 32 } |
33 { | 33 { |
34 bool got_type_error = false; | 34 bool got_type_error = false; |
35 var x = 1; | 35 var x = 1; |
36 try { | 36 try { |
37 Expect.isFalse(x is UndeclaredType); // x is not null. | 37 Expect.isTrue(x is UndeclaredType); // x is not null. |
38 } on TypeError catch (error) { | 38 } on TypeError catch (error) { |
39 got_type_error = true; | 39 got_type_error = true; |
40 } | 40 } |
41 // Type error in production mode and in checked mode. | 41 // No type error. |
42 Expect.isTrue(got_type_error); | 42 Expect.isFalse(got_type_error); |
43 } | 43 } |
44 { | 44 { |
45 bool got_type_error = false; | 45 bool got_type_error = false; |
46 var x = null; | 46 var x = null; |
47 try { | 47 try { |
48 Expect.isFalse(x is List<UndeclaredType>); // x is null. | 48 Expect.isFalse(x is List<UndeclaredType>); // x is null. |
49 } on TypeError catch (error) { | 49 } on TypeError catch (error) { |
50 got_type_error = true; | 50 got_type_error = true; |
51 } | 51 } |
52 // Type error in checked mode only. | 52 // No type error. |
53 Expect.isTrue(got_type_error == isCheckedMode()); | 53 Expect.isFalse(got_type_error); |
54 } | 54 } |
55 { | 55 { |
56 bool got_type_error = false; | 56 bool got_type_error = false; |
57 var x = 1; | 57 var x = 1; |
58 try { | 58 try { |
59 Expect.isFalse(x is List<UndeclaredType>); // x is not a List. | 59 Expect.isFalse(x is List<UndeclaredType>); // x is not a List. |
60 } on TypeError catch (error) { | 60 } on TypeError catch (error) { |
61 got_type_error = true; | 61 got_type_error = true; |
62 } | 62 } |
63 // Type error in checked mode only. | 63 // No type error. |
64 Expect.isTrue(got_type_error == isCheckedMode()); | 64 Expect.isFalse(got_type_error); |
65 } | 65 } |
66 { | 66 { |
67 bool got_type_error = false; | 67 bool got_type_error = false; |
68 var x = new List(); | 68 var x = new List(); |
69 try { | 69 try { |
70 Expect.isTrue(x is List<UndeclaredType>); // x is a List<dynamic>. | 70 Expect.isTrue(x is List<UndeclaredType>); // x is a List<dynamic>. |
71 } on TypeError catch (error) { | 71 } on TypeError catch (error) { |
72 got_type_error = true; | 72 got_type_error = true; |
73 } | 73 } |
74 // Type error in checked mode only. | 74 // No type error. |
75 Expect.isTrue(got_type_error == isCheckedMode()); | 75 Expect.isFalse(got_type_error); |
76 } | 76 } |
77 { | 77 { |
78 bool got_type_error = false; | 78 bool got_type_error = false; |
79 var x = new List<int>(); | 79 var x = new List<int>(); |
80 try { | 80 try { |
81 Expect.isTrue(x is List<UndeclaredType>); // x is a List<int>. | 81 Expect.isTrue(x is List<UndeclaredType>); // x is a List<int>. |
82 } on TypeError catch (error) { | 82 } on TypeError catch (error) { |
83 got_type_error = true; | 83 got_type_error = true; |
84 } | 84 } |
85 // Type error in checked mode only. | 85 // No type error. |
86 Expect.isTrue(got_type_error == isCheckedMode()); | 86 Expect.isFalse(got_type_error); |
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 main() { | 90 main() { |
91 // Repeat type checks so that inlined tests can be tested as well. | 91 // Repeat type checks so that inlined tests can be tested as well. |
92 for (int i = 0; i < 5; i++) { | 92 for (int i = 0; i < 5; i++) { |
93 testAll(); | 93 testAll(); |
94 } | 94 } |
95 } | 95 } |
OLD | NEW |