Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Unified Diff: tests/lib/mirrors/invoke_test.dart

Issue 19839002: Improve test coverage of reflective invocation. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« tests/lib/lib.status ('K') | « tests/lib/lib.status ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/mirrors/invoke_test.dart
===================================================================
--- tests/lib/mirrors/invoke_test.dart (revision 0)
+++ tests/lib/mirrors/invoke_test.dart (revision 0)
@@ -0,0 +1,425 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library test.invoke_test;
+
+import 'dart:mirrors';
+
+import 'package:expect/expect.dart';
+import '../../async_helper.dart';
+
+class C {
+ var field;
ahe 2013/07/20 16:34:59 Weird indentation.
rmacnak 2013/07/22 18:40:48 Oops, Sublime normally converts my tabs to spaces.
+ C() : this.field = 'default';
+ C.named(this.field);
+
+ get getter => 'get $field';
+ set setter(v) => field = 'set $v';
+ method(x, y, z) => '$x+$y+$z';
+ toString() => 'a C';
+
+ noSuchMethod(invocation) => 'DNU';
+
+ static var staticField = 'initial';
+ static get staticGetter => 'sget $staticField';
+ static set staticSetter(v) => staticField = 'sset $v';
+ static staticFunction(x, y) => "($x,$y)";
+}
+
+var libraryField = 'a priori';
+get libraryGetter => 'lget $libraryField';
+set librarySetter(v) => libraryField = 'lset $v';
+libraryFunction(x,y) => '$x$y';
+
+testSync() {
+ var result;
+
+ // InstanceMirror invoke
+ C c = new C();
+ InstanceMirror im = reflect(c);
+ result = im.invoke(const Symbol('method'), [2,4,8]);
+ Expect.equals('2+4+8', result.reflectee);
+ result = im.invoke(const Symbol('doesntExist'), [2,4,8]);
+ Expect.equals('DNU', result.reflectee);
+ result = im.invoke(const Symbol('method'), [2,4]); // Wrong arity.
+ Expect.equals('DNU', result.reflectee);
+
+ // InstanceMirror invokeGetter
+ result = im.getField(const Symbol('getter'));
+ Expect.equals('get default', result.reflectee);
+ result = im.getField(const Symbol('field'));
+ Expect.equals('default', result.reflectee);
+ result = im.getField(const Symbol('doesntExist'));
+ Expect.equals('DNU', result.reflectee);
+
+ // InstanceMirror invokeSetter
+ im.setField(const Symbol('setter'), 'foo');
+ Expect.equals('set foo', c.field);
+ im.setField(const Symbol('field'), 'bar');
+ Expect.equals('bar', c.field);
+ im.setField(const Symbol('doesntExist'), 'bar');
+ // Expect does not throw.
ahe 2013/07/20 16:34:59 What does this mean?
rmacnak 2013/07/22 18:40:48 We expect setField(#doesntExist) to complete witho
ahe 2013/07/22 19:29:49 Please add the comment *above* the line to which i
rmacnak 2013/07/22 20:16:56 Done.
+
+ // ClassMirror invoke
+ ClassMirror cm = reflectClass(C);
+ result = cm.invoke(const Symbol('staticFunction'),[3,4]);
+ Expect.equals('(3,4)', result.reflectee);
+ try {
+ result = cm.invoke(const Symbol('doesntExist'),[3,4]);
ahe 2013/07/20 16:34:59 Weird indentation.
+ Expect.fail('Should not reach');
+ } catch(exception) {}
ahe 2013/07/20 16:34:59 This doesn't work. Also, NEVER have an empty catc
rmacnak 2013/07/22 18:40:48 This works on the VM. In any case, converted to E
ahe 2013/07/22 19:29:49 What I meant is: You could do absolutely anything
rmacnak 2013/07/22 20:16:56 Ah, you mean it doesn't properly assert both contr
+ try {
+ result = cm.invoke(const Symbol('staticFunction'),[3]); // Wrong arity.
ahe 2013/07/20 16:34:59 Weird indention.
+ Expect.fail('Should not reach');
+ } catch(exception) {}
ahe 2013/07/20 16:34:59 This doesn't work.
+
+ // ClassMirror invokeGetter
ahe 2013/07/20 16:34:59 Seem to be a systemic problem below: Weird indent
+ result = cm.getField(const Symbol('staticGetter'));
+ Expect.equals('sget initial', result.reflectee);
+ result = cm.getField(const Symbol('staticField'));
+ Expect.equals('initial', result.reflectee);
+ try {
+ result = cm.getField(const Symbol('doesntExist'));
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+
+ // ClassMirror invokeSetter
+ cm.setField(const Symbol('staticSetter'), 'sfoo');
+ Expect.equals('sset sfoo', C.staticField);
+ cm.setField(const Symbol('staticField'), 'sbar');
+ Expect.equals('sbar', C.staticField);
+ try {
+ result = cm.setField(const Symbol('doesntExist'), 'sbar');
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+
+ // ClassMirror invokeConstructor
+ result = cm.newInstance(const Symbol(''), []);
+ Expect.isTrue(result.reflectee is C);
+ Expect.equals('default', result.reflectee.field);
+ result = cm.newInstance(const Symbol('named'), ['my value']);
+ Expect.isTrue(result.reflectee is C);
+ Expect.equals('my value', result.reflectee.field);
+ try {
+ result = cm.newInstance(const Symbol('doesntExist'), ['my value']);
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+ try {
+ result = cm.newInstance(const Symbol('named'), []); // Wrong arity.
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+
+ // LibraryMirror invoke
+ LibraryMirror lm = cm.owner;
+ result = lm.invoke(const Symbol('libraryFunction'),[':',')']);
+ Expect.equals(':)', result.reflectee);
+ try {
+ result = lm.invoke(const Symbol('doesntExist'), [':',')']);
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+ try {
+ result = lm.invoke(const Symbol('libraryFunction'), [':']); // Wrong arity.
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+
+ // LibraryMirror invokeGetter
+ result = lm.getField(const Symbol('libraryGetter'));
+ Expect.equals('lget a priori', result.reflectee);
+ result = lm.getField(const Symbol('libraryField'));
+ Expect.equals('a priori', result.reflectee);
+ try {
+ result = lm.getField(const Symbol('doesntExist'));
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+
+ // LibraryMirror invokeSetter
+ lm.setField(const Symbol('librarySetter'), 'lfoo');
+ Expect.equals('lset lfoo', libraryField);
+ lm.setField(const Symbol('libraryField'), 'lbar');
+ Expect.equals('lbar', libraryField);
+ try {
+ result = lm.setField(const Symbol('doesntExist'), 'lbar');
+ Expect.fail('Should not reach');
+ } catch(exception) {}
+}
+
+testAsync() {
+ var future;
+
+ // InstanceMirror invoke
+ C c = new C();
+ InstanceMirror im = reflect(c);
+ future = im.invokeAsync(const Symbol('method'), [2,4,8]);
+ asyncStart();
+ future.then((result) {
+ Expect.equals('2+4+8', result.reflectee);
+ asyncEnd();
+ });
+ future = im.invokeAsync(const Symbol('method'), [im,im,im]);
+ asyncStart();
+ future.then((result) {
+ Expect.equals('a C+a C+a C', result.reflectee);
+ asyncEnd();
+ });
+ future = im.invokeAsync(const Symbol('doesntExist'), [2,4,8]);
+ asyncStart();
+ future.then((result) {
+ Expect.equals('DNU', result.reflectee);
+ asyncEnd();
+ });
+ future = im.invokeAsync(const Symbol('method'), [2, 4]); // Wrong arity.
+ asyncStart();
+ future.then((result) {
+ Expect.equals('DNU', result.reflectee);
+ asyncEnd();
+ });
+
+ // InstanceMirror invokeGetter
+ future = im.getFieldAsync(const Symbol('getter'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('get default', result.reflectee);
+ asyncEnd();
+ });
+ future = im.getFieldAsync(const Symbol('field'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('default', result.reflectee);
+ asyncEnd();
+ });
+ future = im.getFieldAsync(const Symbol('doesntExist'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('DNU', result.reflectee);
+ asyncEnd();
+ });
+
+ // InstanceMirror invokeSetter
+ future = im.setFieldAsync(const Symbol('setter'), 'foo');
+ asyncStart();
+ future.then((result) {
+ Expect.equals('set foo', c.field);
+ asyncEnd();
+ var future2 = im.setFieldAsync(const Symbol('field'), 'bar');
+ asyncStart();
+ future2.then((result) {
+ Expect.equals('bar', c.field);
+ asyncEnd();
+ var future3 = im.setFieldAsync(const Symbol('field'), im);
+ asyncStart();
+ future3.then((result) {
+ Expect.equals(c, c.field);
+ asyncEnd();
+ });
+ });
+ });
+ future = im.setFieldAsync(const Symbol('doesntExist'), 'bar');
+ asyncStart();
+ future.then((result) {
+ asyncEnd();
+ }, onError:(exception) {
+ Expect.fail('Should not throw');
+ });
+
+
+ // ClassMirror invoke
+ ClassMirror cm = reflectClass(C);
+ future = cm.invokeAsync(const Symbol('staticFunction'),[3,4]);
+ asyncStart();
+ future.then((result) {
+ Expect.equals('(3,4)', result.reflectee);
+ asyncEnd();
+ });
+ future = cm.invokeAsync(const Symbol('staticFunction'),[im,im]);
+ asyncStart();
+ future.then((result) {
+ Expect.equals('(a C,a C)', result.reflectee);
+ asyncEnd();
+ });
+ future = cm.invokeAsync(const Symbol('doesntExist'),[im,im]);
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+ future = cm.invokeAsync(const Symbol('staticFunction'),[3]); //Wrong arity.
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+
+ // ClassMirror invokeGetter
+ C.staticField = 'initial'; // Reset from synchronous test.
+ future = cm.getFieldAsync(const Symbol('staticGetter'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('sget initial', result.reflectee);
+ asyncEnd();
+ });
+ future = cm.getFieldAsync(const Symbol('staticField'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('initial', result.reflectee);
+ asyncEnd();
+ });
+ future = cm.getFieldAsync(const Symbol('doesntExist'));
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+
+ // ClassMirror invokeSetter
+ future = cm.setFieldAsync(const Symbol('staticSetter'), 'sfoo');
+ asyncStart();
+ future.then((result) {
+ Expect.equals('sset sfoo', C.staticField);
+ asyncEnd();
+ var future2 = cm.setFieldAsync(const Symbol('staticField'), 'sbar');
+ asyncStart();
+ future2.then((result) {
+ Expect.equals('sbar', C.staticField);
+ asyncEnd();
+ var future3 = cm.setFieldAsync(const Symbol('staticField'), im);
+ asyncStart();
+ future3.then((result) {
+ Expect.equals(c, C.staticField);
+ asyncEnd();
+ });
+ });
+ });
+ future = cm.setFieldAsync(const Symbol('doesntExist'), 'sbar');
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+
+ // ClassMirror invokeConstructor
+ future = cm.newInstanceAsync(const Symbol(''), []);
+ asyncStart();
+ future.then((result) {
+ Expect.isTrue(result.reflectee is C);
+ Expect.equals('default', result.reflectee.field);
+ asyncEnd();
+ });
+ future = cm.newInstanceAsync(const Symbol('named'), ['my value']);
+ asyncStart();
+ future.then((result) {
+ Expect.isTrue(result.reflectee is C);
+ Expect.equals('my value', result.reflectee.field);
+ asyncEnd();
+ });
+ future = cm.newInstanceAsync(const Symbol('named'), [im]);
+ asyncStart();
+ future.then((result) {
+ Expect.isTrue(result.reflectee is C);
+ Expect.equals(c, result.reflectee.field);
+ asyncEnd();
+ });
+ future = cm.newInstanceAsync(const Symbol('doesntExist'), ['my value']);
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+ future = cm.newInstanceAsync(const Symbol('named'), []); // Wrong arity.
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+
+ // LibraryMirror invoke
+ LibraryMirror lm = cm.owner;
+ future = lm.invokeAsync(const Symbol('libraryFunction'),[':',')']);
+ asyncStart();
+ future.then((result) {
+ Expect.equals(':)', result.reflectee);
+ asyncEnd();
+ });
+ future = lm.invokeAsync(const Symbol('libraryFunction'),[im,im]);
+ asyncStart();
+ future.then((result) {
+ Expect.equals('a Ca C', result.reflectee);
+ asyncEnd();
+ });
+ future = lm.invokeAsync(const Symbol('doesntExist'),[im,im]);
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+ future = lm.invokeAsync(const Symbol('libraryFunction'),[':']); // Wrong arity.
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+
+ // LibraryMirror invokeGetter
+ libraryField = 'a priori'; // Reset from synchronous test.
+ future = lm.getFieldAsync(const Symbol('libraryGetter'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('lget a priori', result.reflectee);
+ asyncEnd();
+ });
+ future = lm.getFieldAsync(const Symbol('libraryField'));
+ asyncStart();
+ future.then((result) {
+ Expect.equals('a priori', result.reflectee);
+ asyncEnd();
+ });
+ future = lm.getFieldAsync(const Symbol('doesntExist'));
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+
+ // LibraryMirror invokeSetter
+ future = lm.setFieldAsync(const Symbol('librarySetter'), 'lfoo');
+ asyncStart();
+ future.then((result) {
+ Expect.equals('lset lfoo', libraryField);
+ asyncEnd();
+ var future2 = lm.setFieldAsync(const Symbol('libraryField'), 'lbar');
+ asyncStart();
+ future2.then((result) {
+ Expect.equals('lbar', libraryField);
+ asyncEnd();
+ var future3 = lm.setFieldAsync(const Symbol('libraryField'), im);
+ asyncStart();
+ future3.then((result) {
+ Expect.equals(c, libraryField);
+ asyncEnd();
+ });
+ });
+ });
+ future = lm.setFieldAsync(const Symbol('doesntExist'), 'lbar');
+ asyncStart();
+ future.then((result) {
+ Expect.fail('Should not reach');
+ }, onError:(exception){
+ asyncEnd();
+ });
+}
+
+main() {
+ testSync();
+ asyncStart();
+ testAsync();
+ asyncEnd();
+}
« tests/lib/lib.status ('K') | « tests/lib/lib.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698