How to use FormatValue_DirectoryEntryTest method of NUnit.Framework.Constraints.CustomFormattableType class

Best Nunit code snippet using NUnit.Framework.Constraints.CustomFormattableType.FormatValue_DirectoryEntryTest

Run Nunit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

MsgUtilTests.cs

Source: MsgUtilTests.cs Github

copy
1// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2
3using System;
4using System.Collections;
5using System.Collections.Generic;
6
7namespace NUnit.Framework.Constraints
8{
9    /// <summary>
10    /// Summary description for MsgUtilTests.
11    /// </summary>
12    [TestFixture]
13    public static class MsgUtilTests
14    {
15        #region FormatValue
16        class CustomFormattableType { }
17
18        [Test]
19        public static void FormatValue_ContextualCustomFormatterInvoked_FactoryArg()
20        {
21            TestContext.AddFormatter(next => val => (val is CustomFormattableType) ? "custom_formatted" : next(val));
22
23            Assert.That(MsgUtils.FormatValue(new CustomFormattableType()), Is.EqualTo("custom_formatted"));
24        }
25
26        [Test]
27        public static void FormatValue_ContextualCustomFormatterNotInvokedForNull()
28        {
29            // If this factory is actually called with null, it will throw
30            TestContext.AddFormatter(next => val => (val.GetType() == typeof(CustomFormattableType)) ? val.ToString() : next(val));
31
32            Assert.That(MsgUtils.FormatValue(null), Is.EqualTo("null"));
33        }
34
35        [Test]
36        public static void FormatValue_ContextualCustomFormatterInvoked_FormatterArg()
37        {
38            TestContext.AddFormatter<CustomFormattableType>(val => "custom_formatted_using_type");
39
40            Assert.That(MsgUtils.FormatValue(new CustomFormattableType()), Is.EqualTo("custom_formatted_using_type"));
41        }
42
43        [Test]
44        public static void FormatValue_IntegerIsWrittenAsIs()
45        {
46            Assert.That(MsgUtils.FormatValue(42), Is.EqualTo("42"));
47        }
48
49        [Test]
50        public static void FormatValue_StringIsWrittenWithQuotes()
51        {
52            Assert.That(MsgUtils.FormatValue("Hello"), Is.EqualTo("\"Hello\""));
53        }
54
55        // This test currently fails because control character replacement is
56        // done at a higher level...
57        // TODO: See if we should do it at a lower level
58        //            [Test]
59        //            public static void ControlCharactersInStringsAreEscaped()
60        //            {
61        //                WriteValue("Best Wishes,\r\n\tCharlie\r\n");
62        //                Assert.That(writer.ToString(), Is.Is.EqualTo("\"Best Wishes,\\r\\n\\tCharlie\\r\\n\""));
63        //            }
64
65        [Test]
66        public static void FormatValue_FloatIsWrittenWithTrailingF()
67        {
68            Assert.That(MsgUtils.FormatValue(0.5f), Is.EqualTo("0.5f"));
69        }
70
71        [Test]
72        public static void FormatValue_FloatIsWrittenToNineDigits()
73        {
74            string s = MsgUtils.FormatValue(0.33333333333333f);
75            int digits = s.Length - 3;   // 0.dddddddddf
76            Assert.That(digits, Is.EqualTo(9));
77        }
78
79        [Test]
80        public static void FormatValue_DoubleIsWrittenWithTrailingD()
81        {
82            Assert.That(MsgUtils.FormatValue(0.5d), Is.EqualTo("0.5d"));
83        }
84
85        [Test]
86        public static void FormatValue_DoubleIsWrittenToSeventeenDigits()
87        {
88            string s = MsgUtils.FormatValue(0.33333333333333333333333333333333333333333333d);
89            Assert.That(s.Length, Is.EqualTo(20)); // add 3 for leading 0, decimal and trailing d
90        }
91
92        [Test]
93        public static void FormatValue_DecimalIsWrittenWithTrailingM()
94        {
95            Assert.That(MsgUtils.FormatValue(0.5m), Is.EqualTo("0.5m"));
96        }
97
98        [Test]
99        public static void FormatValue_DecimalIsWrittenToTwentyNineDigits()
100        {
101            Assert.That(MsgUtils.FormatValue(12345678901234567890123456789m), Is.EqualTo("12345678901234567890123456789m"));
102        }
103
104        [Test]
105        public static void FormatValue_DateTimeTest()
106        {
107            Assert.That(MsgUtils.FormatValue(new DateTime(2007, 7, 4, 9, 15, 30, 123)), Is.EqualTo("2007-07-04 09:15:30.123"));
108        }
109
110        [Test]
111        public static void FormatValue_DateTimeOffsetTest()
112        {
113            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"));
114        }
115
116        [TestCase('a', "'a'")]
117        [TestCase('h', "'h'")]
118        [TestCase('z', "'z'")]
119        public static void FormatValue_CharTest(char c, string expected)
120        {
121            Assert.That(MsgUtils.FormatValue(c), Is.EqualTo(expected));
122        }
123
124        [TestCase(null, null, "[null, null]")]
125        [TestCase(null, "Second", "[null, \"Second\"]")]
126        [TestCase("First", null, "[\"First\", null]")]
127        [TestCase("First", "Second", "[\"First\", \"Second\"]")]
128        [TestCase(123, 'h', "[123, 'h']")]
129        public static void FormatValue_KeyValuePairTest(object key, object value, string expectedResult)
130        {
131            string s = MsgUtils.FormatValue(new KeyValuePair<object, object>(key, value));
132            Assert.That(s, Is.EqualTo(expectedResult));
133        }
134
135        [TestCase(null, null, "[null, null]")]
136        [TestCase(null, "Second", "[null, \"Second\"]")]
137        [TestCase("First", null, "[\"First\", null]")]
138        [TestCase("First", "Second", "[\"First\", \"Second\"]")]
139        [TestCase(123, 'h', "[123, 'h']")]
140        public static void FormatValue_DirectoryEntryTest(object key, object value, string expectedResult)
141        {
142            string s = MsgUtils.FormatValue(new DictionaryEntry(key, value));
143            Assert.That(s, Is.EqualTo(expectedResult));
144        }
145
146        [Test]
147        public static void FormatValue_EmptyValueTupleTest()
148        {
149            string s = MsgUtils.FormatValue(ValueTuple.Create());
150            Assert.That(s, Is.EqualTo("()"));
151        }
152
153        [Test]
154        public static void FormatValue_OneElementValueTupleTest()
155        {
156            string s = MsgUtils.FormatValue(ValueTuple.Create("Hello"));
157            Assert.That(s, Is.EqualTo("(\"Hello\")"));
158        }
159
160        [Test]
161        public static void FormatValue_TwoElementsValueTupleTest()
162        {
163            string s = MsgUtils.FormatValue(("Hello", 123));
164            Assert.That(s, Is.EqualTo("(\"Hello\", 123)"));
165        }
166
167        [Test]
168        public static void FormatValue_ThreeElementsValueTupleTest()
169        {
170            string s = MsgUtils.FormatValue(("Hello", 123, 'a'));
171            Assert.That(s, Is.EqualTo("(\"Hello\", 123, 'a')"));
172        }
173
174        [Test]
175        public static void FormatValue_EightElementsValueTupleTest()
176        {
177            var tuple = (1, 2, 3, 4, 5, 6, 7, 8);
178
179            string s = MsgUtils.FormatValue(tuple);
180            Assert.That(s, Is.EqualTo("(1, 2, 3, 4, 5, 6, 7, 8)"));
181        }
182
183        [Test]
184        public static void FormatValue_EightElementsValueTupleNestedTest()
185        {
186            var tuple = (1, 2, 3, 4, 5, 6, 7, (8, "9"));
187
188            string s = MsgUtils.FormatValue(tuple);
189            Assert.That(s, Is.EqualTo("(1, 2, 3, 4, 5, 6, 7, (8, \"9\"))"));
190        }
191
192        [Test]
193        public static void FormatValue_FifteenElementsValueTupleTest()
194        {
195            var tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, "12", 13, 14, "15");
196
197            string s = MsgUtils.FormatValue(tuple);
198            Assert.That(s, Is.EqualTo("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, \"12\", 13, 14, \"15\")"));
199        }
200
201        [Test]
202        public static void FormatValue_OneElementTupleTest()
203        {
204            string s = MsgUtils.FormatValue(Tuple.Create("Hello"));
205            Assert.That(s, Is.EqualTo("(\"Hello\")"));
206        }
207
208        [Test]
209        public static void FormatValue_TwoElementsTupleTest()
210        {
211            string s = MsgUtils.FormatValue(Tuple.Create("Hello", 123));
212            Assert.That(s, Is.EqualTo("(\"Hello\", 123)"));
213        }
214
215        [Test]
216        public static void FormatValue_ThreeElementsTupleTest()
217        {
218            string s = MsgUtils.FormatValue(Tuple.Create("Hello", 123, 'a'));
219            Assert.That(s, Is.EqualTo("(\"Hello\", 123, 'a')"));
220        }
221
222        [Test]
223        public static void FormatValue_EightElementsTupleTest()
224        {
225            var tuple = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
226            string s = MsgUtils.FormatValue(tuple);
227            Assert.That(s, Is.EqualTo("(1, 2, 3, 4, 5, 6, 7, 8)"));
228        }
229
230        [Test]
231        public static void FormatValue_EightElementsTupleNestedTest()
232        {
233            var tuple = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, "9"));
234            string s = MsgUtils.FormatValue(tuple);
235            Assert.That(s, Is.EqualTo("(1, 2, 3, 4, 5, 6, 7, (8, \"9\"))"));
236        }
237
238        [Test]
239        public static void FormatValue_FifteenElementsTupleTest()
240        {
241            var tupleLastElements = Tuple.Create(8, 9, 10, 11, "12", 13, 14, "15");
242            var tuple = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int, int, string, int, int, Tuple<string>>>
243                (1, 2, 3, 4, 5, 6, 7, tupleLastElements);
244
245            string s = MsgUtils.FormatValue(tuple);
246            Assert.That(s, Is.EqualTo("(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, \"12\", 13, 14, \"15\")"));
247        }
248
249        #endregion
250
251        #region EscapeControlChars
252
253        [TestCase("\n", "\\n")]
254        [TestCase("\n\n", "\\n\\n")]
255        [TestCase("\n\n\n", "\\n\\n\\n")]
256        [TestCase("\r", "\\r")]
257        [TestCase("\r\r", "\\r\\r")]
258        [TestCase("\r\r\r", "\\r\\r\\r")]
259        [TestCase("\r\n", "\\r\\n")]
260        [TestCase("\n\r", "\\n\\r")]
261        [TestCase("This is a\rtest message", "This is a\\rtest message")]
262        [TestCase("", "")]
263        [TestCase(null, null)]
264        [TestCase("\t", "\\t")]
265        [TestCase("\t\n", "\\t\\n")]
266        [TestCase("\\r\\n", "\\\\r\\\\n")]
267        // TODO: Figure out why this fails in Mono
268        //[TestCase("\0", "\\0")]
269        [TestCase("\a", "\\a")]
270        [TestCase("\b", "\\b")]
271        [TestCase("\f", "\\f")]
272        [TestCase("\v", "\\v")]
273        [TestCase("\x0085", "\\x0085", Description = "Next line character")]
274        [TestCase("\x2028", "\\x2028", Description = "Line separator character")]
275        [TestCase("\x2029", "\\x2029", Description = "Paragraph separator character")]
276        public static void EscapeControlCharsTest(string input, string expected)
277        {
278            Assert.That( MsgUtils.EscapeControlChars(input), Is.EqualTo(expected) );
279        }
280
281        [Test]
282        public static void EscapeNullCharInString()
283        {
284            Assert.That(MsgUtils.EscapeControlChars("\0"), Is.EqualTo("\\0"));
285        }
286
287        #endregion
288        #region EscapeNullChars
289        [TestCase("\n", "\n")]
290        [TestCase("\r", "\r")]
291        [TestCase("\r\n\r", "\r\n\r")]
292        [TestCase("\f", "\f")]
293        [TestCase("\b", "\b")]
294        public static void DoNotEscapeNonNullControlChars(string input, string expected)
295        {
296            Assert.That(MsgUtils.EscapeNullCharacters(input), Is.EqualTo(expected));
297        }
298
299        [Test]
300        public static void EscapesNullControlChars()
301        {
302            Assert.That(MsgUtils.EscapeNullCharacters("\0"), Is.EqualTo("\\0"));
303        }
304
305        #endregion
306        #region ClipString
307
308        private const string s52 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
309
310        [TestCase(s52, 52, 0, s52, TestName="NoClippingNeeded")]
311        [TestCase(s52, 29, 0, "abcdefghijklmnopqrstuvwxyz...", TestName="ClipAtEnd")]
312        [TestCase(s52, 29, 26, "...ABCDEFGHIJKLMNOPQRSTUVWXYZ", TestName="ClipAtStart")]
313        [TestCase(s52, 28, 26, "...ABCDEFGHIJKLMNOPQRSTUV...", TestName="ClipAtStartAndEnd")]
314        public static void TestClipString(string input, int max, int start, string result)
315        {
316            System.Console.WriteLine("input=  \"{0}\"", input);
317            System.Console.WriteLine("result= \"{0}\"", result);
318            Assert.That(MsgUtils.ClipString(input, max, start), Is.EqualTo(result));
319        }
320
321#endregion
322
323#region ClipExpectedAndActual
324
325        [Test]
326        public static void ClipExpectedAndActual_StringsFitInLine()
327        {
328            string eClip = s52;
329            string aClip = "abcde";
330            MsgUtils.ClipExpectedAndActual(ref eClip, ref aClip, 52, 5);
331            Assert.That(eClip, Is.EqualTo(s52));
332            Assert.That(aClip, Is.EqualTo("abcde"));
333
334            eClip = s52;
335            aClip = "abcdefghijklmno?qrstuvwxyz";
336            MsgUtils.ClipExpectedAndActual(ref eClip, ref aClip, 52, 15);
337            Assert.That(eClip, Is.EqualTo(s52));
338            Assert.That(aClip, Is.EqualTo("abcdefghijklmno?qrstuvwxyz"));
339        }
340
341        [Test]
342        public static void ClipExpectedAndActual_StringTailsFitInLine()
343        {
344            string s1 = s52;
345            string s2 = s52.Replace('Z', '?');
346            MsgUtils.ClipExpectedAndActual(ref s1, ref s2, 29, 51);
347            Assert.That(s1, Is.EqualTo("...ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
348        }
349
350        [Test]
351        public static void ClipExpectedAndActual_StringsDoNotFitInLine()
352        {
353            string s1 = s52;
354            string s2 = "abcdefghij";
355            MsgUtils.ClipExpectedAndActual(ref s1, ref s2, 29, 10);
356            Assert.That(s1, Is.EqualTo("abcdefghijklmnopqrstuvwxyz..."));
357            Assert.That(s2, Is.EqualTo("abcdefghij"));
358
359            s1 = s52;
360            s2 = "abcdefghijklmno?qrstuvwxyz";
361            MsgUtils.ClipExpectedAndActual(ref s1, ref s2, 25, 15);
362            Assert.That(s1, Is.EqualTo("...efghijklmnopqrstuvw..."));
363            Assert.That(s2, Is.EqualTo("...efghijklmno?qrstuvwxyz"));
364        }
365
366#endregion
367    }
368}
369
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger FormatValue_DirectoryEntryTest code on LambdaTest Cloud Grid

Execute automation tests with FormatValue_DirectoryEntryTest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)