예제 #1
0
        public void StringConstantTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var records = new List <Test>
                            {
                                new Test {
                                    Id = 1, Name = "one"
                                },
                                new Test {
                                    Id = 2, Name = "two"
                                }
                            };

                            csv.Context.RegisterClassMap <TestStringMap>();
                            csv.WriteRecords(records);
                            writer.Flush();
                            stream.Position = 0;

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("Id,Name");
                            expected.AppendLine("1,constant");
                            expected.AppendLine("2,constant");

                            var result = reader.ReadToEnd();

                            Assert.AreEqual(expected.ToString(), result);
                        }
        }
        public void WriteMultipleFieldsFromSinglePropertyTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, new CultureInfo("en-US")))
                        {
                            var records = new List <Test>
                            {
                                new Test {
                                    Dob = DateTime.Parse("9/6/2017", new CultureInfo("en-US"))
                                }
                            };
                            csv.Context.RegisterClassMap <TestMap>();
                            csv.WriteRecords(records);
                            writer.Flush();
                            stream.Position = 0;

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("A,B,C");
                            expected.AppendLine("9/6/2017 12:00:00 AM,9/6/2017 12:00:00 AM,9/6/2017 12:00:00 AM");

                            Assert.Equal(expected.ToString(), reader.ReadToEnd());
                        }
        }
예제 #3
0
        public void WritePrivateFieldsWithMappingTest()
        {
            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var list = new List <APrivate>
                            {
                                new APrivate(1, "one")
                            };
                            csv.Context.RegisterClassMap <APrivateMap>();
                            csv.WriteRecords(list);
                            writer.Flush();
                            stream.Position = 0;

                            var result = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("idField,nameField");
                            expected.AppendLine("1,one");

                            Assert.AreEqual(expected.ToString(), result);
                        }
        }
예제 #4
0
        public void FullWriteWithHeaderAutoMapTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var list = new List <Test>
                            {
                                new Test {
                                    List = new List <int> {
                                        1, 2, 3
                                    }
                                }
                            };
                            csv.WriteRecords(list);
                            writer.Flush();
                            stream.Position = 0;

                            var result   = reader.ReadToEnd();
                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("Before,After");
                            expected.AppendLine(",");

                            Assert.Equal(expected.ToString(), result);
                        }
        }
예제 #5
0
        public void NoPropertyWithHeaderAndNoNameTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var list = new List <Test>
                            {
                                new Test {
                                    Id = 1
                                },
                                new Test {
                                    Id = 2
                                }
                            };

                            csv.Context.RegisterClassMap <TestWithNoNameMap>();
                            csv.WriteRecords(list);

                            writer.Flush();
                            stream.Position = 0;

                            var result = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("Id,");
                            expected.AppendLine("1,const");
                            expected.AppendLine("2,const");

                            Assert.AreEqual(expected.ToString(), result);
                        }
        }
예제 #6
0
        public void WritePrivateFieldsWithAutoMapTest()
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                MemberTypes           = MemberTypes.Fields,
                IncludePrivateMembers = true,
            };

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var csv = new CsvWriter(writer, config))
                        {
                            var list = new List <APrivate>
                            {
                                new APrivate(1, "one")
                            };

                            csv.WriteRecords(list);
                            writer.Flush();
                            stream.Position = 0;

                            var result = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("idField,nameField");
                            expected.AppendLine("1,one");

                            Assert.AreEqual(expected.ToString(), result);
                        }
        }
예제 #7
0
        public void WriterReferenceTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var list = new List <A>
                            {
                                new A
                                {
                                    AId = 1,
                                    B   = new B
                                    {
                                        BId = 2
                                    }
                                }
                            };
                            csv.WriteRecords(list);
                            writer.Flush();
                            stream.Position = 0;

                            var data = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("AId,BId");
                            expected.AppendLine("1,2");

                            Assert.Equal(expected.ToString(), data);
                        }
        }
예제 #8
0
        public void IgnoreReferenceTest()
        {
            var records = new List <Parent>
            {
                new Parent
                {
                    Id    = 1,
                    Child = new Child
                    {
                        Name       = "one",
                        GrandChild = new GrandChild
                        {
                            Date = DateTimeOffset.Now
                        }
                    }
                }
            };

            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteRecords(records);

                    var expected = new TestStringBuilder(csv.Configuration.NewLine);
                    expected.AppendLine("Id");
                    expected.AppendLine("1");

                    Assert.Equal(expected.ToString(), writer.ToString());
                }
        }
예제 #9
0
        public void WriterSimpleTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var list = new List <Simple>
                            {
                                new Simple {
                                    Id = 1, Name = "one"
                                }
                            };
                            csv.WriteRecords(list);
                            writer.Flush();
                            stream.Position = 0;

                            var data = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("Id,Name");
                            expected.AppendLine("1,one");

                            Assert.Equal(expected.ToString(), data);
                        }
        }
예제 #10
0
        public void DynamicTypeTest()
        {
            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    dynamic a = new ExpandoObject();
                    a.Id = 1;
                    csv.WriteDynamicHeader(a);
                    csv.NextRecord();
                    csv.WriteRecord(a);
                    csv.NextRecord();

                    dynamic b = new ExpandoObject();
                    b.Name = "one";
                    csv.WriteDynamicHeader(b);
                    csv.NextRecord();
                    csv.WriteRecord(b);
                    csv.NextRecord();
                    writer.Flush();

                    var expected = new TestStringBuilder(csv.Configuration.NewLine);
                    expected.AppendLine("Id");
                    expected.AppendLine("1");
                    expected.AppendLine("Name");
                    expected.AppendLine("one");

                    Assert.Equal(expected.ToString(), writer.ToString());
                }
        }
