public void ToStringReturnsTheFormattedLog() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); writer.Default.WriteLine("text"); Assert.AreEqual("*** Log ***\n\ntext\n", writer.ToString()); }
public void WhenActionCompletesNormally_RunReturnsPassedAndLogsNothing() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Passed, sandbox.Run(writer, delegate { }, null)); Assert.AreEqual("", writer.ToString()); }
public void WhenActionThrowsTestExceptionWithExcludedStackTraceAndDefaultMessage_RunReturnsOutputAndLogsNothing() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Canceled, sandbox.Run(writer, delegate { throw new SilentTestException(TestOutcome.Canceled); }, null)); Assert.AreEqual("", writer.ToString()); }
public void WhenActionThrowsANonTestException_RunReturnsFailedAndLogsTheException() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { throw new InvalidOperationException("Foo"); }, null)); Assert.Contains(writer.ToString(), "InvalidOperationException"); }
public void WhenActionThrowsTestExceptionWithIncludedStackTraceAndNonDefaultMessage_RunReturnsOutputAndLogsTheException() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { throw new AssertionException("Reason."); }, null)); Assert.Contains(writer.ToString(), "AssertionException"); Assert.Contains(writer.ToString(), "Reason."); }
public void WhenActionThrowsTestExceptionWithExcludedStackTraceAndNonDefaultMessage_RunReturnsOutputAndLogsTheMessageButNotTheException() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Error, sandbox.Run(writer, delegate { throw new SilentTestException(TestOutcome.Error, "Message."); }, null)); Assert.DoesNotContain(writer.ToString(), "SilentTestException"); Assert.Contains(writer.ToString(), "Message."); }
public void RunExitsImmediatelyAndLogsMessage() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); bool actionWasRun = false; abortedSandbox.Run(writer, () => actionWasRun = true, "Description"); Assert.IsFalse(actionWasRun, "The action should not be run because the sandbox already aborted."); Assert.AreEqual("*** Warnings ***\n\nDescription\nAbort message.\n", writer.ToString()); }
public void WhenSandboxEntersProtectedContext_AbortsAreDeferred() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); bool completed = false; Sandbox sandbox = new Sandbox(); ManualResetEvent barrier = new ManualResetEvent(false); Tasks.StartThreadTask("Wake", () => { barrier.WaitOne(); sandbox.Abort(TestOutcome.Canceled, "Canceled for testing purposes."); }); TestOutcome outcome = sandbox.Run(writer, () => { using (sandbox.Protect()) { barrier.Set(); Thread.Sleep(300); completed = true; } Thread.Sleep(300); }, "Run Description"); Assert.IsTrue(completed); Assert.AreEqual(TestOutcome.Canceled, outcome); Assert.Contains(writer.ToString(), "Canceled for testing purposes."); }
private static void RunWithTimeout(TimeSpan waitTime, TimeSpan? timeout) { StructuredDocumentWriter writer = new StructuredDocumentWriter(); bool completed = false; Sandbox sandbox = new Sandbox(); TestOutcome outcome = TestOutcome.Error; using (sandbox.StartTimer(timeout)) { outcome = sandbox.Run(writer, () => { Thread.Sleep(waitTime); completed = true; }, "Run Description"); } if (timeout.HasValue && timeout.Value < waitTime) { Assert.IsFalse(completed, "The action should have been aborted prior to completion."); Assert.AreEqual(TestOutcome.Timeout, outcome); Assert.AreEqual(TestOutcome.Timeout, sandbox.AbortOutcome); Assert.AreEqual(String.Format("The test timed out after {0} seconds.", timeout.Value.TotalSeconds), sandbox.AbortMessage); Assert.IsTrue(sandbox.WasAborted); Assert.Contains(writer.ToString(), "Run Description"); Assert.Contains(writer.ToString(), String.Format("The test timed out after {0} seconds.", timeout.Value.TotalSeconds)); } else { Assert.IsTrue(completed, "The action should have completed because no timeout occurred."); Assert.AreEqual(TestOutcome.Passed, outcome); Assert.IsFalse(sandbox.WasAborted); } }
public void RunCanBeAbortedInProgress() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); ManualResetEvent ready = new ManualResetEvent(false); bool completed = false; Sandbox sandbox = new Sandbox(); Tasks.StartThreadTask("Background abort.", () => { ready.WaitOne(); sandbox.Abort(TestOutcome.Canceled, "Test was canceled."); }); TestOutcome outcome = sandbox.Run(writer, () => { ready.Set(); Thread.Sleep(10000); completed = true; }, "Run Description"); Assert.IsFalse(completed, "The action should have been aborted prior to completion."); Assert.AreEqual(TestOutcome.Canceled, outcome); Assert.AreEqual(TestOutcome.Canceled, sandbox.AbortOutcome); Assert.AreEqual("Test was canceled.", sandbox.AbortMessage); Assert.IsTrue(sandbox.WasAborted); Assert.Contains(writer.ToString(), "Run Description"); Assert.Contains(writer.ToString(), "Test was canceled."); }
public void CanCatchThreadAbortException() { StructuredDocumentWriter writer = new StructuredDocumentWriter() ; Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { Thread.CurrentThread.Abort(this); }, "Execute")); Assert.Contains(writer.ToString(), "ThreadAbortException"); Assert.Contains(writer.ToString(), "Execute"); }
public void WhenActionFailsAndADescriptionWasProvided_TheDescriptionAppearsInTheLog() { StructuredDocumentWriter writer = new StructuredDocumentWriter(); Sandbox sandbox = new Sandbox(); Assert.AreEqual(TestOutcome.Failed, sandbox.Run(writer, delegate { throw new InvalidOperationException("Foo"); }, "SetUp")); Assert.Contains(writer.ToString(), "InvalidOperationException"); Assert.Contains(writer.ToString(), "SetUp"); }