Skip to content
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 @@ -19,7 +19,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;

/// <summary>
/// Internal implementation of TestContext exposed to the user.
/// The virtual string properties of the TestContext are retreived from the property dictionary
Expand Down Expand Up @@ -269,6 +269,32 @@ public override void EndTimer(string timerName)
throw new NotSupportedException();
}

//
// Summary:
// When overridden in a derived class, used to write trace messages while the
// test is running.
//
// Parameters:
// message:
// The formatted string that contains the trace message.
public override void WriteLine(string message)
{
if (this.stringWriterDisposed)
{
return;
}

try
{
var msg = message?.Replace("\0", "\\0");
stringWriter.WriteLine(msg);
}
catch (ObjectDisposedException)
{
this.stringWriterDisposed = true;
}
}

//
// Summary:
// When overridden in a derived class, used to write trace messages while the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting
/// </summary>
public abstract class TestContext
{
/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="message">formatted message string</param>
public abstract void WriteLine(string message);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if Extension.Core needs a similar API?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not supporting TestContext.WriteLine in Extension.Core, that's why I haven't added this new overloaded method there.


/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,40 @@ public void WriteLineShouldNotThrowIfStringWriterIsDisposed()
// Calling it twice to cover the direct return when we know the object has been disposed.
this.testContextImplementation.WriteLine("{0} Testing write", 1);
}

[TestMethod]
public void WriteLineWithMessageShouldWriteToStringWriter()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);

this.testContextImplementation.WriteLine("1 Testing write");

StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
}

[TestMethod]
public void WriteLineWithMessageShouldWriteToStringWriterForNullCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);

this.testContextImplementation.WriteLine("1 Testing \0 write \0");

StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
}

[TestMethod]
public void WriteLineWithMessageShouldNotThrowIfStringWriterIsDisposed()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);

stringWriter.Dispose();

this.testContextImplementation.WriteLine("1 Testing write");
// Calling it twice to cover the direct return when we know the object has been disposed.
this.testContextImplementation.WriteLine("1 Testing write");
}
}
}