Static methods used in creating messages
Example #1
0
        public static void ClipExpectedAndActual_StringsFitInLine()
        {
            string eClip = s52;
            string aClip = "abcde";

            MsgUtils.ClipExpectedAndActual(ref eClip, ref aClip, 52, 5);
            Assert.That(eClip, Is.EqualTo(s52));
            Assert.That(aClip, Is.EqualTo("abcde"));

            eClip = s52;
            aClip = "abcdefghijklmno?qrstuvwxyz";
            MsgUtils.ClipExpectedAndActual(ref eClip, ref aClip, 52, 15);
            Assert.That(eClip, Is.EqualTo(s52));
            Assert.That(aClip, Is.EqualTo("abcdefghijklmno?qrstuvwxyz"));
        }
Example #2
0
        /// <summary>
        /// Displays a single line showing the types and sizes of the expected
        /// and actual collections or arrays. If both are identical, the value is
        /// only shown once.
        /// </summary>
        /// <param name="writer">The MessageWriter on which to display</param>
        /// <param name="expected">The expected collection or array</param>
        /// <param name="actual">The actual collection or array</param>
        /// <param name="indent">The indentation level for the message line</param>
        private void DisplayCollectionTypesAndSizes(MessageWriter writer, ICollection expected, ICollection actual, int indent)
        {
            string sExpected = MsgUtils.GetTypeRepresentation(expected);
            //if (!(expected is Array))
            //    sExpected += string.Format(" with {0} elements", expected.Count);

            string sActual = MsgUtils.GetTypeRepresentation(actual);

            //if (!(actual is Array))
            //    sActual += string.Format(" with {0} elements", actual.Count);

            if (sExpected == sActual)
            {
                writer.WriteMessageLine(indent, CollectionType_1, sExpected);
            }
            else
            {
                writer.WriteMessageLine(indent, CollectionType_2, sExpected, sActual);
            }
        }
Example #3
0
        private static object GetValueFromCollection(ICollection collection, int index)
        {
            Array array = collection as Array;

            if (array != null && array.Rank > 1)
            {
                return(array.GetValue(MsgUtils.GetArrayIndicesFromCollectionIndex(array, index)));
            }
            if (collection is IList)
            {
                return(((IList)collection)[index]);
            }
            foreach (object item in collection)
            {
                if (--index < 0)
                {
                    return(item);
                }
            }
            return(null);
        }
Example #4
0
        private void DisplayTypesAndSizes(MessageWriter writer, IEnumerable expected, IEnumerable actual, int indent)
        {
            string text = MsgUtils.GetTypeRepresentation(expected);

            if (expected is ICollection && !(expected is Array))
            {
                text += $" with {((ICollection)expected).Count} elements";
            }
            string text2 = MsgUtils.GetTypeRepresentation(actual);

            if (actual is ICollection && !(actual is Array))
            {
                text2 += $" with {((ICollection)actual).Count} elements";
            }
            if (text == text2)
            {
                writer.WriteMessageLine(indent, CollectionType_1, text);
            }
            else
            {
                writer.WriteMessageLine(indent, CollectionType_2, text, text2);
            }
        }
        /// <summary>
        /// Provides standard description of what the constraint tests
        /// based on comparison text.
        /// </summary>
        /// <param name="comparisonText">Describes the comparison being tested, throws <see cref="ArgumentNullException"/>
        /// if null</param>
        /// <exception cref="ArgumentNullException">Is thrown when null passed to a method</exception>
        protected string DefaultDescription(string comparisonText)
        {
            if (comparisonText == null)
            {
                throw new ArgumentNullException(nameof(comparisonText), "Comparison text can not be null");
            }

            StringBuilder sb = new StringBuilder(comparisonText);

            sb.Append(MsgUtils.FormatValue(_expected));

            if (_tolerance != null && !_tolerance.IsUnsetOrDefault)
            {
                sb.Append(" within ");
                sb.Append(MsgUtils.FormatValue(_tolerance.Amount));

                if (_tolerance.Mode == ToleranceMode.Percent)
                {
                    sb.Append(" percent");
                }
            }

            return(sb.ToString());
        }
