public void ShouldDumpObjectWithIndexer_IntegerArray()
        {
            // Arrange
            var tempRecord = new TempRecord
            {
                AProp = 99,
                [0]   = 58.3f,
                [1]   = 60.1f,
                ZProp = "ZZ"
            };

            var dumpOptions = new DumpOptions
            {
                IgnoreIndexers = false
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(tempRecord, dumpOptions);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be("var tempRecord = new TempRecord\r\n{\r\n  AProp = 99,\r\n  [0] = 58.3f,\r\n  [1] = 60.1f,\r\n  ZProp = \"ZZ\"\r\n};");
        }
        public void ShouldDumpArray_TwoDimensional()
        {
            // Arrange
            var array = new int[3, 2]
            {
                { 1, 2 },
                { 3, 4 },
                { 5, 6 }
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(array);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be(
                "var array = new int[3, 2]\r\n" +
                "{\r\n" +
                "  {1, 2},\r\n" +
                "  {3, 4},\r\n" +
                "  {5, 6}\r\n" +
                "};");
        }
        public void ShouldEscapeStrings()
        {
            // Arrange
            var expectedPerson = new Person
            {
                Name = "Boris \"The Blade\", \\GANGSTA\\ aka 'The Bullet Dodger' \a \b \f \r\nOn a new\twith tab \v \0"
            };
            var dumpOptions = new DumpOptions
            {
                SetPropertiesOnly   = true,
                IgnoreDefaultValues = true,
                MaxLevel            = 1,
                ExcludeProperties   = { "ByteArray" }
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(expectedPerson, dumpOptions);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();

            /*dump.Should().Be(
             *  "var person = new Person\r\n" +
             *  "{\r\n" +
             *  "  Name = \"Boris \\\"The Blade\\\", \\\\GANGSTA\\\\ aka \\\'The Bullet Dodger\\\' \\a \\b \\f \r\nOn a new\\twith tab \\v \\0\"\r\n" +
             *  "};");*/

            // Compare generated object with input
            var person = new Person
            {
                Name = "Boris \"The Blade\", \\GANGSTA\\ aka \'The Bullet Dodger\' \a \b \f \r\nOn a new\twith tab \v \0"
            };

            person.Should().BeEquivalentTo(expectedPerson);
        }
        public void ShouldDumpDictionary()
        {
            // Arrange
            var dictionary = new Dictionary <int, string>
            {
                { 1, "Value1" },
                { 2, "Value2" },
                { 3, "Value3" }
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(dictionary);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be(
                "var dictionary = new Dictionary<int, string>\r\n" +
                "{\r\n" +
                "  { 1, \"Value1\" },\r\n" +
                "  { 2, \"Value2\" },\r\n" +
                "  { 3, \"Value3\" }\r\n" +
                "};");
        }
        public void ShouldDumpRecursiveTypes_CircularReference_Case1()
        {
            // Arrange
            var person = new RecursivePerson();

            person.Parent = person;

            var dumpOptions = new DumpOptions
            {
                IgnoreDefaultValues = false
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(person, dumpOptions);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be("var recursivePerson = new RecursivePerson\r\n" +
                             "{\r\n" +
                             "  Id = 0,\r\n" +
                             "  Parent = null // Circular reference detected\r\n" +
                             "};");
        }
        public void ShouldDumpException()
        {
            // Arrange
            var ex      = new KeyNotFoundException("message text");
            var options = new DumpOptions {
                IgnoreDefaultValues = true
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(ex, options);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be(
                "var keyNotFoundException = new KeyNotFoundException\r\n" +
                "{\r\n" +
                "  Message = \"message text\",\r\n" +
                "  Data = new ListDictionaryInternal\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  HResult = -2146232969\r\n" +
                "};");
        }
        public void ShouldDumpAnonymousObject()
        {
            // Arrange
            var dynamicObject = new
            {
                IntProperty    = 10,
                StringProperty = "hello",
                DoubleProperty = 3.14d,
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(dynamicObject);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be(
                "var x = new \r\n" +
                "{\r\n" +
                "  IntProperty = 10,\r\n" +
                "  StringProperty = \"hello\",\r\n" +
                "  DoubleProperty = 3.14d\r\n" +
                "};");
        }
        public void ShouldDumpNestedObjects()
        {
            // Arrange
            var persons      = PersonFactory.GeneratePersons(count: 2).ToList();
            var organization = new Organization {
                Name = "superdev gmbh", Persons = persons
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(organization);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be(
                "var organization = new Organization\r\n" +
                "{\r\n" +
                "  Name = \"superdev gmbh\",\r\n" +
                "  Persons = new List<Person>\r\n" +
                "  {\r\n" +
                "    new Person\r\n" +
                "    {\r\n" +
                "      Name = \"Person 1\",\r\n" +
                "      Char = '',\r\n" +
                "      Age = 3,\r\n" +
                "      GetOnly = 11,\r\n" +
                "      Bool = false,\r\n" +
                "      Byte = 0,\r\n" +
                "      ByteArray = new byte[]\r\n" +
                "      {\r\n" +
                "        1,\r\n" +
                "        2,\r\n" +
                "        3,\r\n" +
                "        4\r\n" +
                "      },\r\n" +
                "      SByte = 0,\r\n" +
                "      Float = 0f,\r\n" +
                "      Uint = 0u,\r\n" +
                "      Long = 0L,\r\n" +
                "      ULong = 0UL,\r\n" +
                "      Short = 0,\r\n" +
                "      UShort = 0,\r\n" +
                "      Decimal = 0m,\r\n" +
                "      Double = 0d,\r\n" +
                "      DateTime = DateTime.MinValue,\r\n" +
                "      NullableDateTime = null,\r\n" +
                "      Enum = DateTimeKind.Unspecified\r\n" +
                "    },\r\n" +
                "    new Person\r\n" +
                "    {\r\n" +
                "      Name = \"Person 2\",\r\n" +
                "      Char = '',\r\n" +
                "      Age = 3,\r\n" +
                "      GetOnly = 11,\r\n" +
                "      Bool = false,\r\n" +
                "      Byte = 0,\r\n" +
                "      ByteArray = new byte[]\r\n" +
                "      {\r\n" +
                "        1,\r\n" +
                "        2,\r\n" +
                "        3,\r\n" +
                "        4\r\n" +
                "      },\r\n" +
                "      SByte = 0,\r\n" +
                "      Float = 0f,\r\n" +
                "      Uint = 0u,\r\n" +
                "      Long = 0L,\r\n" +
                "      ULong = 0UL,\r\n" +
                "      Short = 0,\r\n" +
                "      UShort = 0,\r\n" +
                "      Decimal = 0m,\r\n" +
                "      Double = 0d,\r\n" +
                "      DateTime = DateTime.MinValue,\r\n" +
                "      NullableDateTime = null,\r\n" +
                "      Enum = DateTimeKind.Unspecified\r\n" +
                "    }\r\n" +
                "  },\r\n" +
                "  IsAfterCollection = true\r\n" +
                "};");
        }
        public void ShouldDumpExceptionAfterThrow()
        {
            // Arrange
            Exception ex;

            try
            {
                throw new KeyNotFoundException("message text");
            }
            catch (Exception e)
            {
                ex = e;
            }
            var options = new DumpOptions
            {
                IgnoreDefaultValues = true,
                ExcludeProperties   =
                {
                    "CustomAttributes",
                    "Module",
                    "StackTrace",
                    "MetadataToken"
                }
            };

            // Act
            var dump = ObjectDumperCSharp.Dump(ex, options);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();


#if NETCORE
            dump.Should().Be(
                "var keyNotFoundException = new KeyNotFoundException\r\n" +
                "{\r\n" +
                "  TargetSite = new RuntimeMethodInfo\r\n" +
                "  {\r\n" +
                "    Name = \"ShouldDumpExceptionAfterThrow\",\r\n" +
                "    DeclaringType = typeof(ObjectDumperCSharpTests),\r\n" +
                "    ReflectedType = typeof(ObjectDumperCSharpTests),\r\n" +
                "    MemberType = MemberTypes.Method,\r\n" +
                "    IsSecurityCritical = true,\r\n" +
                "    MethodHandle = new RuntimeMethodHandle\r\n" +
                "    {\r\n" +
                "      Value = new IntPtr\r\n" +
                "      {\r\n" +
                "      }\r\n" +
                "    },\r\n" +
                "    Attributes = MethodAttributes.PrivateScope | MethodAttributes.Public | MethodAttributes.HideBySig,\r\n" +
                "    CallingConvention = CallingConventions.Standard | CallingConventions.HasThis,\r\n" +
                "    ReturnType = typeof(void),\r\n" +
                "    ReturnTypeCustomAttributes = new RuntimeParameterInfo\r\n" +
                "    {\r\n" +
                "      ParameterType = typeof(void),\r\n" +
                "      HasDefaultValue = true,\r\n" +
                "      Position = -1\r\n" +
                "    },\r\n" +
                "    IsHideBySig = true,\r\n" +
                "    IsPublic = true\r\n" +
                "  },\r\n" +
                "  Message = \"message text\",\r\n" +
                "  Data = new ListDictionaryInternal\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  Source = \"ObjectDumper.Tests\",\r\n" +
                "  HResult = -2146232969\r\n" +
                "};");
#endif
        }
        public void ShouldDumpMailMessage()
        {
            // Arrange
            var mailmessage = new MailMessage("*****@*****.**", "*****@*****.**", "Subject", "Body");

            // Act
            var dump = ObjectDumperCSharp.Dump(mailmessage);

            // Assert
            this.testOutputHelper.WriteLine(dump);
            dump.Should().NotBeNull();
            dump.Should().Be(
                "var mailMessage = new MailMessage\r\n" +
                "{\r\n" +
                "  From = new MailAddress\r\n" +
                "  {\r\n" +
                "    DisplayName = \"\",\r\n" +
                "    User = \"sender\",\r\n" +
                "    Host = \"mail.com\",\r\n" +
                "    Address = \"[email protected]\"\r\n" +
                "  },\r\n" +
                "  Sender = null,\r\n" +
                "  ReplyTo = null,\r\n" +
                "  ReplyToList = new MailAddressCollection\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  To = new MailAddressCollection\r\n" +
                "  {\r\n" +
                "    new MailAddress\r\n" +
                "    {\r\n" +
                "      DisplayName = \"\",\r\n" +
                "      User = \"receiver\",\r\n" +
                "      Host = \"mail.com\",\r\n" +
                "      Address = \"[email protected]\"\r\n" +
                "    }\r\n" +
                "  },\r\n" +
                "  Bcc = new MailAddressCollection\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  CC = new MailAddressCollection\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  Priority = MailPriority.Normal,\r\n" +
                "  DeliveryNotificationOptions = DeliveryNotificationOptions.None,\r\n" +
                "  Subject = \"Subject\",\r\n" +
                "  SubjectEncoding = null,\r\n" +
                "  Headers = new HeaderCollection\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  HeadersEncoding = null,\r\n" +
                "  Body = \"Body\",\r\n" +
#if NET452
                "  BodyEncoding = new ASCIIEncoding\r\n" +
#else
                "  BodyEncoding = new ASCIIEncodingSealed\r\n" +
#endif
                "  {\r\n" +
                "    IsSingleByte = true,\r\n" +
#if NETCOREAPP3_1_OR_GREATER || NET5_0_OR_GREATER
                "    Preamble = \"{NotSupportedException: Specified method is not supported.}\",\r\n" +
#endif
                "    BodyName = \"us-ascii\",\r\n" +
                "    EncodingName = \"US-ASCII\",\r\n" +
                "    HeaderName = \"us-ascii\",\r\n" +
                "    WebName = \"us-ascii\",\r\n" +
                "    WindowsCodePage = 1252,\r\n" +
                "    IsBrowserDisplay = false,\r\n" +
                "    IsBrowserSave = false,\r\n" +
                "    IsMailNewsDisplay = true,\r\n" +
                "    IsMailNewsSave = true,\r\n" +
                "    EncoderFallback = new EncoderReplacementFallback\r\n" +
                "    {\r\n" +
                "      DefaultString = \"?\",\r\n" +
                "      MaxCharCount = 1\r\n" +
                "    },\r\n" +
                "    DecoderFallback = new DecoderReplacementFallback\r\n" +
                "    {\r\n" +
                "      DefaultString = \"?\",\r\n" +
                "      MaxCharCount = 1\r\n" +
                "    },\r\n" +
                "    IsReadOnly = true,\r\n" +
                "    CodePage = 20127\r\n" +
                "  },\r\n" +
                "  BodyTransferEncoding = TransferEncoding.Unknown,\r\n" +
                "  IsBodyHtml = false,\r\n" +
                "  Attachments = new AttachmentCollection\r\n" +
                "  {\r\n" +
                "  },\r\n" +
                "  AlternateViews = new AlternateViewCollection\r\n" +
                "  {\r\n" +
                "  }\r\n" +
                "};");
        }