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 // VMOptions=--no_show_internal_names | 4 // VMOptions=--no_show_internal_names |
5 // | 5 // |
6 // Dart test program testing type casts. | 6 // Dart test program testing type casts. |
7 | 7 |
8 // Test that the initializer expression gets properly skipped. | 8 // Test that the initializer expression gets properly skipped. |
9 bool b = "foo" as double; | 9 bool b = "foo" as double; |
10 | 10 |
11 class TypeTest { | 11 class TypeTest { |
12 static test() { | 12 static test() { |
13 int result = 0; | 13 int result = 0; |
14 try { | 14 try { |
15 var i = "hello" as int; // Throws a CastException | 15 var i = "hello" as int; // Throws a CastException |
16 } catch (TypeError error) { | 16 } on TypeError catch (error) { |
17 result = 1; | 17 result = 1; |
18 Expect.isTrue(error is CastException); | 18 Expect.isTrue(error is CastException); |
19 Expect.equals("int", error.dstType); | 19 Expect.equals("int", error.dstType); |
20 Expect.equals("String", error.srcType); | 20 Expect.equals("String", error.srcType); |
21 Expect.equals("type cast", error.dstName); | 21 Expect.equals("type cast", error.dstName); |
22 int pos = error.url.lastIndexOf("/", error.url.length); | 22 int pos = error.url.lastIndexOf("/", error.url.length); |
23 if (pos == -1) { | 23 if (pos == -1) { |
24 pos = error.url.lastIndexOf("\\", error.url.length); | 24 pos = error.url.lastIndexOf("\\", error.url.length); |
25 } | 25 } |
26 String subs = error.url.substring(pos + 1, error.url.length); | 26 String subs = error.url.substring(pos + 1, error.url.length); |
27 Expect.equals("type_cast_vm_test.dart", subs); | 27 Expect.equals("type_cast_vm_test.dart", subs); |
28 Expect.equals(15, error.line); | 28 Expect.equals(15, error.line); |
29 Expect.equals(23, error.column); | 29 Expect.equals(23, error.column); |
30 } | 30 } |
31 return result; | 31 return result; |
32 } | 32 } |
33 | 33 |
34 static testSideEffect() { | 34 static testSideEffect() { |
35 int result = 0; | 35 int result = 0; |
36 int index() { | 36 int index() { |
37 result++; | 37 result++; |
38 return 0; | 38 return 0; |
39 } | 39 } |
40 try { | 40 try { |
41 var a = new List<int>(1) as List<int>; | 41 var a = new List<int>(1) as List<int>; |
42 a[0] = 0; | 42 a[0] = 0; |
43 a[index()]++; // Type check succeeds, but does not create side effects. | 43 a[index()]++; // Type check succeeds, but does not create side effects. |
44 Expect.equals(1, a[0]); | 44 Expect.equals(1, a[0]); |
45 } catch (TypeError error) { | 45 } on TypeError catch (error) { |
46 result = 100; | 46 result = 100; |
47 } | 47 } |
48 return result; | 48 return result; |
49 } | 49 } |
50 | 50 |
51 static testArgument() { | 51 static testArgument() { |
52 int result = 0; | 52 int result = 0; |
53 int f(int i) { | 53 int f(int i) { |
54 return i; | 54 return i; |
55 } | 55 } |
56 try { | 56 try { |
57 int i = f("hello" as int); // Throws a CastException | 57 int i = f("hello" as int); // Throws a CastException |
58 } catch (TypeError error) { | 58 } on TypeError catch (error) { |
59 result = 1; | 59 result = 1; |
60 Expect.isTrue(error is CastException); | 60 Expect.isTrue(error is CastException); |
61 Expect.equals("int", error.dstType); | 61 Expect.equals("int", error.dstType); |
62 Expect.equals("String", error.srcType); | 62 Expect.equals("String", error.srcType); |
63 Expect.equals("type cast", error.dstName); | 63 Expect.equals("type cast", error.dstName); |
64 int pos = error.url.lastIndexOf("/", error.url.length); | 64 int pos = error.url.lastIndexOf("/", error.url.length); |
65 if (pos == -1) { | 65 if (pos == -1) { |
66 pos = error.url.lastIndexOf("\\", error.url.length); | 66 pos = error.url.lastIndexOf("\\", error.url.length); |
67 } | 67 } |
68 String subs = error.url.substring(pos + 1, error.url.length); | 68 String subs = error.url.substring(pos + 1, error.url.length); |
69 Expect.equals("type_cast_vm_test.dart", subs); | 69 Expect.equals("type_cast_vm_test.dart", subs); |
70 Expect.equals(57, error.line); | 70 Expect.equals(57, error.line); |
71 Expect.equals(25, error.column); | 71 Expect.equals(25, error.column); |
72 } | 72 } |
73 return result; | 73 return result; |
74 } | 74 } |
75 | 75 |
76 static testReturn() { | 76 static testReturn() { |
77 int result = 0; | 77 int result = 0; |
78 int f(String s) { | 78 int f(String s) { |
79 return s as int; // Throws a CastException | 79 return s as int; // Throws a CastException |
80 } | 80 } |
81 try { | 81 try { |
82 int i = f("hello"); | 82 int i = f("hello"); |
83 } catch (TypeError error) { | 83 } on TypeError catch (error) { |
84 result = 1; | 84 result = 1; |
85 Expect.isTrue(error is CastException); | 85 Expect.isTrue(error is CastException); |
86 Expect.equals("int", error.dstType); | 86 Expect.equals("int", error.dstType); |
87 Expect.equals("String", error.srcType); | 87 Expect.equals("String", error.srcType); |
88 Expect.equals("type cast", error.dstName); | 88 Expect.equals("type cast", error.dstName); |
89 int pos = error.url.lastIndexOf("/", error.url.length); | 89 int pos = error.url.lastIndexOf("/", error.url.length); |
90 if (pos == -1) { | 90 if (pos == -1) { |
91 pos = error.url.lastIndexOf("\\", error.url.length); | 91 pos = error.url.lastIndexOf("\\", error.url.length); |
92 } | 92 } |
93 String subs = error.url.substring(pos + 1, error.url.length); | 93 String subs = error.url.substring(pos + 1, error.url.length); |
94 Expect.equals("type_cast_vm_test.dart", subs); | 94 Expect.equals("type_cast_vm_test.dart", subs); |
95 Expect.equals(79, error.line); | 95 Expect.equals(79, error.line); |
96 Expect.equals(16, error.column); | 96 Expect.equals(16, error.column); |
97 } | 97 } |
98 return result; | 98 return result; |
99 } | 99 } |
100 | 100 |
101 static var field = "hello"; | 101 static var field = "hello"; |
102 static testField() { | 102 static testField() { |
103 int result = 0; | 103 int result = 0; |
104 Expect.equals(5, (field as String).length); | 104 Expect.equals(5, (field as String).length); |
105 try { | 105 try { |
106 field as int; // Throws a CastException | 106 field as int; // Throws a CastException |
107 } catch (TypeError error) { | 107 } on TypeError catch (error) { |
108 result = 1; | 108 result = 1; |
109 Expect.equals("int", error.dstType); | 109 Expect.equals("int", error.dstType); |
110 Expect.equals("String", error.srcType); | 110 Expect.equals("String", error.srcType); |
111 Expect.equals("type cast", error.dstName); | 111 Expect.equals("type cast", error.dstName); |
112 int pos = error.url.lastIndexOf("/", error.url.length); | 112 int pos = error.url.lastIndexOf("/", error.url.length); |
113 if (pos == -1) { | 113 if (pos == -1) { |
114 pos = error.url.lastIndexOf("\\", error.url.length); | 114 pos = error.url.lastIndexOf("\\", error.url.length); |
115 } | 115 } |
116 String subs = error.url.substring(pos + 1, error.url.length); | 116 String subs = error.url.substring(pos + 1, error.url.length); |
117 Expect.equals("type_cast_vm_test.dart", subs); | 117 Expect.equals("type_cast_vm_test.dart", subs); |
118 Expect.equals(106, error.line); | 118 Expect.equals(106, error.line); |
119 Expect.equals(13, error.column); | 119 Expect.equals(13, error.column); |
120 } | 120 } |
121 return result; | 121 return result; |
122 } | 122 } |
123 | 123 |
124 static testAnyFunction() { | 124 static testAnyFunction() { |
125 int result = 0; | 125 int result = 0; |
126 Function anyFunction; | 126 Function anyFunction; |
127 f() { }; | 127 f() { }; |
128 anyFunction = f as Function; // No error. | 128 anyFunction = f as Function; // No error. |
129 anyFunction = null as Function; // No error. | 129 anyFunction = null as Function; // No error. |
130 try { | 130 try { |
131 var i = f as int; // Throws a TypeError if type checks are enabled. | 131 var i = f as int; // Throws a TypeError if type checks are enabled. |
132 } catch (TypeError error) { | 132 } on TypeError catch (error) { |
133 result = 1; | 133 result = 1; |
134 Expect.equals("int", error.dstType); | 134 Expect.equals("int", error.dstType); |
135 Expect.equals("() => Dynamic", error.srcType); | 135 Expect.equals("() => Dynamic", error.srcType); |
136 Expect.equals("type cast", error.dstName); | 136 Expect.equals("type cast", error.dstName); |
137 int pos = error.url.lastIndexOf("/", error.url.length); | 137 int pos = error.url.lastIndexOf("/", error.url.length); |
138 if (pos == -1) { | 138 if (pos == -1) { |
139 pos = error.url.lastIndexOf("\\", error.url.length); | 139 pos = error.url.lastIndexOf("\\", error.url.length); |
140 } | 140 } |
141 String subs = error.url.substring(pos + 1, error.url.length); | 141 String subs = error.url.substring(pos + 1, error.url.length); |
142 Expect.equals("type_cast_vm_test.dart", subs); | 142 Expect.equals("type_cast_vm_test.dart", subs); |
(...skipping 10 matching lines...) Expand all Loading... |
153 Expect.equals(1, testReturn()); | 153 Expect.equals(1, testReturn()); |
154 Expect.equals(1, testField()); | 154 Expect.equals(1, testField()); |
155 Expect.equals(1, testAnyFunction()); | 155 Expect.equals(1, testAnyFunction()); |
156 } | 156 } |
157 } | 157 } |
158 | 158 |
159 | 159 |
160 main() { | 160 main() { |
161 TypeTest.testMain(); | 161 TypeTest.testMain(); |
162 } | 162 } |
OLD | NEW |