Beispiel #1
0
        private static void IssueWarning(ConstraintResult result, string message, object[] args)
        {
            MessageWriter writer = new TextMessageWriter(message, args);

            result.WriteMessageTo(writer);
            Assert.Warn(writer.ToString());
        }
Beispiel #2
0
        private static void ReportFailure(ConstraintResult result, string message, object[] args)
        {
            MessageWriter writer = new TextMessageWriter(message, args);

            result.WriteMessageTo(writer);
            throw new InconclusiveException(writer.ToString());
        }
Beispiel #3
0
        /// <summary>
        /// Wraps code containing a series of assertions, which should all
        /// be executed, even if they fail. Failed results are saved and
        /// reported at the end of the code block.
        /// </summary>
        /// <param name="testDelegate">A TestDelegate to be executed in Multiple Assertion mode.</param>
        public static void Multiple(TestDelegate testDelegate)
        {
            TestExecutionContext context = TestExecutionContext.CurrentContext;

            Guard.OperationValid(context != null, "Assert.Multiple called outside of a valid TestExecutionContext");

            context.MultipleAssertLevel++;

            try
            {
                testDelegate();
            }
            finally
            {
                context.MultipleAssertLevel--;
            }

            if (context.MultipleAssertLevel == 0)
            {
                int count = context.CurrentResult.AssertionResults.Count;

                if (count > 0)
                {
                    var writer = new TextMessageWriter("Multiple Assert block had {0} failure(s).", count);

                    int counter = 0;
                    foreach (var assertion in context.CurrentResult.AssertionResults)
                    {
                        writer.WriteLine(string.Format("  {0}) {1}", ++counter, assertion.Message));
                    }

                    throw new AssertionException(writer.ToString());
                }
            }
        }
Beispiel #4
0
        public void Setup()
        {
            _textMessageWriter = new TextMessageWriter();

            Console.SetOut(_textMessageWriter);
            ConsoleMessageLogger.WindowWidth = 40;
        }
 public void ProvidesProperFailureMessage()
 {
     TextMessageWriter writer = new TextMessageWriter();
     Matcher.Matches(BadValues[0]);
     Matcher.WriteMessageTo(writer);
     Assert.That(writer.ToString(), Is.StringContaining(Description));
     Assert.That(writer.ToString(), Is.Not.StringContaining("<UNSET>"));
 }
 public void Setup()
 {
     _textMessageWriter = new TextMessageWriter();
     Console.SetOut(_textMessageWriter);
     _subject = new TestSuiteMessageLogger("test");
     _suiteStartedMessage = "##teamcity[testSuiteStarted name='test']\r\n";
     Assert.That(_textMessageWriter.ToString(), Is.EqualTo(_suiteStartedMessage));
 }
Beispiel #7
0
        private static void ReportFailure(ConstraintResult result, string message, params object[] args)
        {
            MessageWriter writer = new TextMessageWriter(message, args);

            result.WriteMessageTo(writer);

            ReportFailure(writer.ToString());
        }
        public void Setup()
        {
            _textMessageWriter = new TextMessageWriter();
            Console.SetOut(_textMessageWriter);

            _subject = new TestSuiteLogger(0, "Suite");
            _suiteStartedMessage = "  [TEST] Suite\r\n";
            Assert.That(_textMessageWriter.ToString(), Is.EqualTo(_suiteStartedMessage));
        }
Beispiel #9
0
 /// <summary>
 /// Apply a constraint to an actual value, succeedingt if the constraint
 /// is satisfied and throwing an assertion exception on failure.
 /// </summary>
 /// <param name="constraint">A Constraint to be applied</param>
 /// <param name="actual">The actual value to test</param>
 /// <param name="message">The message that will be displayed on failure</param>
 /// <param name="args">Arguments to be used in formatting the message</param>
 static public void That(object actual, Constraint constraint, string message, params object[] args)
 {
     Assert.IncrementAssertCount();
     if (!constraint.Matches(actual))
     {
         MessageWriter writer = new TextMessageWriter(message, args);
         constraint.WriteMessageTo(writer);
         throw new AssertionException(writer.ToString());
     }
 }
Beispiel #10
0
 /// <summary>
 /// Helper function that creates a lambda function passed to an Assert method that lazily builds the Exception message.  The function returned from this method ignores the <see cref="ConstraintResult"/> object returned from the <see cref="IConstraint.ApplyTo{TActual}(TActual)"/> method.  It is intended to be used when the client wants to pass a message and parameter arguments to the Assert method overload.
 /// </summary>
 /// <param name="message">The message to initialize the <see cref="Exception"/> with.</param>
 /// <param name="args">Arguments to be used in formatting the message</param>
 /// <returns>A lambda function to lazily build the string</returns>
 public static Func <ConstraintResult, string> BuildDefaultExceptionMessageFunc(string message, object[] args)
 {
     return(result =>
     {
         MessageWriter writer = new TextMessageWriter(message, args);
         result.WriteMessageTo(writer);
         var exceptionMessage = writer.ToString();
         return exceptionMessage;
     });
 }
