コード例 #1
0
        public void ConvertToStringTest1()
        {
            var dt = new DateTime(1972, 01, 31);
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Date);

            //dt = dt.ToLocalTime();

            string?s = conv.ConvertToString(dt);

            Assert.IsNotNull(s);
        }
コード例 #2
0
        public void CreateHexConverterTest2(
            CsvTypeCode typeCode,
            bool nullable,
            bool throwOnParseErrors,
            Type converterType)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateHexConverter(typeCode, nullable, true, throwOnParseErrors);

            Assert.AreEqual(converterType, conv.Type);
            Assert.IsTrue(Convert.IsDBNull(conv.FallbackValue));
            Assert.AreEqual(throwOnParseErrors, conv.ThrowsOnParseErrors);
        }
コード例 #3
0
        public void RoundtripTest8(bool throwOnParseErrors, bool ignoreCase)
        {
            CsvTypeCode?val = null;

            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>(true, false, throwOnParseErrors, ignoreCase);

            string?s = conv.ConvertToString(val);

            Assert.IsNull(s);

            Assert.IsNull(conv.Parse(s));
        }
コード例 #4
0
        public void TryGetValueTest()
        {
            KeyedCollection <string, CsvProperty> kColl = new TestCollection();

            var prop1 = new CsvProperty("Test", new string[0], CsvConverterFactory.CreateConverter(CsvTypeCode.Boolean));

            kColl.Add(prop1);

            Assert.IsTrue(kColl.TryGetValue("Test", out CsvProperty? prop2));

            Assert.AreEqual(prop1, prop2);
        }
コード例 #5
0
        public void ConvertToStringTest()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, true, true);

            Assert.IsNull(conv.ConvertToString(DBNull.Value));

            Assert.IsNull(conv.ConvertToString(null));

            const string test = "Test";

            Assert.AreEqual(test, conv.ConvertToString(test));
        }
コード例 #6
0
        public void CreateConverterTest2(
            CsvTypeCode typeCode,
            bool nullable,
            bool throwOnParseErrors,
            Type converterType,
            object?fallBackValue)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(typeCode, nullable, false, throwOnParseErrors: throwOnParseErrors);

            Assert.AreEqual(converterType, conv.Type);
            Assert.AreEqual(fallBackValue, conv.FallbackValue);
            Assert.AreEqual(throwOnParseErrors, conv.ThrowsOnParseErrors);
        }
コード例 #7
0
        public void Roundtrip1()
        {
            TimeSpan now = DateTime.UtcNow.TimeOfDay;

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.TimeSpan);

            string?tmp = conv.ConvertToString(now);

            Assert.IsNotNull(tmp);

            var now2 = (TimeSpan?)conv.Parse(tmp);

            Assert.AreEqual(now, now2);
        }
コード例 #8
0
        public void ParseTest()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, true);

            Assert.IsNull(conv.Parse(null));

            conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String, false);

            Assert.IsNotNull(conv.Parse(null));

            const string test = "Test";

            Assert.AreEqual(test, conv.Parse(test));
        }
コード例 #9
0
        public void Roundtrip1()
        {
            var guid = Guid.NewGuid();

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Guid);

            string?tmp = conv.ConvertToString(guid);

            Assert.IsNotNull(tmp);

            var now2 = (Guid?)conv.Parse(tmp);

            Assert.AreEqual(guid, now2);
        }
コード例 #10
0
        public void RoundtripTest1()
        {
            double d = 72.81;

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Double);

            string?s = conv.ConvertToString(d);

            Assert.IsNotNull(s);

            double?d2 = (double?)conv.Parse(s);

            Assert.AreEqual(d, d2);
        }
コード例 #11
0
        public void RoundtripTest2()
        {
            int i = 123456789;

            ICsvTypeConverter conv = CsvConverterFactory.CreateHexConverter(CsvTypeCode.Int32);

            string?s = conv.ConvertToString(i)?.ToLowerInvariant();

            Assert.IsNotNull(s);

            var i2 = (int?)conv.Parse(s);

            Assert.AreEqual(i, i2);
        }
コード例 #12
0
        public void RoundtripTest2()
        {
            CsvTypeCode val = CsvTypeCode.DateTimeOffset;

            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>("F");

            string?s = conv.ConvertToString(val);

            Assert.IsNotNull(s);

            var val2 = (CsvTypeCode?)conv.Parse(s);

            Assert.AreEqual(val, val2);
        }
コード例 #13
0
        public AccessBenchmark()
        {
            _csv = Properties.Resources.Test1;
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.String);

            _indexWrapper = new CsvRecordWrapper();
            _indexWrapper.AddProperty(new CsvIndexProperty("Column0", 0, conv));
            _indexWrapper.AddProperty(new CsvIndexProperty("Column1", 1, conv));
            _indexWrapper.AddProperty(new CsvIndexProperty("Column2", 2, conv));

            _nameWrapper = new CsvRecordWrapper();
            _nameWrapper.AddProperty(new CsvProperty("Column0", new string[] { "Column0" }, conv));
            _nameWrapper.AddProperty(new CsvProperty("Column1", new string[] { "Column1" }, conv));
            _nameWrapper.AddProperty(new CsvProperty("Column2", new string[] { "Column2" }, conv));
        }
