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 // VMOptions=--enable_type_checks | 4 // VMOptions=--enable_type_checks |
5 // | 5 // |
6 // Dart test program testing type checks in map literals. | 6 // Dart test program testing type checks in map literals. |
7 | 7 |
8 class MapLiteral4Test<T> { | 8 class MapLiteral4Test<T> { |
9 test() { | 9 test() { |
10 int result = 0; | 10 int result = 0; |
11 try { | 11 try { |
12 var m = <String, String>{"a": 0}; // 0 is not a String. | 12 var m = <String, String>{"a": 0}; // 0 is not a String. |
13 } catch (TypeError error) { | 13 } on TypeError catch (error) { |
14 result += 1; | 14 result += 1; |
15 } | 15 } |
16 try { | 16 try { |
17 var m = <String, int>{"a": 0}; | 17 var m = <String, int>{"a": 0}; |
18 m[2] = 1; // 2 is not a String. | 18 m[2] = 1; // 2 is not a String. |
19 } catch (TypeError error) { | 19 } on TypeError catch (error) { |
20 result += 10; | 20 result += 10; |
21 } | 21 } |
22 try { | 22 try { |
23 var m = <String, T>{"a": "b"}; // "b" is not an int. | 23 var m = <String, T>{"a": "b"}; // "b" is not an int. |
24 } catch (TypeError error) { | 24 } on TypeError catch (error) { |
25 result += 100; | 25 result += 100; |
26 } | 26 } |
27 try { | 27 try { |
28 var m = <String, T>{"a": 0}; // OK. | 28 var m = <String, T>{"a": 0}; // OK. |
29 } catch (TypeError error) { | 29 } on TypeError catch (error) { |
30 result += 1000; | 30 result += 1000; |
31 } | 31 } |
32 try { | 32 try { |
33 var m = <String, T>{"a": 0}; | 33 var m = <String, T>{"a": 0}; |
34 m[2] = 1; // 2 is not a String. | 34 m[2] = 1; // 2 is not a String. |
35 } catch (TypeError error) { | 35 } on TypeError catch (error) { |
36 result += 10000; | 36 result += 10000; |
37 } | 37 } |
38 try { | 38 try { |
39 var m = const <String, int>{"a": 0}; | 39 var m = const <String, int>{"a": 0}; |
40 m[2] = 1; // 2 is not a String. | 40 m[2] = 1; // 2 is not a String. |
41 } catch (TypeError error) { | 41 } on TypeError catch (error) { |
42 result += 100000; | 42 result += 100000; |
43 } | 43 } |
44 return result; | 44 return result; |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 main() { | 48 main() { |
49 var t = new MapLiteral4Test<int>(); | 49 var t = new MapLiteral4Test<int>(); |
50 Expect.equals(110111, t.test()); | 50 Expect.equals(110111, t.test()); |
51 } | 51 } |
52 | 52 |
53 | 53 |
OLD | NEW |