Skip to content

Commit

Permalink
In custom_mocks_test, there were only a handful that were sensitive t…
Browse files Browse the repository at this point in the history
…o 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: 691623798
  • Loading branch information
munificent authored and copybara-github committed Oct 31, 2024
1 parent 57d484f commit 99dc00f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 35 deletions.
81 changes: 61 additions & 20 deletions test/builder/auto_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2250,7 +2250,7 @@ void main() {
bool m() => false;
}
'''),
_containsAllOf('returnValue: false,'),
_containsAllOf('returnValue: false'),
);
});

Expand All @@ -2261,7 +2261,7 @@ void main() {
double m() => 3.14;
}
'''),
_containsAllOf('returnValue: 0.0,'),
_containsAllOf('returnValue: 0.0'),
);
});

Expand All @@ -2272,7 +2272,7 @@ void main() {
int m() => 7;
}
'''),
_containsAllOf('returnValue: 0,'),
_containsAllOf('returnValue: 0'),
);
});

Expand All @@ -2295,7 +2295,7 @@ void main() {
List<Foo> m() => [Foo()];
}
'''),
_containsAllOf('returnValue: <_i2.Foo>[],'),
_containsAllOf('returnValue: <_i2.Foo>[]'),
);
});

Expand All @@ -2306,7 +2306,7 @@ void main() {
Set<Foo> m() => {Foo()};
}
'''),
_containsAllOf('returnValue: <_i2.Foo>{},'),
_containsAllOf('returnValue: <_i2.Foo>{}'),
);
});

Expand All @@ -2317,7 +2317,7 @@ void main() {
Map<int, Foo> m() => {7: Foo()};
}
'''),
_containsAllOf('returnValue: <int, _i2.Foo>{},'),
_containsAllOf('returnValue: <int, _i2.Foo>{}'),
);
});

Expand All @@ -2328,7 +2328,7 @@ void main() {
Map m();
}
'''),
_containsAllOf('returnValue: <dynamic, dynamic>{},'),
_containsAllOf('returnValue: <dynamic, dynamic>{}'),
);
});

Expand All @@ -2339,7 +2339,7 @@ void main() {
Future<bool> m() async => false;
}
'''),
_containsAllOf('returnValue: _i3.Future<bool>.value(false),'),
_containsAllOf('returnValue: _i3.Future<bool>.value(false)'),
);
});

Expand All @@ -2362,7 +2362,7 @@ void main() {
Stream<int> m();
}
'''),
_containsAllOf('returnValue: _i3.Stream<int>.empty(),'),
_containsAllOf('returnValue: _i3.Stream<int>.empty()'),
);
});

Expand All @@ -2384,7 +2384,7 @@ void main() {
#m,
[],
),
),'''),
)'''),
);
});

Expand All @@ -2403,7 +2403,7 @@ void main() {
#m,
[],
),
),'''),
)'''),
);
});

Expand All @@ -2418,7 +2418,7 @@ void main() {
two,
}
'''),
_containsAllOf('returnValue: _i2.Bar.one,'),
_containsAllOf('returnValue: _i2.Bar.one'),
);
});

Expand All @@ -2435,7 +2435,7 @@ void main() {
returnValue: (
int __p0, [
String? __p1,
]) {},'''),
]) {}'''),
);
});

Expand All @@ -2452,7 +2452,7 @@ void main() {
returnValue: (
_i2.Foo __p0, {
bool? b,
}) {},'''),
}) {}'''),
);
});

Expand All @@ -2469,7 +2469,7 @@ void main() {
returnValue: (
_i2.Foo __p0, {
required bool b,
}) {},'''),
}) {}'''),
);
});

Expand All @@ -2489,7 +2489,7 @@ void main() {
#m,
[],
),
),'''),
)'''),
);
});

Expand All @@ -2510,7 +2510,7 @@ void main() {
#m,
[],
),
),
)
'''),
);
});
Expand All @@ -2523,7 +2523,7 @@ void main() {
T? Function<T>(T) m() => (int i, [String s]) {};
}
'''),
_containsAllOf('returnValue: <T>(T __p0) => null,'),
_containsAllOf('returnValue: <T>(T __p0) => null'),
);
});

Expand Down Expand Up @@ -3753,8 +3753,49 @@ void main() {
});
}

TypeMatcher<List<int>> _containsAllOf(a, [b]) => decodedMatches(
b == null ? allOf(contains(a)) : allOf(contains(a), contains(b)));
TypeMatcher<List<int>> _containsAllOf(String a, [String? b]) =>
decodedMatches(b == null
? _ContainsIgnoringFormattingMatcher(a)
: allOf(_ContainsIgnoringFormattingMatcher(a),
_ContainsIgnoringFormattingMatcher(b)));

