forked from felangel/mocktail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_real_call.dart
97 lines (83 loc) · 2.99 KB
/
_real_call.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
part of 'mocktail.dart';
/// {@template real_call}
/// A real invocation on a mock.
/// {@endtemplate}
class RealCall {
/// {@macro real_call}
RealCall(this.mock, this.invocation) : timeStamp = _timer.now();
/// The mock instance.
final Mock mock;
/// The invocation.
final Invocation invocation;
/// When the invocation occurred.
final DateTime timeStamp;
/// Whether it was verified.
bool verified = false;
@override
String toString() {
var verifiedText = verified ? '[VERIFIED] ' : '';
return '$verifiedText$mock.${invocation.toPrettyString()}';
}
}
/// {@template real_call_with_captured_args}
/// A simple struct for storing a [RealCall] and any [capturedArgs] stored
/// during `InvocationMatcher.match`.
/// {@endtemplate}
class RealCallWithCapturedArgs {
/// {@macro real_call_with_captured_args}
const RealCallWithCapturedArgs(this.realCall, this.capturedArgs);
/// The [RealCall] instance.
final RealCall realCall;
/// Any captured arguments.
final List<Object?> capturedArgs;
}
extension on Invocation {
/// Returns a pretty String representing a method (or getter or setter) call
/// including its arguments, separating elements with newlines when it should
/// improve readability.
String toPrettyString() {
String argString;
var args = positionalArguments.map((dynamic v) => '$v');
if (args.any((arg) => arg.contains('\n'))) {
// As one or more arg contains newlines, put each on its own line, and
// indent each, for better readability.
argString =
'''\n${args.map((arg) => arg.splitMapJoin('\n', onNonMatch: (m) => ' $m')).join(',\n')}''';
} else {
// A compact String should be perfect.
argString = args.join(', ');
}
if (namedArguments.isNotEmpty) {
if (argString.isNotEmpty) argString += ', ';
var namedArgs = namedArguments.keys
.map((key) => '${_symbolToString(key)}: ${namedArguments[key]}');
if (namedArgs.any((arg) => arg.contains('\n'))) {
// As one or more arg contains newlines, put each on its own line, and
// indent each, for better readability.
namedArgs = namedArgs
.map((arg) => arg.splitMapJoin('\n', onNonMatch: (m) => ' $m'));
argString += '{\n${namedArgs.join(',\n')}}';
} else {
// A compact String should be perfect.
argString += '{${namedArgs.join(', ')}}';
}
}
var method = _symbolToString(memberName);
if (isMethod) {
var typeArgsString = '';
if (typeArguments.isNotEmpty) {
typeArgsString = '<${typeArguments.join(', ')}>';
}
method = '$method$typeArgsString($argString)';
} else if (isGetter) {
method = '$method';
} else if (isSetter) {
method = '$method=$argString';
} else {
throw StateError('Invocation should be getter, setter or a method call.');
}
return method;
}
}
// Converts a [Symbol] to a meaningful [String].
String _symbolToString(Symbol symbol) => symbol.toString().split('"')[1];