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

Unified Diff: pkg/analysis_server/test/typed_mock_test.dart

Issue 252463002: Typed mocks library. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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
Index: pkg/analysis_server/test/typed_mock_test.dart
diff --git a/pkg/analysis_server/test/typed_mock_test.dart b/pkg/analysis_server/test/typed_mock_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..48114cf98220dc713cc3f603dc7697e74c3e5b60
--- /dev/null
+++ b/pkg/analysis_server/test/typed_mock_test.dart
@@ -0,0 +1,98 @@
+library test.typed_mock;
+
+import 'package:unittest/unittest.dart';
+
+import 'typed_mock.dart';
+
+
+abstract class TestInterface {
+ int get testProperty;
+ set testProperty(x);
+ String testMethod0();
+ String testMethod1(int p);
+}
+
+
+class TestInterfaceMock extends TypedMock implements TestInterface {
+ noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
+}
+
+
+main() {
+ group("TypedMock", () {
+ test('then for getter', () {
+ TestInterface obj = new TestInterfaceMock();
+ when(obj.testProperty).then(() => 123);
+ expect(obj.testProperty, 123);
+ });
+
+ test('then for method with 1 argument', () {
+ TestInterface obj = new TestInterfaceMock();
+ // TODO(scheglov) implement argument matchers
+ when(obj.testMethod1(5)).then((int p) => p + 10);
+ expect(obj.testMethod1(1), equals(1 + 10));
+ expect(obj.testMethod1(2), 2 + 10);
+ expect(obj.testMethod1(10), 10 + 10);
+ });
+
+ test('thenReturn can replace behavior', () {
+ TestInterface obj = new TestInterfaceMock();
+ // set a behavior
+ when(obj.testProperty).thenReturn(10);
+ expect(obj.testProperty, 10);
+ // set another behavior
+ when(obj.testProperty).thenReturn(20);
+ expect(obj.testProperty, 20);
+ });
+
+ test('thenReturn for getter', () {
+ TestInterface obj = new TestInterfaceMock();
+ when(obj.testProperty).thenReturn(42);
+ expect(obj.testProperty, 42);
+ expect(obj.testProperty, 42);
+ });
+
+ test('thenReturn for method with 0 arguments', () {
+ TestInterface obj = new TestInterfaceMock();
+ when(obj.testMethod0()).thenReturn('abc');
+ expect(obj.testMethod0(), 'abc');
+ expect(obj.testMethod0(), 'abc');
+ });
+
+ test('thenReturn for method with 1 argument', () {
+ TestInterface obj = new TestInterfaceMock();
+ // TODO(scheglov) implement argument matchers
+ when(obj.testMethod1(1)).thenReturn('qwerty');
+ expect(obj.testMethod1(2), 'qwerty');
+ expect(obj.testMethod1(3), 'qwerty');
+ });
+
+ test('thenReturnList', () {
+ TestInterface obj = new TestInterfaceMock();
+ when(obj.testProperty).thenReturnList([1, 2, 3]);
+ expect(obj.testProperty, 1);
+ expect(obj.testProperty, 2);
+ expect(obj.testProperty, 3);
+ expect(obj.testProperty, null);
+ });
+
+ test('thenThrow for getter', () {
+ TestInterface obj = new TestInterfaceMock();
+ Exception e = new Exception();
+ when(obj.testProperty).thenThrow(e);
+ expect(() => obj.testProperty, throwsA(e));
+ });
+
+ test('thenThrow for setter', () {
+ TestInterface obj = new TestInterfaceMock();
+ Exception e = new Exception();
+ // TODO(scheglov) implement argument matchers
+ when(obj.testProperty = 1).thenThrow(e);
+ expect(
+ () {
+ obj.testProperty = 2;
+ },
+ throwsA(e));
+ });
+ });
+}
« pkg/analysis_server/test/typed_mock.dart ('K') | « pkg/analysis_server/test/typed_mock.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698