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 library test.instantiate_abstract_class; |
| 6 |
| 7 import 'dart:mirrors'; |
| 8 import 'package:expect/expect.dart'; |
| 9 |
| 10 |
| 11 assertInstanitationErrorOnGenerativeConstructors(classMirror) { |
| 12 classMirror.declarations.values.forEach((decl) { |
| 13 if (decl is! MethodMirror) return; |
| 14 if (!decl.isGenerativeConstructor) return; |
| 15 var args = new List(decl.parameters.length); |
| 16 Expect.throws(() => classMirror.newInstance(decl.constructorName, args), |
| 17 (e) => e is AbstractClassInstantiationError, |
| 18 '${decl.qualifiedName} should have failed'); |
| 19 }); |
| 20 } |
| 21 |
| 22 runFactoryConstructors(classMirror) { |
| 23 classMirror.declarations.values.forEach((decl) { |
| 24 if (decl is! MethodMirror) return; |
| 25 if (!decl.isFactoryConstructor) return; |
| 26 var args = new List(decl.parameters.length); |
| 27 classMirror.newInstance(decl.constructorName, args); // Should not throw. |
| 28 }); |
| 29 } |
| 30 |
| 31 abstract class AbstractClass { |
| 32 AbstractClass(); |
| 33 AbstractClass.named(); |
| 34 factory AbstractClass.named2() => new ConcreteClass(); |
| 35 } |
| 36 |
| 37 class ConcreteClass implements AbstractClass {} |
| 38 |
| 39 main() { |
| 40 assertInstanitationErrorOnGenerativeConstructors(reflectType(num)); |
| 41 assertInstanitationErrorOnGenerativeConstructors(reflectType(double)); |
| 42 assertInstanitationErrorOnGenerativeConstructors(reflectType(StackTrace)); |
| 43 |
| 44 assertInstanitationErrorOnGenerativeConstructors(reflectType(AbstractClass)); |
| 45 runFactoryConstructors(reflectType(AbstractClass)); |
| 46 } |
OLD | NEW |