Exemplo n.º 1
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());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a wrapper for a <see cref="ITestContext" />.
        /// </summary>
        /// <param name="inner">The context to wrap.</param>
        /// <param name="sandbox">The sandbox to use, or null if none.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="inner"/> is null.</exception>
        private TestContext(ITestContext inner, Sandbox sandbox)
        {
            if (inner == null)
                throw new ArgumentNullException("inner");

            this.inner = inner;
            this.sandbox = sandbox;
        }
Exemplo n.º 3
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");
        }
Exemplo n.º 4
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());
        }
Exemplo n.º 5
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.");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Disposes the sandbox.
        /// </summary>
        /// <remarks>
        /// <para>
        /// All currently executing actions are aborted with <see cref="TestOutcome.Error" />
        /// if <see cref="Abort(TestOutcome, string)" /> has not already been called.
        /// </para>
        /// </remarks>
        public void Dispose()
        {
            Abort(TestOutcome.Error, "The sandbox was disposed.", false);

            Sandbox cachedParent;

            lock (syncRoot)
            {
                if (isDisposed)
                {
                    return;
                }

                cachedParent = parent;
                parent       = null;
                isDisposed   = true;
            }

            if (cachedParent != null)
            {
                cachedParent.Aborted -= HandleParentAborted;
            }
        }
Exemplo n.º 7
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.");
        }
Exemplo n.º 8
0
            public RunTestAction(PatternTestExecutor executor, ITestCommand testCommand, Model.Tree.TestStep parentTestStep, Sandbox parentSandbox, PatternTestActionsDecorator testActionsDecorator)
            {
                this.executor = executor;
                this.testCommand = testCommand;
                this.parentTestStep = parentTestStep;
                this.parentSandbox = parentSandbox;
                this.testActionsDecorator = testActionsDecorator;

                result = new TestResult(TestOutcome.Error);
            }
Exemplo n.º 9
0
        /// <summary>
        /// Disposes the sandbox.
        /// </summary>
        /// <remarks>
        /// <para>
        /// All currently executing actions are aborted with <see cref="TestOutcome.Error" />
        /// if <see cref="Abort(TestOutcome, string)" /> has not already been called.
        /// </para>
        /// </remarks>
        public void Dispose()
        {
            Abort(TestOutcome.Error, "The sandbox was disposed.", false);

            Sandbox cachedParent;
            lock (syncRoot)
            {
                if (isDisposed)
                    return;

                cachedParent = parent;
                parent = null;
                isDisposed = true;
            }

            if (cachedParent != null)
                cachedParent.Aborted -= HandleParentAborted;
        }
Exemplo n.º 10
0
 public void RunThrowsIfTestLogWriterIsNull()
 {
     Sandbox sandbox = new Sandbox();
     Assert.Throws<ArgumentNullException>(() =>
         sandbox.Run(null, delegate { }, "description"));
 }
Exemplo n.º 11
0
 public void RunThrowsIfActionIsNull()
 {
     Sandbox sandbox = new Sandbox();
     Assert.Throws<ArgumentNullException>(() =>
         sandbox.Run(MockRepository.GenerateStub<MarkupDocumentWriter>(), null, "description"));
 }
Exemplo n.º 12
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.");
        }
Exemplo n.º 13
0
            public WhenAborted(bool abortTwice)
            {
                abortedSandbox = new Sandbox();
                abortedSandbox.Abort(TestOutcome.Canceled, "Abort message.");

                if (abortTwice)
                    abortedSandbox.Abort(TestOutcome.Passed, "A different message.");
            }
Exemplo n.º 14
0
        /// <summary>
        /// Creates a child sandbox.
        /// </summary>
        /// <remarks>
        /// <para>
        /// When the parent sandbox is aborted, the child will likewise be aborted.  This policy
        /// offers a mechanism to scope actions recursively.
        /// </para>
        /// </remarks>
        /// <returns>The child sandbox.</returns>
        /// <exception cref="ObjectDisposedException">Thrown if the sandbox was disposed.</exception>
        public Sandbox CreateChild()
        {
            ThrowIfDisposed();

            Sandbox child = new Sandbox();
            child.parent = this;
            Aborted += child.HandleParentAborted;
            return child;
        }
Exemplo n.º 15
0
 public RunTestAction CreateActionToRunTest(ITestCommand testCommand, Model.Tree.TestStep parentTestStep,
     Sandbox parentSandbox, PatternTestActionsDecorator testActionsDecorator)
 {
     return new RunTestAction(this, testCommand, parentTestStep, parentSandbox, testActionsDecorator);
 }
Exemplo n.º 16
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");
        }
Exemplo n.º 17
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");
        }
Exemplo n.º 18
0
 /// <summary>
 /// Prepares a <see cref="TestContext" /> wrapper for the given inner context.
 /// </summary>
 /// <remarks>
 /// <para>
 /// The wrapper is cached for the duration of the lifetime of the inner context.
 /// </para>
 /// </remarks>
 /// <param name="inner">The new inner context.</param>
 /// <param name="sandbox">The sandbox to use, or null if none.</param>
 /// <returns>The wrapper context.</returns>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="inner"/> is null.</exception>
 internal static TestContext PrepareContext(ITestContext inner, Sandbox sandbox)
 {
     TestContext context = new TestContext(inner, sandbox);
     inner.Data.SetValue(GallioFrameworkContextKey, context);
     inner.Finishing += context.NotifyFinishing;
     return context;
 }
Exemplo n.º 19
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.");
        }
Exemplo n.º 20
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);
            }
        }