Skip to content

Commit f72791d

Browse files
munificentcopybara-github
authored andcommitted
In my previous change, I tried a simpler approach in custom_mocks_test to try to not be sensitive to formatting changes before I realized that wouldn't work in auto_mocks_test and came up with something more robust.
It turns out the simpler approach is still too brittle and some tests fail under the new style (specifically, the new formatter splits after `=>` when the old one doesn't). So I hoisted containsIgnoringFormatting() into a separate library and refactored custom_mocks_test to use it too. PiperOrigin-RevId: 691836139
1 parent 0d582b7 commit f72791d

File tree

3 files changed

+73
-75
lines changed

3 files changed

+73
-75
lines changed

test/builder/auto_mocks_test.dart

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import 'package:mockito/src/builder.dart';
2222
import 'package:package_config/package_config.dart';
2323
import 'package:test/test.dart';
2424

25+
import 'contains_ignoring_formatting.dart';
26+
2527
const annotationsAsset = {
2628
'mockito|lib/annotations.dart': '''
2729
class GenerateMocks {
@@ -3755,47 +3757,8 @@ void main() {
37553757

37563758
TypeMatcher<List<int>> _containsAllOf(String a, [String? b]) =>
37573759
decodedMatches(b == null
3758-
? _ContainsIgnoringFormattingMatcher(a)
3759-
: allOf(_ContainsIgnoringFormattingMatcher(a),
3760-
_ContainsIgnoringFormattingMatcher(b)));
3761-
3762-
/// Matches a string that contains a given string, ignoring differences related
3763-
/// to formatting: whitespace and trailing commas.
3764-
class _ContainsIgnoringFormattingMatcher extends Matcher {
3765-
/// Matches one or more whitespace characters.
3766-
static final _whitespacePattern = RegExp(r'\s+');
3767-
3768-
/// Matches a trailing comma preceding a closing bracket character.
3769-
static final _trailingCommaPattern = RegExp(r',\s*([)}\]])');
3770-
3771-
/// The string that the actual value must contain in order for the match to
3772-
/// succeed.
3773-
final String _expected;
3774-
3775-
_ContainsIgnoringFormattingMatcher(this._expected);
3776-
3777-
@override
3778-
Description describe(Description description) {
3779-
return description
3780-
.add('Contains "$_expected" when ignoring source formatting');
3781-
}
3782-
3783-
@override
3784-
bool matches(item, Map matchState) =>
3785-
_stripFormatting(item.toString()).contains(_stripFormatting(_expected));
3786-
3787-
/// Removes whitespace and trailing commas.
3788-
///
3789-
/// Note that the result is not valid code because it means adjacent
3790-
///.identifiers and operators may be joined in ways that break the semantics.
3791-
/// The goal is not to produce an but valid version of the code, just to
3792-
/// produce a string that will reliably match the actual string when it has
3793-
/// also been stripped the same way.
3794-
String _stripFormatting(String code) => code
3795-
.replaceAll(_whitespacePattern, '')
3796-
.replaceAllMapped(_trailingCommaPattern, (match) => match[1]!)
3797-
.trim();
3798-
}
3760+
? containsIgnoringFormatting(a)
3761+
: allOf(containsIgnoringFormatting(a), containsIgnoringFormatting(b)));
37993762

38003763
/// Expect that [testBuilder], given [assets], in a package which has opted into
38013764
/// null safety, throws an [InvalidMockitoAnnotationException] with a message
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024 Dart Mockito authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import 'package:test/test.dart';
16+
17+
/// Returns a [Matcher] that checks that the actual string of Dart code
18+
/// contains [expected] when ignoring differences due to formatting: whitespace
19+
/// and trailing commas.
20+
Matcher containsIgnoringFormatting(String expected) =>
21+
_ContainsIgnoringFormattingMatcher(expected);
22+
23+
/// Matches a string that contains a given string, ignoring differences related
24+
/// to formatting: whitespace and trailing commas.
25+
class _ContainsIgnoringFormattingMatcher extends Matcher {
26+
/// Matches one or more whitespace characters.
27+
static final _whitespacePattern = RegExp(r'\s+');
28+
29+
/// Matches a trailing comma preceding a closing bracket character.
30+
static final _trailingCommaPattern = RegExp(r',\s*([)}\]])');
31+
32+
/// The string that the actual value must contain in order for the match to
33+
/// succeed.
34+
final String _expected;
35+
36+
_ContainsIgnoringFormattingMatcher(this._expected);
37+
38+
@override
39+
Description describe(Description description) {
40+
return description
41+
.add('Contains "$_expected" when ignoring source formatting');
42+
}
43+
44+
@override
45+
bool matches(item, Map matchState) =>
46+
_stripFormatting(item.toString()).contains(_stripFormatting(_expected));
47+
48+
/// Removes whitespace and trailing commas.
49+
///
50+
/// Note that the result is not valid code because it means adjacent
51+
///.identifiers and operators may be joined in ways that break the semantics.
52+
/// The goal is not to produce an but valid version of the code, just to
53+
/// produce a string that will reliably match the actual string when it has
54+
/// also been stripped the same way.
55+
String _stripFormatting(String code) => code
56+
.replaceAll(_whitespacePattern, '')
57+
.replaceAllMapped(_trailingCommaPattern, (match) => match[1]!)
58+
.trim();
59+
}

test/builder/custom_mocks_test.dart

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import 'package:mockito/src/builder.dart';
2121
import 'package:package_config/package_config.dart';
2222
import 'package:test/test.dart';
2323

24+
import 'contains_ignoring_formatting.dart';
25+
2426
Builder buildMocks(BuilderOptions options) => MockBuilder();
2527

2628
const annotationsAsset = {
@@ -439,10 +441,9 @@ void main() {
439441
void main() {}
440442
'''
441443
});
442-
expect(mocksContent, contains('m() => throw UnsupportedError(\n'));
443444
expect(
444445
mocksContent,
445-
contains(
446+
containsIgnoringFormatting('m() => throw UnsupportedError('
446447
'r\'"m" cannot be used without a mockito fallback generator.\''));
447448
});
448449

