Skip to content

Commit 413538b

Browse files
committed
Address PR feedback
1 parent 63ec871 commit 413538b

File tree

4 files changed

+140
-72
lines changed

4 files changed

+140
-72
lines changed

lib/src/over_react_test/custom_matchers.dart

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,15 @@ Matcher throwsPropError_Combination(String propName, String prop2Name, [String m
348348
/// logs that are emitted during the function runtime.
349349
class _LoggingFunctionMatcher extends CustomMatcher {
350350
_LoggingFunctionMatcher(dynamic matcher, {this.config, description, name})
351-
: super(description ?? 'emits the logs', name ?? 'logs', wrapMatcher(matcher));
351+
: super(description ?? 'emits the logs', name ?? 'logs', _wrapMatcherForSingleLog(matcher));
352352

353353
final ConsoleConfiguration config;
354354

355+
static dynamic _wrapMatcherForSingleLog(dynamic expected) {
356+
if (expected is Matcher || expected is List) return expected;
357+
return contains(expected);
358+
}
359+
355360
@override
356361
featureValueOf(actual) {
357362
var logs = <String>[];
@@ -378,8 +383,8 @@ class _LoggingFunctionMatcher extends CustomMatcher {
378383
/// `errorConfig`, the actual list of logs will include the error message from the
379384
/// caught error.
380385
///
381-
/// Related: [emitsLogs], [emitsNoLogs]
382-
Matcher emitsLog(String expected, {ConsoleConfiguration consoleConfig}) =>
386+
/// Related: [logsToConsole], [hasNoLogs]
387+
Matcher hasLog(dynamic expected, {ConsoleConfiguration consoleConfig}) =>
383388
_LoggingFunctionMatcher(anyElement(contains(expected)), config: consoleConfig);
384389

385390
/// A Matcher used to compare a list of logs against a provided matcher.
@@ -392,10 +397,10 @@ Matcher emitsLog(String expected, {ConsoleConfiguration consoleConfig}) =>
392397
/// __Examples:__
393398
///
394399
/// To look for a specific `String` in any log index, the best solution is to use
395-
/// [emitsLog], but the behavior can be mimicked by passing in the correct `Iterable`
400+
/// [hasLog], but the behavior can be mimicked by passing in the correct `Iterable`
396401
/// matchers.
397402
/// ```dart
398-
/// expect(callbackFunction, emitsLogs(anyElement(contains('I expect this log'))));
403+
/// expect(callbackFunction, logsToConsole(anyElement(contains('I expect this log'))));
399404
/// ```
400405
///
401406
/// When passed a `List`, the matcher will do an equality check on the actual
@@ -404,34 +409,34 @@ Matcher emitsLog(String expected, {ConsoleConfiguration consoleConfig}) =>
404409
/// Alternatively, the `String` can be wrapped in a `contains` to check the
405410
/// if the comparable index contains that substring.
406411
/// ```dart
407-
/// expect(callbackFunction, emitsLogs(['I expect this log', 'And this Log']));
408-
/// expect(callbackFunction, emitsLogs([
412+
/// expect(callbackFunction, logsToConsole(['I expect this log', 'And this Log']));
413+
/// expect(callbackFunction, logsToConsole([
409414
/// contains('I expect'),
410415
/// contains('And this'),
411416
/// ]));
412417
/// ```
413418
///
414419
/// All usual `Iterable` matchers can also be used.
415420
/// ```dart
416-
/// expect(callbackFunction, emitsLogs(containsAll(['I expect this log'])));
417-
/// expect(callbackFunction, emitsLogs(containsAllInOrder(['I expect this log'])));
418-
/// expect(callbackFunction, emitsLogs(hasLength(1)));
421+
/// expect(callbackFunction, logsToConsole(containsAll(['I expect this log'])));
422+
/// expect(callbackFunction, logsToConsole(containsAllInOrder(['I expect this log'])));
423+
/// expect(callbackFunction, logsToConsole(hasLength(1)));
419424
/// ```
420425
///
421-
/// Related: [emitsLog], [emitsNoLogs]
422-
Matcher emitsLogs(dynamic expected, {ConsoleConfiguration consoleConfig}) =>
426+
/// Related: [hasLog], [hasNoLogs]
427+
Matcher logsToConsole(dynamic expected, {ConsoleConfiguration consoleConfig}) =>
423428
_LoggingFunctionMatcher(expected, config: consoleConfig);
424429

425430
/// A matcher to verify that a callback function does not emit any logs.
426431
///
427432
/// In the case the actual value is a callback that is run, any errors caused by
428433
/// the callback will be caught and ignored.
429434
///
430-
/// Related: [emitsLogs]
431-
final Matcher emitsNoLogs = _LoggingFunctionMatcher(isEmpty);
435+
/// Related: [logsToConsole]
436+
final Matcher hasNoLogs = _LoggingFunctionMatcher(isEmpty);
432437

433438
/// The string used to identify a `propType` error.
434-
const _propTypeErrorMessage = 'Failed prop type';
439+
const _propTypeErrorPrefix = 'Failed prop type';
435440

436441
/// A matcher used to assert an actual `List` contains expected `propType`
437442
/// warnings.
@@ -447,7 +452,7 @@ class _PropTypeLogMatcher extends _LoggingFunctionMatcher {
447452
description: 'emits the propType warning',
448453
name: 'propType warning');
449454

450-
final _filter = contains(_propTypeErrorMessage);
455+
final _filter = contains(_propTypeErrorPrefix);
451456

452457
@override
453458
featureValueOf(actual) {
@@ -463,7 +468,7 @@ class _PropTypeLogMatcher extends _LoggingFunctionMatcher {
463468
/// Matcher used to check for a specific `propType` warning being emitted during
464469
/// the runtime of a callback function.
465470
///
466-
/// Has the same underlying logic as [emitsLog], with the difference being that
471+
/// Has the same underlying logic as [hasLog], with the difference being that
467472
/// console configuration is set to `errorConfig` and non-propType related warnings
468473
/// are filtered out of the list.
469474
///
@@ -472,14 +477,14 @@ class _PropTypeLogMatcher extends _LoggingFunctionMatcher {
472477
/// is set to `errorConfig`, the actual list of logs will include the error
473478
/// message from the caught error.
474479
///
475-
/// Related: [emitsPropTypeWarnings], [emitsNoPropTypeWarnings], [emitsLog]
476-
_PropTypeLogMatcher emitsPropTypeWarning(String expected) =>
480+
/// Related: [logsPropTypeWarnings], [logsNoPropTypeWarnings], [hasLog]
481+
_PropTypeLogMatcher logsPropTypeWarning(String expected) =>
477482
_PropTypeLogMatcher(anyElement(contains(expected)));
478483

479484
/// Matcher used to check for specific `propType` warnings being emitted during
480485
/// the runtime of a callback function.
481486
///
482-
/// Has the same underlying logic as [emitsLogs], with the difference being that
487+
/// Has the same underlying logic as [logsToConsole], with the difference being that
483488
/// console configuration is set to `errorConfig` and non-propType related warnings
484489
/// are filtered out of the list.
485490
///
@@ -488,8 +493,8 @@ _PropTypeLogMatcher emitsPropTypeWarning(String expected) =>
488493
/// is set to `errorConfig`, the actual list of logs will include the error
489494
/// message from the caught error.
490495
///
491-
/// Related: [emitsPropTypeWarning], [emitsNoPropTypeWarnings], [emitsLogs]
492-
_PropTypeLogMatcher emitsPropTypeWarnings(dynamic expected) =>
496+
/// Related: [logsPropTypeWarnings], [logsNoPropTypeWarnings], [logsToConsole]
497+
_PropTypeLogMatcher logsPropTypeWarnings(dynamic expected) =>
493498
_PropTypeLogMatcher(expected);
494499

495500
/// Matcher used enforce that there are no `propType` warnings.
@@ -499,5 +504,43 @@ _PropTypeLogMatcher emitsPropTypeWarnings(dynamic expected) =>
499504
/// is set to `errorConfig`, the actual list of logs will include the error
500505
/// message from the caught error.
501506
///
502-
/// Related: [emitsPropTypeWarning], [emitsPropTypeWarnings]
503-
final _PropTypeLogMatcher emitsNoPropTypeWarnings = _PropTypeLogMatcher(isEmpty);
507+
/// Related: [logsPropTypeWarning], [logsPropTypeWarnings]
508+
final _PropTypeLogMatcher logsNoPropTypeWarnings = _PropTypeLogMatcher(isEmpty);
509+
510+
/// A matcher to verify that a [PropError] is thrown with a provided `propName` and `message`.
511+
///
512+
/// This matcher is built on top of [logsPropTypeWarning] and has the same behavior
513+
/// of running a provided callback, swallowing errors that occur, and looking
514+
/// for the expected [PropError] in the resulting logs.
515+
_PropTypeLogMatcher logsPropError(String propName, [String message = '']) {
516+
return logsPropTypeWarning('PropError: Prop $propName $message'.trim());
517+
}
518+
519+
/// A matcher to verify that a [PropError].required is thrown with a provided `propName` and `message`.
520+
///
521+
/// This matcher is built on top of [logsPropTypeWarning] and has the same behavior
522+
/// of running a provided callback, swallowing errors that occur, and looking
523+
/// for the expected [PropError] in the resulting logs.
524+
_PropTypeLogMatcher logsRequiredPropError(String propName, [String message = '']) {
525+
return logsPropTypeWarning('RequiredPropError: Prop $propName is required. $message'.trim());
526+
}
527+
528+
/// A matcher to verify that a [PropError].value is thrown with a provided `invalidValue`, `propName`, and `message`.
529+
///
530+
/// This matcher is built on top of [logsPropTypeWarning] and has the same behavior
531+
/// of running a provided callback, swallowing errors that occur, and looking
532+
/// for the expected [PropError] in the resulting logs.
533+
_PropTypeLogMatcher logsValuePropError(dynamic invalidValue, String propName, [String message = '']) {
534+
return logsPropTypeWarning('InvalidPropValueError: Prop $propName set to $invalidValue. '
535+
'$message'.trim());
536+
}
537+
538+
/// A matcher to verify that a [PropError] is thrown with a provided `propName`, `prop2Name`, and `message`.
539+
///
540+
/// This matcher is built on top of [logsPropTypeWarning] and has the same behavior
541+
/// of running a provided callback, swallowing errors that occur, and looking
542+
/// for the expected [PropError] in the resulting logs.
543+
_PropTypeLogMatcher logsCombinationPropError(String propName, String prop2Name, [String message = '']) {
544+
return logsPropTypeWarning('InvalidPropCombinationError: Prop $propName and prop $prop2Name are set to '
545+
'incompatible values. $message'.trim());
546+
}

test/over_react_test/console_log_utils_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ main() {
280280
..shouldNeverBeNull = false)());
281281
}, configuration: errorConfig);
282282

283-
expect(logs, hasLength(2));
283+
expect(logs, hasLength(3));
284284
});
285285

286286
test('captures logs', () async {

0 commit comments

Comments
 (0)