//-------------------------------------------------------------------------
        public virtual void test_equalsHashCodeToString()
        {
            CsvFile a1 = CsvFile.of(CharSource.wrap(CSV1), true);
            CsvFile a2 = CsvFile.of(CharSource.wrap(CSV1), true);
            CsvFile b  = CsvFile.of(CharSource.wrap(CSV2), true);
            CsvFile c  = CsvFile.of(CharSource.wrap(CSV3), false);

            // file
            assertEquals(a1.Equals(a1), true);
            assertEquals(a1.Equals(a2), true);
            assertEquals(a1.Equals(b), false);
            assertEquals(a1.Equals(c), false);
            assertEquals(a1.Equals(null), false);
            assertEquals(a1.Equals(ANOTHER_TYPE), false);
            assertEquals(a1.GetHashCode(), a2.GetHashCode());
            assertNotNull(a1.ToString());
            // row
            assertEquals(a1.row(0).Equals(a1.row(0)), true);
            assertEquals(a1.row(0).Equals(a2.row(0)), true);
            assertEquals(a1.row(0).Equals(b.row(0)), false);
            assertEquals(c.row(0).Equals(c.row(1)), false);
            assertEquals(a1.row(0).Equals(ANOTHER_TYPE), false);
            assertEquals(a1.row(0).Equals(null), false);
            assertEquals(a1.row(0).GetHashCode(), a2.row(0).GetHashCode());
            assertNotNull(a1.row(0).ToString());
        }
        public virtual void test_of_simple_with_header_access_by_invalid_field()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), true);

            assertEquals(csvFile.row(0).findField("h3"), null);
            assertThrowsIllegalArg(() => csvFile.row(0).getField("h3"));
        }
        public virtual void test_of_lists_header()
        {
            IList <string>          headers = Arrays.asList("1", "2");
            IList <IList <string> > rows    = Arrays.asList(Arrays.asList("a", "x"), Arrays.asList("b", "y"));
            CsvFile csvFile = CsvFile.of(headers, rows);

            assertEquals(csvFile.headers(), headers);
            assertEquals(csvFile.rows().size(), 2);
            assertEquals(csvFile.row(0).fields(), Arrays.asList("a", "x"));
            assertEquals(csvFile.row(1).fields(), Arrays.asList("b", "y"));
        }
        public virtual void test_of_headerComment()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV7), true);

            assertEquals(csvFile.rowCount(), 1);
            assertEquals(csvFile.row(0).lineNumber(), 3);

            assertEquals(csvFile.headers().size(), 2);
            assertEquals(csvFile.headers().get(0), "h1");
            assertEquals(csvFile.headers().get(1), "h2");
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r1");
            assertEquals(csvFile.row(0).field(1), "r2");
        }
        public virtual void test_of_comment_blank_with_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV2), true);
            ImmutableList <string> headers = csvFile.headers();

            assertEquals(headers.size(), 2);
            assertEquals(headers.get(0), "h1");
            assertEquals(headers.get(1), "h2");
            assertEquals(csvFile.rows().size(), 1);
            assertEquals(csvFile.rowCount(), 1);
            assertEquals(csvFile.row(0).lineNumber(), 5);

            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r21");
            assertEquals(csvFile.row(0).field(1), "r22");
            assertEquals(csvFile.rows().get(0), csvFile.row(0));
        }
        public virtual void test_of_short_data_row()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV6), true);

            assertEquals(csvFile.headers(), ImmutableList.of("a", "b", "c"));
            assertEquals(csvFile.row(0).getField("a"), "r11");
            assertEquals(csvFile.row(0).getField("b"), "");
            assertEquals(csvFile.row(0).getField("c"), "");
            assertEquals(csvFile.row(0).field(0), "r11");
            assertEquals(csvFile.row(0).field(1), "");
            assertEquals(csvFile.row(0).field(2), "");
            assertThrows(() => csvFile.row(0).field(4), typeof(System.IndexOutOfRangeException));

            assertEquals(csvFile.row(1).getField("a"), "r21");
            assertEquals(csvFile.row(1).getField("b"), "r22");
            assertEquals(csvFile.row(1).getField("c"), "");
        }
        public virtual void test_of_quotingWithEquals()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV4B), false);

            assertEquals(csvFile.rowCount(), 3);
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "alpha");
            assertEquals(csvFile.row(0).field(1), "be, \"at\", one");
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "alpha\",\"be\"");
            assertEquals(csvFile.row(1).field(1), "\"at\", one");
            assertEquals(csvFile.row(2).fieldCount(), 2);
            assertEquals(csvFile.row(2).field(0), "r21");
            assertEquals(csvFile.row(2).field(1), " r22 ");
        }
        public virtual void test_of_blank_row()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV3), false);

            assertEquals(csvFile.rowCount(), 2);
            assertEquals(csvFile.row(0).lineNumber(), 1);
            assertEquals(csvFile.row(1).lineNumber(), 3);

            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r11");
            assertEquals(csvFile.row(0).field(1), "r12");
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r21");
            assertEquals(csvFile.row(1).field(1), "r22");
        }
        public virtual void test_of_comment_blank_no_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV2), false);

            assertEquals(csvFile.headers().size(), 0);
            assertEquals(csvFile.rowCount(), 2);
            assertEquals(csvFile.row(0).lineNumber(), 1);
            assertEquals(csvFile.row(1).lineNumber(), 5);

            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "h1");
            assertEquals(csvFile.row(0).field(1), "h2");
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r21");
            assertEquals(csvFile.row(1).field(1), "r22");
        }