コード例 #14
0
        public void Roundtrip1()
        {
            DateTimeOffset now = new DateTime(2021, 3, 1, 17, 25, 38, DateTimeKind.Local);

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTimeOffset);

            string?tmp = conv.ConvertToString(now);

            Assert.IsNotNull(tmp);

            var now2 = (DateTimeOffset?)conv.Parse(tmp);


            Assert.AreEqual(now, now2);
        }
コード例 #15
0
        public void RoundtripTest6()
        {
            CsvTypeCode val = CsvTypeCode.DateTimeOffset;

            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>("F", ignoreCase: false, throwOnParseErrors: true);

            string?s = conv.ConvertToString(val);

            Assert.IsNotNull(s);

            s = s !.ToUpperInvariant();


            _ = (CsvTypeCode?)conv.Parse(s);
        }
コード例 #16
0
        public void DateTimeOffsetConverterTest0()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTimeOffset);

            Assert.IsInstanceOfType(conv, typeof(DateTimeOffsetConverter));

            var dt = new DateTime(1975, 07, 14);

            string?s = conv.ConvertToString(new DateTimeOffset(dt));

            Assert.IsNotNull(s);

            var dto = (DateTimeOffset)conv.Parse(s) !;

            Assert.AreEqual(dt, dto.DateTime);
        }
コード例 #17
0
        public void RoundtripTest3()
        {
            CsvTypeCode val = CsvTypeCode.DateTimeOffset;

            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>(ignoreCase: true);

            string?s = conv.ConvertToString(val);

            Assert.IsNotNull(s);

            s = s !.ToUpperInvariant();

            var val2 = (CsvTypeCode?)conv.Parse(s);

            Assert.AreEqual(val, val2);
        }
コード例 #18
0
        public void CreateConverterTest9(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Guid, false, false, throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(GuidConverter));
            Assert.AreEqual(typeof(Guid), conv.Type);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);
            Assert.AreEqual(conv.FallbackValue, default(Guid));

            var test1 = Guid.NewGuid();

            string?s = conv.ConvertToString(test1);

            var test2 = (Guid?)conv.Parse(s);

            Assert.AreEqual(test1, test2);
        }
コード例 #19
0
        public void RoundtripTest1()
        {
            var bytes = new byte[7];

            using var rnd = RandomNumberGenerator.Create();
            rnd.GetBytes(bytes);

            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.ByteArray);

            string?s = conv.ConvertToString(bytes);

            Assert.IsNotNull(s);

            var bytes2 = (byte[]?)conv.Parse(s);

            CollectionAssert.AreEqual(bytes, bytes2);
        }
