Example #1
0
        public void IgnorableProperty2Test()
        {
            var obj = new IgnorableProperty2
            {
                Field1 = 1,
                Field2 = 2,
                Field3 = 3,
            };

            var settings = new SerializerSettings
            {
                Usings = ImmutableArray.Create("SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(obj, settings);

            var expected = @"new IgnorableProperty2
{
    Field1 = 1,
    Field2 = 2,
    Field3 = 3
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #2
0
        public void NestedObjects()
        {
            var obj = new TestClass3
            {
                Class1 = new TestClass1 {
                    Test = 6
                },
                Class2 = new TestClass2 {
                    Test = "Hello world"
                }
            };

            var result   = SourceCodeSerializer.Serialize(obj);
            var expected = @"new SourceCodeSerializer.TestClass3
{
    Class1 = new SourceCodeSerializer.TestClass1
    {
        Test = 6
    },
    Class2 = new SourceCodeSerializer.TestClass2
    {
        Test = @""Hello world""
    }
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #3
0
        public static string Serialize <T>(T obj, SerializerSettings settings = null)
        {
            var serializer = new SourceCodeSerializer(settings);

            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);

                return(writer.ToString());
            }
        }
Example #4
0
        public void IntegerPropertyDefaultValue()
        {
            var obj = new TestClass1 {
                Test = 0
            };
            var result   = SourceCodeSerializer.Serialize(obj);
            var expected = @"new SourceCodeSerializer.TestClass1 { }";

            _helper.WriteLine(result);
            Assert.Equal(expected, result);
        }
Example #5
0
        public void IntegerProperty()
        {
            var obj = new TestClass1 {
                Test = 5
            };
            var result   = SourceCodeSerializer.Serialize(obj);
            var expected = @"new SourceCodeSerializer.TestClass1
{
    Test = 5
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #6
0
        public void StringProperty()
        {
            var obj = new TestClass2 {
                Test = "Hello world"
            };
            var result = SourceCodeSerializer.Serialize(obj);

            var expected = @"new SourceCodeSerializer.TestClass2
{
    Test = @""Hello world""
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #7
0
        public void NestedObjectsWithClassAndUsings()
        {
            var obj = new TestClass3
            {
                Class1 = new TestClass1 {
                    Test = 6
                },
                Class2 = new TestClass2 {
                    Test = "Hello world"
                }
            };

            var settings = new SerializerSettings
            {
                Generator = new FactoryMethodGenerator("Test", "Factory", "Create"),
                Usings    = ImmutableArray.Create("System", "SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(obj, settings);

            var expected = @"using System;
using SourceCodeSerializer;

namespace Test
{
    partial class Factory
    {
        public TestClass3 Create()
        {
            return new TestClass3
            {
                Class1 = new TestClass1
                {
                    Test = 6
                },
                Class2 = new TestClass2
                {
                    Test = @""Hello world""
                }
            };
        }
    }
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #8
0
        public void IntegerPropertyNoNewLine()
        {
            var obj = new TestClass1 {
                Test = 5
            };

            var settings = new SerializerSettings
            {
                ObjectInitializationNewLine = false
            };

            var result   = SourceCodeSerializer.Serialize(obj, settings);
            var expected = @"new SourceCodeSerializer.TestClass1 { Test = 5 }";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
        public void ReadmeExample()
        {
            var j = new Foo();

            j.Bar  = 5;
            j.Bar2 = new FooBar {
                A = "hello"
            };

            var settings = new SerializerSettings
            {
                Usings = ImmutableArray.Create("SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(j, settings);

            _helper.WriteLine(result);
        }
Example #10
0
        public void IgnoreDefaultValueTest()
        {
            var obj = new ClassWithUShort {
                Item = 0
            };

            var settings = new SerializerSettings
            {
                IgnoreDefaultValues = false,
                Usings = ImmutableArray.Create("SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(obj, settings);

            var expected = @"new ClassWithUShort
{
    Item = 0
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #11
0
        public void NonGenericList()
        {
            var obj = new NonGenericList1
            {
                List = new object[]
                {
                    new TestClass1 {
                        Test = 2
                    },
                    new TestClass2 {
                        Test = "Item2"
                    }
                }
            };

            var settings = new SerializerSettings
            {
                Usings = ImmutableArray.Create("SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(obj, settings);

            var expected = @"new NonGenericList1
{
    List = new object[] {
        new TestClass1
        {
            Test = 2
        },
        new TestClass2
        {
            Test = @""Item2""
        }
    }
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #12
0
        public void GenericList()
        {
            var obj = new GenericList1
            {
                List = new List <TestClass2>()
            };

            obj.List.Add(new TestClass2 {
                Test = "Item1"
            });
            obj.List.Add(new TestClass2 {
                Test = "Item2"
            });

            var settings = new SerializerSettings
            {
                Usings = ImmutableArray.Create("SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(obj, settings);

            var expected = @"new GenericList1
{
    List = new TestClass2[] {
        new TestClass2
        {
            Test = @""Item1""
        },
        new TestClass2
        {
            Test = @""Item2""
        }
    }
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #13
0
        public void DerivedMembers()
        {
            var obj = new DerivedTest1
            {
                Other = 1,
                Test  = 6
            };

            var settings = new SerializerSettings
            {
                Usings = ImmutableArray.Create("SourceCodeSerializer")
            };

            var result = SourceCodeSerializer.Serialize(obj, settings);

            var expected = @"new DerivedTest1
{
    Other = 1,
    Test = 6
}";

            _helper.WriteLine(result);
            Assert.Equal(result, expected);
        }
Example #14
0
        public static void Serialize <T>(TextWriter writer, T obj, SerializerSettings settings = null)
        {
            var serializer = new SourceCodeSerializer(settings);

            serializer.Serialize(writer, obj);
        }