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

Unified Diff: test/codegen/language/no_such_method_mock_test.dart

Issue 2158173003: fix #603, support mock objects (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: add test Created 4 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
« no previous file with comments | « test/browser/language_tests.js ('k') | test/codegen_expected/language/flatten_test_01_multi.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/codegen/language/no_such_method_mock_test.dart
diff --git a/test/codegen/language/no_such_method_mock_test.dart b/test/codegen/language/no_such_method_mock_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..2bcf00b1b4371b97c591fe9cbc709b097357df3e
--- /dev/null
+++ b/test/codegen/language/no_such_method_mock_test.dart
@@ -0,0 +1,62 @@
+// Copyright (c) 2011, 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.
+// Dart test program testing that NoSuchMethod is properly called.
+
+import "package:expect/expect.dart";
+
+class Cat {
+ bool eatFood(String food) => true;
+ int scratch(String furniture) => 100;
+}
+
+class MockCat implements Cat {
+ dynamic noSuchMethod(Invocation invocation) {
+ return (invocation.positionalArguments[0] as String).isNotEmpty;
+ }
+}
+
+
+class MockCat2 extends MockCat {
+ // this apparently works.
+ noSuchMethod(_);
+}
+
+class MockCat3 extends MockCat2 implements Cat {
+ bool eatFood(String food, {double amount});
+ int scratch(String furniture, [String furniture2]);
+
+ dynamic noSuchMethod(Invocation invocation) {
+ return (invocation.positionalArguments[0] as String).isNotEmpty &&
+ invocation.namedArguments[#amount] > 0.5;
+ }
+}
+
+
+class MockWithGenerics {
+ /*=T*/ doStuff/*<T>*/(/*=T*/ t);
+
+ noSuchMethod(i) => i.positionalArguments[0] + 100;
+}
+
+
+void main() {
+ MockCat mock = new MockCat();
+ Expect.isTrue((mock as dynamic).eatFood("cat food"));
+ Expect.isFalse(mock.eatFood(""));
+
+ // In strong mode this will be a runtime type error:
+ // bool is not an int. VM will fail with noSuchMethod +.
+ Expect.throws(() => mock.scratch("couch") + 0);
+
+ var mock2 = new MockCat2();
+ Expect.isTrue(mock2.eatFood("cat food"));
+
+ var mock3 = new MockCat3();
+ Expect.isTrue(mock3.eatFood("cat food", amount: 0.9));
+ Expect.isFalse(mock3.eatFood("cat food", amount: 0.3));
+
+ var g = new MockWithGenerics();
+ Expect.equals(g.doStuff(42), 142);
+ Expect.throws(() => g.doStuff('hi'));
+}
« no previous file with comments | « test/browser/language_tests.js ('k') | test/codegen_expected/language/flatten_test_01_multi.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698