Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More use of pattern matching and lifted operators #1937

Merged
merged 1 commit into from May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,7 +16,7 @@ public ParsingState Parse(char symbol, StringBuilder statement)
}
else if (mode is Mode.RemoveSuperfluousWhitespace)
{
if (precedingSymbol.HasValue && !char.IsWhiteSpace(precedingSymbol.Value))
if (precedingSymbol is char value && !char.IsWhiteSpace(value))
{
statement.Append(symbol);
}
Expand Down
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Common/TypeExtensions.cs
Expand Up @@ -238,7 +238,7 @@ private static IEnumerable<PropertyInfo> GetPropertiesFromHierarchy(Type typeToR
return type
.GetProperties(AllInstanceMembersFlag | BindingFlags.DeclaredOnly)
.Where(property => property.GetMethod?.IsPrivate == false)
.Where(property => includeInternals || (property.GetMethod?.IsAssembly == false && property.GetMethod?.IsFamilyOrAssembly == false))
.Where(property => includeInternals || (property.GetMethod is { IsAssembly: false, IsFamilyOrAssembly: false }))
.ToArray();
});
}
Expand Down Expand Up @@ -344,7 +344,7 @@ private static List<TMemberInfo> GetClassMembers<TMemberInfo>(Type typeToReflect
private static bool HasNonPrivateGetter(PropertyInfo propertyInfo)
{
MethodInfo getMethod = propertyInfo.GetGetMethod(nonPublic: true);
return getMethod is not null && !getMethod.IsPrivate && !getMethod.IsFamily;
return getMethod is { IsPrivate: false, IsFamily: false };
}

/// <summary>
Expand Down
Expand Up @@ -15,7 +15,7 @@ public class MultidimensionalArrayFormatter : IValueFormatter
/// </returns>
public bool CanHandle(object value)
{
return value is Array arr && arr.Rank >= 2;
return value is Array { Rank: >= 2 };
}

public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild)
Expand Down
Expand Up @@ -18,7 +18,7 @@ public void Format(object value, FormattedObjectGraph formattedGraph, Formatting

var reducedExpression = ReduceConstantSubExpressions(lambdaExpression.Body);

if (reducedExpression is BinaryExpression binaryExpression && binaryExpression.NodeType == ExpressionType.AndAlso)
if (reducedExpression is BinaryExpression { NodeType: ExpressionType.AndAlso } binaryExpression)
{
var subExpressions = ExtractChainOfExpressionsJoinedWithAndOperator(binaryExpression);
formattedGraph.AddFragment(string.Join(" AndAlso ", subExpressions.Select(e => e.ToString())));
Expand Down
30 changes: 13 additions & 17 deletions Src/FluentAssertions/Numeric/NumericAssertions.cs
Expand Up @@ -58,7 +58,7 @@ private protected NumericAssertions(T? value)
public AndConstraint<TAssertions> Be(T expected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) == 0)
.ForCondition(Subject?.CompareTo(expected) == 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand All @@ -79,9 +79,7 @@ public AndConstraint<TAssertions> Be(T expected, string because = "", params obj
public AndConstraint<TAssertions> Be(T? expected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(
(!Subject.HasValue && !expected.HasValue)
|| (Subject.HasValue && expected.HasValue && Subject.Value.CompareTo(expected.Value) == 0))
.ForCondition(expected is T value ? Subject?.CompareTo(value) == 0 : !Subject.HasValue)
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand All @@ -102,7 +100,7 @@ public AndConstraint<TAssertions> Be(T? expected, string because = "", params ob
public AndConstraint<TAssertions> NotBe(T unexpected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!Subject.HasValue || Subject.Value.CompareTo(unexpected) != 0)
.ForCondition(Subject?.CompareTo(unexpected) != 0)
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be {0}{reason}.", unexpected);

Expand All @@ -123,9 +121,7 @@ public AndConstraint<TAssertions> NotBe(T unexpected, string because = "", param
public AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(
(!Subject.HasValue == unexpected.HasValue)
|| (Subject.HasValue && unexpected.HasValue && Subject.Value.CompareTo(unexpected.Value) != 0))
.ForCondition(unexpected is T value ? Subject?.CompareTo(value) != 0 : Subject.HasValue)
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be {0}{reason}.", unexpected);

Expand All @@ -145,7 +141,7 @@ public AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", para
public AndConstraint<TAssertions> BePositive(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(default(T)) > 0)
.ForCondition(Subject?.CompareTo(default(T)) > 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be positive{reason}, but found {0}.", Subject);

Expand All @@ -165,7 +161,7 @@ public AndConstraint<TAssertions> BePositive(string because = "", params object[
public AndConstraint<TAssertions> BeNegative(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && !IsNaN(Subject.Value) && Subject.Value.CompareTo(default(T)) < 0)
.ForCondition(Subject is T value && !IsNaN(value) && value.CompareTo(default(T)) < 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be negative{reason}, but found {0}.", Subject);

Expand All @@ -191,7 +187,7 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
}

Execute.Assertion
.ForCondition(Subject.HasValue && !IsNaN(Subject.Value) && Subject.Value.CompareTo(expected) < 0)
.ForCondition(Subject is T value && !IsNaN(value) && value.CompareTo(expected) < 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be less than {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand All @@ -218,7 +214,7 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
}

Execute.Assertion
.ForCondition(Subject.HasValue && !IsNaN(Subject.Value) && Subject.Value.CompareTo(expected) <= 0)
.ForCondition(Subject is T value && !IsNaN(value) && value.CompareTo(expected) <= 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be less than or equal to {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand Down Expand Up @@ -248,7 +244,7 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
}

Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) > 0)
.ForCondition(Subject?.CompareTo(expected) > 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be greater than {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand All @@ -275,7 +271,7 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
}

Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) >= 0)
.ForCondition(Subject?.CompareTo(expected) >= 0)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be greater than or equal to {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand Down Expand Up @@ -313,7 +309,7 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
}

Execute.Assertion
.ForCondition(Subject.HasValue && (Subject.Value.CompareTo(minimumValue) >= 0) && (Subject.Value.CompareTo(maximumValue) <= 0))
.ForCondition(Subject is T value && (value.CompareTo(minimumValue) >= 0) && (value.CompareTo(maximumValue) <= 0))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be between {0} and {1}{reason}, but found {2}.",
minimumValue, maximumValue, Subject);
Expand Down Expand Up @@ -349,7 +345,7 @@ public AndConstraint<TAssertions> BeLessThan(T expected, string because = "", pa
}

Execute.Assertion
.ForCondition(Subject.HasValue && !((Subject.Value.CompareTo(minimumValue) >= 0) && (Subject.Value.CompareTo(maximumValue) <= 0)))
.ForCondition(Subject is T value && !((value.CompareTo(minimumValue) >= 0) && (value.CompareTo(maximumValue) <= 0)))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to not be between {0} and {1}{reason}, but found {2}.",
minimumValue, maximumValue, Subject);
Expand Down Expand Up @@ -385,7 +381,7 @@ public AndConstraint<TAssertions> BeOneOf(params T[] validValues)
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && validValues.Contains((T)Subject))
.ForCondition(Subject is T value && validValues.Contains(value))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be one of {0}{reason}, but found {1}.", validValues, Subject);

Expand Down
8 changes: 4 additions & 4 deletions Src/FluentAssertions/Primitives/DateOnlyAssertions.cs
Expand Up @@ -142,7 +142,7 @@ public AndConstraint<TAssertions> Be(DateOnly? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) < 0)
.ForCondition(Subject < expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:date} to be before {0}{reason}, but found {1}.", expected,
Subject);
Expand Down Expand Up @@ -182,7 +182,7 @@ public AndConstraint<TAssertions> Be(DateOnly? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) <= 0)
.ForCondition(Subject <= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:date} to be on or before {0}{reason}, but found {1}.", expected,
Subject);
Expand Down Expand Up @@ -222,7 +222,7 @@ public AndConstraint<TAssertions> Be(DateOnly? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) > 0)
.ForCondition(Subject > expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:date} to be after {0}{reason}, but found {1}.", expected,
Subject);
Expand Down Expand Up @@ -262,7 +262,7 @@ public AndConstraint<TAssertions> Be(DateOnly? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) >= 0)
.ForCondition(Subject >= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:date} to be on or after {0}{reason}, but found {1}.", expected,
Subject);
Expand Down
8 changes: 4 additions & 4 deletions Src/FluentAssertions/Primitives/DateTimeAssertions.cs
Expand Up @@ -239,7 +239,7 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) < 0)
.ForCondition(Subject < expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be before {0}{reason}, but found {1}.", expected,
Subject);
Expand Down Expand Up @@ -279,7 +279,7 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) <= 0)
.ForCondition(Subject <= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be on or before {0}{reason}, but found {1}.", expected,
Subject);
Expand Down Expand Up @@ -319,7 +319,7 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) > 0)
.ForCondition(Subject > expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be after {0}{reason}, but found {1}.", expected,
Subject);
Expand Down Expand Up @@ -359,7 +359,7 @@ public AndConstraint<TAssertions> Be(DateTime? expected, string because = "", pa
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) >= 0)
.ForCondition(Subject >= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be on or after {0}{reason}, but found {1}.", expected,
Subject);
Expand Down
8 changes: 4 additions & 4 deletions Src/FluentAssertions/Primitives/DateTimeOffsetAssertions.cs
Expand Up @@ -375,7 +375,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) < 0)
.ForCondition(Subject < expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be before {0}{reason}, but it was {1}.", expected,
Subject);
Expand Down Expand Up @@ -415,7 +415,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) <= 0)
.ForCondition(Subject <= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be on or before {0}{reason}, but it was {1}.", expected,
Subject);
Expand Down Expand Up @@ -455,7 +455,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) > 0)
.ForCondition(Subject > expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be after {0}{reason}, but it was {1}.", expected,
Subject);
Expand Down Expand Up @@ -495,7 +495,7 @@ public DateTimeOffsetAssertions(DateTimeOffset? value)
params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && Subject.Value.CompareTo(expected) >= 0)
.ForCondition(Subject >= expected)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the date and time} to be on or after {0}{reason}, but it was {1}.", expected,
Subject);
Expand Down
12 changes: 6 additions & 6 deletions Src/FluentAssertions/Primitives/EnumAssertions.cs
Expand Up @@ -195,7 +195,7 @@ public AndConstraint<TAssertions> NotBeDefined(string because = "", params objec
public AndConstraint<TAssertions> HaveValue(decimal expected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && (GetValue(Subject.Value) == expected))
.ForCondition(Subject is TEnum value && (GetValue(value) == expected))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the enum} to have value {0}{reason}, but found {1}.",
expected, Subject);
Expand All @@ -217,7 +217,7 @@ public AndConstraint<TAssertions> HaveValue(decimal expected, string because = "
public AndConstraint<TAssertions> NotHaveValue(decimal unexpected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!(Subject.HasValue && (GetValue(Subject.Value) == unexpected)))
.ForCondition(!(Subject is TEnum value && (GetValue(value) == unexpected)))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the enum} to not have value {0}{reason}, but found {1}.",
unexpected, Subject);
Expand All @@ -240,7 +240,7 @@ public AndConstraint<TAssertions> HaveSameValueAs<T>(T expected, string because
where T : struct, Enum
{
Execute.Assertion
.ForCondition(Subject.HasValue && (GetValue(Subject.Value) == GetValue(expected)))
.ForCondition(Subject is TEnum value && (GetValue(value) == GetValue(expected)))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the enum} to have same value as {0}{reason}, but found {1}.",
expected, Subject);
Expand All @@ -263,7 +263,7 @@ public AndConstraint<TAssertions> NotHaveSameValueAs<T>(T unexpected, string bec
where T : struct, Enum
{
Execute.Assertion
.ForCondition(!(Subject.HasValue && (GetValue(Subject.Value) == GetValue(unexpected))))
.ForCondition(!(Subject is TEnum value && (GetValue(value) == GetValue(unexpected))))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the enum} to not have same value as {0}{reason}, but found {1}.",
unexpected, Subject);
Expand All @@ -286,7 +286,7 @@ public AndConstraint<TAssertions> HaveSameNameAs<T>(T expected, string because =
where T : struct, Enum
{
Execute.Assertion
.ForCondition(Subject.HasValue && (GetName(Subject.Value) == GetName(expected)))
.ForCondition(Subject is TEnum value && (GetName(value) == GetName(expected)))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the enum} to have same name as {0}{reason}, but found {1}.",
expected, Subject);
Expand All @@ -309,7 +309,7 @@ public AndConstraint<TAssertions> NotHaveSameNameAs<T>(T unexpected, string beca
where T : struct, Enum
{
Execute.Assertion
.ForCondition(!(Subject.HasValue && (GetName(Subject.Value) == GetName(unexpected))))
.ForCondition(!(Subject is TEnum value && (GetName(value) == GetName(unexpected))))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:the enum} to not have same name as {0}{reason}, but found {1}.",
unexpected, Subject);
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/GuidAssertions.cs
Expand Up @@ -70,7 +70,7 @@ public AndConstraint<TAssertions> BeEmpty(string because = "", params object[] b
public AndConstraint<TAssertions> NotBeEmpty(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(Subject.HasValue && (Subject.Value != Guid.Empty))
.ForCondition(Subject is Guid value && value != Guid.Empty)
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:Guid} to be empty{reason}.");

Expand Down