Example #6
0
        /// <summary>
        /// Displays a single line showing the point in the expected and actual
        /// arrays at which the comparison failed. If the arrays have different
        /// structures or dimensions, both values are shown.
        /// </summary>
        /// <param name="writer">The MessageWriter on which to display</param>
        /// <param name="expected">The expected array</param>
        /// <param name="actual">The actual array</param>
        /// <param name="failurePoint">Index of the failure point in the underlying collections</param>
        /// <param name="indent">The indentation level for the message line</param>
        private void DisplayFailurePoint(MessageWriter writer, IEnumerable expected, IEnumerable actual, NUnitEqualityComparer.FailurePoint failurePoint, int indent)
        {
            Array expectedArray = expected as Array;
            Array actualArray   = actual as Array;

            int expectedRank = expectedArray != null ? expectedArray.Rank : 1;
            int actualRank   = actualArray != null ? actualArray.Rank : 1;

            bool useOneIndex = expectedRank == actualRank;

            if (expectedArray != null && actualArray != null)
            {
                for (int r = 1; r < expectedRank && useOneIndex; r++)
                {
                    if (expectedArray.GetLength(r) != actualArray.GetLength(r))
                    {
                        useOneIndex = false;
                    }
                }
            }

            int[] expectedIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(expected, failurePoint.Position);
            if (useOneIndex)
            {
                writer.WriteMessageLine(indent, ValuesDiffer_1, MsgUtils.GetArrayIndicesAsString(expectedIndices));
            }
            else
            {
                int[] actualIndices = MsgUtils.GetArrayIndicesFromCollectionIndex(actual, failurePoint.Position);
                writer.WriteMessageLine(indent, ValuesDiffer_2,
                                        MsgUtils.GetArrayIndicesAsString(expectedIndices), MsgUtils.GetArrayIndicesAsString(actualIndices));
            }
        }
Example #7
0
 public static void FormatValue_FloatIsWrittenWithTrailingF()
 {
     Assert.That(MsgUtils.FormatValue(0.5f), Is.EqualTo("0.5f"));
 }
Example #8
0
 public static void FormatValue_IntegerIsWrittenAsIs()
 {
     Assert.That(MsgUtils.FormatValue(42), Is.EqualTo("42"));
 }
Example #9
0
        public static void FormatValue_ContextualCustomFormatterInvoked_FactoryArg()
        {
            TestContext.AddFormatter(next => val => (val is CustomFormattableType) ? "custom_formatted" : next(val));

            Assert.That(MsgUtils.FormatValue(new CustomFormattableType()), Is.EqualTo("custom_formatted"));
        }
Example #10
0
 public static void EscapesNullControlChars()
 {
     Assert.That(MsgUtils.EscapeNullCharacters("\0"), Is.EqualTo("\\0"));
 }
Example #11
0
 public static void FormatValue_DateTimeTest()
 {
     Assert.That(MsgUtils.FormatValue(new DateTime(2007, 7, 4, 9, 15, 30, 123)), Is.EqualTo("2007-07-04 09:15:30.123"));
 }
Example #12
0
 public static void FormatValue_DecimalIsWrittenToTwentyNineDigits()
 {
     Assert.That(MsgUtils.FormatValue(12345678901234567890123456789m), Is.EqualTo("12345678901234567890123456789m"));
 }
Example #13
0
 public static void FormatValue_DecimalIsWrittenWithTrailingM()
 {
     Assert.That(MsgUtils.FormatValue(0.5m), Is.EqualTo("0.5m"));
 }
Example #14
0
        public static void FormatValue_DoubleIsWrittenToSeventeenDigits()
        {
            string s = MsgUtils.FormatValue(0.33333333333333333333333333333333333333333333d);

            Assert.That(s.Length, Is.EqualTo(20)); // add 3 for leading 0, decimal and trailing d
        }
Example #15
0
 public static void FormatValue_DoubleIsWrittenWithTrailingD()
 {
     Assert.That(MsgUtils.FormatValue(0.5d), Is.EqualTo("0.5d"));
 }
Example #16
0
 public static void EscapeNullCharInString()
 {
     Assert.That(MsgUtils.EscapeControlChars("\0"), Is.EqualTo("\\0"));
 }
