File: ObjectInfoTest.cs

package info (click to toggle)
mono 5.18.0.240%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,253,216 kB
  • sloc: cs: 10,925,936; xml: 2,804,987; ansic: 643,970; cpp: 120,384; perl: 59,272; asm: 21,383; sh: 20,162; makefile: 18,157; python: 4,715; pascal: 924; sql: 859; sed: 16; php: 1
file content (730 lines) | stat: -rw-r--r-- 22,643 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Dynamic;
using System.Linq;
using System.Web.TestUtil;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;

namespace System.Web.Helpers.Test
{
    public class ObjectInfoTest
    {
        [Fact]
        public void PrintWithNegativeDepthThrows()
        {
            // Act & Assert
            Assert.ThrowsArgumentGreaterThanOrEqualTo(() => ObjectInfo.Print(null, depth: -1), "depth", "0");
        }

        [Fact]
        public void PrintWithInvalidEnumerationLength()
        {
            // Act & Assert
            Assert.ThrowsArgumentGreaterThan(() => ObjectInfo.Print(null, enumerationLength: -1), "enumerationLength", "0");
        }

        [Fact]
        public void PrintWithNull()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();

            // Act
            visitor.Print(null);

            // Assert
            Assert.Equal(1, visitor.Values.Count);
            Assert.Equal("null", visitor.Values[0]);
        }

        [Fact]
        public void PrintWithEmptyString()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();

            // Act
            visitor.Print(String.Empty);

