Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
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
34 changes: 10 additions & 24 deletions src/System.Diagnostics.Debug/tests/DebugTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void VerifyLogged(Action test, string expectedOutput)

// First use our test logger to verify the output
var originalWriteCoreHook = writeCoreHook.GetValue(null);
writeCoreHook.SetValue(null, new Action<string>(WriteLogger.s_instance.MockWrite));
writeCoreHook.SetValue(null, new Action<string>(WriteLogger.s_instance.WriteCore));

try
{
Expand All @@ -62,11 +62,12 @@ protected void VerifyLogged(Action test, string expectedOutput)
protected void VerifyAssert(Action test, params string[] expectedOutputStrings)
{
FieldInfo writeCoreHook = typeof(DebugProvider).GetField("s_WriteCore", BindingFlags.Static | BindingFlags.NonPublic);
s_defaultProvider = Debug.SetProvider(WriteLogger.s_instance);
WriteLogger.s_instance.OriginalProvider = s_defaultProvider;

var originalWriteCoreHook = writeCoreHook.GetValue(null);
writeCoreHook.SetValue(null, new Action<string>(WriteLogger.s_instance.MockWrite));
writeCoreHook.SetValue(null, new Action<string>(WriteLogger.s_instance.WriteCore));

FieldInfo failCoreHook = typeof(DebugProvider).GetField("s_FailCore", BindingFlags.Static | BindingFlags.NonPublic);
var originalFailCoreHook = failCoreHook.GetValue(null);
failCoreHook.SetValue(null, new Action<string, string, string, string>(WriteLogger.s_instance.FailCore));

try
{
Expand All @@ -82,19 +83,16 @@ protected void VerifyAssert(Action test, params string[] expectedOutputStrings)
finally
{
writeCoreHook.SetValue(null, originalWriteCoreHook);
Debug.SetProvider(s_defaultProvider);
failCoreHook.SetValue(null, originalFailCoreHook);
}
}

private static DebugProvider s_defaultProvider;
private class WriteLogger : DebugProvider
internal class WriteLogger
{
public static readonly WriteLogger s_instance = new WriteLogger();

private WriteLogger() { }

public DebugProvider OriginalProvider { get; set; }

public string LoggedOutput { get; private set; }

public string AssertUIOutput { get; private set; }
Expand All @@ -105,24 +103,12 @@ public void Clear()
AssertUIOutput = string.Empty;
}

public override void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource)
public void FailCore(string stackTrace, string message, string detailMessage, string errorSource)
{
AssertUIOutput += stackTrace + message + detailMessage + errorSource;
}

public override void OnIndentLevelChanged(int indentLevel)
{
OriginalProvider.OnIndentLevelChanged(indentLevel);
}

public override void OnIndentSizeChanged(int indentSize)
{
OriginalProvider.OnIndentLevelChanged(indentSize);
}
public override void Write(string message) { OriginalProvider.Write(message); }
public override void WriteLine(string message) { OriginalProvider.WriteLine(message); }

public void MockWrite(string message)
public void WriteCore(string message)
{
Assert.NotNull(message);
LoggedOutput += message;
Expand Down
57 changes: 57 additions & 0 deletions src/System.Diagnostics.Debug/tests/DebugTestsUsingListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,62 @@ public void TraceWriteLineIf()
VerifyLogged(() => Trace.WriteLineIf(true, "logged", "category"), "category: logged" + Environment.NewLine);
VerifyLogged(() => Trace.WriteLineIf(false, "logged", "category"), "");
}

[Fact]
public void Trace_AssertUiEnabledFalse_SkipsFail()
{
var initialListener = (DefaultTraceListener)Trace.Listeners[0];
Trace.Listeners.Clear();
Trace.Fail("Skips fail fast");
Debug.Fail("Skips fail fast");

initialListener.AssertUiEnabled = false;
Trace.Listeners.Add(initialListener);
Debug.Fail("Skips fail fast");
Trace.Fail("Skips fail fast");

var myListener = new MyTraceListener();
string expectedDialog = $"Mock dialog - message: short, detailed message: long";
Trace.Listeners.Clear();
Trace.Listeners.Add(myListener);

try
{
myListener.AssertUiEnabled = false;
Debug.Fail("short", "long");
Assert.Equal("", myListener.OutputString);
Trace.Fail("short", "long");
Assert.Equal("", myListener.OutputString);

myListener.AssertUiEnabled = true;
Debug.Fail("short", "long");
Assert.Equal(expectedDialog, myListener.OutputString);
Trace.Fail("short", "long");
Assert.Equal(expectedDialog + expectedDialog, myListener.OutputString);
}
finally
{
Trace.Listeners.Clear();
Trace.Listeners.Add(initialListener);
}
}

class MyTraceListener : DefaultTraceListener
{
private void FailCore(string stackTrace, string message, string detailMessage, string errorSource)
{
OutputString += $"Mock dialog - message: {message}, detailed message: {detailMessage}";
}
public string OutputString { get; private set; } = string.Empty;

public override void Fail(string message, string detailMessage)
{
WriteLine(message, detailMessage);
if (AssertUiEnabled)
{
FailCore(string.Empty, message, detailMessage, "Assertion Failed");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,10 @@ public override void Fail(string message, string detailMessage)
{
stackTrace = "";
}
// Tracked by #32955: WriteAssert should write "stackTrace" rather than string.Empty.
WriteAssert(string.Empty, message, detailMessage);
WriteAssert(stackTrace, message, detailMessage);
if (AssertUiEnabled)
{
// Tracked by #32955: Currently AssertUiEnabled is true by default but we are not calling Enviroment.FailFast as Debug.Fail does
// s_provider.ShowDialog(stackTrace, message, detailMessage, "Assertion Failed");
}
if (Debugger.IsAttached)
{
Debugger.Break();
DebugProvider.FailCore(stackTrace, message, detailMessage, "Assertion Failed");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal static class TraceInternal
{
private class TraceProvider : DebugProvider
{
public override void Fail(string message, string detailMessage) { TraceInternal.Fail(message, detailMessage); }
public override void OnIndentLevelChanged(int indentLevel)
{
lock (TraceInternal.critSec)
Expand Down