Example #17
0
 public static void DoNotEscapeNonNullControlChars(string input, string expected)
 {
     Assert.That(MsgUtils.EscapeNullCharacters(input), Is.EqualTo(expected));
 }
Example #18
0
 public static void FormatValue_DateTimeOffsetTest()
 {
     Assert.That(MsgUtils.FormatValue(new DateTimeOffset(2007, 7, 4, 9, 15, 30, 123, TimeSpan.FromHours(8))), Is.EqualTo("2007-07-04 09:15:30.123+08:00"));
 }
Example #19
0
 public static void TestClipString(string input, int max, int start, string result)
 {
     System.Console.WriteLine("input=  \"{0}\"", input);
     System.Console.WriteLine("result= \"{0}\"", result);
     Assert.That(MsgUtils.ClipString(input, max, start), Is.EqualTo(result));
 }
Example #20
0
 public static void FormatValue_CharTest(char c, string expected)
 {
     Assert.That(MsgUtils.FormatValue(c), Is.EqualTo(expected));
 }
Example #21
0
        public static void FormatValue_ContextualCustomFormatterInvoked_FormatterArg()
        {
            TestContext.AddFormatter <CustomFormattableType>(val => "custom_formatted_using_type");

            Assert.That(MsgUtils.FormatValue(new CustomFormattableType()), Is.EqualTo("custom_formatted_using_type"));
        }
Example #22
0
        public static void FormatValue_KeyValuePairTest(object key, object value, string expectedResult)
        {
            string s = MsgUtils.FormatValue(new KeyValuePair <object, object>(key, value));

            Assert.That(s, Is.EqualTo(expectedResult));
        }
Example #23
0
 public static void FormatValue_StringIsWrittenWithQuotes()
 {
     Assert.That(MsgUtils.FormatValue("Hello"), Is.EqualTo("\"Hello\""));
 }
Example #24
0
        public static void FormatValue_EmptyValueTupleTest()
        {
            string s = MsgUtils.FormatValue(ValueTuple.Create());

            Assert.That(s, Is.EqualTo("()"));
        }
Example #25
0
        public void SimpleTolerance_Failure(object actual, object expected, object tolerance)
        {
            var ex = Assert.Throws <AssertionException>(
                () => Assert.That(actual, Is.LessThan(expected).Within(tolerance)),
                "Assertion should have failed");

            Assert.That(ex.Message, Contains.Substring("Expected: less than " + MsgUtils.FormatValue(expected) + " within " + MsgUtils.FormatValue(tolerance)));
        }
Example #26
0
        public static void FormatValue_OneElementTupleTest()
        {
            string s = MsgUtils.FormatValue(Tuple.Create("Hello"));

            Assert.That(s, Is.EqualTo("(\"Hello\")"));
        }
Example #27
0
 /// <summary>
 /// Construct a TypeConstraint for a given Type
 /// </summary>
 /// <param name="type">The expected type for the constraint</param>
 /// <param name="descriptionPrefix">Prefix used in forming the constraint description</param>
 protected TypeConstraint(Type type, string descriptionPrefix)
     : base(type)
 {
     this.expectedType = type;
     this.Description  = descriptionPrefix + MsgUtils.FormatValue(expectedType);
 }
Example #28
0
        public static void FormatValue_ThreeElementsTupleTest()
        {
            string s = MsgUtils.FormatValue(Tuple.Create("Hello", 123, 'a'));

            Assert.That(s, Is.EqualTo("(\"Hello\", 123, 'a')"));
        }
        public void PercentTolerance_Failure(object actual, object expected, object tolerance)
        {
            var ex = Assert.Throws <AssertionException>(
                () => Assert.That(actual, Is.LessThanOrEqualTo(expected).Within(tolerance).Percent),
                "Assertion should have failed");

            Assert.That(ex.Message, Contains.Substring("Expected: less than or equal to " + MsgUtils.FormatValue(expected) + " within " + MsgUtils.FormatValue(tolerance) + " percent"));
        }
Example #30
0
 public static void EscapeControlCharsTest(string input, string expected)
 {
     Assert.That(MsgUtils.EscapeControlChars(input), Is.EqualTo(expected));
 }