Skip to content

Commit

Permalink
Merge pull request #42 from matanlurey/flip_api_defaults
Browse files Browse the repository at this point in the history
Move all mirrors-based API to mirrors.dart
  • Loading branch information
TedSander authored Oct 7, 2016
2 parents 9820ba3 + 6975b6e commit 2421d78
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 58 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.0-dev

* Remove export of `spy` and any `dart:mirrors` based API from
`mockito.dart`. Users may import as `package:mockito/mirrors.dart`
going forward.
* Deprecated `mockito_no_mirrors.dart`; replace with `mockito.dart`.

## 1.0.1

* Add a new `thenThrow` method to the API.
Expand Down
2 changes: 2 additions & 0 deletions lib/mirrors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'mockito.dart';
export 'src/spy.dart' show spy;
31 changes: 29 additions & 2 deletions lib/mockito.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
export 'mockito_no_mirrors.dart';
export 'src/spy.dart' show spy;
export 'src/mock.dart'
show
Mock,
named,

// -- setting behaviour
when,
any,
argThat,
captureAny,
captureThat,
typed,
Answering,
Expectation,
PostExpectation,

// -- verification
verify,
verifyInOrder,
verifyNever,
verifyNoMoreInteractions,
verifyZeroInteractions,
VerificationResult,
Verification,

// -- misc
clearInteractions,
reset,
logInvocations;
31 changes: 3 additions & 28 deletions lib/mockito_no_mirrors.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
export 'src/mock.dart'
show
Mock,
named,
@Deprecated('Import "package:mockito/mockito.dart" instead.')
library mockito.mockito_no_mirrors;

// -- setting behaviour
when,
any,
argThat,
captureAny,
captureThat,
typed,
Answering,
Expectation,
PostExpectation,

// -- verification
verify,
verifyInOrder,
verifyNever,
verifyNoMoreInteractions,
verifyZeroInteractions,
VerificationResult,
Verification,

// -- misc
clearInteractions,
reset,
logInvocations;
export 'mockito.dart';
3 changes: 1 addition & 2 deletions lib/src/mock.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Warning: Do not import dart:mirrors in this library, as it's exported via
// lib/mockito_no_mirrors.dart, which is used for Dart AOT projects such as
// Flutter.
// lib/mockito.dart, which is used for Dart AOT projects such as Flutter.

import 'package:meta/meta.dart';
import 'package:test/test.dart';
Expand Down
14 changes: 10 additions & 4 deletions lib/src/spy.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// This file is intentionally separated from 'mock.dart' in order to avoid
// bringing in the mirrors dependency into mockito_no_mirrors.dart.
// bringing in the mirrors dependency into mockito.dart.
import 'dart:mirrors';

import 'mock.dart' show CannedResponse, setDefaultResponse;
import 'mock.dart' show CannedResponse, Mock, setDefaultResponse;

dynamic spy(dynamic mock, dynamic spiedObject) {
var mirror = reflect(spiedObject);
/// Sets the default response of [mock] to be delegated to [spyOn].
///
/// __Example use__:
/// var mockAnimal = new MockAnimal();
/// var realAnimal = new RealAnimal();
/// spy(mockAnimal, realAnimal);
/*=E*/ spy/*<E>*/(Mock mock, Object /*=E*/ spyOn) {
var mirror = reflect(spyOn);
setDefaultResponse(
mock,
() => new CannedResponse(null,
Expand Down
2 changes: 1 addition & 1 deletion test/invocation_matcher_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {
var call2 = stub.value;
stub.value = true;
var call3 = Stub.lastInvocation;
shouldPass(call1, isInvocation(call2));
shouldPass(call1, isInvocation(call2 as Invocation));
shouldFail(
call1,
isInvocation(call3),
Expand Down
23 changes: 2 additions & 21 deletions test/mockito_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:mockito/mockito.dart';
import 'package:mockito/src/mock.dart' show resetMockitoState;
import 'package:test/test.dart';

import 'package:mockito/src/mock.dart';
import 'package:mockito/src/spy.dart';

class RealClass {
String methodWithoutArgs() => "Real";
String methodWithNormalArgs(int x) => "Real";
Expand Down Expand Up @@ -69,24 +68,6 @@ void main() {
resetMockitoState();
});

group("spy", () {
setUp(() {
mock = spy(new MockedClass(), new RealClass());
});

test("should delegate to real object by default", () {
expect(mock.methodWithoutArgs(), 'Real');
});
test("should record interactions delegated to real object", () {
mock.methodWithoutArgs();
verify(mock.methodWithoutArgs());
});
test("should behave as mock when expectation are set", () {
when(mock.methodWithoutArgs()).thenReturn('Spied');
expect(mock.methodWithoutArgs(), 'Spied');
});
});

group("mixin support", () {
test("should work", () {
var foo = new MockFoo();
Expand Down
37 changes: 37 additions & 0 deletions test/spy_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:mockito/src/mock.dart' show resetMockitoState;
import 'package:mockito/mirrors.dart';
import 'package:test/test.dart';

import 'mockito_test.dart' show MockedClass, RealClass;

void main() {
RealClass mock;

setUp(() {
mock = new MockedClass();
});

tearDown(() {
// In some of the tests that expect an Error to be thrown, Mockito's
// global state can become invalid. Reset it.
resetMockitoState();
});

group("spy", () {
setUp(() {
mock = spy/*<RealClass>*/(new MockedClass(), new RealClass());
});

test("should delegate to real object by default", () {
expect(mock.methodWithoutArgs(), 'Real');
});
test("should record interactions delegated to real object", () {
mock.methodWithoutArgs();
verify(mock.methodWithoutArgs());
});
test("should behave as mock when expectation are set", () {
when(mock.methodWithoutArgs()).thenReturn('Spied');
expect(mock.methodWithoutArgs(), 'Spied');
});
});
}

0 comments on commit 2421d78

Please sign in to comment.