예제 #1
0
        public void CsvImportQuotedStringTest()
        {
            var lines = new[]
            {
                "ColString1;ColString2",
                "No quote1;No quote2",
                "\"Single Quote1\";\"Single Quote2\"",
                "\"Semicolon in string ; Quoted1\";\"Semicolon in string ; Quoted2\"",
                "\"newline in string ", "Quoted1\";\"newline in string ", "Quoted2\"",
                "\"Quote \"\"in\"\" string1\";\"Quote \"\"in\"\" string2\"",
                "\"\"\"\"\"\"\"\";\"\"\"\""
            };

            var csvList = new CsvImport <CsvImportStringClass>().Read(lines);

            csvList.Should().HaveCount(lines.Length - 3);

            csvList[0].Should().BeEquivalentTo(new CsvImportStringClass()
            {
                ColString1 = "No quote1", ColString2 = "No quote2"
            });
            csvList[1].Should().BeEquivalentTo(new CsvImportStringClass()
            {
                ColString1 = "Single Quote1", ColString2 = "Single Quote2"
            });
            csvList[2].Should().BeEquivalentTo(new CsvImportStringClass()
            {
                ColString1 = "Semicolon in string ; Quoted1", ColString2 = "Semicolon in string ; Quoted2"
            });
            csvList[3].Should().BeEquivalentTo(new CsvImportStringClass()
            {
                ColString1 = "newline in string \nQuoted1", ColString2 = "newline in string \nQuoted2"
            });
            csvList[4].Should().BeEquivalentTo(new CsvImportStringClass()
            {
                ColString1 = "Quote \"in\" string1", ColString2 = "Quote \"in\" string2"
            });
            csvList[5].Should().BeEquivalentTo(new CsvImportStringClass()
            {
                ColString1 = "\"\"\"", ColString2 = "\""
            });
        }
예제 #2
0
        public void CsvImportFileTest()
        {
            var lines = new[]
            {
                "ColString;ColInt;ColShort;ColDecimal;ColByte;ColBool;ColLong;ColEnum;ColDate;ColDateAndTime;ColDateAndTimeFraction;ColTimeSpan;ColDouble;ColIntNull;ColShortNull;ColDecimalNull;ColByteNull;ColBoolNull;ColLongNull;ColEnumNull;ColDateNull;ColDateAndTimeNull;ColDateAndTimeFractionNull;ColDoubleNull;ColTimeSpanNull",
                "Str;1;2;2.5;127;true;1234567890;EnumValue1;2018/12/31;2018/12/31 15:56:45;2018/12/31 15:56:45.123;15:56:45;34.12;1;2;2.5;127;false;9876543210;EnumValue1;2018/12/31;2018/12/31 15:56:45;2018/12/31 15:56:45.123;34.12;15:56:45",
                ";1;2;2.5;127;true;1234567890;EnumValue2;2018/12/31;2018/12/31 15:56:45;2018/12/31 15:56:45.123;15:56:45.123;34.12;;;;;;;;;;;"
            };

            var csvList = new CsvImport <CsvImportClass>().Read(lines);

            csvList.Should().HaveCount(lines.Length - 1);

            var csvObjectShouldBe = new CsvImportClass
            {
                ColString                  = "Str",
                ColInt                     = 1,
                ColShort                   = 2,
                ColDecimal                 = 2.5m,
                ColByte                    = 127,
                ColBool                    = true,
                ColLong                    = 1234567890,
                ColEnum                    = CsvImportClass.TestEnum.EnumValue1,
                ColDate                    = 31.December(2018),
                ColDateAndTime             = new DateTime(2018, 12, 31, 15, 56, 45),
                ColDateAndTimeFraction     = new DateTime(2018, 12, 31, 15, 56, 45, 123),
                ColTimeSpan                = new TimeSpan(15, 56, 45),
                ColDouble                  = 34.12,
                ColIntNull                 = 1,
                ColShortNull               = 2,
                ColDecimalNull             = 2.5m,
                ColByteNull                = 127,
                ColBoolNull                = false,
                ColLongNull                = 9876543210,
                ColEnumNull                = CsvImportClass.TestEnum.EnumValue1,
                ColDateNull                = 31.December(2018),
                ColDateAndTimeNull         = new DateTime(2018, 12, 31, 15, 56, 45),
                ColDateAndTimeFractionNull = new DateTime(2018, 12, 31, 15, 56, 45, 123),
                ColTimeSpanNull            = new TimeSpan(15, 56, 45),
                ColDoubleNull              = 34.12
            };
            var csvObjectShouldBeNull = new CsvImportClass
            {
                ColString              = string.Empty,
                ColInt                 = 1,
                ColShort               = 2,
                ColDecimal             = 2.5m,
                ColByte                = 127,
                ColBool                = true,
                ColLong                = 1234567890,
                ColEnum                = CsvImportClass.TestEnum.EnumValue2,
                ColDate                = 31.December(2018),
                ColDateAndTime         = new DateTime(2018, 12, 31, 15, 56, 45),
                ColDateAndTimeFraction = new DateTime(2018, 12, 31, 15, 56, 45, 123),
                ColTimeSpan            = new TimeSpan(0, 15, 56, 45, 123),
                ColDouble              = 34.12,
            };

            var csvObject     = csvList[0];
            var csvObjectNull = csvList[1];

            csvObject.Should().BeEquivalentTo(csvObjectShouldBe);
            csvObjectNull.Should().BeEquivalentTo(csvObjectShouldBeNull);
        }