Skip to content

Fix AOT / Trimming Warnings #2411

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

Merged
merged 2 commits into from
May 19, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Library.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<TrimmerSingleWarn>false</TrimmerSingleWarn>

<IsAotCompatible
Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>

Expand Down
2 changes: 1 addition & 1 deletion TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public bool ContainsKey(string key)
return true;
}

public bool TryGetValue(string key, [MaybeNullWhen(false)] out string value)
public bool TryGetValue(string key, out string value)
{
value = "Value";
return true;
Expand Down
6 changes: 2 additions & 4 deletions TUnit.Assertions/AssertConditions/BaseAssertCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ public AssertionResult FailWithMessage(string message)
/// </summary>
public virtual TimeSpan? WaitFor { get; protected set; }

protected abstract string GetExpectation();

internal string Expectation => GetExpectation();

internal protected abstract string GetExpectation();

internal virtual string GetExpectationWithReason()
=> $"{GetExpectation()}{GetBecauseReason()}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AndAssertCondition(BaseAssertCondition condition1, BaseAssertCondition co

// This method is not used, as the GetExpectationWithReason is overwritten
// and uses the expectation from the two conditions.
protected override string GetExpectation() => "";
internal protected override string GetExpectation() => "";

internal override string GetExpectationWithReason()
=> $"{_condition1.GetExpectationWithReason()}{Environment.NewLine} and {_condition2.GetExpectationWithReason()}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public OrAssertCondition(BaseAssertCondition condition1, BaseAssertCondition con

// This method is not used, as the GetExpectationWithReason is overwritten
// and uses the expectation from the two conditions.
protected override string GetExpectation() => "";
internal protected override string GetExpectation() => "";

internal override string GetExpectationWithReason()
=> $"{_condition1.GetExpectationWithReason()}{Environment.NewLine} or {_condition2.GetExpectationWithReason()}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ AssertionMetadata assertionMetadata

protected virtual string GetFailureMessage(TException? exception) => "";

protected override string GetExpectation()
internal protected override string GetExpectation()
{
return GetFailureMessage(Exception as TException);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public EnumerableSatisfiesAssertCondition(Func<TInner?, Task<TExpected>?> mapper
SetSubject(mapperExpression);
}

protected override string GetExpectation()
internal protected override string GetExpectation()
=> $"to satisfy {_assertionBuilderExpression}";

protected override async ValueTask<AssertionResult> GetResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ExceptionMessageContainingExpectedValueAssertCondition<TException>(
: ExpectedValueAssertCondition<TException, string>(expected)
where TException : Exception
{
protected override string GetExpectation()
=> $"message to contain {Formatter.Format(expected).TruncateWithEllipsis(100)}";
internal protected override string GetExpectation()
=> $"message to contain {Formatter.Format(ExpectedValue).TruncateWithEllipsis(100)}";

protected override ValueTask<AssertionResult> GetResult(TException? actualValue, string? expectedValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ExceptionMessageEndingWithExpectedValueAssertCondition<TException>(
: ExpectedValueAssertCondition<TException, string>(expected)
where TException : Exception
{
protected override string GetExpectation()
=> $"message to end with {Formatter.Format(expected).TruncateWithEllipsis(100)}";
internal protected override string GetExpectation()
=> $"message to end with {Formatter.Format(ExpectedValue).TruncateWithEllipsis(100)}";

protected override ValueTask<AssertionResult> GetResult(TException? actualValue, string? expectedValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ExceptionMessageEqualsExpectedValueAssertCondition<TException>(stri
: ExpectedValueAssertCondition<TException, string>(expected)
where TException : Exception
{
protected override string GetExpectation()
internal protected override string GetExpectation()
=> $"message to be equal to {Formatter.Format(expected).TruncateWithEllipsis(100)}";

protected override ValueTask<AssertionResult> GetResult(TException? actualValue, string? expectedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ExceptionMessageMatchingExpectedAssertCondition<TException>(StringM
: ExpectedValueAssertCondition<TException, StringMatcher>(match)
where TException : Exception
{
protected override string GetExpectation()
=> $"message to match {Formatter.Format(match).TruncateWithEllipsis(100)}";
internal protected override string GetExpectation()
=> $"message to match {Formatter.Format(ExpectedValue).TruncateWithEllipsis(100)}";

protected override ValueTask<AssertionResult> GetResult(TException? actualValue, StringMatcher? expectedValue)
{
Expand All @@ -22,7 +22,7 @@ protected override ValueTask<AssertionResult> GetResult(TException? actualValue,
return AssertionResult
.FailIf(expectedValue is null,
"expected value was null")
.OrFailIf(!match.Matches(actualValue.Message),
.OrFailIf(!ExpectedValue!.Matches(actualValue.Message),
$"found message {Formatter.Format(actualValue.Message).TruncateWithEllipsis(100)}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class ExceptionMessageStartingWithExpectedValueAssertCondition<TException
: ExpectedValueAssertCondition<TException, string>(expected)
where TException : Exception
{
protected override string GetExpectation()
=> $"message to start with {Formatter.Format(expected).TruncateWithEllipsis(100)}";
internal protected override string GetExpectation()
=> $"message to start with {Formatter.Format(ExpectedValue).TruncateWithEllipsis(100)}";

protected override ValueTask<AssertionResult> GetResult(TException? actualValue, string? expectedValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ string expectation
)
: ExpectedValueAssertCondition<TActual, TExpected>(expected)
{
protected override string GetExpectation() => expectation;
internal protected override string GetExpectation() => expectation;

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, TExpected? expectedValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace TUnit.Assertions.AssertConditions;

public class NotNullExpectedValueAssertCondition<TActual> : ConvertToAssertCondition<TActual?, TActual> where TActual : class?
{
protected override string GetExpectation()
internal protected override string GetExpectation()
=> "to not be null";

public override ValueTask<(AssertionResult, TActual?)> ConvertValue(TActual? value)
Expand All @@ -19,7 +19,7 @@ protected override string GetExpectation()

public class NotNullStructExpectedValueAssertCondition<TActual> : ConvertToAssertCondition<TActual?, TActual> where TActual : struct
{
protected override string GetExpectation()
internal protected override string GetExpectation()
=> "to not be null";

public override ValueTask<(AssertionResult, TActual)> ConvertValue(TActual? value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace TUnit.Assertions.AssertConditions;

public class NullExpectedValueAssertCondition<TActual> : BaseAssertCondition<TActual>
{
protected override string GetExpectation()
internal protected override string GetExpectation()
=> "to be null";

protected override ValueTask<AssertionResult> GetResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SatisfiesAssertCondition(Func<TActual, Task<TExpected>?> mapper,
SetSubject(mapperExpression);
}

protected override string GetExpectation()
internal protected override string GetExpectation()
=> $"to satisfy {_assertionBuilderExpression}";

protected override async ValueTask<AssertionResult> GetResult(
Expand Down
2 changes: 1 addition & 1 deletion TUnit.Assertions/AssertConditions/ValueAssertCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ AssertionMetadata assertionMetadata

protected abstract string GetFailureMessage(TActual? actualValue);

protected override string GetExpectation()
internal protected override string GetExpectation()
{
return GetFailureMessage(ActualValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace TUnit.Assertions.AssertionBuilders;
public class InvokableAssertionBuilder<TActual> :
AssertionBuilder, IInvokableAssertionBuilder
{
private readonly ISource _source;
protected readonly ISource Source;

internal InvokableAssertionBuilder(ISource source) : base(source.AssertionDataTask, source.ActualExpression!,
source.ExpressionBuilder, source.Assertions)
{
_source = source;
Source = source;

if (source is InvokableAssertionBuilder<TActual> invokableAssertionBuilder)
{
Expand All @@ -36,7 +36,7 @@ public async Task<IEnumerable<AssertionResult>> GetAssertionResults()

string IInvokableAssertionBuilder.GetExpression()
{
var expression = _source.ExpressionBuilder.ToString();
var expression = Source.ExpressionBuilder.ToString();

if (expression.Length < 100)
{
Expand All @@ -46,5 +46,5 @@ string IInvokableAssertionBuilder.GetExpression()
return $"{expression[..100]}...";
}

internal protected Stack<BaseAssertCondition> Assertions => _source.Assertions;
internal protected Stack<BaseAssertCondition> Assertions => Source.Assertions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class InvokableValueAssertionBuilder<TActual>(ISource source) : Invokable
public InvokableValueAssertionBuilder<TActual> Because(string reason)
{
var becauseReason = new BecauseReason(reason);
var assertion = source.Assertions.Peek();
var assertion = Source.Assertions.Peek();
assertion.SetBecauseReason(becauseReason);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class DateOnlyEqualsExpectedValueAssertCondition(DateOnly expected) : Exp
{
private int? _tolerance;

protected override string GetExpectation()
internal protected override string GetExpectation()
{
if (_tolerance == null || _tolerance == default)
if (_tolerance is null or 0)
{
return $"to be equal to {expected}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class DateTimeEqualsExpectedValueAssertCondition(DateTime expected) : Exp
{
private TimeSpan? _tolerance;

protected override string GetExpectation()
internal protected override string GetExpectation()
{
if (_tolerance == null || _tolerance == default)
if (_tolerance == null || _tolerance == TimeSpan.Zero)
{
return $"to be equal to {expected}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class DateTimeOffsetEqualsExpectedValueAssertCondition(DateTimeOffset exp
{
private TimeSpan? _tolerance;

protected override string GetExpectation()
internal protected override string GetExpectation()
{
if (_tolerance == null || _tolerance == default)
if (_tolerance == null || _tolerance == TimeSpan.Zero)
{
return $"to be equal to {expected}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class TimeOnlyEqualsExpectedValueAssertCondition(TimeOnly expected) : Exp
{
private TimeSpan? _tolerance;

protected override string GetExpectation()
internal protected override string GetExpectation()
{
if (_tolerance == null || _tolerance == default)
if (_tolerance == null || _tolerance == TimeSpan.Zero)
{
return $"to be equal to {expected}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public class TimeSpanEqualsExpectedValueAssertCondition(TimeSpan expected) : Exp
{
private TimeSpan? _tolerance;

protected override string GetExpectation()
internal protected override string GetExpectation()
{
if (_tolerance == null || _tolerance == default)
if (_tolerance == null || _tolerance == TimeSpan.Zero)
{
return $"to be equal to {expected}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace TUnit.Assertions.AssertConditions.ClassMember;
public class PropertyEqualsExpectedValueAssertCondition<TRootObjectType, TPropertyType>(Expression<Func<TRootObjectType, TPropertyType>> propertySelector, TPropertyType expected, bool isEqual)
: ExpectedValueAssertCondition<TRootObjectType, TPropertyType>(expected)
{
protected override string GetExpectation()
internal protected override string GetExpectation()
{
return $"{typeof(TRootObjectType).Name}.{ExpressionHelpers.GetName(propertySelector)} to be equal to {expected}";
return $"{typeof(TRootObjectType).Name}.{ExpressionHelpers.GetName(propertySelector)} to be equal to {ExpectedValue}";
}

protected override ValueTask<AssertionResult> GetResult(TRootObjectType? actualValue, TPropertyType? expectedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class EnumerableAllExpectedFuncAssertCondition<TActual, TInner>(
: BaseAssertCondition<TActual>
where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => $"to contain only entries matching {matcherString ?? "null"}";
internal protected override string GetExpectation() => $"to contain only entries matching {matcherString ?? "null"}";

protected override ValueTask<AssertionResult> GetResult(
TActual? actualValue, Exception? exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EnumerableContainsExpectedFuncAssertCondition<TActual, TInner>(
where TActual : IEnumerable<TInner>
{
private bool _wasFound;
protected override string GetExpectation() => $"to contain an entry matching {matcherString ?? "null"}";
internal protected override string GetExpectation() => $"to contain an entry matching {matcherString ?? "null"}";

protected override ValueTask<AssertionResult> GetResult(
TActual? actualValue, Exception? exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EnumerableContainsExpectedValueAssertCondition<TActual, TInner>(
: ExpectedValueAssertCondition<TActual, TInner>(expected)
where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => $"to contain {expected}";
internal protected override string GetExpectation() => $"to contain {ExpectedValue}";

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, TInner? inner)
=> AssertionResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace TUnit.Assertions.AssertConditions.Collections;
public class EnumerableCountEqualToExpectedValueAssertCondition<TActual, TInner>(int expected)
: ExpectedValueAssertCondition<TActual, int>(expected) where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => $"to have a count of {expected}";
internal protected override string GetExpectation() => $"to have a count of {ExpectedValue}";

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, int count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace TUnit.Assertions.AssertConditions.Collections;
public class EnumerableCountNotEqualToExpectedValueAssertCondition<TActual, TInner>(int expected)
: ExpectedValueAssertCondition<TActual, int>(expected) where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => $"to have a count different to {expected}";
internal protected override string GetExpectation() => $"to have a count different to {ExpectedValue}";

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, int count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace TUnit.Assertions.AssertConditions.Collections;
public class EnumerableDistinctItemsExpectedValueAssertCondition<TActual, TInner>(IEqualityComparer<TInner?>? equalityComparer)
: BaseAssertCondition<TActual> where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => "items to be distinct";
internal protected override string GetExpectation() => "items to be distinct";

protected override ValueTask<AssertionResult> GetResult(
TActual? actualValue, Exception? exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public class EnumerableEquivalentToExpectedValueAssertCondition<TActual, TInner>
: ExpectedValueAssertCondition<TActual, IEnumerable<TInner>>(expected)
where TActual : IEnumerable<TInner>?
{
protected override string GetExpectation()
internal protected override string GetExpectation()
{
if (!typeof(TInner).IsSimpleType()
&& equalityComparer is EquivalentToEqualityComparer<TInner> { ComparisonFailures.Length: > 0 })
{
return "to match";
}

return $"to be equivalent to {(expected != null ? Formatter.Format(expected) : null)}";
return $"to be equivalent to {(ExpectedValue != null ? Formatter.Format(ExpectedValue) : null)}";
}

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, IEnumerable<TInner>? expectedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class EnumerableNotContainsExpectedFuncAssertCondition<TActual, TInner>(
: BaseAssertCondition<TActual>
where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => $"to contain no entry matching {matcherString ?? "null"}";
internal protected override string GetExpectation() => $"to contain no entry matching {matcherString ?? "null"}";

protected override ValueTask<AssertionResult> GetResult(
TActual? actualValue, Exception? exception,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EnumerableNotContainsExpectedValueAssertCondition<TActual, TInner>(
: ExpectedValueAssertCondition<TActual, TInner>(expected)
where TActual : IEnumerable<TInner>
{
protected override string GetExpectation() => $"to not contain {expected}";
internal protected override string GetExpectation() => $"to not contain {ExpectedValue}";

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, TInner? inner)
=> AssertionResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class EnumerableNotEquivalentToExpectedValueAssertCondition<TActual, TInn
: ExpectedValueAssertCondition<TActual, IEnumerable<TInner>>(expected)
where TActual : IEnumerable<TInner>?
{
protected override string GetExpectation() => $"to not be equivalent to {(expected != null ? Formatter.Format(expected) : null)}";
internal protected override string GetExpectation() => $"to not be equivalent to {(ExpectedValue != null ? Formatter.Format(ExpectedValue) : null)}";

protected override ValueTask<AssertionResult> GetResult(TActual? actualValue, IEnumerable<TInner>? expectedValue)
{
Expand Down
Loading
Loading