/// Matches a string that contains a given string, ignoring differences related
/// to formatting: whitespace and trailing commas.
class _ContainsIgnoringFormattingMatcher extends Matcher {
/// Matches one or more whitespace characters.
static final _whitespacePattern = RegExp(r'\s+');

/// Matches a trailing comma preceding a closing bracket character.
static final _trailingCommaPattern = RegExp(r',\s*([)}\]])');

/// The string that the actual value must contain in order for the match to
/// succeed.
final String _expected;

_ContainsIgnoringFormattingMatcher(this._expected);

@override
Description describe(Description description) {
return description
.add('Contains "$_expected" when ignoring source formatting');
}

@override
bool matches(item, Map matchState) =>
_stripFormatting(item.toString()).contains(_stripFormatting(_expected));

/// Removes whitespace and trailing commas.
///
/// Note that the result is not valid code because it means adjacent
///.identifiers and operators may be joined in ways that break the semantics.
/// The goal is not to produce an but valid version of the code, just to
/// produce a string that will reliably match the actual string when it has
/// also been stripped the same way.
String _stripFormatting(String code) => code
.replaceAll(_whitespacePattern, '')
.replaceAllMapped(_trailingCommaPattern, (match) => match[1]!)
.trim();
}

/// Expect that [testBuilder], given [assets], in a package which has opted into
/// null safety, throws an [InvalidMockitoAnnotationException] with a message
Expand Down
51 changes: 36 additions & 15 deletions test/builder/custom_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,11 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('m() => throw UnsupportedError(\n'));
expect(
mocksContent,
contains(' m() => throw UnsupportedError(\n'
' r\'"m" cannot be used without a mockito fallback generator.\');'));
contains(
'r\'"m" cannot be used without a mockito fallback generator.\''));
});

test('generates mock getters with private types, given unsupportedMembers',
Expand All @@ -465,10 +466,11 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('get f => throw UnsupportedError('));
expect(
mocksContent,
contains(' get f => throw UnsupportedError(\n'
' r\'"f" cannot be used without a mockito fallback generator.\');'));
contains(
'r\'"f" cannot be used without a mockito fallback generator.\''));
});

test('generates mock setters with private types, given unsupportedMembers',
Expand All @@ -491,10 +493,11 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('set f(value) => throw UnsupportedError('));
expect(
mocksContent,
contains(' set f(value) => throw UnsupportedError(\n'
' r\'"f=" cannot be used without a mockito fallback generator.\');'));
contains(
'r\'"f=" cannot be used without a mockito fallback generator.\''));
});

test(
Expand All @@ -518,10 +521,11 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('m() => throw UnsupportedError('));
expect(
mocksContent,
contains(' m() => throw UnsupportedError(\n'
' r\'"m" cannot be used without a mockito fallback generator.\');'));
contains(
'r\'"m" cannot be used without a mockito fallback generator.\''));
});

test(
Expand All @@ -545,10 +549,11 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('m() => throw UnsupportedError('));
expect(
mocksContent,
contains(' m() => throw UnsupportedError(\n'
' r\'"m" cannot be used without a mockito fallback generator.\');'));
contains(
'r\'"m" cannot be used without a mockito fallback generator.\''));
});

test(
Expand All @@ -572,10 +577,11 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('void m(b) => throw UnsupportedError('));
expect(
mocksContent,
contains(' void m(b) => throw UnsupportedError(\n'
' r\'"m" cannot be used without a mockito fallback generator.\');'));
contains(
'r\'"m" cannot be used without a mockito fallback generator.\''));
});

test(
Expand Down Expand Up @@ -629,7 +635,20 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('''

// TODO(rnystrom): Allow the test to pass using the old or new formatting
// styles. Remove the test for the old style once google3 is migrated to
// the new formatter.
expect(
mocksContent,
anyOf(
contains('''
returnValue: _FakeBar_0(this, Invocation.method(#m, [])),
returnValueForMissingStub: _FakeBar_0(
this,
Invocation.method(#m, []),
),'''),
contains('''
returnValue: _FakeBar_0(
this,
Invocation.method(
Expand All @@ -643,7 +662,9 @@ void main() {
#m,
[],
),
),'''));
),'''),
),
);
});

test('generates mock classes including a fallback generator for a getter',
Expand Down Expand Up @@ -672,7 +693,7 @@ void main() {
void main() {}
'''
});
expect(mocksContent, contains('returnValue: _i3.fShim(),'));
expect(mocksContent, contains('returnValue: _i3.fShim()'));
});

test(
Expand Down

0 comments on commit 99dc00f

Please sign in to comment.