@@ -466,10 +467,9 @@ void main() {
466467
void main() {}
467468
'''
468469
});
469-
expect(mocksContent, contains('get f => throw UnsupportedError('));
470470
expect(
471471
mocksContent,
472-
contains(
472+
containsIgnoringFormatting('get f => throw UnsupportedError('
473473
'r\'"f" cannot be used without a mockito fallback generator.\''));
474474
});
475475

@@ -493,10 +493,9 @@ void main() {
493493
void main() {}
494494
'''
495495
});
496-
expect(mocksContent, contains('set f(value) => throw UnsupportedError('));
497496
expect(
498497
mocksContent,
499-
contains(
498+
containsIgnoringFormatting('set f(value) => throw UnsupportedError('
500499
'r\'"f=" cannot be used without a mockito fallback generator.\''));
501500
});
502501

@@ -521,10 +520,9 @@ void main() {
521520
void main() {}
522521
'''
523522
});
524-
expect(mocksContent, contains('m() => throw UnsupportedError('));
525523
expect(
526524
mocksContent,
527-
contains(
525+
containsIgnoringFormatting('m() => throw UnsupportedError('
528526
'r\'"m" cannot be used without a mockito fallback generator.\''));
529527
});
530528

@@ -549,10 +547,9 @@ void main() {
549547
void main() {}
550548
'''
551549
});
552-
expect(mocksContent, contains('m() => throw UnsupportedError('));
553550
expect(
554551
mocksContent,
555-
contains(
552+
containsIgnoringFormatting('m() => throw UnsupportedError('
556553
'r\'"m" cannot be used without a mockito fallback generator.\''));
557554
});
558555

@@ -577,10 +574,9 @@ void main() {
577574
void main() {}
578575
'''
579576
});
580-
expect(mocksContent, contains('void m(b) => throw UnsupportedError('));
581577
expect(
582578
mocksContent,
583-
contains(
579+
containsIgnoringFormatting('void m(b) => throw UnsupportedError('
584580
'r\'"m" cannot be used without a mockito fallback generator.\''));
585581
});
586582

@@ -636,34 +632,14 @@ void main() {
636632
'''
637633
});
638634

639-
// TODO(rnystrom): Allow the test to pass using the old or new formatting
640-
// styles. Remove the test for the old style once google3 is migrated to
641-
// the new formatter.
642635
expect(
643636
mocksContent,
644-
anyOf(
645-
contains('''
637+
containsIgnoringFormatting('''
646638
returnValue: _FakeBar_0(this, Invocation.method(#m, [])),
647639
returnValueForMissingStub: _FakeBar_0(
648640
this,
649641
Invocation.method(#m, []),
650-
),'''),
651-
contains('''
652-
returnValue: _FakeBar_0(
653-
this,
654-
Invocation.method(
655-
#m,
656-
[],
657-
),
658-
),
659-
returnValueForMissingStub: _FakeBar_0(
660-
this,
661-
Invocation.method(
662-
#m,
663-
[],
664-
),
665-
),'''),
666-
),
642+
)'''),
667643
);
668644
});
669645

0 commit comments

Comments
 (0)