Beispiel #11
0
        /// <summary>
        /// Apply a constraint to an actual value, succeeding if the constraint
        /// is satisfied and throwing an InconclusiveException on failure.
        /// </summary>
        /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
        /// <param name="expr">A Constraint expression to be applied</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That <T>(ActualValueDelegate <T> del, IResolveConstraint expr, string message, params object[] args)
        {
            Constraint constraint = expr.Resolve();

            if (!constraint.Matches(del))
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                constraint.WriteMessageTo(writer);
                throw new InconclusiveException(writer.ToString());
            }
        }
Beispiel #12
0
        /// <summary>
        /// Apply a constraint to an actual value, succeeding if the constraint
        /// is satisfied and throwing an InconclusiveException on failure.
        /// </summary>
        /// <param name="expression">A Constraint expression to be applied</param>
        /// <param name="actual">The actual value to test</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That(object actual, IResolveConstraint expression, string message, params object[] args)
        {
            Constraint constraint = expression.Resolve();

            if (!constraint.Matches(actual))
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                constraint.WriteMessageTo(writer);
                throw new InconclusiveException(writer.ToString());
            }
        }
Beispiel #13
0
        public static void That <T>(ref T actual, IResolveConstraint expression, string message, params object[] args)
        {
            Constraint constraint = expression.Resolve();

            TestExecutionContext.CurrentContext.IncrementAssertCount();
            if (!constraint.Matches(ref actual))
            {
                MessageWriter messageWriter = new TextMessageWriter(message, args);
                constraint.WriteMessageTo(messageWriter);
                throw new AssertionException(messageWriter.ToString());
            }
        }
Beispiel #14
0
        /// <summary>
        /// Apply a constraint to a referenced value, succeeding if the constraint
        /// is satisfied and throwing an assertion exception on failure.
        /// </summary>
        /// <param name="constraint">A Constraint to be applied</param>
        /// <param name="actual">The actual value to test</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That(ref bool actual, IResolveConstraint expression, string message, params object[] args)
        {
            Constraint constraint = expression.Resolve();

            Assert.IncrementAssertCount();
            if (!constraint.Matches(ref actual))
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                constraint.WriteMessageTo(writer);
                throw new AssertionException(writer.ToString());
            }
        }
Beispiel #15
0
        /// <summary>
        /// Apply a constraint to an actual value, succeeding if the constraint
        /// is satisfied and throwing an InconclusiveException on failure.
        /// </summary>
        /// <param name="del">An ActualValueDelegate returning the value to be tested</param>
        /// <param name="expr">A Constraint expression to be applied</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That <TActual>(ActualValueDelegate <TActual> del, IResolveConstraint expr, string message, params object[] args)
        {
            var constraint = expr.Resolve();

            var result = constraint.ApplyTo(del);

            if (!result.IsSuccess)
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                result.WriteMessageTo(writer);
                throw new InconclusiveException(writer.ToString());
            }
        }
Beispiel #16
0
 /// <summary>
 /// Verifies that a delegate does not throw an exception
 /// </summary>
 /// <param name="code">A TestSnippet delegate</param>
 /// <param name="message">The message that will be displayed on failure</param>
 /// <param name="args">Arguments to be used in formatting the message</param>
 public static void DoesNotThrow(TestDelegate code, string message, params object[] args)
 {
     try
     {
         code();
     }
     catch (Exception ex)
     {
         TextMessageWriter writer = new TextMessageWriter(message, args);
         writer.WriteLine("Unexpected exception: {0}", ex.GetType());
         Assert.Fail(writer.ToString());
     }
 }
        protected string TransformDecimalToPercentage(Action <NUnitFwk.TextMessageWriter> action)
        {
            var sb          = new System.Text.StringBuilder();
            var localWriter = new NUnitFwk.TextMessageWriter();

            action(localWriter);
            var childMessage = localWriter.ToString();

            sb.Append(childMessage.Substring(0, childMessage.LastIndexOf(" ") + 1));
            sb.Append(Decimal.Parse(childMessage.Substring(childMessage.LastIndexOf(" ") + 1).Replace("m", ""), System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat));
            sb.Append("%");

            return(sb.ToString());
        }
