How to use TestShortenedNameDifference method of B.Dummy class

Best Nunit code snippet using B.Dummy.TestShortenedNameDifference

Run Nunit automation tests on LambdaTest cloud grid

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

TypeNameDifferenceTests.cs

Source: TypeNameDifferenceTests.cs Github

copy
1// ***********************************************************************
2// Copyright (c) 2017 Charlie Poole, Rob Prouse
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;
25using System.Collections.Generic;
26
27namespace NUnit.Framework.Internal
28{
29    #region Mock types
30
31    namespace DifferingNamespace1
32    {
33        class Dummy
34        {
35            internal readonly int value;
36
37            public Dummy(int value)
38            {
39                this.value = value;
40            }
41
42            public override string ToString()
43            {
44                return "Dummy " + value;
45            }
46        }
47
48        class DummyGeneric<T>
49        {
50            public DummyGeneric(T obj)
51            { }
52        }
53    }
54
55    namespace DifferingNamespace2
56    {
57        class Dummy
58        {
59            internal readonly int value;
60
61            public Dummy(int value)
62            {
63                this.value = value;
64            }
65
66            public override string ToString()
67            {
68                return "Dummy " + value;
69            }
70        }
71    }
72
73    namespace A
74    {
75        class GenA<T>
76        { }
77
78        class GenB<T>
79        { }
80
81        class GenC<T, R>
82        { }
83
84        namespace B
85        {
86            class GenX<T>
87            { }
88
89            class GenY<T>
90            { }
91        }
92    }
93
94    namespace B
95    {
96        class GenA<T>
97        { }
98
99        class GenB<T>
100        { }
101
102        class GenC<T, R>
103        { }
104
105        namespace B
106        {
107            class GenX<T>
108            { }
109
110            class GenY<T>
111            { }
112        }
113    }
114
115    #endregion
116
117    public class TypeNameDifferenceTests
118    {
119        #region Mock types
120
121        class Dummy
122        {
123            internal readonly int value;
124
125            public Dummy(int value)
126            {
127                this.value = value;
128            }
129
130            public override string ToString()
131            {
132                return "Dummy " + value;
133            }
134        }
135
136        class Dummy1
137        {
138            internal readonly int value;
139
140            public Dummy1(int value)
141            {
142                this.value = value;
143            }
144
145            public override string ToString()
146            {
147                return "Dummy " + value;
148            }
149        }
150
151        class DummyGenericClass<T>
152        {
153            private object _obj;
154            public DummyGenericClass(object obj)
155            {
156                _obj = obj;
157            }
158
159            public override string ToString()
160            {
161                return _obj.ToString();
162            }
163        }
164
165        #endregion
166
167        TypeNameDifferenceResolver _differenceGetter;
168
169        [SetUp]
170        public void TestSetup()
171        {
172            _differenceGetter = new TypeNameDifferenceResolver();
173        }
174
175        private void TestShortenedNameDifference(object objA, object objB, string expectedA, string expectedB)
176        {
177            string actualA, actualB;
178
179            _differenceGetter.ResolveTypeNameDifference(
180                 objA, objB, out actualA, out actualB);
181
182            Assert.That(actualA, Is.EqualTo(expectedA));
183            Assert.That(actualB, Is.EqualTo(expectedB));
184        }
185
186        [Test]
187        public void TestResolveTypeNameDifferenceNonGenericDifferingTypes()
188        {
189            TestShortenedNameDifference(
190                new Dummy(1),
191                new Dummy1(1),
192                "TypeNameDifferenceTests+Dummy",
193                "TypeNameDifferenceTests+Dummy1");
194        }
195
196        [Test]
197        public void TestResolveTypeNameDifferenceNonGenericNonDifferingTypes()
198        {
199            TestShortenedNameDifference(
200                new Dummy(1),
201                new Dummy(1),
202                "TypeNameDifferenceTests+Dummy",
203                "TypeNameDifferenceTests+Dummy");
204        }
205
206        [Test]
207        public void TestResolveTypeNameDifferenceNonGenericNonDifferingTypesSingularDiffNamespace()
208        {
209            TestShortenedNameDifference(
210                new DifferingNamespace1.Dummy(1),
211                new Dummy(1),
212                "Dummy",
213                "TypeNameDifferenceTests+Dummy");
214        }
215
216        [Test]
217        public void TestResolveTypeNameDifferenceNonGenericNonDifferingTypesBothDiffNamespace()
218        {
219            TestShortenedNameDifference(
220                new DifferingNamespace1.Dummy(1),
221                new DifferingNamespace2.Dummy(1),
222                "DifferingNamespace1.Dummy",
223                "DifferingNamespace2.Dummy");
224        }
225
226        [Test]
227        public void TestResolveTypeNameDifferenceGeneric()
228        {
229            TestShortenedNameDifference(
230                new DummyGenericClass<Dummy1>(new Dummy(1)),
231                new DummyGenericClass<Dummy>(new Dummy(1)),
232                "TypeNameDifferenceTests+DummyGenericClass`1[TypeNameDifferenceTests+Dummy1]",
233                "TypeNameDifferenceTests+DummyGenericClass`1[TypeNameDifferenceTests+Dummy]");
234        }
235
236        [Test]
237        public void TestResolveTypeNameDifferenceGenericDifferingNamespaces()
238        {
239            TestShortenedNameDifference(
240                new DummyGenericClass<Dummy>(new Dummy(1)),
241                new DummyGenericClass<DifferingNamespace1.Dummy>(new DifferingNamespace1.Dummy(1)),
242                "TypeNameDifferenceTests+DummyGenericClass`1[TypeNameDifferenceTests+Dummy]",
243                "TypeNameDifferenceTests+DummyGenericClass`1[Dummy]");
244
245            TestShortenedNameDifference(
246                new DummyGenericClass<DifferingNamespace1.Dummy>(new DifferingNamespace1.Dummy(1)),
247                new DummyGenericClass<Dummy>(new Dummy(1)),
248                "TypeNameDifferenceTests+DummyGenericClass`1[Dummy]",
249                "TypeNameDifferenceTests+DummyGenericClass`1[TypeNameDifferenceTests+Dummy]");
250
251            TestShortenedNameDifference(
252                new DummyGenericClass<DifferingNamespace1.Dummy>(new DifferingNamespace1.Dummy(1)),
253                new DummyGenericClass<DifferingNamespace2.Dummy>(new DifferingNamespace2.Dummy(1)),
254                "TypeNameDifferenceTests+DummyGenericClass`1[DifferingNamespace1.Dummy]",
255                "TypeNameDifferenceTests+DummyGenericClass`1[DifferingNamespace2.Dummy]");
256        }
257
258        [Test]
259        public void TestResolveTypeNameDifferenceGenericDifferentAmountGenericParams()
260        {
261            TestShortenedNameDifference(
262                new DummyGenericClass<Dummy>(new Dummy(1)),
263                new KeyValuePair<int, string>(1, ""),
264                "TypeNameDifferenceTests+DummyGenericClass`1[TypeNameDifferenceTests+Dummy]",
265                "KeyValuePair`2[Int32,String]");
266
267            TestShortenedNameDifference(
268                new KeyValuePair<int, string>(1, ""),
269                new DummyGenericClass<Dummy>(new Dummy(1)),
270                "KeyValuePair`2[Int32,String]",
271                "TypeNameDifferenceTests+DummyGenericClass`1[TypeNameDifferenceTests+Dummy]");
272        }
273
274        [Test]
275        public void TestResolveNameDifferenceOneIsGenericOtherIsNot()
276        {
277            TestShortenedNameDifference(
278                new DummyGenericClass<Dummy>(new Dummy(1)),
279                new Dummy(1),
280                "DummyGenericClass`1[Dummy]",
281                "Dummy");
282
283            TestShortenedNameDifference(
284                new Dummy(1),
285                new DummyGenericClass<Dummy>(new Dummy(1)),
286                "Dummy",
287                "DummyGenericClass`1[Dummy]");
288
289            TestShortenedNameDifference(
290                new KeyValuePair<string, int>("str", 0),
291                new Dummy(1),
292                "KeyValuePair`2[String,Int32]",
293                "Dummy");
294
295            TestShortenedNameDifference(
296                new Dummy(1),
297                new KeyValuePair<string, int>("str", 0),
298                "Dummy",
299                "KeyValuePair`2[String,Int32]");
300
301            TestShortenedNameDifference(
302                new Dummy(1),
303                new A.GenA<B.GenA<B.GenC<string, int>>>(),
304                "Dummy",
305                "GenA`1[GenA`1[GenC`2[String,Int32]]]");
306
307            TestShortenedNameDifference(
308                new A.GenA<B.GenA<B.GenC<string, int>>>(),
309                new Dummy(1),
310                "GenA`1[GenA`1[GenC`2[String,Int32]]]",
311                "Dummy");
312        }
313
314        [Test]
315        public void TestNestedGenerics()
316        {
317            TestShortenedNameDifference(
318                new DifferingNamespace1.DummyGeneric<List<string>>(new List<string>()),
319                new DifferingNamespace1.DummyGeneric<IEnumerable<string>>(new List<string>()),
320                "DummyGeneric`1[List`1[String]]",
321                "DummyGeneric`1[IEnumerable`1[String]]");
322
323            TestShortenedNameDifference(
324                new DifferingNamespace1.DummyGeneric<IEnumerable<string>>(new List<string>()),
325                new DifferingNamespace1.DummyGeneric<List<string>>(new List<string>()),
326                "DummyGeneric`1[IEnumerable`1[String]]",
327                "DummyGeneric`1[List`1[String]]");
328
329            TestShortenedNameDifference(
330                new DifferingNamespace1.DummyGeneric<KeyValuePair<DifferingNamespace1.Dummy, DifferingNamespace2.Dummy>>(new KeyValuePair<DifferingNamespace1.Dummy, DifferingNamespace2.Dummy>()),
331                new DifferingNamespace1.DummyGeneric<KeyValuePair<DifferingNamespace2.Dummy, DifferingNamespace1.Dummy>>(new KeyValuePair<DifferingNamespace2.Dummy, DifferingNamespace1.Dummy>()),
332                "DummyGeneric`1[KeyValuePair`2[DifferingNamespace1.Dummy,DifferingNamespace2.Dummy]]",
333                "DummyGeneric`1[KeyValuePair`2[DifferingNamespace2.Dummy,DifferingNamespace1.Dummy]]");
334
335            TestShortenedNameDifference(
336                new DifferingNamespace1.DummyGeneric<KeyValuePair<DifferingNamespace2.Dummy, DifferingNamespace1.Dummy>>(new KeyValuePair<DifferingNamespace2.Dummy, DifferingNamespace1.Dummy>()),
337                new DifferingNamespace1.DummyGeneric<KeyValuePair<DifferingNamespace1.Dummy, DifferingNamespace2.Dummy>>(new KeyValuePair<DifferingNamespace1.Dummy, DifferingNamespace2.Dummy>()),
338                "DummyGeneric`1[KeyValuePair`2[DifferingNamespace2.Dummy,DifferingNamespace1.Dummy]]",
339                "DummyGeneric`1[KeyValuePair`2[DifferingNamespace1.Dummy,DifferingNamespace2.Dummy]]");
340
341            TestShortenedNameDifference(
342                new A.GenA<A.B.GenX<int>>(),
343                new B.GenA<A.B.GenX<short>>(),
344                "A.GenA`1[GenX`1[Int32]]",
345                "B.GenA`1[GenX`1[Int16]]");
346
347            TestShortenedNameDifference(
348                new B.GenA<A.B.GenX<short>>(), 
349                new A.GenA<A.B.GenX<int>>(),
350                "B.GenA`1[GenX`1[Int16]]",
351                "A.GenA`1[GenX`1[Int32]]");
352
353            TestShortenedNameDifference(
354                new A.GenC<int, string>(),
355                new B.GenC<int, A.GenA<string>>(),
356                "A.GenC`2[Int32,String]",
357                "B.GenC`2[Int32,GenA`1[String]]");
358
359            TestShortenedNameDifference(
360                new A.GenA<A.GenC<string, int>>(),
361                new B.GenC<A.GenA<List<int>>, B.GenC<string, int>>(),
362                "GenA`1[GenC`2[String,Int32]]",
363                "GenC`2[GenA`1[List`1[Int32]],GenC`2[String,Int32]]");
364
365            TestShortenedNameDifference(
366               new B.GenC<A.GenA<List<int>>, B.GenC<string, int>>(),
367               new A.GenA<A.GenC<string, int>>(),
368               "GenC`2[GenA`1[List`1[Int32]],GenC`2[String,Int32]]",
369               "GenA`1[GenC`2[String,Int32]]");
370
371            TestShortenedNameDifference(
372               new B.GenC<A.GenA<List<int>>, B.GenC<string, B.GenC<string, int>>>(),
373               new A.GenA<B.GenC<string, B.GenC<string,int>>>(),
374               "GenC`2[GenA`1[List`1[Int32]],GenC`2[String,GenC`2[String,Int32]]]",
375               "GenA`1[GenC`2[String,GenC`2[String,Int32]]]");
376
377            TestShortenedNameDifference(
378               new A.GenA<B.GenC<string, B.GenC<string, int>>>(),
379               new B.GenC<A.GenA<List<int>>, B.GenC<string, B.GenC<string, int>>>(),
380               "GenA`1[GenC`2[String,GenC`2[String,Int32]]]",
381               "GenC`2[GenA`1[List`1[Int32]],GenC`2[String,GenC`2[String,Int32]]]");
382        }
383
384        [Test]
385        public void TestIsObjectInstanceGeneric()
386        {
387            var notGeneric = new DifferingNamespace1.Dummy(1);
388
389            Assert.False(_differenceGetter.IsTypeGeneric(notGeneric.GetType()));
390
391            var generic = new DifferingNamespace1.DummyGeneric<DifferingNamespace1.Dummy>(new DifferingNamespace1.Dummy(1));
392
393            Assert.That(_differenceGetter.IsTypeGeneric(generic.GetType()));
394        }
395
396        [Test]
397        public void TestGetTopLevelGenericName()
398        {
399            var generic = new DifferingNamespace1.DummyGeneric<int>(1).GetType();
400
401            var expected = "NUnit.Framework.Internal.DifferingNamespace1.DummyGeneric`1";
402
403            var actual = _differenceGetter.GetGenericTypeName(generic);
404
405            Assert.AreEqual(expected, actual);
406        }
407
408        [Test]
409        public void TestGetTopLevelGenericNameThrowsWhenNotGeneric()
410        {
411            var notGeneric = new object().GetType();
412
413            Assert.Throws<ArgumentException>(() => _differenceGetter.GetGenericTypeName(notGeneric));
414        }
415
416        [Test]
417        public void TestReconstructShortenedGenericTypeName()
418        {
419            var expected = "KeyValuePair`2[String,Int32]";
420
421            var actual = _differenceGetter.ReconstructGenericTypeName(
422                "KeyValuePair`2",
423                new List<string>() { "String", "Int32" });
424
425            Assert.AreEqual(expected, actual);
426        }
427
428        private void TestShortenTypeNames(object objA, object objB, string shortenedA, string shortenedB)
429        {
430            string actualA, actualB;
431
432            _differenceGetter.ShortenTypeNames(objA.GetType(), objB.GetType(), out actualA, out actualB);
433
434            Assert.AreEqual(shortenedA, actualA);
435            Assert.AreEqual(shortenedB, actualB);
436        }
437
438        [Test]
439        public void TestShortenTypeNamesDifferingNamespace()
440        {
441            TestShortenTypeNames(
442               new DifferingNamespace1.Dummy(1),
443               new DifferingNamespace2.Dummy(1),
444               "DifferingNamespace1.Dummy",
445               "DifferingNamespace2.Dummy");
446        }
447
448        private void TestShortenGenericTopLevelTypeNames(object objA, object objB, string shortenedA, string shortenedB)
449        {
450            string actualA, actualB;
451
452            _differenceGetter.GetShortenedGenericTypes(objA.GetType(), objB.GetType(), out actualA, out actualB);
453
454            Assert.AreEqual(shortenedA, actualA);
455            Assert.AreEqual(shortenedB, actualB);
456        }
457
458        [Test]
459        public void TestShortenGenericTopLevelTypes()
460        {
461            TestShortenGenericTopLevelTypeNames(
462                new A.GenA<int>(),
463                new B.GenA<int>(),
464                "A.GenA`1",
465                "B.GenA`1");
466
467            TestShortenGenericTopLevelTypeNames(
468                new KeyValuePair<string, int>(),
469                new KeyValuePair<int, string>(),
470                "KeyValuePair`2",
471                "KeyValuePair`2");
472        }
473
474        private void TestFullyShortenTypeName(Type type, string expectedOutput)
475        {
476            string actual = _differenceGetter.FullyShortenTypeName(type);
477
478            Assert.AreEqual(expectedOutput, actual);
479        }
480
481        [Test]
482        public void TestFullyShortenTypeName()
483        {
484            TestFullyShortenTypeName(
485                new A.GenA<A.GenA<int>>().GetType(),
486                "GenA`1[GenA`1[Int32]]");
487
488            TestFullyShortenTypeName(
489                new A.GenC<B.GenA<int>, A.GenA<int>>().GetType(),
490                "GenC`2[GenA`1[Int32],GenA`1[Int32]]");
491        }
492    }
493}
494
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
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)