OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'package:expect/expect.dart'; |
| 5 |
| 6 class Fields<T> { |
| 7 T x; |
| 8 T _y; |
| 9 T _z; |
| 10 |
| 11 m() { |
| 12 _y = x; |
| 13 } |
| 14 |
| 15 n(Fields<T> c) { |
| 16 c._z = x; |
| 17 } |
| 18 } |
| 19 |
| 20 testField() { |
| 21 Fields<Object> c = new Fields<int>(); |
| 22 Expect.throws(() { |
| 23 c.x = 'hello'; |
| 24 }); |
| 25 } |
| 26 |
| 27 testPrivateFields() { |
| 28 Fields<Object> c = new Fields<int>()..x = 42; |
| 29 c.m(); |
| 30 Expect.equals(c._y, 42); |
| 31 |
| 32 Fields<Object> c2 = new Fields<String>()..x = 'hi'; |
| 33 c2.n(c2); |
| 34 Expect.equals(c2._z, 'hi'); |
| 35 Expect.throws(() { |
| 36 c.n(c2); |
| 37 }); |
| 38 Expect.equals(c2._z, 'hi'); |
| 39 } |
| 40 |
| 41 class NumBounds<T extends num> { |
| 42 bool m(T t) => t.isNegative; |
| 43 } |
| 44 |
| 45 class MethodTakesNum extends NumBounds<int> { |
| 46 bool m(num obj) => obj.isNegative; // does not need check |
| 47 } |
| 48 |
| 49 class MethodTakesInt extends NumBounds<int> { |
| 50 bool m(int obj) => obj.isNegative; // needs a check |
| 51 } |
| 52 |
| 53 testClassBounds() { |
| 54 NumBounds<num> d = new MethodTakesNum(); |
| 55 Expect.equals(d.m(-1.1), true); |
| 56 d = new MethodTakesInt(); |
| 57 Expect.throws(() { |
| 58 d.m(-1.1); |
| 59 }); |
| 60 } |
| 61 |
| 62 typedef void F<T>(T t); |
| 63 typedef G<T> = void Function<S extends T>(S s); |
| 64 |
| 65 class FnChecks<T> { |
| 66 F<T> f; |
| 67 G<T> g; |
| 68 T _t; |
| 69 T getT() => _t; |
| 70 F<T> setterForT() { |
| 71 return (T t) { |
| 72 _t = t; |
| 73 }; |
| 74 } |
| 75 } |
| 76 |
| 77 testReturnOfFunctionType() { |
| 78 FnChecks<int> cInt = new FnChecks<int>(); |
| 79 FnChecks<Object> cObj = cInt; |
| 80 Expect.throws(() => cObj.setterForT()); |
| 81 cInt.setterForT()(42); |
| 82 Expect.equals(cObj.getT(), 42); |
| 83 } |
| 84 |
| 85 testFieldOfFunctionType() { |
| 86 FnChecks<Object> c = new FnChecks<String>()..f = (String b) {}; |
| 87 Expect.throws(() { |
| 88 F<Object> f = c.f; |
| 89 }); |
| 90 Expect.throws(() { |
| 91 Object f = c.f; |
| 92 }); |
| 93 Expect.throws(() => c.f); |
| 94 Expect.throws(() => c.f(42)); |
| 95 Expect.throws(() => c.f('hi')); |
| 96 FnChecks<String> cStr = c; |
| 97 cStr.f('hi'); |
| 98 } |
| 99 |
| 100 testFieldOfGenericFunctionType() { |
| 101 FnChecks<Object> c = new FnChecks<num>() |
| 102 ..g = <S extends num>(S s) => s.isNegative; |
| 103 |
| 104 Expect.throws(() { |
| 105 G<Object> g = c.g; |
| 106 }); |
| 107 Expect.throws(() { |
| 108 var g = c.g; |
| 109 }); |
| 110 Expect.throws(() { |
| 111 c.g<String>('hi'); |
| 112 }); |
| 113 Expect.throws(() { |
| 114 c.g<int>(42); |
| 115 }); |
| 116 FnChecks<num> cNum = c; |
| 117 cNum.g(42); |
| 118 } |
| 119 |
| 120 class Base { |
| 121 int _t = 0; |
| 122 add(int t) { |
| 123 _t += t; |
| 124 } |
| 125 } |
| 126 |
| 127 abstract class I<T> { |
| 128 add(T t); |
| 129 } |
| 130 |
| 131 class ExtendsBase extends Base implements I<int> {} |
| 132 |
| 133 class MixinBase extends Object with Base implements I<int> {} |
| 134 |
| 135 class MixinBase2 = Object with Base implements I<int>; |
| 136 |
| 137 testMixinApplication() { |
| 138 I<Object> i = new ExtendsBase(); |
| 139 I<Object> j = new MixinBase(); |
| 140 I<Object> k = new MixinBase2(); |
| 141 Expect.throws(() { |
| 142 i.add('hi'); |
| 143 }); |
| 144 Expect.throws(() { |
| 145 j.add('hi'); |
| 146 }); |
| 147 // TODO(jmesserly): this should also throw. It does not because DDC's |
| 148 // technique for generating mixin aliases (mixin applications of the form |
| 149 // `class X = Object with Y /* optional implements */;`) does not allow |
| 150 // adding any methods in the class. The normal technique of generating |
| 151 // a method that performs the check and then calls `super` will not work, |
| 152 // because there is no superclass to call. We will need some sort of |
| 153 // special case code to implement this, perhaps move the original |
| 154 // method to a symbol, then generate a wrapper with the original method name, |
| 155 // that checks and calls it. |
| 156 k.add('hi'); |
| 157 } |
| 158 |
| 159 abstract class GenericAdd<T> { |
| 160 add<S extends T>(S t); |
| 161 } |
| 162 |
| 163 class GenericAdder implements GenericAdd<num> { |
| 164 num _t = 0; |
| 165 add<T extends num>(T t) { |
| 166 _t = t; |
| 167 } |
| 168 } |
| 169 |
| 170 testGenericMethodBounds() { |
| 171 GenericAdd<Object> i = new GenericAdder(); |
| 172 Expect.throws(() { |
| 173 i.add('hi'); |
| 174 }); |
| 175 Expect.throws(() { |
| 176 i.add<String>(null); |
| 177 }); |
| 178 i.add(null); |
| 179 i.add(42); |
| 180 } |
| 181 |
| 182 class ClassF<T> { |
| 183 T x; |
| 184 void call(T t) { |
| 185 x = t; |
| 186 } |
| 187 } |
| 188 |
| 189 testCallMethod() { |
| 190 ClassF<int> cc = new ClassF<int>(); |
| 191 ClassF<Object> ca = cc; // An upcast, per covariance. |
| 192 F<Object> f = ca; |
| 193 Expect.equals(f.runtimeType.toString(), 'ClassF<int>'); |
| 194 Expect.throws(() { |
| 195 f(new Object()); |
| 196 }); |
| 197 } |
| 198 |
| 199 main() { |
| 200 testField(); |
| 201 testPrivateFields(); |
| 202 testClassBounds(); |
| 203 testReturnOfFunctionType(); |
| 204 testFieldOfFunctionType(); |
| 205 testFieldOfGenericFunctionType(); |
| 206 testMixinApplication(); |
| 207 testGenericMethodBounds(); |
| 208 testCallMethod(); |
| 209 } |
OLD | NEW |