Skip to content

Commit 5ad2ff4

Browse files
Ilya Yanokcopybara-github
authored andcommitted
Change default dummy value for String to contain some info
PiperOrigin-RevId: 571241586
1 parent 78c650b commit 5ad2ff4

File tree

7 files changed

+37
-14
lines changed

7 files changed

+37
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* Require analyzer 5.12.0, allow analyzer version 6.x;
44
* Add example of writing a class to mock function objects.
55
* Add support for the `build_extensions` build.yaml option
6+
* Require Dart >= 3.0.0.
7+
* **Potentially breaking** Changed default `String` value returned by nice
8+
mocks' unstubbed method to include some useful info. This could break the
9+
tests that relied on getting an empty `String` from unstubbed methods.
610

711
## 5.4.2
812

lib/src/builder.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,8 +1567,6 @@ class _MockClassInfo {
15671567
..url = 'dart:async'
15681568
..types.add(elementType);
15691569
}).property('empty').call([]);
1570-
} else if (type.isDartCoreString) {
1571-
return literalString('');
15721570
} else if (type.isDartTypedDataSealed) {
15731571
// These types (XXXList + ByteData) from dart:typed_data are
15741572
// sealed, e.g. "non-subtypeable", but they

lib/src/dummies.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ import 'dart:async';
1616
import 'dart:collection';
1717
import 'dart:typed_data';
1818

19-
import 'mock.dart' show FakeFunctionUsedError;
19+
import 'mock.dart' show FakeFunctionUsedError, PrettyString;
2020
import 'platform_dummies_js.dart'
2121
if (dart.library.io) 'platform_dummies_vm.dart';
2222

2323
// TODO(yanok): try to change these to _unreasonable_ values, for example,
2424
// String could totally contain an explanation.
2525
const int _dummyInt = 0;
2626
const double _dummyDouble = 0.0;
27-
const String _dummyString = '';
27+
28+
// Create a dummy String with info on why it was created.
29+
String _dummyString(Object o, Invocation i) => Uri.encodeComponent(
30+
'Dummy String created while calling ${i.toPrettyString()} on $o.'
31+
.replaceAll(' ', '_'));
2832

2933
// This covers functions with up to 20 positional arguments, for more arguments,
3034
// type arguments or named arguments, users would have to provide a dummy
@@ -104,7 +108,7 @@ Map<Type, DummyBuilder> _defaultDummyBuilders = {
104108
int: (_, _i) => _dummyInt,
105109
num: (_, _i) => _dummyInt,
106110
double: (_, _i) => _dummyDouble,
107-
String: (_, _i) => _dummyString,
111+
String: _dummyString,
108112
Int8List: (_, _i) => Int8List(0),
109113
Int16List: (_, _i) => Int16List(0),
110114
Int32List: (_, _i) => Int32List(0),

lib/src/mock.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void throwOnMissingStub(
123123
/// be generated. As such, [Mock] should strictly _not_ be used in any
124124
/// production code, especially if used within the context of Dart for Web
125125
/// (dart2js, DDC) and Dart for Mobile (Flutter).
126-
class Mock {
126+
mixin class Mock {
127127
static Null _answerNull(_) => null;
128128

129129
static const _nullResponse = CallPair<Null>.allInvocations(_answerNull);
@@ -1270,7 +1270,7 @@ void resetMockitoState() {
12701270
resetDummyBuilders();
12711271
}
12721272

1273-
extension on Invocation {
1273+
extension PrettyString on Invocation {
12741274
/// Returns a pretty String representing a method (or getter or setter) call
12751275
/// including its arguments, separating elements with newlines when it should
12761276
/// improve readability.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: >-
66
repository: https://github.com/dart-lang/mockito
77

88
environment:
9-
sdk: '>=2.19.0 <4.0.0'
9+
sdk: '>=3.0.0 <4.0.0'
1010

1111
dependencies:
1212
analyzer: '>=5.12.0 <7.0.0'

test/builder/auto_mocks_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,14 +2259,15 @@ void main() {
22592259
);
22602260
});
22612261

2262-
test('creates dummy non-null String return value', () async {
2262+
test('calls dummyValue to get a dummy non-null String return value',
2263+
() async {
22632264
await expectSingleNonNullableOutput(
22642265
dedent(r'''
22652266
class Foo {
22662267
String m() => "Hello";
22672268
}
22682269
'''),
2269-
_containsAllOf("returnValue: '',"),
2270+
_containsAllOf('returnValue: _i3.dummyValue<String>('),
22702271
);
22712272
});
22722273

test/end2end/generated_mocks_test.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,19 @@ void main() {
263263

264264
test('an unstubbed method returns a value', () {
265265
when(foo.namedParameter(x: 42)).thenReturn('Stubbed');
266-
expect(foo.namedParameter(x: 43), equals(''));
266+
expect(
267+
foo.namedParameter(x: 43),
268+
contains(Uri.encodeComponent(
269+
'Dummy String created while calling namedParameter({x: 43})'
270+
.replaceAll(' ', '_'))));
267271
});
268272

269273
test('an unstubbed getter returns a value', () {
270-
expect(foo.getter, equals(''));
274+
expect(
275+
foo.getter,
276+
contains(Uri.encodeComponent(
277+
'Dummy String created while calling getter'
278+
.replaceAll(' ', '_'))));
271279
});
272280
});
273281

@@ -280,11 +288,19 @@ void main() {
280288

281289
test('an unstubbed method returns a value', () {
282290
when(foo.namedParameter(x: 42)).thenReturn('Stubbed');
283-
expect(foo.namedParameter(x: 43), equals(''));
291+
expect(
292+
foo.namedParameter(x: 43),
293+
contains(Uri.encodeComponent(
294+
'Dummy String created while calling namedParameter({x: 43})'
295+
.replaceAll(' ', '_'))));
284296
});
285297

286298
test('an unstubbed getter returns a value', () {
287-
expect(foo.getter, equals(''));
299+
expect(
300+
foo.getter,
301+
contains(Uri.encodeComponent(
302+
'Dummy String created while calling getter'
303+
.replaceAll(' ', '_'))));
288304
});
289305

290306
test('an unstubbed method returning non-core type returns a fake', () {

0 commit comments

Comments
 (0)