Skip to content

Commit

Permalink
Format source code and add format check on ci (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
Romfos authored Dec 24, 2023
1 parent 3ce2d66 commit 02f67a8
Show file tree
Hide file tree
Showing 79 changed files with 418 additions and 367 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ trim_trailing_whitespace = true
indent_size = 4
indent_style = space

[*.{sln,csproj,fsproj,yml}]
[*.{sln,csproj,fsproj,yml,props}]
indent_size = 2
indent_style = space

Expand Down
34 changes: 24 additions & 10 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Build and Test

name: Build, Test, and Format
on: [push, pull_request]

jobs:
test:
strategy:
Expand All @@ -10,42 +10,42 @@ jobs:
include:
- os: windows-latest
framework: net462

runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
- name: Build
run: dotnet build

- name: Test
run: dotnet test -f ${{ matrix.framework }} --no-build --no-restore

test-documentation:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: |
6.0.x
7.0.x
8.0.x
# used for documentation
- name: Setup Ruby
uses: ruby/setup-ruby@v1
Expand All @@ -54,4 +54,18 @@ jobs:
bundler-cache: true

- name: Build all targets
run: build\build.cmd --target All
run: build\build.cmd --target All

format-verify:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x

- name: Format
run: dotnet format --verify-no-changes
4 changes: 2 additions & 2 deletions src/NSubstitute/Arg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static ref TDelegate InvokeDelegate<TDelegate>(params object[] arguments)
/// </summary>
public static ref T Do<T>(Action<T> useArgument)
{
return ref ArgumentMatcher.Enqueue<T>(new AnyArgumentMatcher(typeof(T)), x => useArgument((T) x!));
return ref ArgumentMatcher.Enqueue<T>(new AnyArgumentMatcher(typeof(T)), x => useArgument((T)x!));
}

/// <summary>
Expand Down Expand Up @@ -225,7 +225,7 @@ public static class Compat

private static Action<object> InvokeDelegateAction(params object[] arguments)
{
return x => ((Delegate) x).DynamicInvoke(arguments);
return x => ((Delegate)x).DynamicInvoke(arguments);
}
}
}
40 changes: 20 additions & 20 deletions src/NSubstitute/ClearOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

namespace NSubstitute
{
[Flags]
public enum ClearOptions
{
/// <summary>
/// Clear all the received calls
/// </summary>
ReceivedCalls = 1,
[Flags]
public enum ClearOptions
{
/// <summary>
/// Clear all the received calls
/// </summary>
ReceivedCalls = 1,

/// <summary>
/// Clear all configured return results (including auto-substituted values).
/// </summary>
ReturnValues = 2,
/// <summary>
/// Clear all configured return results (including auto-substituted values).
/// </summary>
ReturnValues = 2,

/// <summary>
/// Clear all call actions configured for this substitute (via When..Do, Arg.Invoke, and Arg.Do)
/// </summary>
CallActions = 4,
/// <summary>
/// Clear all call actions configured for this substitute (via When..Do, Arg.Invoke, and Arg.Do)
/// </summary>
CallActions = 4,

/// <summary>
/// Clears all received calls and configured return values and callbacks.
/// </summary>
All = ReceivedCalls | ReturnValues | CallActions
}
/// <summary>
/// Clears all received calls and configured return values and callbacks.
/// </summary>
All = ReceivedCalls | ReturnValues | CallActions
}
}
6 changes: 3 additions & 3 deletions src/NSubstitute/Core/Arguments/ArgumentFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ private string Format(object? arg)
{
return arg switch
{
null => "<null>",
string str => $"\"{str}\"",
null => "<null>",
string str => $"\"{str}\"",
{ } obj when HasDefaultToString(obj) => arg.GetType().GetNonMangledTypeName(),
_ => arg.ToString() ?? string.Empty
_ => arg.ToString() ?? string.Empty
};

static bool HasDefaultToString(object obj)
Expand Down
8 changes: 4 additions & 4 deletions src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof (ArgumentMatchInfo)) return false;
return Equals((ArgumentMatchInfo) obj);
if (obj.GetType() != typeof(ArgumentMatchInfo)) return false;
return Equals((ArgumentMatchInfo)obj);
}

