Skip to content

Commit 0d582b7

Browse files
munificentcopybara-github
authored andcommitted
In custom_mocks_test, there were only a handful that were sensitive to formatting, so I just loosened the tests a bit manually.
In auto_mocks_test, there were about 50 failures, so I cobbled together a more generic approach. I used the existing _containsAllOf() function to hook in a custom matcher that ignores differences in whitespace and trailing commas. PiperOrigin-RevId: 691644163
1 parent 57d484f commit 0d582b7

File tree

2 files changed

+97
-35
lines changed

2 files changed

+97
-35
lines changed

test/builder/auto_mocks_test.dart

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ void main() {
22502250
bool m() => false;
22512251
}
22522252
'''),
2253-
_containsAllOf('returnValue: false,'),
2253+
_containsAllOf('returnValue: false'),
22542254
);
22552255
});
22562256

@@ -2261,7 +2261,7 @@ void main() {
22612261
double m() => 3.14;
22622262
}
22632263
'''),
2264-
_containsAllOf('returnValue: 0.0,'),
2264+
_containsAllOf('returnValue: 0.0'),
22652265
);
22662266
});
22672267

@@ -2272,7 +2272,7 @@ void main() {
22722272
int m() => 7;
22732273
}
22742274
'''),
2275-
_containsAllOf('returnValue: 0,'),
2275+
_containsAllOf('returnValue: 0'),
22762276
);
22772277
});
22782278

@@ -2295,7 +2295,7 @@ void main() {
22952295
List<Foo> m() => [Foo()];
22962296
}
22972297
'''),
2298-
_containsAllOf('returnValue: <_i2.Foo>[],'),
2298+
_containsAllOf('returnValue: <_i2.Foo>[]'),
22992299
);
23002300
});
23012301

@@ -2306,7 +2306,7 @@ void main() {
23062306
Set<Foo> m() => {Foo()};
23072307
}
23082308
'''),
2309-
_containsAllOf('returnValue: <_i2.Foo>{},'),
2309+
_containsAllOf('returnValue: <_i2.Foo>{}'),
23102310
);
23112311
});
23122312

@@ -2317,7 +2317,7 @@ void main() {
23172317
Map<int, Foo> m() => {7: Foo()};
23182318
}
23192319
'''),
2320-
_containsAllOf('returnValue: <int, _i2.Foo>{},'),
2320+
_containsAllOf('returnValue: <int, _i2.Foo>{}'),
23212321
);
23222322
});
23232323

@@ -2328,7 +2328,7 @@ void main() {
23282328
Map m();
23292329
}
23302330
'''),
2331-
_containsAllOf('returnValue: <dynamic, dynamic>{},'),
2331+
_containsAllOf('returnValue: <dynamic, dynamic>{}'),
23322332
);
23332333
});
23342334

@@ -2339,7 +2339,7 @@ void main() {
23392339
Future<bool> m() async => false;
23402340
}
23412341
'''),
2342-
_containsAllOf('returnValue: _i3.Future<bool>.value(false),'),
2342+
_containsAllOf('returnValue: _i3.Future<bool>.value(false)'),
23432343
);
23442344
});
23452345

@@ -2362,7 +2362,7 @@ void main() {
23622362
Stream<int> m();
23632363
}
23642364
'''),
2365-
_containsAllOf('returnValue: _i3.Stream<int>.empty(),'),
2365+
_containsAllOf('returnValue: _i3.Stream<int>.empty()'),
23662366
);
23672367
});
23682368

@@ -2384,7 +2384,7 @@ void main() {
23842384
#m,
23852385
[],
23862386
),
2387-
),'''),
2387+
)'''),
23882388
);
23892389
});
23902390

@@ -2403,7 +2403,7 @@ void main() {
24032403
#m,
24042404
[],
24052405
),
2406-
),'''),
2406+
)'''),
24072407
);
24082408
});
24092409

@@ -2418,7 +2418,7 @@ void main() {
24182418
two,
24192419
}
24202420
'''),
2421-
_containsAllOf('returnValue: _i2.Bar.one,'),
2421+
_containsAllOf('returnValue: _i2.Bar.one'),
24222422
);
24232423
});
24242424

@@ -2435,7 +2435,7 @@ void main() {
24352435
returnValue: (
24362436
int __p0, [
24372437
String? __p1,
2438-
]) {},'''),
2438+
]) {}'''),
24392439
);
24402440
});
24412441

@@ -2452,7 +2452,7 @@ void main() {
24522452
returnValue: (
24532453
_i2.Foo __p0, {
24542454
bool? b,
2455-
}) {},'''),
2455+
}) {}'''),
24562456
);
24572457
});
24582458

@@ -2469,7 +2469,7 @@ void main() {
24692469
returnValue: (
24702470
_i2.Foo __p0, {
24712471
required bool b,
2472-
}) {},'''),
2472+
}) {}'''),
24732473
);
24742474
});
24752475

@@ -2489,7 +2489,7 @@ void main() {
24892489
#m,
24902490
[],
24912491
),
2492-
),'''),
2492+
)'''),
24932493
);
24942494
});
24952495