Пример #10
0
        public virtual void test_of_duplicate_headers()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV5), true);

            assertEquals(csvFile.headers(), ImmutableList.of("a", "b", "c", "b", "c"));
            assertEquals(csvFile.containsHeader("Foo"), false);
            assertEquals(csvFile.containsHeader("a"), true);
            assertEquals(csvFile.row(0).getField("a"), "aa");
            assertEquals(csvFile.row(0).getField("b"), "b1");
            assertEquals(csvFile.row(0).getField("c"), "c1");

            assertEquals(csvFile.row(0).subRow(1, 3).getField("b"), "b1");
            assertEquals(csvFile.row(0).subRow(1, 3).getField("c"), "c1");
            assertEquals(csvFile.row(0).subRow(3).getField("b"), "b2");
            assertEquals(csvFile.row(0).subRow(3).getField("c"), "c2");
        }
Пример #11
0
        public virtual void test_of_simple_with_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), true);

            assertEquals(csvFile.containsHeader("Foo"), false);
            assertEquals(csvFile.containsHeader("h1"), true);
            assertEquals(csvFile.containsHeader(Pattern.compile("Foo")), false);
            assertEquals(csvFile.containsHeader(Pattern.compile("h[0-9]")), true);
            ImmutableList <string> headers = csvFile.headers();

            assertEquals(headers.size(), 2);
            assertEquals(headers.get(0), "h1");
            assertEquals(headers.get(1), "h2");
            assertEquals(csvFile.rowCount(), 3);
            assertEquals(csvFile.row(0).lineNumber(), 2);
            assertEquals(csvFile.row(1).lineNumber(), 3);
            assertEquals(csvFile.row(2).lineNumber(), 4);

            assertEquals(csvFile.row(0).headers(), headers);
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r11");
            assertEquals(csvFile.row(0).field(1), "r12");
            assertEquals(csvFile.row(1).headers(), headers);
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r21");
            assertEquals(csvFile.row(1).field(1), "r22");

            assertEquals(csvFile.row(0).getField("h1"), "r11");
            assertEquals(csvFile.row(0).getField("h2"), "r12");
            assertEquals(csvFile.row(1).getField("h1"), "r21");
            assertEquals(csvFile.row(1).getField("h2"), "r22");
            assertThrowsIllegalArg(() => csvFile.row(0).getField("zzz"));

            assertEquals(csvFile.row(0).getValue("h1"), "r11");
            assertEquals(csvFile.row(0).getValue("h2"), "r12");
            assertEquals(csvFile.row(1).getValue("h1"), "r21");
            assertEquals(csvFile.row(1).getValue("h2"), "r22");
            assertThrowsIllegalArg(() => csvFile.row(0).getValue("zzz"));
            assertThrowsIllegalArg(() => csvFile.row(2).getValue("h2"));

            assertEquals(csvFile.row(0).findField("h1"), ("r11"));
            assertEquals(csvFile.row(0).findField("h2"), ("r12"));
            assertEquals(csvFile.row(1).findField("h1"), ("r21"));
            assertEquals(csvFile.row(1).findField("h2"), ("r22"));
            assertEquals(csvFile.row(0).findField("zzz"), null);

            assertEquals(csvFile.row(0).findValue("h1"), ("r11"));
            assertEquals(csvFile.row(0).findValue("h2"), ("r12"));
            assertEquals(csvFile.row(1).findValue("h1"), ("r21"));
            assertEquals(csvFile.row(1).findValue("h2"), ("r22"));
            assertEquals(csvFile.row(0).findValue("zzz"), null);
            assertEquals(csvFile.row(2).findValue("h2"), null);

            assertEquals(csvFile.row(0).getField(Pattern.compile("h[13]")), "r11");
            assertEquals(csvFile.row(0).getField(Pattern.compile("h[24]")), "r12");
            assertThrowsIllegalArg(() => csvFile.row(0).getField(Pattern.compile("zzz")));

            assertEquals(csvFile.row(0).getValue(Pattern.compile("h[13]")), "r11");
            assertEquals(csvFile.row(0).getValue(Pattern.compile("h[24]")), "r12");
            assertThrowsIllegalArg(() => csvFile.row(0).getValue(Pattern.compile("zzz")));
            assertThrowsIllegalArg(() => csvFile.row(2).getValue(Pattern.compile("h2")));

            assertEquals(csvFile.row(0).findField(Pattern.compile("h[13]")), ("r11"));
            assertEquals(csvFile.row(0).findField(Pattern.compile("h[24]")), ("r12"));
            assertEquals(csvFile.row(0).findField(Pattern.compile("zzz")), null);

            assertEquals(csvFile.row(0).findValue(Pattern.compile("h[13]")), ("r11"));
            assertEquals(csvFile.row(0).findValue(Pattern.compile("h[24]")), ("r12"));
            assertEquals(csvFile.row(0).findValue(Pattern.compile("zzz")), null);
            assertEquals(csvFile.row(2).findValue(Pattern.compile("h2")), null);

            assertEquals(csvFile.row(0).subRow(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).subRow(1).fieldCount(), 1);
            assertEquals(csvFile.row(0).subRow(2).fieldCount(), 0);

            assertEquals(csvFile.row(0).subRow(0, 0).fieldCount(), 0);
            assertEquals(csvFile.row(0).subRow(0, 1).fieldCount(), 1);
            assertEquals(csvFile.row(0).subRow(2, 2).fieldCount(), 0);
        }
Пример #12
0
        public virtual void test_of_simple_no_header_tabs()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1T), false, '\t');

            assertEquals(csvFile.headers().size(), 0);
            assertEquals(csvFile.containsHeader("Foo"), false);
            assertEquals(csvFile.containsHeader(Pattern.compile("Foo")), false);
            assertEquals(csvFile.rowCount(), 3);
            assertEquals(csvFile.row(0).lineNumber(), 1);
            assertEquals(csvFile.row(1).lineNumber(), 2);
            assertEquals(csvFile.row(2).lineNumber(), 3);

            assertEquals(csvFile.row(0).headers().size(), 0);
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "h1");
            assertEquals(csvFile.row(0).field(1), "h2");
            assertEquals(csvFile.row(1).headers().size(), 0);
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r11");
            assertEquals(csvFile.row(1).field(1), "r12");
            assertEquals(csvFile.row(2).headers().size(), 0);
            assertEquals(csvFile.row(2).fieldCount(), 2);
            assertEquals(csvFile.row(2).field(0), "r21");
            assertEquals(csvFile.row(2).field(1), "r22");
        }