public void ToStringReturnsTheFormattedLog()
        {
            StructuredDocumentWriter writer = new StructuredDocumentWriter();
            writer.Default.WriteLine("text");

            Assert.AreEqual("*** Log ***\n\ntext\n", writer.ToString());
        }
Example #2
0
        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());
        }
Example #3
0
        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());
        }
Example #4
0
        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");
        }
Example #5
0
        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.");
        }
Example #6
0
        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.");
        }
Example #7
0
            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());
            }
Example #8
0
        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.");
        }
Example #9
0
        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);
            }
        }
Example #10
0
        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.");
        }
Example #11
0
        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");
        }
Example #12
0
        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");
        }