Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, 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 that malformed types are handled as dynamic, and that types with the | |
| 6 // wrong number of type arguments are handled as raw types. | |
| 7 | |
| 8 import 'package:expect/expect.dart'; | |
| 9 | |
| 10 checkIsUnresolved(var v) { | |
| 11 Expect.isTrue(v is Unresolved); | |
| 12 Expect.isTrue(v is Unresolved<int>); | |
| 13 Expect.isTrue(v is prefix.Unresolved); | |
| 14 Expect.isTrue(v is prefix.Unresolved<int>); | |
| 15 } | |
| 16 | |
| 17 checkIsListUnresolved(bool expect, var v) { | |
| 18 Expect.equals(expect, v is List<Unresolved>); | |
| 19 Expect.equals(expect, v is List<Unresolved<int>>); | |
| 20 Expect.equals(expect, v is List<prefix.Unresolved>); | |
| 21 Expect.equals(expect, v is List<prefix.Unresolved<int>>); | |
| 22 Expect.equals(expect, v is List<int, String>); | |
| 23 } | |
| 24 | |
| 25 checkIsListDynamic(bool expect, var v) { | |
| 26 checkIsListUnresolved(true, v); | |
| 27 Expect.equals(expect, v is List<int> && v is List<String>); | |
| 28 } | |
| 29 | |
| 30 checkAsUnresolved(var v) { | |
| 31 Expect.equals(v, v as Unresolved); | |
| 32 Expect.equals(v, v as Unresolved<int>); | |
| 33 Expect.equals(v, v as prefix.Unresolved); | |
| 34 Expect.equals(v, v as prefix.Unresolved<int>); | |
| 35 } | |
| 36 | |
| 37 checkAsListUnresolved(bool expect, var v) { | |
| 38 if (expect) { | |
| 39 Expect.equals(v, v as List<Unresolved>); | |
| 40 Expect.equals(v, v as List<Unresolved<int>>); | |
| 41 Expect.equals(v, v as List<prefix.Unresolved>); | |
| 42 Expect.equals(v, v as List<prefix.Unresolved<int>>); | |
| 43 Expect.equals(v, v as List<int, String>); | |
| 44 } else { | |
| 45 Expect.throws(() => v as List<Unresolved>, (e) => e is CastError); | |
| 46 Expect.throws(() => v as List<Unresolved<int>>, (e) => e is CastError); | |
| 47 Expect.throws(() => v as List<prefix.Unresolved>, (e) => e is CastError); | |
| 48 Expect.throws(() => v as List<prefix.Unresolved<int>>, (e) => e is CastError ); | |
|
karlklose
2013/07/19 11:45:40
Long line.
Johnni Winther
2013/07/29 09:59:48
Done.
| |
| 49 Expect.throws(() => v as List<int, String>, (e) => e is CastError); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 checkIsMapDynamic(bool first, second, var v) { | |
|
karlklose
2013/07/19 11:45:40
bool second.
Johnni Winther
2013/07/29 09:59:48
Done.
| |
| 54 Expect.equals(first, v is Map<String, int> && v is Map<int, int>); | |
|
karlklose
2013/07/19 11:45:40
You could use 'v is Map<String, Object> && v is Ma
Johnni Winther
2013/07/29 09:59:48
Done.
| |
| 55 Expect.equals(second, v is Map<String, int> && v is Map<String, String>); | |
| 56 } | |
| 57 | |
| 58 | |
| 59 void main() { | |
| 60 checkIsUnresolved(''); | |
| 61 checkIsUnresolved(0); | |
| 62 checkIsListUnresolved(false, ''); | |
| 63 checkIsListUnresolved(true, new List()); | |
| 64 checkIsListUnresolved(true, new List<int>()); | |
| 65 checkIsListUnresolved(true, new List<String>()); | |
| 66 checkIsListUnresolved(true, new List<int, String>()); | |
| 67 | |
| 68 checkAsUnresolved(''); | |
| 69 checkAsUnresolved(0); | |
| 70 checkAsListUnresolved(false, ''); | |
| 71 checkAsListUnresolved(true, new List()); | |
| 72 checkAsListUnresolved(true, new List<int>()); | |
| 73 checkAsListUnresolved(true, new List<String>()); | |
| 74 checkAsListUnresolved(true, new List<int, String>()); | |
| 75 | |
| 76 checkIsListDynamic(true, []); | |
| 77 checkIsListDynamic(true, <>[]); /// 01: compile-time error | |
| 78 // TODO(johnniwinther): Create runtime type information on literal lists. | |
| 79 // checkIsListDynamic(false, <int>[]); | |
| 80 checkIsListDynamic(true, <Unresolved>[]); | |
| 81 checkIsListDynamic(true, <Unresolved<int>>[]); | |
| 82 checkIsListDynamic(true, <prefix.Unresolved>[]); | |
| 83 checkIsListDynamic(true, <prefix.Unresolved<int>>[]); | |
| 84 checkIsListDynamic(true, <int, String>[]); | |
| 85 | |
| 86 checkIsListDynamic(true, new List()); | |
| 87 checkIsListDynamic(true, new List<>()); /// 02: compile-time error | |
| 88 checkIsListDynamic(true, new List<Unresolved>()); | |
| 89 checkIsListDynamic(true, new List<Unresolved<int>>()); | |
| 90 checkIsListDynamic(true, new List<prefix.Unresolved>()); | |
| 91 checkIsListDynamic(true, new List<prefix.Unresolved<int>>()); | |
| 92 checkIsListDynamic(true, new List<int, String>()); | |
| 93 | |
| 94 checkIsMapDynamic(true, true, {}); | |
| 95 checkIsMapDynamic(true, true, <>{}); /// 03: compile-time error | |
| 96 checkIsMapDynamic(true, true, <int>{}); | |
| 97 // TODO(johnniwinther): Create runtime type information on literal maps. | |
| 98 // checkIsMapDynamic(false, false, <String, int>{}); | |
| 99 checkIsMapDynamic(true, true, <String, int, String>{}); | |
| 100 // checkIsMapDynamic(true, false, <Unresolved, int>{}); | |
| 101 // checkIsMapDynamic(false, true, <String, Unresolved<int>>{}); | |
| 102 // checkIsMapDynamic(true, false, <prefix.Unresolved, int>{}); | |
| 103 // checkIsMapDynamic(false, true, <String, prefix.Unresolved<int>>{}); | |
| 104 | |
| 105 checkIsMapDynamic(true, true, new Map()); | |
| 106 checkIsMapDynamic(true, true, new Map<>); /// 04: compile-time error | |
| 107 checkIsMapDynamic(true, true, new Map<int>()); | |
| 108 checkIsMapDynamic(false, false, new Map<String, int>()); | |
| 109 checkIsMapDynamic(true, true, new Map<String, int, String>()); | |
| 110 checkIsMapDynamic(true, false, new Map<Unresolved, int>()); | |
| 111 checkIsMapDynamic(false, true, new Map<String, Unresolved<int>>()); | |
| 112 checkIsMapDynamic(true, false, new Map<prefix.Unresolved, int>()); | |
| 113 checkIsMapDynamic(false, true, new Map<String, prefix.Unresolved<int>>()); | |
| 114 | |
| 115 Expect.throws(() => new Unresolved(), (e) => true); | |
| 116 Expect.throws(() => new Unresolved<int>(), (e) => true); | |
| 117 Expect.throws(() => new prefix.Unresolved(), (e) => true); | |
| 118 Expect.throws(() => new prefix.Unresolved<int>(), (e) => true); | |
| 119 | |
| 120 try { | |
| 121 throw 'foo'; | |
| 122 } on Unresolved catch (e) { | |
| 123 } catch (e) { | |
| 124 Expect.fail(); | |
| 125 } | |
| 126 try { | |
| 127 throw 'foo'; | |
| 128 } on Unresolved<int> catch (e) { | |
| 129 } catch (e) { | |
| 130 Expect.fail(); | |
| 131 } | |
| 132 try { | |
| 133 throw 'foo'; | |
| 134 } on prefix.Unresolved catch (e) { | |
| 135 } catch (e) { | |
| 136 Expect.fail(); | |
| 137 } | |
| 138 try { | |
| 139 throw 'foo'; | |
| 140 } on prefix.Unresolved<int> catch (e) { | |
| 141 } catch (e) { | |
| 142 Expect.fail(); | |
| 143 } | |
| 144 } | |
| OLD | NEW |