Exemple #1
0
        public async Task TestTypedValues()
        {
            var csvData =
                "A;B;C;D;E" + Environment.NewLine +
                "22;Text;true;01.01.2010;A" + Environment.NewLine +
                "-31;A long text;false;20.01.2050;B" + Environment.NewLine +
                "55;\"A text with ;\";;31.07.1971;C";

            using var csvReader = new CsvReader(csvData);

            var csvTableReader = new CsvTableReader(csvReader);

            Assert.NotNull(csvTableReader);

            csvTableReader.SetFormats <DateTime>("dd.MM.yyyy");

            await csvTableReader.ReadHeadersAsync();

            Assert.Equal(5, csvTableReader.Headers.Count);
            Assert.Equal("A", csvTableReader.Headers[0]);
            Assert.Equal("B", csvTableReader.Headers[1]);
            Assert.Equal("C", csvTableReader.Headers[2]);
            Assert.Equal("D", csvTableReader.Headers[3]);
            Assert.Equal("E", csvTableReader.Headers[4]);

            await csvTableReader.ReadAsync();

            Assert.Equal(22, csvTableReader.GetValue <int>("A"));
            Assert.Equal("Text", csvTableReader.GetValue <string>("B"));
            Assert.True(csvTableReader.GetValue <bool>("C"));
            Assert.Equal(new DateTime(2010, 1, 1), csvTableReader.GetValue <DateTime>("D"));
            Assert.Equal(SampleEnum.A, csvTableReader.GetValue <SampleEnum>("E"));

            await csvTableReader.ReadAsync();

            Assert.Equal(-31, csvTableReader.GetValue <int>("A"));
            Assert.Equal("A long text", csvTableReader.GetValue <string>("B"));
            Assert.False(csvTableReader.GetValue <bool>("C"));
            Assert.Equal(new DateTime(2050, 1, 20), csvTableReader.GetValue <DateTime>("D"));
            Assert.Equal(SampleEnum.B, csvTableReader.GetValue <SampleEnum?>("E"));

            await csvTableReader.ReadAsync();

            Assert.True(csvTableReader.TryGetValue <int>("A", out var a));
            Assert.Equal(55, a);
            Assert.True(csvTableReader.TryGetValue <string>("B", out var b));
            Assert.Equal("A text with ;", b);
            Assert.True(csvTableReader.TryGetValue <bool?>("C", out var c));
            Assert.Null(c);
            Assert.True(csvTableReader.TryGetValue <DateTime>("D", out var d));
            Assert.Equal(new DateTime(1971, 7, 31), d);
        }
Exemple #2
0
        public async Task TestJsonValues()
        {
            var csvData =
                "A;B" + Environment.NewLine +
                "42;\"{\"\"IntValue\"\":42,\"\"StrValue\"\":\"\"Forty-Two\"\"}\"" + Environment.NewLine +
                "5;\"{\"\"IntValue\"\":5,\"\"StrValue\"\":\"\"Five\"\"}\"";

            using var csvReader = new CsvReader(csvData);

            var csvTableReader = new CsvTableReader(csvReader);

            Assert.NotNull(csvTableReader);

            csvTableReader.AddConverter <CustomType>(new CustomTypeConverter());

            await csvTableReader.ReadHeadersAsync();

            Assert.Equal(2, csvTableReader.Headers.Count);
            Assert.Equal("A", csvTableReader.Headers[0]);
            Assert.Equal("B", csvTableReader.Headers[1]);

            await csvTableReader.ReadAsync();

            Assert.Equal(42, csvTableReader.GetValue <int>("A"));
            var o1 = csvTableReader.GetValue <CustomType>("B");

            Assert.Equal(42, o1.IntValue);
            Assert.Equal("Forty-Two", o1.StrValue);

            await csvTableReader.ReadAsync();

            Assert.Equal(5, csvTableReader.GetValue <int>("A"));
            var o2 = csvTableReader.GetValue <CustomType>("B");

            Assert.Equal(5, o2.IntValue);
            Assert.Equal("Five", o2.StrValue);
        }