Skip to content

Commit a4e2e6d

Browse files
authored
Merge pull request #639 from zvirja/consume-latest-language-features
Slightly refactor code to make it look fresh
2 parents 46d4e06 + 10101d6 commit a4e2e6d

File tree

93 files changed

+903
-975
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+903
-975
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ dotnet_style_qualification_for_field = false:suggestion
1717
dotnet_style_qualification_for_property = false:suggestion
1818
dotnet_style_qualification_for_method = false:suggestion
1919
dotnet_style_qualification_for_event = false:suggestion
20+
21+
# ReSharper properties
22+
resharper_int_align_switch_expressions = true
23+
resharper_keep_existing_invocation_parens_arrangement = false
24+
resharper_space_within_single_line_array_initializer_braces = true

src/NSubstitute/Callback.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static Callback AlwaysThrow<TException>(Func<CallInfo, TException> throwT
7272
/// <returns></returns>
7373
public static Callback AlwaysThrow<TException>(TException exception) where TException : Exception
7474
{
75-
return AlwaysThrow(info => exception);
75+
return AlwaysThrow(_ => exception);
7676
}
7777

7878
protected static Action<CallInfo> ToCallback<TException>(Func<CallInfo, TException> throwThis)
@@ -93,12 +93,12 @@ protected void AddCallback(Action<CallInfo> doThis)
9393

9494
protected void SetAlwaysDo(Action<CallInfo> always)
9595
{
96-
alwaysDo = always == null ? x => { } : always;
96+
alwaysDo = always ?? (_ => { });
9797
}
9898

9999
protected void SetKeepDoing(Action<CallInfo> keep)
100100
{
101-
keepDoing = keep == null ? x => { } : keep;
101+
keepDoing = keep ?? (_ => { });
102102
}
103103

104104
public void Call(CallInfo callInfo)

src/NSubstitute/Callbacks/ConfiguredCallback.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ internal ConfiguredCallback() { }
1313
/// <summary>
1414
/// Perform this action once in chain of called callbacks.
1515
/// </summary>
16-
/// <param name="doThis"></param>
17-
/// <returns></returns>
1816
public ConfiguredCallback Then(Action<CallInfo> doThis)
1917
{
2018
AddCallback(doThis);
@@ -24,8 +22,6 @@ public ConfiguredCallback Then(Action<CallInfo> doThis)
2422
/// <summary>
2523
/// Keep doing this action after the other callbacks have run.
2624
/// </summary>
27-
/// <param name="doThis"></param>
28-
/// <returns></returns>
2925
public EndCallbackChain ThenKeepDoing(Action<CallInfo> doThis)
3026
{
3127
SetKeepDoing(doThis);
@@ -35,31 +31,20 @@ public EndCallbackChain ThenKeepDoing(Action<CallInfo> doThis)
3531
/// <summary>
3632
/// Keep throwing this exception after the other callbacks have run.
3733
/// </summary>
38-
/// <typeparam name="TException"></typeparam>
39-
/// <param name="throwThis"></param>
40-
/// <returns></returns>
41-
public EndCallbackChain ThenKeepThrowing<TException>(Func<CallInfo, TException> throwThis) where TException : Exception
42-
{
43-
return ThenKeepDoing(ToCallback(throwThis));
44-
}
34+
public EndCallbackChain ThenKeepThrowing<TException>(Func<CallInfo, TException> throwThis) where TException : Exception =>
35+
ThenKeepDoing(ToCallback(throwThis));
4536

4637
/// <summary>
4738
/// Keep throwing this exception after the other callbacks have run.
4839
/// </summary>
49-
/// <typeparam name="TException"></typeparam>
50-
/// <param name="throwThis"></param>
51-
/// <returns></returns>
52-
public EndCallbackChain ThenKeepThrowing<TException>(TException throwThis) where TException : Exception
53-
{
54-
return ThenKeepThrowing(info => throwThis);
55-
}
40+
public EndCallbackChain ThenKeepThrowing<TException>(TException throwThis) where TException : Exception =>
41+
ThenKeepThrowing(info => throwThis);
5642

5743
/// <summary>
5844
/// Throw exception returned by function once when called in a chain of callbacks.
5945
/// </summary>
6046
/// <typeparam name="TException">The type of the exception</typeparam>
6147
/// <param name="throwThis">Produce the exception to throw for a CallInfo</param>
62-
/// <returns></returns>
6348
public ConfiguredCallback ThenThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception
6449
{
6550
AddCallback(ToCallback(throwThis));
@@ -71,11 +56,8 @@ public ConfiguredCallback ThenThrow<TException>(Func<CallInfo, TException> throw
7156
/// </summary>
7257
/// <typeparam name="TException">The type of the exception</typeparam>
7358
/// <param name="exception">The exception to throw</param>
74-
/// <returns></returns>
75-
public ConfiguredCallback ThenThrow<TException>(TException exception) where TException : Exception
76-
{
77-
return ThenThrow(ci => exception);
78-
}
59+
public ConfiguredCallback ThenThrow<TException>(TException exception) where TException : Exception =>
60+
ThenThrow(_ => exception);
7961
}
8062

8163
public class EndCallbackChain : Callback
@@ -86,7 +68,6 @@ internal EndCallbackChain() { }
8668
/// Perform the given action for every call.
8769
/// </summary>
8870
/// <param name="doThis">The action to perform for every call</param>
89-
/// <returns></returns>
9071
public Callback AndAlways(Action<CallInfo> doThis)
9172
{
9273
SetAlwaysDo(doThis);

src/NSubstitute/Core/Argument.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Argument(Type declaredType, Func<object?> getValue, Action<object?> setVa
2222

2323
public Argument(ICall call, int argIndex)
2424
{
25-
_call = call ?? throw new ArgumentNullException(nameof(call));
25+
_call = call;
2626
_argIndex = argIndex;
2727
}
2828

@@ -48,24 +48,16 @@ public object? Value
4848

4949
public Type ActualType => Value == null ? DeclaredType : Value.GetType();
5050

51-
public bool IsDeclaredTypeEqualToOrByRefVersionOf(Type type)
52-
{
53-
return AsNonByRefType(DeclaredType) == type;
54-
}
51+
public bool IsDeclaredTypeEqualToOrByRefVersionOf(Type type) =>
52+
AsNonByRefType(DeclaredType) == type;
5553

56-
public bool IsValueAssignableTo(Type type)
57-
{
58-
return type.IsAssignableFrom(AsNonByRefType(ActualType));
59-
}
54+
public bool IsValueAssignableTo(Type type) =>
55+
type.IsAssignableFrom(AsNonByRefType(ActualType));
6056

61-
public bool CanSetValueWithInstanceOf(Type type)
62-
{
63-
return AsNonByRefType(DeclaredType).IsAssignableFrom(type);
64-
}
57+
public bool CanSetValueWithInstanceOf(Type type) =>
58+
AsNonByRefType(DeclaredType).IsAssignableFrom(type);
6559

66-
private static Type AsNonByRefType(Type type)
67-
{
68-
return type.IsByRef ? type.GetElementType()! : type;
69-
}
60+
private static Type AsNonByRefType(Type type) =>
61+
type.IsByRef ? type.GetElementType()! : type;
7062
}
7163
}
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32
using System.Reflection;
43

54
namespace NSubstitute.Core.Arguments
@@ -16,20 +15,16 @@ public string Format(object? argument, bool highlight)
1615

1716
private string Format(object? arg)
1817
{
19-
switch (arg)
18+
return arg switch
2019
{
21-
case null:
22-
return "<null>";
20+
null => "<null>",
21+
string str => $"\"{str}\"",
22+
{ } obj when HasDefaultToString(obj) => arg.GetType().GetNonMangledTypeName(),
23+
_ => arg.ToString() ?? string.Empty
24+
};
2325

24-
case string str:
25-
return string.Format(CultureInfo.InvariantCulture, "\"{0}\"", str);
26-
27-
case object obj when obj.GetType().GetMethod(nameof(ToString), Type.EmptyTypes)!.DeclaringType == typeof(object):
28-
return arg.GetType().GetNonMangledTypeName();
29-
30-
default:
31-
return arg.ToString() ?? string.Empty;
32-
}
33-
}
26+
static bool HasDefaultToString(object obj)
27+
=> obj.GetType().GetMethod(nameof(ToString), Type.EmptyTypes)!.DeclaringType == typeof(object);
28+
}
3429
}
3530
}

