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

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

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// ***********************************************************************
2// Copyright (c) 2012 Charlie Poole
3//
4// Permission is hereby granted, free of charge, to any person obtaining
5// a copy of this software and associated documentation files (the
6// "Software"), to deal in the Software without restriction, including
7// without limitation the rights to use, copy, modify, merge, publish,
8// distribute, sublicense, and/or sell copies of the Software, and to
9// permit persons to whom the Software is furnished to do so, subject to
10// the following conditions:
11// 
12// The above copyright notice and this permission notice shall be
13// included in all copies or substantial portions of the Software.
14// 
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22// ***********************************************************************
23
24using System;
25
26namespace NUnit.Framework.Constraints
27{
28    /// <summary>
29    /// Summary description for MsgUtilTests.
30    /// </summary>
31    [TestFixture]
32    public static class MsgUtilTests
33    {
34        #region FormatValue
35        class CustomFormattableType { }
36
37        [Test]
38        public static void FormatValue_ContextualCustomFormatterInvoked_FactoryArg()
39        {
40            TestContext.AddFormatter(next => val => (val is CustomFormattableType) ? "custom_formatted" : next(val));
41
42            Assert.That(MsgUtils.FormatValue(new CustomFormattableType()), Is.EqualTo("custom_formatted"));
43        }
44
45        [Test]
46        public static void FormatValue_ContextualCustomFormatterNotInvokedForNull()
47        {
48            // If this factory is actually called with null, it will throw
49            TestContext.AddFormatter(next => val => (val.GetType() == typeof(CustomFormattableType)) ? val.ToString() : next(val));
50
51            Assert.That(MsgUtils.FormatValue(null), Is.EqualTo("null"));
52        }
53
54        [Test]
55        public static void FormatValue_ContextualCustomFormatterInvoked_FormatterArg()
56        {
57            TestContext.AddFormatter<CustomFormattableType>(val => "custom_formatted_using_type");
58
59            Assert.That(MsgUtils.FormatValue(new CustomFormattableType()), Is.EqualTo("custom_formatted_using_type"));
60        }
61
62        [Test]
63        public static void FormatValue_IntegerIsWrittenAsIs()
64        {
65            Assert.That(MsgUtils.FormatValue(42), Is.EqualTo("42"));
66        }
67
68        [Test]
69        public static void FormatValue_StringIsWrittenWithQuotes()
70        {
71            Assert.That(MsgUtils.FormatValue("Hello"), Is.EqualTo("\"Hello\""));
72        }
73
74        // This test currently fails because control character replacement is
75        // done at a higher level...
76        // TODO: See if we should do it at a lower level
77        //            [Test]
78        //            public static void ControlCharactersInStringsAreEscaped()
79        //            {
80        //                WriteValue("Best Wishes,\r\n\tCharlie\r\n");
81        //                Assert.That(writer.ToString(), Is.Is.EqualTo("\"Best Wishes,\\r\\n\\tCharlie\\r\\n\""));
82        //            }
83
84        [Test]
85        public static void FormatValue_FloatIsWrittenWithTrailingF()
86        {
87            Assert.That(MsgUtils.FormatValue(0.5f), Is.EqualTo("0.5f"));
88        }
89
90        [Test]
91        public static void FormatValue_FloatIsWrittenToNineDigits()
92        {
93            string s = MsgUtils.FormatValue(0.33333333333333f);
94            int digits = s.Length - 3;   // 0.dddddddddf
95            Assert.That(digits, Is.EqualTo(9));
96        }
97
98        [Test]
99        public static void FormatValue_DoubleIsWrittenWithTrailingD()
100        {
101            Assert.That(MsgUtils.FormatValue(0.5d), Is.EqualTo("0.5d"));
102        }
103
104        [Test]
105        public static void FormatValue_DoubleIsWrittenToSeventeenDigits()
106        {
107            string s = MsgUtils.FormatValue(0.33333333333333333333333333333333333333333333d);
108            Assert.That(s.Length, Is.EqualTo(20)); // add 3 for leading 0, decimal and trailing d
109        }
110
111        [Test]
112        public static void FormatValue_DecimalIsWrittenWithTrailingM()
113        {
114            Assert.That(MsgUtils.FormatValue(0.5m), Is.EqualTo("0.5m"));
115        }
116
117        [Test]
118        public static void FormatValue_DecimalIsWrittenToTwentyNineDigits()
119        {
120            Assert.That(MsgUtils.FormatValue(12345678901234567890123456789m), Is.EqualTo("12345678901234567890123456789m"));
121        }
122
123        [Test]
124        public static void FormatValue_DateTimeTest()
125        {
126            Assert.That(MsgUtils.FormatValue(new DateTime(2007, 7, 4, 9, 15, 30, 123)), Is.EqualTo("2007-07-04 09:15:30.123"));
127        }
128
129#if !NETCF
130		[Test]
131        public static void FormatValue_DateTimeOffsetTest()
132        {
133            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"));
134        }
135#endif
136
137#endregion
138
139		#region EscapeControlChars
140
141		[TestCase ("\n", "\\n")]
142        [TestCase("\n\n", "\\n\\n")]
143        [TestCase("\n\n\n", "\\n\\n\\n")]
144        [TestCase("\r", "\\r")]
145        [TestCase("\r\r", "\\r\\r")]
146        [TestCase("\r\r\r", "\\r\\r\\r")]
147        [TestCase("\r\n", "\\r\\n")]
148        [TestCase("\n\r", "\\n\\r")]
149        [TestCase("This is a\rtest message", "This is a\\rtest message")]
150        [TestCase("", "")]
151        [TestCase(null, null)]
152        [TestCase("\t", "\\t")]
153        [TestCase("\t\n", "\\t\\n")]
154        [TestCase("\\r\\n", "\\\\r\\\\n")]
155        // TODO: Figure out why this fails in Mono
156        //[TestCase("\0", "\\0")]
157        [TestCase("\a", "\\a")]
158        [TestCase("\b", "\\b")]
159        [TestCase("\f", "\\f")]
160        [TestCase("\v", "\\v")]
161        [TestCase("\x0085", "\\x0085", Description = "Next line character")]
162        [TestCase("\x2028", "\\x2028", Description = "Line separator character")]
163        [TestCase("\x2029", "\\x2029", Description = "Paragraph separator character")]
164        public static void EscapeControlCharsTest(string input, string expected)
165        {
166            Assert.That( MsgUtils.EscapeControlChars(input), Is.EqualTo(expected) );
167        }
168
169        [Test]
170        public static void EscapeNullCharInString()
171        {
172            Assert.That(MsgUtils.EscapeControlChars("\0"), Is.EqualTo("\\0"));
173        }
174
175        #endregion
176        #region EscapeNullChars
177        [TestCase("\n", "\n")]
178        [TestCase("\r", "\r")]
179        [TestCase("\r\n\r", "\r\n\r")]
180        [TestCase("\f", "\f")]
181        [TestCase("\b", "\b")]
182        public static void DoNotEscapeNonNullControlChars(string input, string expected)
183        {
184            Assert.That(MsgUtils.EscapeNullCharacters(input), Is.EqualTo(expected));
185        }
186
187        [Test]
188        public static void EscapesNullControlChars()
189        {
190            Assert.That(MsgUtils.EscapeNullCharacters("\0"), Is.EqualTo("\\0"));
191        }
192
193        #endregion
194        #region ClipString
195
196        private const string s52 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
197
198        [TestCase(s52, 52, 0, s52, TestName="NoClippingNeeded")]
199        [TestCase(s52, 29, 0, "abcdefghijklmnopqrstuvwxyz...", TestName="ClipAtEnd")]
200        [TestCase(s52, 29, 26, "...ABCDEFGHIJKLMNOPQRSTUVWXYZ", TestName="ClipAtStart")]
201        [TestCase(s52, 28, 26, "...ABCDEFGHIJKLMNOPQRSTUV...", TestName="ClipAtStartAndEnd")]
202        public static void TestClipString(string input, int max, int start, string result)
203        {
204#if !PORTABLE
205            System.Console.WriteLine("input=  \"{0}\"", input);
206            System.Console.WriteLine("result= \"{0}\"", result);
207#endif
208            Assert.That(MsgUtils.ClipString(input, max, start), Is.EqualTo(result));
209        }
210
211#endregion
212
213        //[TestCase('\0')]
214        //[TestCase('\r')]
215        //public void CharacterArgumentTest(char c)
216        //{
217        //}
218
219#region ClipExpectedAndActual
220
221        [Test]
222        public static void ClipExpectedAndActual_StringsFitInLine()
223        {
224            string eClip = s52;
225            string aClip = "abcde";
226            MsgUtils.ClipExpectedAndActual(ref eClip, ref aClip, 52, 5);
227            Assert.That(eClip, Is.EqualTo(s52));
228            Assert.That(aClip, Is.EqualTo("abcde"));
229
230            eClip = s52;
231            aClip = "abcdefghijklmno?qrstuvwxyz";
232            MsgUtils.ClipExpectedAndActual(ref eClip, ref aClip, 52, 15);
233            Assert.That(eClip, Is.EqualTo(s52));
234            Assert.That(aClip, Is.EqualTo("abcdefghijklmno?qrstuvwxyz"));
235        }
236
237        [Test]
238        public static void ClipExpectedAndActual_StringTailsFitInLine()
239        {
240            string s1 = s52;
241            string s2 = s52.Replace('Z', '?');
242            MsgUtils.ClipExpectedAndActual(ref s1, ref s2, 29, 51);
243            Assert.That(s1, Is.EqualTo("...ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
244        }
245
246        [Test]
247        public static void ClipExpectedAndActual_StringsDoNotFitInLine()
248        {
249            string s1 = s52;
250            string s2 = "abcdefghij";
251            MsgUtils.ClipExpectedAndActual(ref s1, ref s2, 29, 10);
252            Assert.That(s1, Is.EqualTo("abcdefghijklmnopqrstuvwxyz..."));
253            Assert.That(s2, Is.EqualTo("abcdefghij"));
254
255            s1 = s52;
256            s2 = "abcdefghijklmno?qrstuvwxyz";
257            MsgUtils.ClipExpectedAndActual(ref s1, ref s2, 25, 15);
258            Assert.That(s1, Is.EqualTo("...efghijklmnopqrstuvw..."));
259            Assert.That(s2, Is.EqualTo("...efghijklmno?qrstuvwxyz"));
260        }
261
262#endregion
263    }
264}
265
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_DateTimeTest code on LambdaTest Cloud Grid

Execute automation tests with FormatValue_DateTimeTest 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)