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

Create Write() in TestContext #686

Merged
merged 4 commits into from
Feb 26, 2020
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 @@ -249,6 +249,53 @@ 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.
/// </summary>
/// <param name="message">The formatted string that contains the trace message.</param>
public override void Write(string message)
{
if (this.stringWriterDisposed)
{
return;
}

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

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
/// </summary>
/// <param name="format">The string that contains the trace message.</param>
/// <param name="args">Arguments to add to the trace message.</param>
public override void Write(string format, params object[] args)
{
if (this.stringWriterDisposed)
{
return;
}

try
{
string message = string.Format(CultureInfo.CurrentCulture, format?.Replace("\0", "\\0"), args);
this.stringWriter.Write(message);
}
catch (ObjectDisposedException)
{
this.stringWriterDisposed = true;
}
}

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,53 @@ public IList<string> GetResultFiles()
return results;
}

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
/// </summary>
/// <param name="message">The formatted string that contains the trace message.</param>
public override void Write(string message)
{
if (this.stringWriterDisposed)
{
return;
}

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

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
/// </summary>
/// <param name="format">The string that contains the trace message.</param>
/// <param name="args">Arguments to add to the trace message.</param>
public override void Write(string format, params object[] args)
{
if (this.stringWriterDisposed)
{
return;
}

try
{
string message = string.Format(CultureInfo.CurrentCulture, format?.Replace("\0", "\\0"), args);
this.stringWriter.Write(message);
}
catch (ObjectDisposedException)
{
this.stringWriterDisposed = true;
}
}

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,53 @@ public void AddProperty(string propertyName, string propertyValue)
this.properties.Add(propertyName, propertyValue);
}

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
/// </summary>
/// <param name="message">The formatted string that contains the trace message.</param>
public override void Write(string message)
{
if (this.stringWriterDisposed)
{
return;
}

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

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
/// </summary>
/// <param name="format">The string that contains the trace message.</param>
/// <param name="args">Arguments to add to the trace message.</param>
public override void Write(string format, params object[] args)
{
if (this.stringWriterDisposed)
{
return;
}

try
{
string message = string.Format(CultureInfo.CurrentCulture, format?.Replace("\0", "\\0"), args);
this.stringWriter.Write(message);
}
catch (ObjectDisposedException)
{
this.stringWriterDisposed = true;
}
}

/// <summary>
/// When overridden in a derived class, used to write trace messages while the
/// test is running.
Expand Down
13 changes: 13 additions & 0 deletions src/TestFramework/Extension.Core/NetCoreTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ public abstract class TestContext
/// </param>
public abstract void AddResultFile(string fileName);

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="message">formatted message string</param>
public abstract void Write(string message);

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="format">format string</param>
/// <param name="args">the arguments</param>
public abstract void Write(string format, params object[] args);

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/TestFramework/Extension.Desktop/DesktopTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ public abstract class TestContext
/// </summary>
public virtual UnitTestOutcome CurrentTestOutcome => UnitTestOutcome.Unknown;

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="message">formatted message string</param>
public abstract void Write(string message);

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="format">format string</param>
/// <param name="args">the arguments</param>
public abstract void Write(string format, params object[] args);

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions src/TestFramework/Extension.Shared/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public abstract class TestContext
/// </summary>
public virtual UnitTestOutcome CurrentTestOutcome => UnitTestOutcome.Unknown;

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="message">formatted message string</param>
public abstract void Write(string message);

/// <summary>
/// Used to write trace messages while the test is running
/// </summary>
/// <param name="format">format string</param>
/// <param name="args">the arguments</param>
public abstract void Write(string format, params object[] args);

/// <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 @@ -25,7 +25,7 @@ namespace MSTestAdapter.PlatformServices.Desktop.UnitTests.Services
using UnitTestOutcome = FrameworkV2::Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome;

[TestClass]
public class DEsktopTestContextImplTests
public class DesktopTestContextImplTests
{
private Mock<ITestMethod> testMethod;

Expand Down Expand Up @@ -215,6 +215,76 @@ public void AddResultFileShouldAddMultipleFilestoResultsFiles()
CollectionAssert.Contains(resultsFiles.ToList(), "C:\\temp2.txt");
}

[TestMethod]
public void WriteShouldWriteToStringWriter()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("{0} Testing write", 1);
StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
}

[TestMethod]
public void WriteShouldWriteToStringWriterForNullCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("{0} Testing \0 write \0", 1);
StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
}

[TestMethod]
public void WriteShouldNotThrowIfStringWriterIsDisposed()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
stringWriter.Dispose();
this.testContextImplementation.Write("{0} Testing write", 1);

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

[TestMethod]
public void WriteWithMessageShouldWriteToStringWriter()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("1 Testing write");
StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
}

[TestMethod]
public void WriteWithMessageShouldWriteToStringWriterForNullCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("1 Testing \0 write \0");
StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
}

[TestMethod]
public void WriteWithMessageShouldNotThrowIfStringWriterIsDisposed()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
stringWriter.Dispose();
this.testContextImplementation.Write("1 Testing write");

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

[TestMethod]
public void WriteWithMessageShouldWriteToStringWriterForReturnCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("2 Testing write \n\r");
this.testContextImplementation.Write("3 Testing write\n\r");
StringAssert.Equals(stringWriter.ToString(), "2 Testing write 3 Testing write");
nohwnd marked this conversation as resolved.
Show resolved Hide resolved
}

[TestMethod]
public void WriteLineShouldWriteToStringWriter()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,76 @@ public void AddPropertyShouldAddPropertiesToThePropertyBag()
new KeyValuePair<string, object>("SomeNewProperty", "SomeValue"));
}

[TestMethod]
public void WriteShouldWriteToStringWriter()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("{0} Testing write", 1);
StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
}

[TestMethod]
public void WriteShouldWriteToStringWriterForNullCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("{0} Testing \0 write \0", 1);
StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
}

[TestMethod]
public void WriteShouldNotThrowIfStringWriterIsDisposed()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
stringWriter.Dispose();
this.testContextImplementation.Write("{0} Testing write", 1);

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

[TestMethod]
public void WriteWithMessageShouldWriteToStringWriter()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("1 Testing write");
StringAssert.Contains(stringWriter.ToString(), "1 Testing write");
}

[TestMethod]
public void WriteWithMessageShouldWriteToStringWriterForNullCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("1 Testing \0 write \0");
StringAssert.Contains(stringWriter.ToString(), "1 Testing \\0 write \\0");
}

[TestMethod]
public void WriteWithMessageShouldNotThrowIfStringWriterIsDisposed()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
stringWriter.Dispose();
this.testContextImplementation.Write("1 Testing write");

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

[TestMethod]
public void WriteWithMessageShouldWriteToStringWriterForReturnCharacters()
{
var stringWriter = new StringWriter();
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, stringWriter, this.properties);
this.testContextImplementation.Write("2 Testing write \n\r");
this.testContextImplementation.Write("3 Testing write\n\r");
StringAssert.Equals(stringWriter.ToString(), "2 Testing write 3 Testing write");
}

[TestMethod]
public void WriteLineShouldWriteToStringWriter()
{
Expand Down
Loading