public override int GetHashCode()
{
unchecked
{
int result = Index;
result = (result*397) ^ (_argument != null ? _argument.GetHashCode() : 0);
result = (result*397) ^ _specification.GetHashCode();
result = (result * 397) ^ (_argument != null ? _argument.GetHashCode() : 0);
result = (result * 397) ^ _specification.GetHashCode();
return result;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/NSubstitute/Core/Arguments/ArgumentMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ArgumentMatcher
IArgumentMatcher nonGenericMatcher = argumentMatcher switch
{
IDescribeNonMatches => new GenericToNonGenericMatcherProxyWithDescribe<T>(argumentMatcher),
_ => new GenericToNonGenericMatcherProxy<T>(argumentMatcher)
_ => new GenericToNonGenericMatcherProxy<T>(argumentMatcher)
};

return ref EnqueueArgSpecification<T>(new ArgumentSpecification(typeof(T), nonGenericMatcher));
Expand All @@ -44,7 +44,7 @@ public GenericToNonGenericMatcherProxy(IArgumentMatcher<T> matcher)
_matcher = matcher;
}

public bool IsSatisfiedBy(object? argument) => _matcher.IsSatisfiedBy((T?) argument!);
public bool IsSatisfiedBy(object? argument) => _matcher.IsSatisfiedBy((T?)argument!);
}

private class GenericToNonGenericMatcherProxyWithDescribe<T> : GenericToNonGenericMatcherProxy<T>, IDescribeNonMatches
Expand All @@ -54,7 +54,7 @@ public GenericToNonGenericMatcherProxyWithDescribe(IArgumentMatcher<T> matcher)
if (matcher is not IDescribeNonMatches) throw new SubstituteInternalException("Should implement IDescribeNonMatches type.");
}

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

