Exemplo n.º 1
0
        public void RoundtripTest5()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>(ignoreCase: false, throwOnParseErrors: true);

            string s = CsvTypeCode.DateTimeOffset.ToString().ToUpperInvariant();

            _ = (CsvTypeCode?)conv.Parse(s);
        }
Exemplo n.º 2
0
        public void RoundtripTest10(bool throwOnParseErrors, bool ignoreCase, bool nullable)
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>(nullable, true, throwOnParseErrors, ignoreCase);

            string?s = conv.ConvertToString(DBNull.Value);

            Assert.IsNull(s);

            Assert.IsTrue(Convert.IsDBNull(conv.Parse(s)));
        }
Exemplo n.º 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));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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);
    }
Exemplo n.º 8
0
        public void EnumConverterTest1()
        {
            ICsvTypeConverter conv = CsvConverterFactory.CreateEnumConverter <CsvTypeCode>();

            Assert.IsInstanceOfType(conv, typeof(EnumConverter <CsvTypeCode>));
        }
Exemplo n.º 9
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();
        }
    }