How to use TestFullyShortenTypeName method of B.Dummy class

Best Nunit code snippet using B.Dummy.TestFullyShortenTypeName

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