Ejemplo n.º 1
0
        public void Build_NoSpecifiedHeaders_InfersMemberNames()
        {
            var result   = CsvWriter.CreateStream <TestRow>(FullDefinition(), twoRows).GetString();
            var expected = "\"Id\";\"Name\";\"IsActive\"\r\n"
                           + "1;\"OK_NAME1\";True\r\n"
                           + "2;\"OK_NAME2\";False\r\n";

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 2
0
        public void Build_TwoTestRowsWithHeader_CreatesCsv()
        {
            var result   = CsvWriter.CreateStream <TestRow>(FullDefinitionWithHeaders(), twoRows).GetString();
            var expected = "\"OK_ID_HEADER\";\"OK_NAME_HEADER\";\"OK_ISACTIVE_HEADER\"\r\n"
                           + "1;\"OK_NAME1\";True\r\n"
                           + "2;\"OK_NAME2\";False\r\n";

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 3
0
        public void Build_UTF8_AddsUTF8Bom()
        {
            var definition = NameDefinition();

            definition.WriteBitOrderMarker = true;
            definition.Encoding            = Encoding.UTF8;
            var result = CsvWriter.CreateStream <TestRow>(definition, oneRow).GetString();

            Assert.AreEqual(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()) + "\"OK_NAME_HEADER\"\r\n\"OK_NAME\"\r\n", result);
        }
Ejemplo n.º 4
0
        public void Build_WithWithUTF8Bom_WritesUTF8Bom()
        {
            var definition = NameDefinition();

            definition.WriteBitOrderMarker = true;

            var result = CsvWriter.CreateStream <TestRow>(definition, oneRow).GetString();

            Assert.AreEqual(UTF8Bom + "\"OK_NAME_HEADER\"\r\n\"OK_NAME\"\r\n", result);
        }
Ejemplo n.º 5
0
        public void Render_ColorNamesArray_WritesAllColorsInQuotedElement()
        {
            var rows = this.GetModelRows();

            var definition = this.GetDefinition();

            var result = CsvWriter.CreateStream(definition, rows).GetString();

            Assert.AreEqual("\"Name\";\"TestColors\"\r\n"
                            + "\"OK_NAME\";\"Red,Green,Blue\"\r\n", result);
        }
Ejemplo n.º 6
0
        public void Build_NoHeaderNamesInFirstRow_HeaderRowIsSkipped()
        {
            var definition = new CsvDefinition <TestRow>(
                new TextCsvColumn <TestRow>(e => e.Name))
            {
                FirstRowContainsHeaders = false,
                WriteBitOrderMarker     = false
            };

            var result = CsvWriter.CreateStream <TestRow>(definition, oneRow).GetString();

            Assert.AreEqual("\"OK_NAME\"\r\n", result);
        }
Ejemplo n.º 7
0
        public void Build_WithoutHeaderNames_NamesAreReflected()
        {
            var definition = new CsvDefinition <TestRow>(
                new TextCsvColumn <TestRow>(e => e.Name))
            {
                FirstRowContainsHeaders = true,
                WriteBitOrderMarker     = false
            };

            var result = CsvWriter.CreateStream <TestRow>(definition, oneRow).GetString();

            Assert.AreEqual("\"Name\"\r\n\"OK_NAME\"\r\n", result);
        }
Ejemplo n.º 8
0
        public void Build_OneTestRowNoEnters_CreatesCsv()
        {
            var def = NameDefinition();

            def.RemoveNewLineCharacters = true;

            var newlines = new[] { new TestRow {
                                       Id = 1, Name = "OK_NAME\r\nENTER", IsActive = true
                                   } };

            var result = CsvWriter.CreateStream <TestRow>(def, newlines).GetString();

            Assert.AreEqual("\"OK_NAME_HEADER\"\r\n\"OK_NAMEENTER\"\r\n", result);
        }
        public void Composition_NotInCollection_ThrowsException()
        {
            var rows = this.GetCompositeRows();

            var definition =
                new CsvDefinition <CompositeTestModel>(
                    new TextCsvColumn <CompositeTestModel>(e => e.Name),
                    new ObjectCsvColumn <CompositeTestModel>(e => e.TestColor),
                    new CompositeCsvColumn <CompositeTestModel, string>(e => e.GetTestColors(), Header1, "NotInCollection"))
            {
                FirstRowContainsHeaders = true
            };

            var result = CsvWriter.CreateStream(definition, rows).GetString();
        }
Ejemplo n.º 10
0
        public void Composition_FromPropertyWithAllHeaderNames_ResolvesAllHeaderName()
        {
            var rows = new[] {
                new DynamicTestModel
                {
                    Name    = "Main name",
                    Columns = new[] { new TestColumn {
                                          Name = "Test One", Type = 1, Value = "One"
                                      }, new TestColumn {
                                          Name = "Test Two", Type = 2, Value = "Two"
                                      } }
                }
            };

            var definition =
                new CsvDefinition <DynamicTestModel>(
                    new TextCsvColumn <DynamicTestModel>(e => e.Name),
                    new DynamicCsvColumn <DynamicTestModel, TestColumn>(e => e.Columns, column =>
            {
                if (column.Type == 1)
                {
                    return(new TextCsvColumn <TestColumn>(sf => sf.Value)
                    {
                        HeaderName = column.Name
                    });
                }
                else if (column.Type == 2)
                {
                    return(new TextCsvColumn <TestColumn>(sf => sf.Value)
                    {
                        HeaderName = column.Name, Format = (v, c) => v.ToString()
                    });
                }

                throw new InvalidOperationException();
            }))
            {
                FirstRowContainsHeaders = true,
                WriteBitOrderMarker     = false
            };

            var result = CsvWriter.CreateStream(definition, rows).GetString();

            Assert.AreEqual("\"Name\";\"Test One\";\"Test Two\"\r\n\"Main name\";\"One\";\"Two\"\r\n", result);
        }
Ejemplo n.º 11
0
        public void Render_ColorNamesArray_DoesNotEscapeSemicolon()
        {
            var rows = new[]
            {
                new ColorArrayModel
                {
                    Name       = "OK_NAME",
                    TestColors = new[] { "Red;Test", "Green,Test", "Blue" }
                }
            };

            var definition = this.GetDefinition();

            var result = CsvWriter.CreateStream(definition, rows).GetString();

            Assert.AreEqual("\"Name\";\"TestColors\"\r\n"
                            + "\"OK_NAME\";\"Red;Test,Green_Test,Blue\"\r\n", result);
        }
        public void CompositeProperty_WithoutHeadernames_ShouldGenerateHeadernames()
        {
            var rows = this.GetCompositeRows();

            var definition =
                new CsvDefinition <CompositeTestModel>(
                    new TextCsvColumn <CompositeTestModel>(e => e.Name),
                    new ObjectCsvColumn <CompositeTestModel>(e => e.TestColor),
                    new CompositeCsvColumn <CompositeTestModel, string>(e => e.GetTestColors())) //// Purple is not in the source
            {
                FirstRowContainsHeaders = true,
                WriteBitOrderMarker     = false
            };

            var result = CsvWriter.CreateStream(definition, rows).GetString();

            Assert.AreEqual("\"Name\";\"TestColor\";\"Header1\";\"Header2\";\"Header3\"\r\n"
                            + "\"OK_NAME\";0;\"ValueOne\";\"ValueTwo\";\"ValueThree\"\r\n", result);
        }
        public void Composition_FromPropertyWithAllHeaderNames_ResolvesAllHeaderName()
        {
            var rows = this.GetCompositeRows();

            var definition =
                new CsvDefinition <CompositeTestModel>(
                    new TextCsvColumn <CompositeTestModel>(e => e.Name),
                    new ObjectCsvColumn <CompositeTestModel>(e => e.TestColor),
                    new CompositeCsvColumn <CompositeTestModel, string>(e => e.TestColors, Header1, Header2, Header3))
            {
                FirstRowContainsHeaders = true,
                WriteBitOrderMarker     = false
            };

            var result = CsvWriter.CreateStream(definition, rows).GetString();

            Assert.AreEqual("\"Name\";\"TestColor\";\"Header1\";\"Header2\";\"Header3\"\r\n"
                            + "\"OK_NAME\";0;\"ValueOne\";\"ValueTwo\";\"ValueThree\"\r\n", result);
        }
Ejemplo n.º 14
0
        public void Render_ForceExcelTextCell_AddsSingleQuote()
        {
            var rows = this.GetModelRows();

            var definition =
                new CsvDefinition <ColorArrayModel>(
                    new TextCsvColumn <ColorArrayModel>(e => e.Name)
            {
                ForceNumberToTextFormatting = true
            })
            {
                FirstRowContainsHeaders = true,
                WriteBitOrderMarker     = false
            };

            var result = CsvWriter.CreateStream(definition, rows).GetString();

            Assert.AreEqual("\"Name\"\r\n"
                            + "=\"OK_NAME\"\r\n", result);
        }
Ejemplo n.º 15
0
        public void Build_OneTestRow_CreatesCsv()
        {
            var result = CsvWriter.CreateStream <TestRow>(NameDefinition(), oneRow).GetString();

            Assert.AreEqual("\"OK_NAME_HEADER\"\r\n\"OK_NAME\"\r\n", result);
        }