src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public ArgumentMatchInfo(int index, object? argument, IArgumentSpecification spe
1111

1212
private readonly object? _argument;
1313
private readonly IArgumentSpecification _specification;
14-
public int Index { get; private set; }
14+
public int Index { get; }
1515

16-
public bool IsMatch { get { return _specification.IsSatisfiedBy(_argument); } }
16+
public bool IsMatch => _specification.IsSatisfiedBy(_argument);
1717

1818
public string DescribeNonMatch()
1919
{

src/NSubstitute/Core/Arguments/ArgumentMatcher.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using NSubstitute.Exceptions;
34

45
namespace NSubstitute.Core.Arguments
56
{
@@ -13,28 +14,20 @@ public static class ArgumentMatcher
1314
{
1415
if (argumentMatcher == null) throw new ArgumentNullException(nameof(argumentMatcher));
1516

16-
IArgumentMatcher nonGenericMatcher;
17-
if (argumentMatcher is IDescribeNonMatches)
17+
IArgumentMatcher nonGenericMatcher = argumentMatcher switch
1818
{
19-
nonGenericMatcher = new GenericToNonGenericMatcherProxyWithDescribe<T>(argumentMatcher);
20-
}
21-
else
22-
{
23-
nonGenericMatcher = new GenericToNonGenericMatcherProxy<T>(argumentMatcher);
24-
}
19+
IDescribeNonMatches => new GenericToNonGenericMatcherProxyWithDescribe<T>(argumentMatcher),
20+
_ => new GenericToNonGenericMatcherProxy<T>(argumentMatcher)
21+
};
2522

2623
return ref EnqueueArgSpecification<T>(new ArgumentSpecification(typeof(T), nonGenericMatcher));
2724
}
2825

29-
internal static ref T? Enqueue<T>(IArgumentMatcher argumentMatcher)
30-
{
31-
return ref EnqueueArgSpecification<T>(new ArgumentSpecification(typeof(T), argumentMatcher));
32-
}
26+
internal static ref T? Enqueue<T>(IArgumentMatcher argumentMatcher) =>
27+
ref EnqueueArgSpecification<T>(new ArgumentSpecification(typeof(T), argumentMatcher));
3328

34-
internal static ref T? Enqueue<T>(IArgumentMatcher argumentMatcher, Action<object?> action)
35-
{
36-
return ref EnqueueArgSpecification<T>(new ArgumentSpecification(typeof(T), argumentMatcher, action));
37-
}
29+
internal static ref T? Enqueue<T>(IArgumentMatcher argumentMatcher, Action<object?> action) =>
30+
ref EnqueueArgSpecification<T>(new ArgumentSpecification(typeof(T), argumentMatcher, action));
3831

3932
private static ref T? EnqueueArgSpecification<T>(IArgumentSpecification specification)
4033
{
@@ -51,14 +44,14 @@ public GenericToNonGenericMatcherProxy(IArgumentMatcher<T> matcher)
5144
_matcher = matcher;
5245
}
5346

54-
public bool IsSatisfiedBy(object? argument) => _matcher.IsSatisfiedBy((T) argument!);
47+
public bool IsSatisfiedBy(object? argument) => _matcher.IsSatisfiedBy((T?) argument!);
5548
}
5649

5750
private class GenericToNonGenericMatcherProxyWithDescribe<T> : GenericToNonGenericMatcherProxy<T>, IDescribeNonMatches
5851
{
5952
public GenericToNonGenericMatcherProxyWithDescribe(IArgumentMatcher<T> matcher) : base(matcher)
6053
{
61-
if (matcher as IDescribeNonMatches == null) throw new ArgumentException("Should implement IDescribeNonMatches type.");
54+
if (matcher is not IDescribeNonMatches) throw new SubstituteInternalException("Should implement IDescribeNonMatches type.");
6255
}
6356

6457
public string DescribeFor(object? argument) => ((IDescribeNonMatches) _matcher).DescribeFor(argument);

src/NSubstitute/Core/Arguments/ArgumentSpecification.cs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ namespace NSubstitute.Core.Arguments
44
{
55
public class ArgumentSpecification : IArgumentSpecification
66
{
7-
private static readonly Action<object?> NoOpAction = x => { };
8-
7+
private static readonly Action<object?> NoOpAction = _ => { };
8+
99
private readonly IArgumentMatcher _matcher;
1010
private readonly Action<object?> _action;
1111
public Type ForType { get; }
@@ -22,26 +22,40 @@ public ArgumentSpecification(Type forType, IArgumentMatcher matcher, Action<obje
2222

2323
public bool IsSatisfiedBy(object? argument)
2424
{
25-
if (!IsCompatibleWith(argument)) return false;
26-
try { return _matcher.IsSatisfiedBy(argument); }
27-
catch { return false; }
25+
if (!IsCompatibleWith(argument))
26+
{
27+
return false;
28+
}
29+
30+
try
31+
{
32+
return _matcher.IsSatisfiedBy(argument);
33+
}
34+
catch
35+
{
36+
return false;
37+
}
2838
}
2939

3040
public string DescribeNonMatch(object? argument)
3141
{
32-
var describable = _matcher as IDescribeNonMatches;
33-
if (describable == null) return string.Empty;
42+
if (!IsCompatibleWith(argument))
43+
{
44+
return GetIncompatibleTypeMessage(argument);
45+
}
3446

35-
return IsCompatibleWith(argument) ? describable.DescribeFor(argument) : GetIncompatibleTypeMessage(argument);
47+
return _matcher is IDescribeNonMatches describe
48+
? describe.DescribeFor(argument)
49+
: string.Empty;
3650
}
3751

3852
public string FormatArgument(object? argument)
3953
{
4054
var isSatisfiedByArg = IsSatisfiedBy(argument);
41-
var matcherFormatter = _matcher as IArgumentFormatter;
42-
return matcherFormatter == null
43-
? ArgumentFormatter.Default.Format(argument, !isSatisfiedByArg)
44-
: matcherFormatter.Format(argument, isSatisfiedByArg);
55+
56+
return _matcher is IArgumentFormatter matcherFormatter
57+
? matcherFormatter.Format(argument, highlight: !isSatisfiedByArg)
58+
: ArgumentFormatter.Default.Format(argument, highlight: !isSatisfiedByArg);
4559
}
4660

4761
public override string ToString() => _matcher.ToString() ?? string.Empty;
@@ -63,19 +77,18 @@ public void RunAction(object? argument)
6377

6478
private void RunActionIfTypeIsCompatible(object? argument)
6579
{
66-
if (!argument.IsCompatibleWith(ForType)) return;
67-
_action(argument);
80+
if (argument.IsCompatibleWith(ForType))
81+
{
82+
_action(argument);
83+
}
6884
}
6985

70-
private bool IsCompatibleWith(object? argument)
71-
{
72-
return argument.IsCompatibleWith(ForType);
73-
}
86+
private bool IsCompatibleWith(object? argument) => argument.IsCompatibleWith(ForType);
7487

7588
private string GetIncompatibleTypeMessage(object? argument)
7689
{
7790
var argumentType = argument == null ? typeof(object) : argument.GetType();
78-
return string.Format("Expected an argument compatible with type {0}. Actual type was {1}.", ForType, argumentType);
91+
return $"Expected an argument compatible with type '{ForType}'. Actual type was '{argumentType}'.";
7992
}
8093
}
8194
}

src/NSubstitute/Core/Arguments/ArgumentSpecificationCompatibilityTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ArgumentSpecificationCompatibilityTester : IArgumentSpecificationCo
99

1010
public ArgumentSpecificationCompatibilityTester(IDefaultChecker defaultChecker)
1111
{
12-
_defaultChecker = defaultChecker ?? throw new ArgumentNullException(nameof(defaultChecker));
12+
_defaultChecker = defaultChecker;
1313
}
1414

1515
public bool IsSpecificationCompatible(IArgumentSpecification specification, object? argumentValue, Type argumentType)

src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ public bool IsSatisfiedBy(object? argument)
1818
if (argument != null)
1919
{
2020
var argumentArray = ((IEnumerable) argument).Cast<object>().ToArray();
21-
if (argumentArray.Length == _argumentSpecifications.Count())
21+
if (argumentArray.Length == _argumentSpecifications.Length)
2222
{
23-
return
24-
_argumentSpecifications.Select(
25-
(value, index) => value.IsSatisfiedBy(argumentArray[index])).All(x => x);
23+
return _argumentSpecifications
24+
.Select((spec, index) => spec.IsSatisfiedBy(argumentArray[index]))
25+
.All(x => x);
2626
}
2727
}
28+
2829
return false;
2930
}
3031

31-
public override string ToString()
32-
{
33-
return string.Join(", ", _argumentSpecifications.Select(x => x.ToString()));
34-
}
32+
public override string ToString() => string.Join(", ", _argumentSpecifications.Select(x => x.ToString()));
3533

3634
public string Format(object? argument, bool highlight)
3735
{

0 commit comments

Comments
 (0)