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

Use OverloadResolutionPriority with Debug/Trace.Assert(string) #104942

Merged
merged 2 commits into from
Jul 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,10 @@ internal Trace() { }
public static System.Diagnostics.TraceListenerCollection Listeners { get { throw null; } }
public static bool UseGlobalLock { get { throw null; } set { } }
[System.Diagnostics.ConditionalAttribute("TRACE")]
[System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute(-1)]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition) { }
[System.Diagnostics.ConditionalAttribute("TRACE")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, string? message) { }
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("condition")] string? message = null) { }
[System.Diagnostics.ConditionalAttribute("TRACE")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, string? message, string? detailMessage) { }
[System.Diagnostics.ConditionalAttribute("TRACE")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#define TRACE
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;

namespace System.Diagnostics
Expand Down Expand Up @@ -114,6 +115,7 @@ public static void Close()
/// is <see langword='false'/>.</para>
/// </devdoc>
[Conditional("TRACE")]
[OverloadResolutionPriority(-1)] // lower priority than (bool, string) overload so that the compiler prefers using CallerArgumentExpression
public static void Assert([DoesNotReturnIf(false)] bool condition)
{
TraceInternal.Assert(condition);
Expand All @@ -124,7 +126,7 @@ public static void Assert([DoesNotReturnIf(false)] bool condition)
/// <see langword='false'/>. </para>
/// </devdoc>
[Conditional("TRACE")]
public static void Assert([DoesNotReturnIf(false)] bool condition, string? message)
public static void Assert([DoesNotReturnIf(false)] bool condition, [CallerArgumentExpression(nameof(condition))] string? message = null)
{
TraceInternal.Assert(condition, message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,19 @@ public void Assert2Test()
Trace.Listeners.Clear();
Trace.Listeners.Add(listener);
Trace.Listeners.Add(text);
Trace.Assert(true, "Message");
bool someSuccessfulCondition = true;
Trace.Assert(someSuccessfulCondition, "Message");
Assert.Equal(0, listener.GetCallCount(Method.WriteLine));
Assert.Equal(0, listener.GetCallCount(Method.Fail));
text.Flush();
Assert.DoesNotContain("Message", text.Output);
Trace.Assert(false, "Message");
bool someFailureCondition = false;
Trace.Assert(someFailureCondition);
Trace.Assert(someFailureCondition, "Message");
Assert.Equal(0, listener.GetCallCount(Method.WriteLine));
Assert.Equal(1, listener.GetCallCount(Method.Fail));
Assert.Equal(2, listener.GetCallCount(Method.Fail));
text.Flush();
Assert.Contains("someFailureCondition", text.Output);
Assert.Contains("Message", text.Output);
}

Expand Down Expand Up @@ -384,7 +388,13 @@ public void TraceTest02()
Trace.WriteLine("Message end.");
textTL.Flush();
newLine = Environment.NewLine;
var expected = "Message start." + newLine + " This message should be indented.This should not be indented." + newLine + " " + fail + "This failure is reported with a detailed message" + newLine + " " + fail + newLine + " " + fail + "This assert is reported" + newLine + "Message end." + newLine;
var expected =
"Message start." + newLine +
" This message should be indented.This should not be indented." + newLine +
" " + fail + "This failure is reported with a detailed message" + newLine +
" " + fail + "false" + newLine +
" " + fail + "This assert is reported" + newLine +
"Message end." + newLine;
Assert.Equal(expected, textTL.Output);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ public static void Print([StringSyntax(StringSyntaxAttribute.CompositeFormat)] s
WriteLine(string.Format(null, format, args));

[Conditional("DEBUG")]
[OverloadResolutionPriority(-1)] // lower priority than (bool, string) overload so that the compiler prefers using CallerArgumentExpression
public static void Assert([DoesNotReturnIf(false)] bool condition) =>
Assert(condition, string.Empty, string.Empty);

[Conditional("DEBUG")]
public static void Assert([DoesNotReturnIf(false)] bool condition, string? message) =>
public static void Assert([DoesNotReturnIf(false)] bool condition, [CallerArgumentExpression(nameof(condition))] string? message = null) =>
Assert(condition, message, string.Empty);

[Conditional("DEBUG")]
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8535,13 +8535,14 @@ public static partial class Debug
public static int IndentLevel { get { throw null; } set { } }
public static int IndentSize { get { throw null; } set { } }
[System.Diagnostics.ConditionalAttribute("DEBUG")]
[System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute(-1)]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition) { }
[System.Diagnostics.ConditionalAttribute("DEBUG")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute("condition")] ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message) { }
[System.Diagnostics.ConditionalAttribute("DEBUG")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute("condition")] ref System.Diagnostics.Debug.AssertInterpolatedStringHandler message, [System.Runtime.CompilerServices.InterpolatedStringHandlerArgumentAttribute("condition")] ref System.Diagnostics.Debug.AssertInterpolatedStringHandler detailMessage) { }
[System.Diagnostics.ConditionalAttribute("DEBUG")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, string? message) { }
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("condition")] string? message = null) { }
[System.Diagnostics.ConditionalAttribute("DEBUG")]
public static void Assert([System.Diagnostics.CodeAnalysis.DoesNotReturnIfAttribute(false)] bool condition, string? message, string? detailMessage) { }
[System.Diagnostics.ConditionalAttribute("DEBUG")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ public void Debug_WriteLineNull_IndentsEmptyStringProperly()
[Fact]
public void Asserts()
{
VerifyLogged(() => Debug.Assert(true), "");
VerifyLogged(() => Debug.Assert(true, "assert passed"), "");
VerifyLogged(() => Debug.Assert(true, "assert passed", "nothing is wrong"), "");
VerifyLogged(() => Debug.Assert(true, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'), "");

VerifyAssert(() => Debug.Assert(false), "");
VerifyAssert(() => Debug.Assert(false, "assert passed"), "assert passed");
VerifyAssert(() => Debug.Assert(false, "assert passed", "nothing is wrong"), "assert passed", "nothing is wrong");
VerifyAssert(() => Debug.Assert(false, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'), "assert passed", "nothing is wrong a b");
bool someSuccessfulCondition = true;
VerifyLogged(() => Debug.Assert(someSuccessfulCondition), "");
VerifyLogged(() => Debug.Assert(someSuccessfulCondition, "assert passed"), "");
VerifyLogged(() => Debug.Assert(someSuccessfulCondition, "assert passed", "nothing is wrong"), "");
VerifyLogged(() => Debug.Assert(someSuccessfulCondition, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'), "");

bool someFailedCondition = false;
VerifyAssert(() => Debug.Assert(someFailedCondition), "someFailedCondition");
VerifyAssert(() => Debug.Assert(someFailedCondition, "assert passed"), "assert passed");
VerifyAssert(() => Debug.Assert(someFailedCondition, "assert passed", "nothing is wrong"), "assert passed", "nothing is wrong");
VerifyAssert(() => Debug.Assert(someFailedCondition, "assert passed", "nothing is wrong {0} {1}", 'a', 'b'), "assert passed", "nothing is wrong a b");
}

[Fact]
Expand Down
Loading