private class DefaultValueContainer<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public bool IsSatisfiedBy(object? argument)
{
if (argument != null)
{
var argumentArray = ((IEnumerable) argument).Cast<object>().ToArray();
var argumentArray = ((IEnumerable)argument).Cast<object>().ToArray();
if (argumentArray.Length == _argumentSpecifications.Length)
{
return _argumentSpecifications
Expand All @@ -41,9 +41,10 @@ private IEnumerable<string> Format(object[] args, IArgumentSpecification[] specs
{
if (specs.Any() && !args.Any())
{
return new [] { "**" };
return new[] { "**" };
}
return args.Select((arg, index) => {
return args.Select((arg, index) =>
{
var hasSpecForThisArg = index < specs.Length;
return hasSpecForThisArg ? specs[index].FormatArgument(arg) : ArgumentFormatter.Default.Format(arg, true);
});
Expand Down
2 changes: 1 addition & 1 deletion src/NSubstitute/Core/CallFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CallFactory : ICallFactory
{
public ICall Create(MethodInfo methodInfo, object?[] arguments, object target, IList<IArgumentSpecification> argumentSpecifications, Func<object>? baseMethod)
{
return new Call(methodInfo, arguments, target, argumentSpecifications , baseMethod);
return new Call(methodInfo, arguments, target, argumentSpecifications, baseMethod);
}

public ICall Create(MethodInfo methodInfo, object?[] arguments, object target, IList<IArgumentSpecification> argumentSpecifications)
Expand Down
2 changes: 1 addition & 1 deletion src/NSubstitute/Core/CallInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public T ArgAt<T>(int position)

try
{
return (T) _callArguments[position].Value!;
return (T)_callArguments[position].Value!;
}
catch (InvalidCastException)
{
Expand Down
6 changes: 3 additions & 3 deletions src/NSubstitute/Core/CallRouterResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public ICallRouter ResolveFor(object substitute)
{
return substitute switch
{
null => throw new NullSubstituteReferenceException(),
ICallRouterProvider provider => provider.GetCallRouter(),
null => throw new NullSubstituteReferenceException(),
ICallRouterProvider provider => provider.GetCallRouter(),
Delegate { Target: ICallRouterProvider provider } => provider.GetCallRouter(),
_ => throw new NotASubstituteException()
_ => throw new NotASubstituteException()
};
}
}
Expand Down
54 changes: 27 additions & 27 deletions src/NSubstitute/Core/CallSpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public bool IsSatisfiedBy(ICall call)
}

private static bool AreComparable(MethodInfo a, MethodInfo b)
{
{
if (a == b)
{
return true;
Expand All @@ -53,29 +53,29 @@ private static bool AreComparable(MethodInfo a, MethodInfo b)
return CanCompareGenericMethods(a, b);
}

return false;
}
return false;
}

private static bool CanCompareGenericMethods(MethodInfo a, MethodInfo b)
{
return
AreEquivalentDefinitions(a, b)
&& TypesAreAllEquivalent(ParameterTypes(a), ParameterTypes(b))
&& TypesAreAllEquivalent(a.GetGenericArguments(), b.GetGenericArguments());
return
AreEquivalentDefinitions(a, b)
&& TypesAreAllEquivalent(ParameterTypes(a), ParameterTypes(b))
&& TypesAreAllEquivalent(a.GetGenericArguments(), b.GetGenericArguments());
}

private static Type[] ParameterTypes(MethodInfo info)
{
return info.GetParameters().Select(p=>p.ParameterType).ToArray();
}

internal static bool TypesAreAllEquivalent(Type[] aArgs, Type[] bArgs)
{
if (aArgs.Length != bArgs.Length) return false;
for (var i = 0; i < aArgs.Length; i++)
{
var first = aArgs[i];
var second = bArgs[i];
{
return info.GetParameters().Select(p => p.ParameterType).ToArray();
}

internal static bool TypesAreAllEquivalent(Type[] aArgs, Type[] bArgs)
{
if (aArgs.Length != bArgs.Length) return false;
for (var i = 0; i < aArgs.Length; i++)
{
var first = aArgs[i];
var second = bArgs[i];

if (first.IsGenericType && second.IsGenericType
&& first.GetGenericTypeDefinition() == second.GetGenericTypeDefinition())
Expand All @@ -90,17 +90,17 @@ internal static bool TypesAreAllEquivalent(Type[] aArgs, Type[] bArgs)

var areEquivalent = first.IsAssignableFrom(second) || second.IsAssignableFrom(first) ||
typeof(Arg.AnyType).IsAssignableFrom(first) || typeof(Arg.AnyType).IsAssignableFrom(second);
if (!areEquivalent) return false;
}
return true;
}

private static bool AreEquivalentDefinitions(MethodInfo a, MethodInfo b)
{
return a.IsGenericMethod == b.IsGenericMethod
if (!areEquivalent) return false;
}
return true;
}

private static bool AreEquivalentDefinitions(MethodInfo a, MethodInfo b)
{
return a.IsGenericMethod == b.IsGenericMethod
&& a.ReturnType == b.ReturnType
&& a.Name.Equals(b.Name, StringComparison.Ordinal);
}
}

private bool IsMatchingArgumentSpecifications(ICall call)
{
Expand Down
8 changes: 4 additions & 4 deletions src/NSubstitute/Core/DependencyInjection/NSubContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ object Factory(Scope scope)
}

public IConfigurableNSubContainer Register<TKey>(Func<INSubResolver, TKey> factory, NSubLifetime lifetime)
where TKey: notnull
where TKey : notnull
{
object Factory(Scope scope)
{
Expand All @@ -85,14 +85,14 @@ public IConfigurableNSubContainer Decorate<TKey>(Func<TKey, INSubResolver, TKey>
Registration? existingRegistration = TryFindRegistration(typeof(TKey));
if (existingRegistration == null)
{
throw new ArgumentException("Cannot decorate type " + typeof(TKey).FullName +" as implementation is not registered.");
throw new ArgumentException("Cannot decorate type " + typeof(TKey).FullName + " as implementation is not registered.");
}

object Factory(Scope scope)
{
// Freeze original registration discovered during decoration.
// This way we avoid recursion and support nested decorators.
var originalInstance = (TKey) scope.Resolve(existingRegistration);
var originalInstance = (TKey)scope.Resolve(existingRegistration);
return factory.Invoke(originalInstance, scope);
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public Scope(NSubContainer mostNestedContainer)
_mostNestedContainer = mostNestedContainer;
}

public T Resolve<T>() where T : notnull => (T) Resolve(typeof(T));
public T Resolve<T>() where T : notnull => (T)Resolve(typeof(T));

public bool TryGetCached(Registration registration, [MaybeNullWhen(false)] out object result)
{
Expand Down
Loading

0 comments on commit 02f67a8

Please sign in to comment.