コード例 #20
0
        public void CreateConverterTest12(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTime, false, false, CultureInfo.InvariantCulture, throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(DateTimeConverter));
            Assert.AreEqual(typeof(DateTime), conv.Type);
            Assert.AreEqual(default(DateTime), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            var    test = new DateTime(1956, 12, 24, 14, 27, 32);
            string?s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            var test2 = (DateTime?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
コード例 #21
0
        public void CreateConverterTest7(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.Decimal, false, false, CultureInfo.CreateSpecificCulture("de-DE"), throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(NumberConverter <decimal>));
            Assert.AreEqual(typeof(decimal), conv.Type);
            Assert.AreEqual(default(decimal), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            decimal test = 57.839m;
            string? s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            decimal?test2 = (decimal?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
コード例 #22
0
        public void CreateConverterTest15(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.DateTimeOffset, false, false, CultureInfo.CreateSpecificCulture("de-DE"), throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(DateTimeOffsetConverter));
            Assert.AreEqual(typeof(DateTimeOffset), conv.Type);
            Assert.AreEqual(default(DateTimeOffset), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            var    test = new DateTimeOffset(1956, 12, 24, 14, 27, 32, TimeSpan.FromHours(3));
            string?s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            var test2 = (DateTimeOffset?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
コード例 #23
0
        public void CreateConverterTest17(bool throwsOnParseErrors)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateConverter(CsvTypeCode.TimeSpan, false, false, CultureInfo.CreateSpecificCulture("de-DE"), throwOnParseErrors: throwsOnParseErrors);

            Assert.IsInstanceOfType(conv, typeof(TimeSpanConverter));
            Assert.AreEqual(typeof(TimeSpan), conv.Type);
            Assert.AreEqual(default(TimeSpan), conv.FallbackValue);
            Assert.AreEqual(throwsOnParseErrors, conv.ThrowsOnParseErrors);

            var    test = new TimeSpan(16, 24, 53);
            string?s    = conv.ConvertToString(test);

            Assert.IsNotNull(s);

            var test2 = (TimeSpan?)conv.Parse(s);

            Assert.AreEqual(test, test2);
        }
コード例 #24
0
    protected override void InitCsvRecordWrapperUndefinedValues(Tuple <string, ContactProp?, IList <string> > tpl, CsvRecordWrapper wrapper)
    {
        Debug.Assert(tpl.Item2.HasValue);


        if (tpl.Item2 == (ContactProp)AdditionalProp.Swap)
        {
            wrapper.AddProperty(
                new CsvProperty(
                    tpl.Item1,
                    tpl.Item3,
                    CsvConverterFactory.CreateConverter(CsvTypeCode.String, false)));
        }
        else
        {
            wrapper.AddProperty(
                new CsvProperty(
                    tpl.Item1,
                    tpl.Item3,
                    this.StringConverter));
        }
    }
コード例 #25
0
    private static CsvRecordWrapper InitCsvRecordWrapper()
    {
        var wrapper = new CsvRecordWrapper();

        // Store the stringConverter because you can reuse the same
        // converter for more than one property in CsvRecordWrapper.
        ICsvTypeConverter stringConverter =
            CsvConverterFactory.CreateConverter(CsvTypeCode.String, maybeDBNull: true);

        wrapper.AddProperty
        (
            new CsvProperty(PUPILS_NAME,
                            new string[] { PUPILS_NAME },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty(SUBJECT,
                            new string[] { SUBJECT },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty(LESSON_DAY,
                            new string[] { LESSON_DAY },
                            CsvConverterFactory
                            .CreateEnumConverter <DayOfWeek>("G", maybeDBNull: true))
        );
        wrapper.AddProperty
        (
            new CsvProperty(LESSON_BEGIN,
                            new string[] { LESSON_BEGIN },
                            CsvConverterFactory
                            .CreateConverter(CsvTypeCode.TimeSpan, maybeDBNull: true))
        );

        return(wrapper);
    }
コード例 #26
0
        public void CsvPropertyTest1()
        {
            var prop = new CsvProperty("Prop", new string[] { "Col1" }, CsvConverterFactory.CreateConverter(CsvTypeCode.String));

            Assert.IsInstanceOfType(prop, typeof(CsvProperty));
        }
コード例 #27
0
        public void HexConverterTest1()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateHexConverter(CsvTypeCode.Int32);

            Assert.IsInstanceOfType(conv, typeof(HexConverter <int>));
        }
コード例 #28
0
 public void HexConverterTest2() => _ = CsvConverterFactory.CreateHexConverter(CsvTypeCode.Double);
コード例 #29
0
        public void EnumConverterTest1()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>();

            Assert.IsInstanceOfType(conv, typeof(EnumConverter <CsvTypeCode>));
        }
コード例 #30
0
    public static void TestDeserializingClassesFromCsv()
    {
        const string csvFileName = "Objects.csv";

        // Create a nonstandard CSV-File
        File.WriteAllText(csvFileName, new StringBuilder()
                          .AppendLine(
                              "Unterrichtstag;Unterrichtsbeginn;Vollständiger Name;Unterrichtsfach;")
                          .AppendLine(
                              "Wednesday;14:30;Susi Meyer;Piano")
                          .AppendLine(
                              "Thursday;15:15;Carl Czerny;Piano;")
                          .AppendLine(
                              ";;Frederic Chopin")
                          .ToString());

        // Initialize a CsvRecordWrapper which retrieves the data from
        // the CSV-Columns and converts it to the right data type.
        // Aliases with wildcards can be used to match the column-headers
        // of the CSV file.
        var wrapper = new CsvRecordWrapper();

        // Reuse a converter for more than one property:
        ICsvTypeConverter stringConverter =
            CsvConverterFactory.CreateConverter(CsvTypeCode.String, nullable: true);

        wrapper.AddProperty
        (
            new CsvProperty("Name",
                            new string[] { "*name" },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty("Subject",
                            new string[] { "*subject", "*fach" },
                            stringConverter)
        );
        wrapper.AddProperty
        (
            new CsvProperty("LessonDay",
                            new string[] { "*day", "*tag" },
                            CsvConverterFactory
                            .CreateEnumConverter <DayOfWeek>(nullable: true))
        );
        wrapper.AddProperty
        (
            new CsvProperty("LessonBegin",
                            new string[] { "*begin?" },
                            CsvConverterFactory
                            .CreateConverter(CsvTypeCode.TimeSpan, nullable: true))
        );

        // Analyze the CSV file to determine the right parameters
        // for proper reading:
        var analyzer = new CsvAnalyzer();

        analyzer.Analyze(csvFileName);

        // Read the CSV file:
        using var reader =
                  new CsvReader(csvFileName,
                                analyzer.HasHeaderRow,
                                analyzer.Options,
                                analyzer.FieldSeparator);

        var pupilsList = new List <Pupil>();

        foreach (CsvRecord record in reader.Read())
        {
            wrapper.Record = record;

            // Using a dynamic variable enables you to assign
            // the properties without having to explicitely cast them
            // to the target data type:
            dynamic dynWrapper = wrapper;

            pupilsList.Add(new Pupil
            {
                Name        = dynWrapper.Name,
                LessonBegin = dynWrapper.LessonBegin,
                LessonDay   = dynWrapper.LessonDay,
                Subject     = dynWrapper.Subject
            });
        }

        // Write the results to Console:
        foreach (Pupil pupil in pupilsList)
        {
            Console.WriteLine(pupil);
            Console.WriteLine();
        }
    }