            // Assert
            Assert.Equal(1, visitor.Values.Count);
            Assert.Equal(String.Empty, visitor.Values[0]);
        }

        [Fact]
        public void PrintWithInt()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();

            // Act
            visitor.Print(404);

            // Assert
            Assert.Equal(1, visitor.Values.Count);
            Assert.Equal("404", visitor.Values[0]);
        }

        [Fact]
        public void PrintWithIDictionary()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            IDictionary dict = new OrderedDictionary();
            dict.Add("foo", "bar");
            dict.Add("abc", 500);

            // Act
            visitor.Print(dict);

            // Assert
            Assert.Equal("foo = bar", visitor.KeyValuePairs[0]);
            Assert.Equal("abc = 500", visitor.KeyValuePairs[1]);
        }

        [Fact]
        public void PrintWithIEnumerable()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var values = Enumerable.Range(0, 10);

            // Act
            visitor.Print(values);

            // Assert
            foreach (var num in values)
            {
                Assert.True(visitor.Values.Contains(num.ToString()));
            }
        }

        [Fact]
        public void PrintWithGenericIListPrintsIndex()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var values = Enumerable.Range(0, 10).ToList();

            // Act
            visitor.Print(values);

            // Assert
            for (int i = 0; i < values.Count; i++)
            {
                Assert.True(visitor.Values.Contains(values[i].ToString()));
                Assert.True(visitor.Indexes.Contains(i));
            }
        }

        [Fact]
        public void PrintWithArrayPrintsIndex()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var values = Enumerable.Range(0, 10).ToArray();

            // Act
            visitor.Print(values);

            // Assert
            for (int i = 0; i < values.Length; i++)
            {
                Assert.True(visitor.Values.Contains(values[i].ToString()));
                Assert.True(visitor.Indexes.Contains(i));
            }
        }

        [Fact]
        public void PrintNameValueCollectionPrintsKeysAndValues()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var values = new NameValueCollection();
            values["a"] = "1";
            values["b"] = null;

            // Act            
            visitor.Print(values);

            // Assert
            Assert.Equal("a = 1", visitor.KeyValuePairs[0]);
            Assert.Equal("b = null", visitor.KeyValuePairs[1]);
        }

        [Fact]
        public void PrintDateTime()
        {
            using (new CultureReplacer())
            {
                // Arrange
                MockObjectVisitor visitor = CreateObjectVisitor();
                var dt = new DateTime(2001, 11, 20, 10, 30, 1);

                // Act            
                visitor.Print(dt);

                // Assert
                Assert.Equal("11/20/2001 10:30:01 AM", visitor.Values[0]);
            }
        }

        [Fact]
        public void PrintCustomObjectPrintsMembers()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var person = new Person
            {
                Name = "David",
                Age = 23.3,
                Dob = new DateTime(1986, 11, 19),
                LongType = 1000000000,
                Type = 1
            };

            using (new CultureReplacer())
            {
                // Act            
                visitor.Print(person);

                // Assert
                Assert.Equal(9, visitor.Members.Count);
                Assert.True(visitor.Members.Contains("double Age = 23.3"));
                Assert.True(visitor.Members.Contains("string Name = David"));
                Assert.True(visitor.Members.Contains("DateTime Dob = 11/19/1986 12:00:00 AM"));
                Assert.True(visitor.Members.Contains("short Type = 1"));
                Assert.True(visitor.Members.Contains("float Float = 0"));
                Assert.True(visitor.Members.Contains("byte Byte = 0"));
                Assert.True(visitor.Members.Contains("decimal Decimal = 0"));
                Assert.True(visitor.Members.Contains("bool Bool = False"));
            }
        }

        [Fact]
        public void PrintShowsVisitedWhenCircularReferenceInObjectGraph()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            PersonNode node = new PersonNode
            {
                Person = new Person
                {
                    Name = "David",
                    Age = 23.3
                }
            };
            node.Next = node;

            // Act
            visitor.Print(node);

            // Assert            
            Assert.True(visitor.Members.Contains("string Name = David"));
            Assert.True(visitor.Members.Contains("double Age = 23.3"));
            Assert.True(visitor.Members.Contains("PersonNode Next = Visited"));
        }

        [Fact]
        public void PrintShowsVisitedWhenCircularReferenceIsIEnumerable()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            List<object> values = new List<object>();
            values.Add(values);

            // Act
            visitor.Print(values);

            // Assert            
            Assert.Equal("Visited", visitor.Values[0]);
            Assert.Equal("Visited " + values.GetHashCode(), visitor.Visited[0]);
        }

        [Fact]
        public void PrintShowsVisitedWhenCircularReferenceIsIDictionary()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            OrderedDictionary values = new OrderedDictionary();
            values[values] = values;

            // Act
            visitor.Print(values);

            // Assert            
            Assert.Equal("Visited", visitor.Values[0]);
            Assert.Equal("Visited " + values.GetHashCode(), visitor.Visited[0]);
        }

        [Fact]
        public void PrintShowsVisitedWhenCircularReferenceIsNameValueCollection()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            NameValueCollection nameValues = new NameValueCollection();
            nameValues["id"] = "1";
            List<NameValueCollection> values = new List<NameValueCollection>();
            values.Add(nameValues);
            values.Add(nameValues);

            // Act
            visitor.Print(values);

            // Assert            
            Assert.True(visitor.Values.Contains("Visited"));
            Assert.True(visitor.Visited.Contains("Visited " + nameValues.GetHashCode()));
        }

        [Fact]
        public void PrintExcludesWriteOnlyProperties()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            ClassWithWriteOnlyProperty cls = new ClassWithWriteOnlyProperty();

            // Act
            visitor.Print(cls);

            // Assert
            Assert.Equal(0, visitor.Members.Count);
        }

        [Fact]
        public void PrintWritesEnumeratedElementsUntilLimitIsHit()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var enumeration = Enumerable.Range(0, 2000);

            // Act
            visitor.Print(enumeration);

            // Assert
            for (int i = 0; i <= 2000; i++)
            {
                if (i < 1000)
                {
                    Assert.True(visitor.Values.Contains(i.ToString()));
                }
                else
                {
                    Assert.False(visitor.Values.Contains(i.ToString()));
                }
            }
            Assert.True(visitor.Values.Contains("Limit Exceeded"));
        }

        [Fact]
        public void PrintWithAnonymousType()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            var value = new { Name = "John", X = 1 };

            // Act
            visitor.Print(value);

            // Assert
            Assert.True(visitor.Members.Contains("string Name = John"));
            Assert.True(visitor.Members.Contains("int X = 1"));
        }

        [Fact]
        public void PrintClassWithPublicFields()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            ClassWithFields value = new ClassWithFields();
            value.Foo = "John";
            value.Bar = 1;

            // Actt
            visitor.Print(value);

            // Assert            
            Assert.True(visitor.Members.Contains("string Foo = John"));
            Assert.True(visitor.Members.Contains("int Bar = 1"));
        }

        [Fact]
        public void PrintClassWithDynamicMembersPrintsMembersIfGetDynamicMemberNamesIsImplemented()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            dynamic d = new DynamicDictionary();
            d.Cycle = d;
            d.Name = "Foo";
            d.Value = null;

            // Act
            visitor.Print(d);

            // Assert                                   
            Assert.True(visitor.Members.Contains("DynamicDictionary Cycle = Visited"));
            Assert.True(visitor.Members.Contains("string Name = Foo"));
            Assert.True(visitor.Members.Contains("Value = null"));
        }

        [Fact]
        public void PrintClassWithDynamicMembersReturningNullPrintsNoMembers()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            dynamic d = new ClassWithDynamicAnNullMemberNames();
            d.Cycle = d;
            d.Name = "Foo";
            d.Value = null;

            // Act
            visitor.Print(d);

            // Assert                                   
            Assert.False(visitor.Members.Any());
        }

        [Fact]
        public void PrintUsesToStringOfIConvertibleObjects()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            ConvertibleClass cls = new ConvertibleClass();

            // Act
            visitor.Print(cls);

            // Assert
            Assert.Equal("Test", visitor.Values[0]);
        }

        [Fact]
        public void PrintConvertsTypeToString()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();

            // Act
            visitor.Print(typeof(string));

            // Assert
            Assert.Equal("typeof(string)", visitor.Values[0]);
        }

        [Fact]
        public void PrintClassWithPropertyThatThrowsExceptionPrintsException()
        {
            // Arrange
            MockObjectVisitor visitor = CreateObjectVisitor();
            ClassWithPropertyThatThrowsException value = new ClassWithPropertyThatThrowsException();

            // Act
            visitor.Print(value);

            // Assert            
            Assert.Equal("int MyProperty = Property accessor 'MyProperty' on object 'System.Web.Helpers.Test.ObjectInfoTest+ClassWithPropertyThatThrowsException' threw the following exception:'Property that shows an exception'", visitor.Members[0]);
        }

        [Fact]
        public void ConvertEscapeSequencesPrintsStringEscapeSequencesAsLiterals()
        {
            // Act
            string value = HtmlObjectPrinter.ConvertEscapseSequences("\\\'\"\0\a\b\f\n\r\t\v");

            // Assert
            Assert.Equal("\\\\'\\\"\\0\\a\\b\\f\\n\\r\\t\\v", value);
        }

        [Fact]
        public void ConvertEscapeSequencesDoesNotEscapeUnicodeSequences()
        {
            // Act
            string value = HtmlObjectPrinter.ConvertEscapseSequences("\u1023\x2045");

            // Assert
            Assert.Equal("\u1023\x2045", value);
        }

        [Fact]
        public void PrintCharPrintsQuotedString()
        {
            // Arrange
            HtmlObjectPrinter printer = new HtmlObjectPrinter(100, 100);
            HtmlElement element = new HtmlElement("span");
            printer.PushElement(element);

            // Act            
            printer.VisitConvertedValue('x', "x");

            // Assert
            Assert.Equal(1, element.Children.Count);
            HtmlElement child = element.Children[0];
            Assert.Equal("'x'", child.InnerText);
            Assert.Equal("quote", child["class"]);
        }

        [Fact]
        public void PrintEscapeCharPrintsEscapedCharAsLiteral()
        {
            // Arrange
            HtmlObjectPrinter printer = new HtmlObjectPrinter(100, 100);
            HtmlElement element = new HtmlElement("span");
            printer.PushElement(element);

            // Act            
            printer.VisitConvertedValue('\t', "\t");

            // Assert
            Assert.Equal(1, element.Children.Count);
            HtmlElement child = element.Children[0];
            Assert.Equal("'\\t'", child.InnerText);
            Assert.Equal("quote", child["class"]);
        }

        [Fact]
        public void GetTypeNameConvertsGenericTypesToCsharpSyntax()
        {
            // Act
            string value = ObjectVisitor.GetTypeName(typeof(Func<Func<Func<int, int, object>, Action<int>>>));

            // Assert
            Assert.Equal("Func<Func<Func<int, int, object>, Action<int>>>", value);
        }

        private class ConvertibleClass : IConvertible
        {
            public TypeCode GetTypeCode()
            {
                throw new NotImplementedException();
            }

            public bool ToBoolean(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public byte ToByte(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public char ToChar(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public DateTime ToDateTime(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public decimal ToDecimal(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public double ToDouble(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public short ToInt16(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public int ToInt32(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public long ToInt64(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public sbyte ToSByte(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public float ToSingle(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public string ToString(IFormatProvider provider)
            {
                return "Test";
            }

            public object ToType(Type conversionType, IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public ushort ToUInt16(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public uint ToUInt32(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }

            public ulong ToUInt64(IFormatProvider provider)
            {
                throw new NotImplementedException();
            }
        }

        private class ClassWithPropertyThatThrowsException
        {
            public int MyProperty
            {
                get { throw new InvalidOperationException("Property that shows an exception"); }
            }
        }

        private class ClassWithDynamicAnNullMemberNames : DynamicObject
        {
            public override IEnumerable<string> GetDynamicMemberNames()
            {
                return null;
            }

            public override bool TryGetMember(GetMemberBinder binder, out object result)
            {
                result = null;
                return true;
            }

            public override bool TrySetMember(SetMemberBinder binder, object value)
            {
                return true;
            }
        }

        private class Person
        {
            public string Name { get; set; }
            public double Age { get; set; }
            public DateTime Dob { get; set; }
            public short Type { get; set; }
            public long LongType { get; set; }
            public float Float { get; set; }
            public byte Byte { get; set; }
            public decimal Decimal { get; set; }
            public bool Bool { get; set; }
        }

        private class ClassWithFields
        {
            public string Foo;
            public int Bar = 13;
        }

        private class ClassWithWriteOnlyProperty
        {
            public int Value
            {
                set { }
            }
        }

        private class PersonNode
        {
            public Person Person { get; set; }
            public PersonNode Next { get; set; }
        }

        private MockObjectVisitor CreateObjectVisitor(int recursionLimit = 10, int enumerationLimit = 1000)
        {
            return new MockObjectVisitor(recursionLimit, enumerationLimit);
        }

        private class MockObjectVisitor : ObjectVisitor
        {
            public MockObjectVisitor(int recursionLimit, int enumerationLimit)
                : base(recursionLimit, enumerationLimit)
            {
                Values = new List<string>();
                KeyValuePairs = new List<string>();
                Members = new List<string>();
                Indexes = new List<int>();
                Visited = new List<string>();
            }

            public List<string> Values { get; set; }
            public List<string> KeyValuePairs { get; set; }
            public List<string> Members { get; set; }
            public List<int> Indexes { get; set; }
            public List<string> Visited { get; set; }

            public void Print(object value)
            {
                Visit(value, 0);
            }

            public override void VisitObjectVisitorException(ObjectVisitorException exception)
            {
                Values.Add(exception.InnerException.Message);
            }

            public override void VisitStringValue(string stringValue)
            {
                Values.Add(stringValue);
                base.VisitStringValue(stringValue);
            }

            public override void VisitVisitedObject(string id, object value)
            {
                Visited.Add(String.Format("Visited {0}", id));
                Values.Add("Visited");
                base.VisitVisitedObject(id, value);
            }

            public override void VisitIndexedEnumeratedValue(int index, object item, int depth)
            {
                Indexes.Add(index);
                base.VisitIndexedEnumeratedValue(index, item, depth);
            }

            public override void VisitEnumeratonLimitExceeded()
            {
                Values.Add("Limit Exceeded");
                base.VisitEnumeratonLimitExceeded();
            }

            public override void VisitMember(string name, Type type, object value, int depth)
            {
                base.VisitMember(name, type, value, depth);
                type = type ?? (value != null ? value.GetType() : null);
                if (type == null)
                {
                    Members.Add(String.Format("{0} = null", name));
                }
                else
                {
                    Members.Add(String.Format("{0} {1} = {2}", GetTypeName(type), name, Values.Last()));
                }
            }

            public override void VisitNull()
            {
                Values.Add("null");
                base.VisitNull();
            }

            public override void VisitKeyValue(object key, object value, int depth)
            {
                base.VisitKeyValue(key, value, depth);
                KeyValuePairs.Add(String.Format("{0} = {1}", Values[Values.Count - 2], Values[Values.Count - 1]));
            }
        }
    }
}