@@ -2510,7 +2510,7 @@ void main() {
25102510
#m,
25112511
[],
25122512
),
2513-
),
2513+
)
25142514
'''),
25152515
);
25162516
});
@@ -2523,7 +2523,7 @@ void main() {
25232523
T? Function<T>(T) m() => (int i, [String s]) {};
25242524
}
25252525
'''),
2526-
_containsAllOf('returnValue: <T>(T __p0) => null,'),
2526+
_containsAllOf('returnValue: <T>(T __p0) => null'),
25272527
);
25282528
});
25292529

@@ -3753,8 +3753,49 @@ void main() {
37533753
});
37543754
}
37553755

3756-
TypeMatcher<List<int>> _containsAllOf(a, [b]) => decodedMatches(
3757-
b == null ? allOf(contains(a)) : allOf(contains(a), contains(b)));
3756+
TypeMatcher<List<int>> _containsAllOf(String a, [String? b]) =>
3757+
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+
}
37583799

37593800
/// Expect that [testBuilder], given [assets], in a package which has opted into
37603801
/// null safety, throws an [InvalidMockitoAnnotationException] with a message

test/builder/custom_mocks_test.dart

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,11 @@ void main() {
439439
void main() {}
440440
'''
441441
});
442+
expect(mocksContent, contains('m() => throw UnsupportedError(\n'));
442443
expect(
443444
mocksContent,
444-
contains(' m() => throw UnsupportedError(\n'
445-
' r\'"m" cannot be used without a mockito fallback generator.\');'));
445+
contains(
446+
'r\'"m" cannot be used without a mockito fallback generator.\''));
446447
});
447448

448449
test('generates mock getters with private types, given unsupportedMembers',
@@ -465,10 +466,11 @@ void main() {
465466
void main() {}
466467
'''
467468
});
469+
expect(mocksContent, contains('get f => throw UnsupportedError('));
468470
expect(
469471
mocksContent,
470-
contains(' get f => throw UnsupportedError(\n'
471-
' r\'"f" cannot be used without a mockito fallback generator.\');'));
472+
contains(
473+
'r\'"f" cannot be used without a mockito fallback generator.\''));
472474
});
473475

474476
test('generates mock setters with private types, given unsupportedMembers',
@@ -491,10 +493,11 @@ void main() {
491493
void main() {}
492494
'''
493495
});
496+
expect(mocksContent, contains('set f(value) => throw UnsupportedError('));
494497
expect(
495498
mocksContent,
496-
contains(' set f(value) => throw UnsupportedError(\n'
497-
' r\'"f=" cannot be used without a mockito fallback generator.\');'));
499+
contains(
500+
'r\'"f=" cannot be used without a mockito fallback generator.\''));
498501
});
499502

500503
test(
@@ -518,10 +521,11 @@ void main() {
518521
void main() {}
519522
'''
520523
});
524+
expect(mocksContent, contains('m() => throw UnsupportedError('));
521525
expect(
522526
mocksContent,
523-
contains(' m() => throw UnsupportedError(\n'
524-
' r\'"m" cannot be used without a mockito fallback generator.\');'));
527+
contains(
528+
'r\'"m" cannot be used without a mockito fallback generator.\''));
525529
});
526530

527531
test(
@@ -545,10 +549,11 @@ void main() {
545549
void main() {}
546550
'''
547551
});
552+
expect(mocksContent, contains('m() => throw UnsupportedError('));
548553
expect(
549554
mocksContent,
550-
contains(' m() => throw UnsupportedError(\n'
551-
' r\'"m" cannot be used without a mockito fallback generator.\');'));
555+
contains(
556+
'r\'"m" cannot be used without a mockito fallback generator.\''));
552557
});
553558

554559
test(
@@ -572,10 +577,11 @@ void main() {
572577
void main() {}
573578
'''
574579
});
580+
expect(mocksContent, contains('void m(b) => throw UnsupportedError('));
575581
expect(
576582
mocksContent,
577-
contains(' void m(b) => throw UnsupportedError(\n'
578-
' r\'"m" cannot be used without a mockito fallback generator.\');'));
583+
contains(
584+
'r\'"m" cannot be used without a mockito fallback generator.\''));
579585
});
580586

581587
test(
@@ -629,7 +635,20 @@ void main() {
629635
void main() {}
630636
'''
631637
});
632-
expect(mocksContent, contains('''
638+
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.
642+
expect(
643+
mocksContent,
644+
anyOf(
645+
contains('''
646+
returnValue: _FakeBar_0(this, Invocation.method(#m, [])),
647+
returnValueForMissingStub: _FakeBar_0(
648+
this,
649+
Invocation.method(#m, []),
650+
),'''),
651+
contains('''
633652
returnValue: _FakeBar_0(
634653
this,
635654
Invocation.method(
@@ -643,7 +662,9 @@ void main() {
643662
#m,
644663
[],
645664
),
646-
),'''));
665+
),'''),
666+
),
667+
);
647668
});
648669

649670
test('generates mock classes including a fallback generator for a getter',
@@ -672,7 +693,7 @@ void main() {
672693
void main() {}
673694
'''
674695
});
675-
expect(mocksContent, contains('returnValue: _i3.fShim(),'));
696+
expect(mocksContent, contains('returnValue: _i3.fShim()'));
676697
});
677698

678699
test(

0 commit comments

Comments
 (0)