Beispiel #18
0
        /// <summary>
        /// Apply a constraint to an actual value, succeeding if the constraint
        /// is satisfied and throwing an assertion exception on failure.
        /// </summary>
        /// <param name="expression">A Constraint expression to be applied</param>
        /// <param name="actual">The actual value to test</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        static public void That <TActual>(TActual actual, IResolveConstraint expression, string message, params object[] args)
        {
            var constraint = expression.Resolve();

            IncrementAssertCount();
            var result = constraint.ApplyTo(actual);

            if (!result.IsSuccess)
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                result.WriteMessageTo(writer);
                throw new AssertionException(writer.ToString());
            }
        }
Beispiel #19
0
        /// <summary>
        /// Apply a constraint to an actual value, succeeding if the constraint
        /// is satisfied and throwing an InconclusiveException on failure.
        /// </summary>
        /// <typeparam name="TActual">The Type being compared.</typeparam>
        /// <param name="actual">The actual value to test</param>
        /// <param name="expression">A Constraint expression to be applied</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        public static void That <TActual>(TActual actual, IResolveConstraint expression, string message, params object[] args)
        {
            CheckMultipleAssertLevel();

            var constraint = expression.Resolve();

            var result = constraint.ApplyTo(actual);

            if (!result.IsSuccess)
            {
                MessageWriter writer = new TextMessageWriter(message, args);
                result.WriteMessageTo(writer);
                throw new InconclusiveException(writer.ToString());
            }
        }
        public void SetUp()
        {
            _securityTokenMock = new Mock<IJwtSecurityToken>();
            _securityTokenHandlerMock = new Mock<IJwtSecurityTokenHandler>();
            _principalTransformerMock = new Mock<IPrincipalTransformer>();

            _textMessageWriter = new TextMessageWriter();

            _authenticationMessageHandler = new JwtAuthenticationMessageHandlerTestDouble(_securityTokenMock.Object,
                _securityTokenHandlerMock.Object);

            Thread.CurrentPrincipal = null;
            HttpContext.Current = new HttpContext(new HttpRequest("foo", "http://www.foo.com", null),
                new HttpResponse(_textMessageWriter));
        }
Beispiel #21
0
        private static void ReportFailure(ConstraintResult result, string message, params object[] args)
        {
            MessageWriter writer = new TextMessageWriter(message, args);

            result.WriteMessageTo(writer);
            string formattedMessage = writer.ToString();
            string stackTrace       = GetStackTrace();

            // Failure is recorded in <assertion> element in all cases
            TestExecutionContext.CurrentContext.CurrentResult.RecordAssertion(
                AssertionStatus.Failed, formattedMessage, stackTrace);

            // If we are outside any multiple assert block, then throw
            if (TestExecutionContext.CurrentContext.MultipleAssertLevel == 0)
            {
                throw new AssertionException(formattedMessage);
            }
        }
 protected override void SetUp()
 {
     writer = new TextMessageWriter();
 }
Beispiel #23
0
		/// <summary>
		/// Apply a constraint to an actual value, succeedingt if the constraint
		/// is satisfied and throwing an assertion exception on failure.
		/// </summary>
		/// <param name="constraint">A Constraint to be applied</param>
		/// <param name="actual">The actual value to test</param>
		/// <param name="message">The message that will be displayed on failure</param>
		/// <param name="args">Arguments to be used in formatting the message</param>
		static public void That( object actual, Constraint constraint, string message, params object[] args )
		{
			Assert.IncrementAssertCount();
			if ( !constraint.Matches( actual ) )
			{
				MessageWriter writer = new TextMessageWriter( message, args );
				constraint.WriteMessageTo( writer );
				throw new AssertionException( writer.ToString() );
			}
		}
 /// <summary>
 /// this is not a real test, is used only to see the log that 
 /// a failing assertion does.
 /// </summary>
 public void TestBasicCompareFalseVerifyLog()
 {
     AnEntity entity1 = AnEntity.Create(10, "test", 100);
     AnEntity entity2 = AnEntity.Create(10, "test", 110);
     ObjectEqualConstraint c = new ObjectEqualConstraint(entity2);
     c.Matches(entity1);
     MessageWriter mw = new TextMessageWriter();
     c.WriteDescriptionTo(mw);
     Assert.That(entity1, DotNetMarche.TestHelpers.SyntaxHelpers.Is.ObjectEqual(entity2));
 }
 public void Setup()
 {
     _textMessageWriter = new TextMessageWriter();
     Console.SetOut(_textMessageWriter);
 }
 public void SetUp()
 {
     writer = new TextMessageWriter();
 }
 public void ProvidesProperDescription()
 {
     TextMessageWriter writer = new TextMessageWriter();
     Matcher.WriteDescriptionTo(writer);
     Assert.That(writer.ToString(), Is.EqualTo(Description), null);
 }
 public void Setup()
 {
     _textMessageWriter = new TextMessageWriter();
     Console.SetOut(_textMessageWriter);
     _subject = new MessageLogger();
 }