예제 #11
0
        public void GenericTypeTest()
        {
            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader <A>();
                    csv.NextRecord();
                    csv.WriteRecord(new A {
                        Id = 1
                    });
                    csv.NextRecord();

                    csv.WriteHeader <B>();
                    csv.NextRecord();
                    csv.WriteRecord(new B {
                        Name = "one"
                    });
                    csv.NextRecord();
                    writer.Flush();

                    var expected = new TestStringBuilder(csv.Configuration.NewLine);
                    expected.AppendLine("Id");
                    expected.AppendLine("1");
                    expected.AppendLine("Name");
                    expected.AppendLine("one");

                    Assert.Equal(expected.ToString(), writer.ToString());
                }
        }
예제 #12
0
        public void IgnoreReferncesWritingTest()
        {
            var records = new List <Foo>
            {
                new Foo
                {
                    Id  = 1,
                    Bar = new Bar {
                        Name = "one"
                    }
                }
            };

            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                IgnoreReferences = true,
            };

            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, config))
                {
                    csv.WriteRecords(records);
                    writer.Flush();

                    var expected = new TestStringBuilder(csv.Configuration.NewLine);
                    expected.AppendLine("Id");
                    expected.AppendLine("1");

                    Assert.AreEqual(expected.ToString(), writer.ToString());
                }
        }
예제 #13
0
        public async Task WriteRecordsTest()
        {
            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            var records = new List <Simple>
                            {
                                new Simple {
                                    Id = 1, Name = "one"
                                },
                                new Simple {
                                    Id = 2, Name = "two"
                                },
                            };
                            await csv.WriteRecordsAsync(records);

                            writer.Flush();
                            stream.Position = 0;

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("Id,Name");
                            expected.AppendLine("1,one");
                            expected.AppendLine("2,two");

                            Assert.AreEqual(expected.ToString(), reader.ReadToEnd());
                        }
        }
예제 #14
0
        public void EnsureChildNotWrittenWhenListIsParent()
        {
            var record = new Child
            {
                ChildProp  = "child",
                ParentProp = "parent"
            };

            Parent[] records = { record };

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            csv.WriteRecords(records);
                            writer.Flush();
                            stream.Position = 0;

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("ParentProp");
                            expected.AppendLine("parent");

                            Assert.AreEqual(expected.ToString(), reader.ReadToEnd());
                        }
        }
예제 #15
0
        public void WriterHasNoDefaultConstructorReferenceHasNoDefaultConstructorTest()
        {
            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    var list = new List <SimpleHasNoDefaultConstructorReferenceHasNoDefaultConstructor>
                    {
                        new SimpleHasNoDefaultConstructorReferenceHasNoDefaultConstructor(1, new NoDefaultConstructor("one"))
                    };
                    csv.WriteRecords(list);
                    writer.Flush();

                    var expected = new TestStringBuilder(csv.Configuration.NewLine);
                    expected.AppendLine("Id,Name");
                    expected.AppendLine("1,one");

                    Assert.Equal(expected.ToString(), writer.ToString());
                }
        }
예제 #16
0
        public void WriteWithAutoMapTest()
        {
            var records = new List <Bar>
            {
                new Bar {
                    Id = 1
                },
            };

            using (var writer = new StringWriter())
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteRecords(records);

                    var expected = new TestStringBuilder(csv.Configuration.NewLine);
                    expected.AppendLine("Id");
                    expected.AppendLine("1");

                    Assert.Equal(expected.ToString(), writer.ToString());
                }
        }
예제 #17
0
        public void WritePublicFieldsAndPropertiesWithAutoMapTest()
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                MemberTypes = MemberTypes.Properties | MemberTypes.Fields,
            };

            using (var stream = new MemoryStream())
                using (var writer = new StreamWriter(stream))
                    using (var reader = new StreamReader(stream))
                        using (var csv = new CsvWriter(writer, config))
                        {
                            var list = new List <APublic>
                            {
                                new APublic
                                {
                                    IdField = 1,
                                    BField  = new BPublic
                                    {
                                        NameField = "one",
                                        NameProp  = "two"
                                    },
                                    IdProp = 2
                                }
                            };
                            csv.WriteRecords(list);
                            writer.Flush();
                            stream.Position = 0;

                            var result = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("IdProp,IdField,NameProp,NameField");
                            expected.AppendLine("2,1,two,one");

                            Assert.AreEqual(expected.ToString(), result);
                        }
        }
예제 #18
0
        public void OutOfOrderTest()
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                HasHeaderRecord = false,
            };

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, config))
                        {
                            var list = new List <Test>
                            {
                                new Test {
                                    Id = 1, Name = "one"
                                },
                                new Test {
                                    Id = 2, Name = "two"
                                }
                            };

                            csv.Context.RegisterClassMap <TestMapOutOfOrderWithEmptyFieldsMap>();
                            csv.WriteRecords(list);

                            writer.Flush();
                            stream.Position = 0;

                            var result = reader.ReadToEnd();

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("one,,,1");
                            expected.AppendLine("two,,,2");

                            Assert.AreEqual(expected.ToString(), result);
                        }
        }
예제 #19
0
        public void Test()
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                TrimOptions = TrimOptions.Trim,
            };

            using (var stream = new MemoryStream())
                using (var reader = new StreamReader(stream))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, config))
                        {
                            csv.WriteField("  a b c  ");
                            csv.WriteField("  d e f  ");
                            csv.NextRecord();
                            writer.Flush();
                            stream.Position = 0;

                            var expected = new TestStringBuilder(csv.Configuration.NewLine);
                            expected.AppendLine("a b c,d e f");

                            Assert.Equal(expected.ToString(), reader.ReadToEnd());
                        }
        }