コード例 #1
0
        public void GetColumnsTest2()
        {
            string data = "name,age\n" +
                          "Robert,40\n" +
                          "Tom,20\n";

            var csv = CsvDocument.FromCsv(data);

            var columns = csv.GetColumns("age", "name");

            Assert.AreEqual(new[] { "Robert", "Tom" }, columns.ElementAt(1).ToArray());
            Assert.AreEqual(new[] { "40", "20" }, columns.ElementAt(0).ToArray());
        }
コード例 #2
0
        public void FromCsvTest1()
        {
            var csv = "Name,Age\r\n" +
                      "Kara,20\r\n" +
                      "Markus,25,RK-200\r\n";

            var document = CsvDocument.FromCsv(csv, flexible: true);

            Assert.AreEqual(2, document.Count);
            Assert.AreEqual(CsvHeader.FromValues("Name", "Age"), document.Header);
            Assert.AreEqual(new CsvRecord(document.Header, new string[] { "Kara", "20" }), document[0]);
            Assert.AreEqual(new CsvRecord(document.Header, new string[] { "Markus", "25", "RK-200" }), document[1]);
        }
コード例 #3
0
        public void FromCsvTest()
        {
            string csv = "Name,Age\r\n" +
                         "Red,23\r\n" +
                         "Blue,24\r\n";

            var document = CsvDocument <Person> .FromCsv(csv);

            Assert.AreEqual(2, document.Count);
            Assert.AreEqual(new Person {
                Name = "Red", Age = 23
            }, document.GetValue(0));
            Assert.AreEqual(new Person {
                Name = "Blue", Age = 24
            }, document.GetValue(1));
        }
コード例 #4
0
        public void GetColumnTest2()
        {
            string data = "name,age\n" +
                          "Robert,40\n" +
                          "Tom,20\n";

            var csv = CsvDocument.FromCsv(data);

            var names = csv.GetColumn(0);

            Assert.AreEqual(new[] { "Robert", "Tom" }, names.ToArray());

            var ages = csv.GetColumn(1);

            Assert.AreEqual(new[] { "40", "20" }, ages.ToArray());
        }
コード例 #5
0
        public void FromCsvTest1()
        {
            string csv = "Name,Age,PhoneNumber\r\n" +
                         "Red,23,200-1200\r\n" +
                         "Blue,24,233-5565\r\n";

            var phoneNumberParser = new ValueParser(keys: "PhoneNumber", parser: (string key, string value, out object result) =>
            {
                var number = value.Replace("-", string.Empty)
                             .ToCharArray()
                             .Select(e => (byte)char.GetNumericValue(e))
                             .ToArray();

                result = new PhoneNumber(number).ToNullable();
                return(true);
            });

            var document = CsvDocument <Employee> .FromCsv(csv, parsers : new IValueParser[] { phoneNumberParser });

            Assert.AreEqual(2, document.Count);
            Assert.AreEqual(new Employee("Red", 23, new PhoneNumber(2, 0, 0, 1, 2, 0, 0)), document.GetValue(0));
            Assert.AreEqual(new Employee("Blue", 24, new PhoneNumber(2, 3, 3, 5, 5, 6, 5)), document.GetValue(1));
        }