Skip to content

Commit

Permalink
More use of pattern matching and lifted operators
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed May 21, 2022
1 parent 17c52d0 commit 575a1a5
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
28 changes: 14 additions & 14 deletions Src/FluentAssertions/Numeric/NumericAssertions.cs
Original file line number Diff line number Diff line change
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 @@ -81,7 +81,7 @@ public AndConstraint<TAssertions> Be(T? expected, string because = "", params ob
Execute.Assertion
.ForCondition(
(!Subject.HasValue && !expected.HasValue)
|| (Subject.HasValue && expected.HasValue && Subject.Value.CompareTo(expected.Value) == 0))
|| (expected is T value && Subject?.CompareTo(value) == 0))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:value} to be {0}{reason}, but found {1}" + GenerateDifferenceMessage(expected), expected, Subject);

Expand All @@ -102,7 +102,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 @@ -124,8 +124,8 @@ public AndConstraint<TAssertions> NotBe(T? unexpected, string because = "", para
{
Execute.Assertion
.ForCondition(
(!Subject.HasValue == unexpected.HasValue)
|| (Subject.HasValue && unexpected.HasValue && Subject.Value.CompareTo(unexpected.Value) != 0))
(Subject.HasValue != unexpected.HasValue)
|| (unexpected is T value && Subject?.CompareTo(value) != 0))
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:value} to be {0}{reason}.", unexpected);

Expand All @@ -145,7 +145,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 +165,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 +191,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 +218,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 +248,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 +275,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 +313,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 +349,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 +385,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Primitives/NullableBooleanAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public AndConstraint<TAssertions> NotBe(bool? unexpected, string because = "", p
public AndConstraint<TAssertions> NotBeFalse(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!Subject.HasValue || Subject.Value)
.ForCondition(Subject is not false)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:nullable boolean} not to be {0}{reason}, but found {1}.", false, Subject);

Expand All @@ -172,7 +172,7 @@ public AndConstraint<TAssertions> NotBeFalse(string because = "", params object[
public AndConstraint<TAssertions> NotBeTrue(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(!Subject.HasValue || !Subject.Value)
.ForCondition(Subject is not true)
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:nullable boolean} not to be {0}{reason}, but found {1}.", true, Subject);

Expand Down
Loading

0 comments on commit 575a1a5

Please sign in to comment.