StephanZ-fastercsv 1.4.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/csv_converters.rb +28 -0
 - data/examples/csv_filter.rb +23 -0
 - data/examples/csv_reading.rb +57 -0
 - data/examples/csv_table.rb +56 -0
 - data/examples/csv_writing.rb +67 -0
 - data/examples/purchase.csv +3 -0
 - data/examples/shortcut_interface.rb +36 -0
 - data/lib/faster_csv.rb +1972 -0
 - data/lib/fastercsv.rb +10 -0
 - data/test/tc_csv_parsing.rb +171 -0
 - data/test/tc_csv_writing.rb +96 -0
 - data/test/tc_data_converters.rb +260 -0
 - data/test/tc_encodings.rb +23 -0
 - data/test/tc_features.rb +212 -0
 - data/test/tc_headers.rb +277 -0
 - data/test/tc_interface.rb +388 -0
 - data/test/tc_row.rb +305 -0
 - data/test/tc_serialization.rb +154 -0
 - data/test/tc_speed.rb +65 -0
 - data/test/tc_table.rb +400 -0
 - data/test/test_data.csv +1000 -0
 - metadata +24 -3
 - data/test/line_endings.gz +0 -0
 
| 
         @@ -0,0 +1,388 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/local/bin/ruby -w
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            # tc_interface.rb
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            #  Created by James Edward Gray II on 2005-11-14.
         
     | 
| 
      
 6 
     | 
    
         
            +
            #  Copyright 2005 Gray Productions. All rights reserved.
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            require "test/unit"
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            require "faster_csv"
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            class TestFasterCSVInterface < Test::Unit::TestCase
         
     | 
| 
      
 13 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 14 
     | 
    
         
            +
                @path = File.join(File.dirname(__FILE__), "temp_test_data.csv")
         
     | 
| 
      
 15 
     | 
    
         
            +
                
         
     | 
| 
      
 16 
     | 
    
         
            +
                File.open(@path, "w") do |file|
         
     | 
| 
      
 17 
     | 
    
         
            +
                  file << "1\t2\t3\r\n"
         
     | 
| 
      
 18 
     | 
    
         
            +
                  file << "4\t5\r\n"
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                @expected = [%w{1 2 3}, %w{4 5}]
         
     | 
| 
      
 22 
     | 
    
         
            +
              end
         
     | 
| 
      
 23 
     | 
    
         
            +
              
         
     | 
| 
      
 24 
     | 
    
         
            +
              def teardown
         
     | 
| 
      
 25 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 26 
     | 
    
         
            +
              end
         
     | 
| 
      
 27 
     | 
    
         
            +
              
         
     | 
| 
      
 28 
     | 
    
         
            +
              ### Test Read Interface ###
         
     | 
| 
      
 29 
     | 
    
         
            +
              
         
     | 
| 
      
 30 
     | 
    
         
            +
              def test_foreach
         
     | 
| 
      
 31 
     | 
    
         
            +
                FasterCSV.foreach(@path, :col_sep => "\t", :row_sep => "\r\n") do |row|
         
     | 
| 
      
 32 
     | 
    
         
            +
                  assert_equal(@expected.shift, row)
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
              end
         
     | 
| 
      
 35 
     | 
    
         
            +
              
         
     | 
| 
      
 36 
     | 
    
         
            +
              def test_open_and_close
         
     | 
| 
      
 37 
     | 
    
         
            +
                csv = FasterCSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n")
         
     | 
| 
      
 38 
     | 
    
         
            +
                assert_not_nil(csv)
         
     | 
| 
      
 39 
     | 
    
         
            +
                assert_instance_of(FasterCSV, csv)
         
     | 
| 
      
 40 
     | 
    
         
            +
                assert_equal(false, csv.closed?)
         
     | 
| 
      
 41 
     | 
    
         
            +
                csv.close
         
     | 
| 
      
 42 
     | 
    
         
            +
                assert(csv.closed?)
         
     | 
| 
      
 43 
     | 
    
         
            +
                
         
     | 
| 
      
 44 
     | 
    
         
            +
                ret = FasterCSV.open(@path) do |csv|
         
     | 
| 
      
 45 
     | 
    
         
            +
                  assert_instance_of(FasterCSV, csv)
         
     | 
| 
      
 46 
     | 
    
         
            +
                  "Return value."
         
     | 
| 
      
 47 
     | 
    
         
            +
                end
         
     | 
| 
      
 48 
     | 
    
         
            +
                assert(csv.closed?)
         
     | 
| 
      
 49 
     | 
    
         
            +
                assert_equal("Return value.", ret)
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
              
         
     | 
| 
      
 52 
     | 
    
         
            +
              def test_parse
         
     | 
| 
      
 53 
     | 
    
         
            +
                data = File.read(@path)
         
     | 
| 
      
 54 
     | 
    
         
            +
                assert_equal( @expected,
         
     | 
| 
      
 55 
     | 
    
         
            +
                              FasterCSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") )
         
     | 
| 
      
 56 
     | 
    
         
            +
             
     | 
| 
      
 57 
     | 
    
         
            +
                FasterCSV.parse(data, :col_sep => "\t", :row_sep => "\r\n") do |row|
         
     | 
| 
      
 58 
     | 
    
         
            +
                  assert_equal(@expected.shift, row)
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
              end
         
     | 
| 
      
 61 
     | 
    
         
            +
              
         
     | 
| 
      
 62 
     | 
    
         
            +
              def test_parse_line
         
     | 
| 
      
 63 
     | 
    
         
            +
                row = FasterCSV.parse_line("1;2;3", :col_sep => ";")
         
     | 
| 
      
 64 
     | 
    
         
            +
                assert_not_nil(row)
         
     | 
| 
      
 65 
     | 
    
         
            +
                assert_instance_of(Array, row)
         
     | 
| 
      
 66 
     | 
    
         
            +
                assert_equal(%w{1 2 3}, row)
         
     | 
| 
      
 67 
     | 
    
         
            +
                
         
     | 
| 
      
 68 
     | 
    
         
            +
                # shortcut interface
         
     | 
| 
      
 69 
     | 
    
         
            +
                row = "1;2;3".parse_csv(:col_sep => ";")
         
     | 
| 
      
 70 
     | 
    
         
            +
                assert_not_nil(row)
         
     | 
| 
      
 71 
     | 
    
         
            +
                assert_instance_of(Array, row)
         
     | 
| 
      
 72 
     | 
    
         
            +
                assert_equal(%w{1 2 3}, row)
         
     | 
| 
      
 73 
     | 
    
         
            +
              end
         
     | 
| 
      
 74 
     | 
    
         
            +
              
         
     | 
| 
      
 75 
     | 
    
         
            +
              def test_read_and_readlines
         
     | 
| 
      
 76 
     | 
    
         
            +
                assert_equal( @expected,
         
     | 
| 
      
 77 
     | 
    
         
            +
                              FasterCSV.read(@path, :col_sep => "\t", :row_sep => "\r\n") )
         
     | 
| 
      
 78 
     | 
    
         
            +
                assert_equal( @expected,
         
     | 
| 
      
 79 
     | 
    
         
            +
                              FasterCSV.readlines( @path,
         
     | 
| 
      
 80 
     | 
    
         
            +
                                                   :col_sep => "\t", :row_sep => "\r\n") )
         
     | 
| 
      
 81 
     | 
    
         
            +
                
         
     | 
| 
      
 82 
     | 
    
         
            +
                
         
     | 
| 
      
 83 
     | 
    
         
            +
                data = FasterCSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
         
     | 
| 
      
 84 
     | 
    
         
            +
                  csv.read
         
     | 
| 
      
 85 
     | 
    
         
            +
                end
         
     | 
| 
      
 86 
     | 
    
         
            +
                assert_equal(@expected, data)
         
     | 
| 
      
 87 
     | 
    
         
            +
                data = FasterCSV.open(@path, :col_sep => "\t", :row_sep => "\r\n") do |csv|
         
     | 
| 
      
 88 
     | 
    
         
            +
                  csv.readlines
         
     | 
| 
      
 89 
     | 
    
         
            +
                end
         
     | 
| 
      
 90 
     | 
    
         
            +
                assert_equal(@expected, data)
         
     | 
| 
      
 91 
     | 
    
         
            +
              end
         
     | 
| 
      
 92 
     | 
    
         
            +
              
         
     | 
| 
      
 93 
     | 
    
         
            +
              def test_table
         
     | 
| 
      
 94 
     | 
    
         
            +
                table = FasterCSV.table(@path, :col_sep => "\t", :row_sep => "\r\n")
         
     | 
| 
      
 95 
     | 
    
         
            +
                assert_instance_of(FasterCSV::Table, table)
         
     | 
| 
      
 96 
     | 
    
         
            +
                assert_equal([[:"1", :"2", :"3"], [4, 5, nil]], table.to_a)
         
     | 
| 
      
 97 
     | 
    
         
            +
              end
         
     | 
| 
      
 98 
     | 
    
         
            +
              
         
     | 
| 
      
 99 
     | 
    
         
            +
              def test_shift  # aliased as gets() and readline()
         
     | 
| 
      
 100 
     | 
    
         
            +
                FasterCSV.open(@path, "r+", :col_sep => "\t", :row_sep => "\r\n") do |csv|
         
     | 
| 
      
 101 
     | 
    
         
            +
                  assert_equal(@expected.shift, csv.shift)
         
     | 
| 
      
 102 
     | 
    
         
            +
                  assert_equal(@expected.shift, csv.shift)
         
     | 
| 
      
 103 
     | 
    
         
            +
                  assert_equal(nil, csv.shift)
         
     | 
| 
      
 104 
     | 
    
         
            +
                end
         
     | 
| 
      
 105 
     | 
    
         
            +
              end
         
     | 
| 
      
 106 
     | 
    
         
            +
             
     | 
| 
      
 107 
     | 
    
         
            +
              def test_long_line # ruby's regex parser may have problems with long rows
         
     | 
| 
      
 108 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
                long_field_length = 2800
         
     | 
| 
      
 111 
     | 
    
         
            +
                File.open(@path, "w") do |file|
         
     | 
| 
      
 112 
     | 
    
         
            +
                  file << "1\t2\t#{'3' * long_field_length}\r\n"
         
     | 
| 
      
 113 
     | 
    
         
            +
                end
         
     | 
| 
      
 114 
     | 
    
         
            +
                @expected = [%w{1 2} + ['3' * long_field_length]]
         
     | 
| 
      
 115 
     | 
    
         
            +
                test_shift
         
     | 
| 
      
 116 
     | 
    
         
            +
              end
         
     | 
| 
      
 117 
     | 
    
         
            +
              
         
     | 
| 
      
 118 
     | 
    
         
            +
              ### Test Write Interface ###
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
              def test_generate
         
     | 
| 
      
 121 
     | 
    
         
            +
                str = FasterCSV.generate do |csv|  # default empty String
         
     | 
| 
      
 122 
     | 
    
         
            +
                  assert_instance_of(FasterCSV, csv)
         
     | 
| 
      
 123 
     | 
    
         
            +
                  assert_equal(csv, csv << [1, 2, 3])
         
     | 
| 
      
 124 
     | 
    
         
            +
                  assert_equal(csv, csv << [4, nil, 5])
         
     | 
| 
      
 125 
     | 
    
         
            +
                end
         
     | 
| 
      
 126 
     | 
    
         
            +
                assert_not_nil(str)
         
     | 
| 
      
 127 
     | 
    
         
            +
                assert_instance_of(String, str)
         
     | 
| 
      
 128 
     | 
    
         
            +
                assert_equal("1,2,3\n4,,5\n", str)
         
     | 
| 
      
 129 
     | 
    
         
            +
             
     | 
| 
      
 130 
     | 
    
         
            +
                FasterCSV.generate(str) do |csv|   # appending to a String
         
     | 
| 
      
 131 
     | 
    
         
            +
                  assert_equal(csv, csv << ["last", %Q{"row"}])
         
     | 
| 
      
 132 
     | 
    
         
            +
                end
         
     | 
| 
      
 133 
     | 
    
         
            +
                assert_equal(%Q{1,2,3\n4,,5\nlast,"""row"""\n}, str)
         
     | 
| 
      
 134 
     | 
    
         
            +
              end
         
     | 
| 
      
 135 
     | 
    
         
            +
              
         
     | 
| 
      
 136 
     | 
    
         
            +
              def test_generate_line
         
     | 
| 
      
 137 
     | 
    
         
            +
                line = FasterCSV.generate_line(%w{1 2 3}, :col_sep => ";")
         
     | 
| 
      
 138 
     | 
    
         
            +
                assert_not_nil(line)
         
     | 
| 
      
 139 
     | 
    
         
            +
                assert_instance_of(String, line)
         
     | 
| 
      
 140 
     | 
    
         
            +
                assert_equal("1;2;3\n", line)
         
     | 
| 
      
 141 
     | 
    
         
            +
                
         
     | 
| 
      
 142 
     | 
    
         
            +
                # shortcut interface
         
     | 
| 
      
 143 
     | 
    
         
            +
                line = %w{1 2 3}.to_csv(:col_sep => ";")
         
     | 
| 
      
 144 
     | 
    
         
            +
                assert_not_nil(line)
         
     | 
| 
      
 145 
     | 
    
         
            +
                assert_instance_of(String, line)
         
     | 
| 
      
 146 
     | 
    
         
            +
                assert_equal("1;2;3\n", line)
         
     | 
| 
      
 147 
     | 
    
         
            +
              end
         
     | 
| 
      
 148 
     | 
    
         
            +
             
     | 
| 
      
 149 
     | 
    
         
            +
              def test_write_header_detection
         
     | 
| 
      
 150 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 151 
     | 
    
         
            +
             
     | 
| 
      
 152 
     | 
    
         
            +
                headers = %w{a b c}
         
     | 
| 
      
 153 
     | 
    
         
            +
                FasterCSV.open(@path, "w", :headers => true) do |csv|
         
     | 
| 
      
 154 
     | 
    
         
            +
                  csv << headers
         
     | 
| 
      
 155 
     | 
    
         
            +
                  csv << %w{1 2 3}
         
     | 
| 
      
 156 
     | 
    
         
            +
                  assert_equal(headers, csv.instance_variable_get(:@headers))
         
     | 
| 
      
 157 
     | 
    
         
            +
                end
         
     | 
| 
      
 158 
     | 
    
         
            +
              end
         
     | 
| 
      
 159 
     | 
    
         
            +
             
     | 
| 
      
 160 
     | 
    
         
            +
              def test_write_lineno
         
     | 
| 
      
 161 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
                FasterCSV.open(@path, "w") do |csv|
         
     | 
| 
      
 164 
     | 
    
         
            +
                  lines = 20
         
     | 
| 
      
 165 
     | 
    
         
            +
                  lines.times { csv << %w{a b c} }
         
     | 
| 
      
 166 
     | 
    
         
            +
                  assert_equal(lines, csv.lineno)
         
     | 
| 
      
 167 
     | 
    
         
            +
                end
         
     | 
| 
      
 168 
     | 
    
         
            +
              end
         
     | 
| 
      
 169 
     | 
    
         
            +
             
     | 
| 
      
 170 
     | 
    
         
            +
              def test_write_hash
         
     | 
| 
      
 171 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 172 
     | 
    
         
            +
             
     | 
| 
      
 173 
     | 
    
         
            +
                lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
         
     | 
| 
      
 174 
     | 
    
         
            +
                FasterCSV.open( @path, "w", :headers           => true,
         
     | 
| 
      
 175 
     | 
    
         
            +
                                            :header_converters => :symbol ) do |csv|
         
     | 
| 
      
 176 
     | 
    
         
            +
                  csv << lines.first.keys
         
     | 
| 
      
 177 
     | 
    
         
            +
                  lines.each { |line| csv << line }
         
     | 
| 
      
 178 
     | 
    
         
            +
                end
         
     | 
| 
      
 179 
     | 
    
         
            +
                FasterCSV.open( @path, "w", :headers           => true,
         
     | 
| 
      
 180 
     | 
    
         
            +
                                            :converters        => :all,
         
     | 
| 
      
 181 
     | 
    
         
            +
                                            :header_converters => :symbol ) do |csv|
         
     | 
| 
      
 182 
     | 
    
         
            +
                  csv.each { |line| assert_equal(lines.shift, line.to_hash) }
         
     | 
| 
      
 183 
     | 
    
         
            +
                end
         
     | 
| 
      
 184 
     | 
    
         
            +
              end
         
     | 
| 
      
 185 
     | 
    
         
            +
             
     | 
| 
      
 186 
     | 
    
         
            +
              def test_write_hash_with_headers_array
         
     | 
| 
      
 187 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 188 
     | 
    
         
            +
             
     | 
| 
      
 189 
     | 
    
         
            +
                lines = [{:a => 1, :b => 2, :c => 3}, {:a => 4, :b => 5, :c => 6}]
         
     | 
| 
      
 190 
     | 
    
         
            +
                FasterCSV.open(@path, "w", :headers => [:b, :a, :c]) do |csv|
         
     | 
| 
      
 191 
     | 
    
         
            +
                  lines.each { |line| csv << line }
         
     | 
| 
      
 192 
     | 
    
         
            +
                end
         
     | 
| 
      
 193 
     | 
    
         
            +
             
     | 
| 
      
 194 
     | 
    
         
            +
                # test writing fields in the correct order
         
     | 
| 
      
 195 
     | 
    
         
            +
                File.open(@path, "r") do |f|
         
     | 
| 
      
 196 
     | 
    
         
            +
                  assert_equal("2,1,3", f.gets.strip)
         
     | 
| 
      
 197 
     | 
    
         
            +
                  assert_equal("5,4,6", f.gets.strip)
         
     | 
| 
      
 198 
     | 
    
         
            +
                end
         
     | 
| 
      
 199 
     | 
    
         
            +
             
     | 
| 
      
 200 
     | 
    
         
            +
                # test reading CSV with headers
         
     | 
| 
      
 201 
     | 
    
         
            +
                FasterCSV.open( @path, "r", :headers    => [:b, :a, :c],
         
     | 
| 
      
 202 
     | 
    
         
            +
                                            :converters => :all ) do |csv|
         
     | 
| 
      
 203 
     | 
    
         
            +
                  csv.each { |line| assert_equal(lines.shift, line.to_hash) }
         
     | 
| 
      
 204 
     | 
    
         
            +
                end
         
     | 
| 
      
 205 
     | 
    
         
            +
              end
         
     | 
| 
      
 206 
     | 
    
         
            +
             
     | 
| 
      
 207 
     | 
    
         
            +
              def test_write_hash_with_headers_string
         
     | 
| 
      
 208 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 209 
     | 
    
         
            +
             
     | 
| 
      
 210 
     | 
    
         
            +
                lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}]
         
     | 
| 
      
 211 
     | 
    
         
            +
                FasterCSV.open( @path, "w", :headers => "b|a|c",
         
     | 
| 
      
 212 
     | 
    
         
            +
                                            :col_sep => "|" ) do |csv|
         
     | 
| 
      
 213 
     | 
    
         
            +
                  lines.each { |line| csv << line }
         
     | 
| 
      
 214 
     | 
    
         
            +
                end
         
     | 
| 
      
 215 
     | 
    
         
            +
             
     | 
| 
      
 216 
     | 
    
         
            +
                # test writing fields in the correct order
         
     | 
| 
      
 217 
     | 
    
         
            +
                File.open(@path, "r") do |f|
         
     | 
| 
      
 218 
     | 
    
         
            +
                  assert_equal("2|1|3", f.gets.strip)
         
     | 
| 
      
 219 
     | 
    
         
            +
                  assert_equal("5|4|6", f.gets.strip)
         
     | 
| 
      
 220 
     | 
    
         
            +
                end
         
     | 
| 
      
 221 
     | 
    
         
            +
             
     | 
| 
      
 222 
     | 
    
         
            +
                # test reading CSV with headers
         
     | 
| 
      
 223 
     | 
    
         
            +
                FasterCSV.open( @path, "r", :headers    => "b|a|c",
         
     | 
| 
      
 224 
     | 
    
         
            +
                                            :col_sep    => "|",
         
     | 
| 
      
 225 
     | 
    
         
            +
                                            :converters => :all ) do |csv|
         
     | 
| 
      
 226 
     | 
    
         
            +
                  csv.each { |line| assert_equal(lines.shift, line.to_hash) }
         
     | 
| 
      
 227 
     | 
    
         
            +
                end
         
     | 
| 
      
 228 
     | 
    
         
            +
              end
         
     | 
| 
      
 229 
     | 
    
         
            +
              
         
     | 
| 
      
 230 
     | 
    
         
            +
              def test_write_headers
         
     | 
| 
      
 231 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 232 
     | 
    
         
            +
             
     | 
| 
      
 233 
     | 
    
         
            +
                lines = [{"a" => 1, "b" => 2, "c" => 3}, {"a" => 4, "b" => 5, "c" => 6}]
         
     | 
| 
      
 234 
     | 
    
         
            +
                FasterCSV.open( @path, "w", :headers       => "b|a|c",
         
     | 
| 
      
 235 
     | 
    
         
            +
                                            :write_headers => true,
         
     | 
| 
      
 236 
     | 
    
         
            +
                                            :col_sep       => "|" ) do |csv|
         
     | 
| 
      
 237 
     | 
    
         
            +
                  lines.each { |line| csv << line }
         
     | 
| 
      
 238 
     | 
    
         
            +
                end
         
     | 
| 
      
 239 
     | 
    
         
            +
             
     | 
| 
      
 240 
     | 
    
         
            +
                # test writing fields in the correct order
         
     | 
| 
      
 241 
     | 
    
         
            +
                File.open(@path, "r") do |f|
         
     | 
| 
      
 242 
     | 
    
         
            +
                  assert_equal("b|a|c", f.gets.strip)
         
     | 
| 
      
 243 
     | 
    
         
            +
                  assert_equal("2|1|3", f.gets.strip)
         
     | 
| 
      
 244 
     | 
    
         
            +
                  assert_equal("5|4|6", f.gets.strip)
         
     | 
| 
      
 245 
     | 
    
         
            +
                end
         
     | 
| 
      
 246 
     | 
    
         
            +
             
     | 
| 
      
 247 
     | 
    
         
            +
                # test reading CSV with headers
         
     | 
| 
      
 248 
     | 
    
         
            +
                FasterCSV.open( @path, "r", :headers    => true,
         
     | 
| 
      
 249 
     | 
    
         
            +
                                            :col_sep    => "|",
         
     | 
| 
      
 250 
     | 
    
         
            +
                                            :converters => :all ) do |csv|
         
     | 
| 
      
 251 
     | 
    
         
            +
                  csv.each { |line| assert_equal(lines.shift, line.to_hash) }
         
     | 
| 
      
 252 
     | 
    
         
            +
                end
         
     | 
| 
      
 253 
     | 
    
         
            +
              end
         
     | 
| 
      
 254 
     | 
    
         
            +
              
         
     | 
| 
      
 255 
     | 
    
         
            +
              def test_disable_write_headers_when_taken_from_first_line
         
     | 
| 
      
 256 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 257 
     | 
    
         
            +
             
     | 
| 
      
 258 
     | 
    
         
            +
                lines = [['a','b','c'], [1,2,3], [4,5,6]]
         
     | 
| 
      
 259 
     | 
    
         
            +
                FasterCSV.open( @path, "w", :headers       => :frist_line,
         
     | 
| 
      
 260 
     | 
    
         
            +
                                            :write_headers => false,
         
     | 
| 
      
 261 
     | 
    
         
            +
                                            :col_sep       => "|" ) do |csv|
         
     | 
| 
      
 262 
     | 
    
         
            +
                  lines.each { |line| csv << line }
         
     | 
| 
      
 263 
     | 
    
         
            +
                end
         
     | 
| 
      
 264 
     | 
    
         
            +
             
     | 
| 
      
 265 
     | 
    
         
            +
                # test writing fields in the correct order
         
     | 
| 
      
 266 
     | 
    
         
            +
                File.open(@path, "r") do |f|
         
     | 
| 
      
 267 
     | 
    
         
            +
                  assert_equal("1|2|3", f.gets.strip)
         
     | 
| 
      
 268 
     | 
    
         
            +
                  assert_equal("4|5|6", f.gets.strip)
         
     | 
| 
      
 269 
     | 
    
         
            +
                end
         
     | 
| 
      
 270 
     | 
    
         
            +
              end
         
     | 
| 
      
 271 
     | 
    
         
            +
             
     | 
| 
      
 272 
     | 
    
         
            +
              def test_append  # aliased add_row() and puts()
         
     | 
| 
      
 273 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 274 
     | 
    
         
            +
                
         
     | 
| 
      
 275 
     | 
    
         
            +
                FasterCSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
         
     | 
| 
      
 276 
     | 
    
         
            +
                  @expected.each { |row| csv << row }
         
     | 
| 
      
 277 
     | 
    
         
            +
                end
         
     | 
| 
      
 278 
     | 
    
         
            +
             
     | 
| 
      
 279 
     | 
    
         
            +
                test_shift
         
     | 
| 
      
 280 
     | 
    
         
            +
             
     | 
| 
      
 281 
     | 
    
         
            +
                # same thing using FasterCSV::Row objects
         
     | 
| 
      
 282 
     | 
    
         
            +
                File.unlink(@path)
         
     | 
| 
      
 283 
     | 
    
         
            +
                
         
     | 
| 
      
 284 
     | 
    
         
            +
                FasterCSV.open(@path, "w", :col_sep => "\t", :row_sep => "\r\n") do |csv|
         
     | 
| 
      
 285 
     | 
    
         
            +
                  @expected.each { |row| csv << FasterCSV::Row.new(Array.new, row) }
         
     | 
| 
      
 286 
     | 
    
         
            +
                end
         
     | 
| 
      
 287 
     | 
    
         
            +
             
     | 
| 
      
 288 
     | 
    
         
            +
                test_shift
         
     | 
| 
      
 289 
     | 
    
         
            +
              end
         
     | 
| 
      
 290 
     | 
    
         
            +
              
         
     | 
| 
      
 291 
     | 
    
         
            +
              ### Test Read and Write Interface ###
         
     | 
| 
      
 292 
     | 
    
         
            +
              
         
     | 
| 
      
 293 
     | 
    
         
            +
              def test_filter
         
     | 
| 
      
 294 
     | 
    
         
            +
                assert_respond_to(FasterCSV, :filter)
         
     | 
| 
      
 295 
     | 
    
         
            +
                
         
     | 
| 
      
 296 
     | 
    
         
            +
                expected = [[1, 2, 3], [4, 5]]
         
     | 
| 
      
 297 
     | 
    
         
            +
                FasterCSV.filter( "1;2;3\n4;5\n", (result = String.new),
         
     | 
| 
      
 298 
     | 
    
         
            +
                                  :in_col_sep => ";", :out_col_sep => ",",
         
     | 
| 
      
 299 
     | 
    
         
            +
                                  :converters => :all ) do |row|
         
     | 
| 
      
 300 
     | 
    
         
            +
                  assert_equal(row, expected.shift)
         
     | 
| 
      
 301 
     | 
    
         
            +
                  row.map! { |n| n * 2 }
         
     | 
| 
      
 302 
     | 
    
         
            +
                  row << "Added\r"
         
     | 
| 
      
 303 
     | 
    
         
            +
                end
         
     | 
| 
      
 304 
     | 
    
         
            +
                assert_equal("2,4,6,\"Added\r\"\n8,10,\"Added\r\"\n", result)
         
     | 
| 
      
 305 
     | 
    
         
            +
              end
         
     | 
| 
      
 306 
     | 
    
         
            +
              
         
     | 
| 
      
 307 
     | 
    
         
            +
              def test_instance
         
     | 
| 
      
 308 
     | 
    
         
            +
                csv = String.new
         
     | 
| 
      
 309 
     | 
    
         
            +
                
         
     | 
| 
      
 310 
     | 
    
         
            +
                first = nil
         
     | 
| 
      
 311 
     | 
    
         
            +
                assert_nothing_raised(Exception) do 
         
     | 
| 
      
 312 
     | 
    
         
            +
                  first =  FasterCSV.instance(csv, :col_sep => ";")
         
     | 
| 
      
 313 
     | 
    
         
            +
                  first << %w{a b c}
         
     | 
| 
      
 314 
     | 
    
         
            +
                end
         
     | 
| 
      
 315 
     | 
    
         
            +
                
         
     | 
| 
      
 316 
     | 
    
         
            +
                assert_equal("a;b;c\n", csv)
         
     | 
| 
      
 317 
     | 
    
         
            +
                
         
     | 
| 
      
 318 
     | 
    
         
            +
                second = nil
         
     | 
| 
      
 319 
     | 
    
         
            +
                assert_nothing_raised(Exception) do 
         
     | 
| 
      
 320 
     | 
    
         
            +
                  second =  FasterCSV.instance(csv, :col_sep => ";")
         
     | 
| 
      
 321 
     | 
    
         
            +
                  second << [1, 2, 3]
         
     | 
| 
      
 322 
     | 
    
         
            +
                end
         
     | 
| 
      
 323 
     | 
    
         
            +
                
         
     | 
| 
      
 324 
     | 
    
         
            +
                assert_equal(first.object_id, second.object_id)
         
     | 
| 
      
 325 
     | 
    
         
            +
                assert_equal("a;b;c\n1;2;3\n", csv)
         
     | 
| 
      
 326 
     | 
    
         
            +
                
         
     | 
| 
      
 327 
     | 
    
         
            +
                # shortcuts
         
     | 
| 
      
 328 
     | 
    
         
            +
                assert_equal(STDOUT, FasterCSV.instance.instance_eval { @io })
         
     | 
| 
      
 329 
     | 
    
         
            +
                assert_equal(STDOUT, FasterCSV { |csv| csv.instance_eval { @io } })
         
     | 
| 
      
 330 
     | 
    
         
            +
                assert_equal(STDOUT, FCSV.instance.instance_eval { @io })
         
     | 
| 
      
 331 
     | 
    
         
            +
                assert_equal(STDOUT, FCSV { |csv| csv.instance_eval { @io } })
         
     | 
| 
      
 332 
     | 
    
         
            +
              end
         
     | 
| 
      
 333 
     | 
    
         
            +
              
         
     | 
| 
      
 334 
     | 
    
         
            +
              ### Test Alternate Interface ###
         
     | 
| 
      
 335 
     | 
    
         
            +
              
         
     | 
| 
      
 336 
     | 
    
         
            +
              def test_csv_interface
         
     | 
| 
      
 337 
     | 
    
         
            +
                require "csv"
         
     | 
| 
      
 338 
     | 
    
         
            +
                data      = ["Number", 42, "Tricky Field", 'This has embedded "quotes"!']
         
     | 
| 
      
 339 
     | 
    
         
            +
                data_file = File.join(File.dirname(__FILE__), "temp_csv_data.csv")
         
     | 
| 
      
 340 
     | 
    
         
            +
                CSV.open(data_file, "w") { |f| 10.times { f << data } }
         
     | 
| 
      
 341 
     | 
    
         
            +
                csv   = CSV.generate_line(data)
         
     | 
| 
      
 342 
     | 
    
         
            +
                tests = { :foreach       => Array.new,
         
     | 
| 
      
 343 
     | 
    
         
            +
                          :generate_line => csv,
         
     | 
| 
      
 344 
     | 
    
         
            +
                          :open          => Array.new,
         
     | 
| 
      
 345 
     | 
    
         
            +
                          :parse         => CSV.parse(csv),
         
     | 
| 
      
 346 
     | 
    
         
            +
                          :parse_w_block => Array.new,
         
     | 
| 
      
 347 
     | 
    
         
            +
                          :parse_line    => CSV.parse_line(csv),
         
     | 
| 
      
 348 
     | 
    
         
            +
                          :readlines     => CSV.readlines(data_file) }
         
     | 
| 
      
 349 
     | 
    
         
            +
                CSV.foreach(data_file) { |row| tests[:foreach] << row }
         
     | 
| 
      
 350 
     | 
    
         
            +
                CSV.open(data_file, "r") { |row| tests[:open] << row }
         
     | 
| 
      
 351 
     | 
    
         
            +
                CSV.parse(([csv] * 3).join("\n")) { |row| tests[:parse_w_block] << row }
         
     | 
| 
      
 352 
     | 
    
         
            +
                Object.send(:remove_const, :CSV)
         
     | 
| 
      
 353 
     | 
    
         
            +
                
         
     | 
| 
      
 354 
     | 
    
         
            +
                assert_nothing_raised(Exception) do 
         
     | 
| 
      
 355 
     | 
    
         
            +
                  FasterCSV.build_csv_interface
         
     | 
| 
      
 356 
     | 
    
         
            +
                end
         
     | 
| 
      
 357 
     | 
    
         
            +
                
         
     | 
| 
      
 358 
     | 
    
         
            +
                %w{ foreach
         
     | 
| 
      
 359 
     | 
    
         
            +
                    generate_line
         
     | 
| 
      
 360 
     | 
    
         
            +
                    open
         
     | 
| 
      
 361 
     | 
    
         
            +
                    parse
         
     | 
| 
      
 362 
     | 
    
         
            +
                    parse_line
         
     | 
| 
      
 363 
     | 
    
         
            +
                    readlines }.each do |meth|
         
     | 
| 
      
 364 
     | 
    
         
            +
                  assert_respond_to(::CSV, meth)
         
     | 
| 
      
 365 
     | 
    
         
            +
                end
         
     | 
| 
      
 366 
     | 
    
         
            +
                
         
     | 
| 
      
 367 
     | 
    
         
            +
                faster_csv = Array.new
         
     | 
| 
      
 368 
     | 
    
         
            +
                CSV.foreach(data_file) { |row| faster_csv << row }
         
     | 
| 
      
 369 
     | 
    
         
            +
                assert_equal(tests[:foreach], faster_csv)
         
     | 
| 
      
 370 
     | 
    
         
            +
                assert_equal(tests[:generate_line], CSV.generate_line(data))
         
     | 
| 
      
 371 
     | 
    
         
            +
                faster_csv.clear
         
     | 
| 
      
 372 
     | 
    
         
            +
                CSV.open(data_file, "r") { |row| faster_csv << row }
         
     | 
| 
      
 373 
     | 
    
         
            +
                assert_equal(tests[:open], faster_csv)
         
     | 
| 
      
 374 
     | 
    
         
            +
                comp_file = data_file.sub("_csv_data", "_faster_csv_data")
         
     | 
| 
      
 375 
     | 
    
         
            +
                CSV.open(comp_file, "w") { |f| 10.times { f << data } }
         
     | 
| 
      
 376 
     | 
    
         
            +
                assert_equal(File.read(data_file), File.read(comp_file))
         
     | 
| 
      
 377 
     | 
    
         
            +
                assert_equal(tests[:parse], CSV.parse(csv))
         
     | 
| 
      
 378 
     | 
    
         
            +
                faster_csv.clear
         
     | 
| 
      
 379 
     | 
    
         
            +
                CSV.parse(([csv] * 3).join("\n")) { |row| faster_csv << row }
         
     | 
| 
      
 380 
     | 
    
         
            +
                assert_equal(tests[:parse_w_block], faster_csv)
         
     | 
| 
      
 381 
     | 
    
         
            +
                assert_equal(tests[:parse_line], CSV.parse_line(csv))
         
     | 
| 
      
 382 
     | 
    
         
            +
                assert_equal(tests[:readlines], CSV.readlines(data_file))
         
     | 
| 
      
 383 
     | 
    
         
            +
                
         
     | 
| 
      
 384 
     | 
    
         
            +
                Object.send(:remove_const, :CSV)
         
     | 
| 
      
 385 
     | 
    
         
            +
                load "csv.rb"
         
     | 
| 
      
 386 
     | 
    
         
            +
                [data_file, comp_file].each { |file| File.unlink(file) }
         
     | 
| 
      
 387 
     | 
    
         
            +
              end
         
     | 
| 
      
 388 
     | 
    
         
            +
            end
         
     | 
    
        data/test/tc_row.rb
    ADDED
    
    | 
         @@ -0,0 +1,305 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/local/bin/ruby -w
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            # tc_row.rb
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            #  Created by James Edward Gray II on 2006-02-24.
         
     | 
| 
      
 6 
     | 
    
         
            +
            #  Copyright 2006 Gray Productions. All rights reserved.
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            require "test/unit"
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            require "faster_csv"
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
            class TestFasterCSVRow < Test::Unit::TestCase
         
     | 
| 
      
 13 
     | 
    
         
            +
              def setup
         
     | 
| 
      
 14 
     | 
    
         
            +
                @row = FasterCSV::Row.new(%w{A B C A A}, [1, 2, 3, 4])
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
              
         
     | 
| 
      
 17 
     | 
    
         
            +
              def test_initialize
         
     | 
| 
      
 18 
     | 
    
         
            +
                # basic
         
     | 
| 
      
 19 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3])
         
     | 
| 
      
 20 
     | 
    
         
            +
                assert_not_nil(row)
         
     | 
| 
      
 21 
     | 
    
         
            +
                assert_instance_of(FasterCSV::Row, row)
         
     | 
| 
      
 22 
     | 
    
         
            +
                assert_equal([["A", 1], ["B", 2], ["C", 3]], row.to_a)
         
     | 
| 
      
 23 
     | 
    
         
            +
                
         
     | 
| 
      
 24 
     | 
    
         
            +
                # missing headers
         
     | 
| 
      
 25 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A}, [1, 2, 3])
         
     | 
| 
      
 26 
     | 
    
         
            +
                assert_not_nil(row)
         
     | 
| 
      
 27 
     | 
    
         
            +
                assert_instance_of(FasterCSV::Row, row)
         
     | 
| 
      
 28 
     | 
    
         
            +
                assert_equal([["A", 1], [nil, 2], [nil, 3]], row.to_a)
         
     | 
| 
      
 29 
     | 
    
         
            +
                
         
     | 
| 
      
 30 
     | 
    
         
            +
                # missing fields
         
     | 
| 
      
 31 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A B C}, [1, 2])
         
     | 
| 
      
 32 
     | 
    
         
            +
                assert_not_nil(row)
         
     | 
| 
      
 33 
     | 
    
         
            +
                assert_instance_of(FasterCSV::Row, row)
         
     | 
| 
      
 34 
     | 
    
         
            +
                assert_equal([["A", 1], ["B", 2], ["C", nil]], row.to_a)
         
     | 
| 
      
 35 
     | 
    
         
            +
              end
         
     | 
| 
      
 36 
     | 
    
         
            +
              
         
     | 
| 
      
 37 
     | 
    
         
            +
              def test_row_type
         
     | 
| 
      
 38 
     | 
    
         
            +
                # field rows
         
     | 
| 
      
 39 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3])         # implicit
         
     | 
| 
      
 40 
     | 
    
         
            +
                assert(!row.header_row?)
         
     | 
| 
      
 41 
     | 
    
         
            +
                assert(row.field_row?)
         
     | 
| 
      
 42 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3], false)  # explicit
         
     | 
| 
      
 43 
     | 
    
         
            +
                assert(!row.header_row?)
         
     | 
| 
      
 44 
     | 
    
         
            +
                assert(row.field_row?)
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
                # header row
         
     | 
| 
      
 47 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3], true)
         
     | 
| 
      
 48 
     | 
    
         
            +
                assert(row.header_row?)
         
     | 
| 
      
 49 
     | 
    
         
            +
                assert(!row.field_row?)
         
     | 
| 
      
 50 
     | 
    
         
            +
              end
         
     | 
| 
      
 51 
     | 
    
         
            +
              
         
     | 
| 
      
 52 
     | 
    
         
            +
              def test_headers
         
     | 
| 
      
 53 
     | 
    
         
            +
                assert_equal(%w{A B C A A}, @row.headers)
         
     | 
| 
      
 54 
     | 
    
         
            +
              end
         
     | 
| 
      
 55 
     | 
    
         
            +
              
         
     | 
| 
      
 56 
     | 
    
         
            +
              def test_field
         
     | 
| 
      
 57 
     | 
    
         
            +
                # by name
         
     | 
| 
      
 58 
     | 
    
         
            +
                assert_equal(2, @row.field("B"))
         
     | 
| 
      
 59 
     | 
    
         
            +
                assert_equal(2, @row["B"])  # alias
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                # by index
         
     | 
| 
      
 62 
     | 
    
         
            +
                assert_equal(3, @row.field(2))
         
     | 
| 
      
 63 
     | 
    
         
            +
                
         
     | 
| 
      
 64 
     | 
    
         
            +
                # missing
         
     | 
| 
      
 65 
     | 
    
         
            +
                assert_nil(@row.field("Missing"))
         
     | 
| 
      
 66 
     | 
    
         
            +
                assert_nil(@row.field(10))
         
     | 
| 
      
 67 
     | 
    
         
            +
                
         
     | 
| 
      
 68 
     | 
    
         
            +
                # minimum index
         
     | 
| 
      
 69 
     | 
    
         
            +
                assert_equal(1, @row.field("A"))
         
     | 
| 
      
 70 
     | 
    
         
            +
                assert_equal(1, @row.field("A", 0))
         
     | 
| 
      
 71 
     | 
    
         
            +
                assert_equal(4, @row.field("A", 1))
         
     | 
| 
      
 72 
     | 
    
         
            +
                assert_equal(4, @row.field("A", 2))
         
     | 
| 
      
 73 
     | 
    
         
            +
                assert_equal(4, @row.field("A", 3))
         
     | 
| 
      
 74 
     | 
    
         
            +
                assert_equal(nil, @row.field("A", 4))
         
     | 
| 
      
 75 
     | 
    
         
            +
                assert_equal(nil, @row.field("A", 5))
         
     | 
| 
      
 76 
     | 
    
         
            +
              end
         
     | 
| 
      
 77 
     | 
    
         
            +
              
         
     | 
| 
      
 78 
     | 
    
         
            +
              def test_set_field
         
     | 
| 
      
 79 
     | 
    
         
            +
                # set field by name
         
     | 
| 
      
 80 
     | 
    
         
            +
                assert_equal(100, @row["A"] = 100)
         
     | 
| 
      
 81 
     | 
    
         
            +
                
         
     | 
| 
      
 82 
     | 
    
         
            +
                # set field by index
         
     | 
| 
      
 83 
     | 
    
         
            +
                assert_equal(300, @row[3] = 300)
         
     | 
| 
      
 84 
     | 
    
         
            +
                
         
     | 
| 
      
 85 
     | 
    
         
            +
                # set field by name and minimum index
         
     | 
| 
      
 86 
     | 
    
         
            +
                assert_equal([:a, :b, :c], @row["A", 4] = [:a, :b, :c])
         
     | 
| 
      
 87 
     | 
    
         
            +
                
         
     | 
| 
      
 88 
     | 
    
         
            +
                # verify the changes
         
     | 
| 
      
 89 
     | 
    
         
            +
                assert_equal( [ ["A", 100],
         
     | 
| 
      
 90 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 91 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 92 
     | 
    
         
            +
                                ["A", 300],
         
     | 
| 
      
 93 
     | 
    
         
            +
                                ["A", [:a, :b, :c]] ], @row.to_a )
         
     | 
| 
      
 94 
     | 
    
         
            +
                
         
     | 
| 
      
 95 
     | 
    
         
            +
                # assigning an index past the end
         
     | 
| 
      
 96 
     | 
    
         
            +
                assert_equal("End", @row[10] = "End")
         
     | 
| 
      
 97 
     | 
    
         
            +
                assert_equal( [ ["A", 100],
         
     | 
| 
      
 98 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 99 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 100 
     | 
    
         
            +
                                ["A", 300],
         
     | 
| 
      
 101 
     | 
    
         
            +
                                ["A", [:a, :b, :c]],
         
     | 
| 
      
 102 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 103 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 104 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 105 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 106 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 107 
     | 
    
         
            +
                                [nil, "End"] ], @row.to_a )
         
     | 
| 
      
 108 
     | 
    
         
            +
                
         
     | 
| 
      
 109 
     | 
    
         
            +
                # assigning a new field by header
         
     | 
| 
      
 110 
     | 
    
         
            +
                assert_equal("New", @row[:new] = "New")
         
     | 
| 
      
 111 
     | 
    
         
            +
                assert_equal( [ ["A", 100],
         
     | 
| 
      
 112 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 113 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 114 
     | 
    
         
            +
                                ["A", 300],
         
     | 
| 
      
 115 
     | 
    
         
            +
                                ["A", [:a, :b, :c]],
         
     | 
| 
      
 116 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 117 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 118 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 119 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 120 
     | 
    
         
            +
                                [nil, nil],
         
     | 
| 
      
 121 
     | 
    
         
            +
                                [nil, "End"],
         
     | 
| 
      
 122 
     | 
    
         
            +
                                [:new, "New"] ], @row.to_a )
         
     | 
| 
      
 123 
     | 
    
         
            +
              end
         
     | 
| 
      
 124 
     | 
    
         
            +
              
         
     | 
| 
      
 125 
     | 
    
         
            +
              def test_append
         
     | 
| 
      
 126 
     | 
    
         
            +
                # add a value
         
     | 
| 
      
 127 
     | 
    
         
            +
                assert_equal(@row, @row << "Value")
         
     | 
| 
      
 128 
     | 
    
         
            +
                assert_equal( [ ["A", 1],
         
     | 
| 
      
 129 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 130 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 131 
     | 
    
         
            +
                                ["A", 4],
         
     | 
| 
      
 132 
     | 
    
         
            +
                                ["A", nil],
         
     | 
| 
      
 133 
     | 
    
         
            +
                                [nil, "Value"] ], @row.to_a )
         
     | 
| 
      
 134 
     | 
    
         
            +
                
         
     | 
| 
      
 135 
     | 
    
         
            +
                # add a pair
         
     | 
| 
      
 136 
     | 
    
         
            +
                assert_equal(@row, @row << %w{Header Field})
         
     | 
| 
      
 137 
     | 
    
         
            +
                assert_equal( [ ["A", 1],
         
     | 
| 
      
 138 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 139 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 140 
     | 
    
         
            +
                                ["A", 4],
         
     | 
| 
      
 141 
     | 
    
         
            +
                                ["A", nil],
         
     | 
| 
      
 142 
     | 
    
         
            +
                                [nil, "Value"],
         
     | 
| 
      
 143 
     | 
    
         
            +
                                %w{Header Field} ], @row.to_a )
         
     | 
| 
      
 144 
     | 
    
         
            +
                
         
     | 
| 
      
 145 
     | 
    
         
            +
                # a pair with Hash syntax
         
     | 
| 
      
 146 
     | 
    
         
            +
                assert_equal(@row, @row << {:key => :value})
         
     | 
| 
      
 147 
     | 
    
         
            +
                assert_equal( [ ["A", 1],
         
     | 
| 
      
 148 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 149 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 150 
     | 
    
         
            +
                                ["A", 4],
         
     | 
| 
      
 151 
     | 
    
         
            +
                                ["A", nil],
         
     | 
| 
      
 152 
     | 
    
         
            +
                                [nil, "Value"],
         
     | 
| 
      
 153 
     | 
    
         
            +
                                %w{Header Field},
         
     | 
| 
      
 154 
     | 
    
         
            +
                                [:key, :value] ], @row.to_a )
         
     | 
| 
      
 155 
     | 
    
         
            +
                
         
     | 
| 
      
 156 
     | 
    
         
            +
                # multiple fields at once
         
     | 
| 
      
 157 
     | 
    
         
            +
                assert_equal(@row, @row.push(100, 200, [:last, 300]))
         
     | 
| 
      
 158 
     | 
    
         
            +
                assert_equal( [ ["A", 1],
         
     | 
| 
      
 159 
     | 
    
         
            +
                                ["B", 2],
         
     | 
| 
      
 160 
     | 
    
         
            +
                                ["C", 3],
         
     | 
| 
      
 161 
     | 
    
         
            +
                                ["A", 4],
         
     | 
| 
      
 162 
     | 
    
         
            +
                                ["A", nil],
         
     | 
| 
      
 163 
     | 
    
         
            +
                                [nil, "Value"],
         
     | 
| 
      
 164 
     | 
    
         
            +
                                %w{Header Field},
         
     | 
| 
      
 165 
     | 
    
         
            +
                                [:key, :value],
         
     | 
| 
      
 166 
     | 
    
         
            +
                                [nil, 100],
         
     | 
| 
      
 167 
     | 
    
         
            +
                                [nil, 200],
         
     | 
| 
      
 168 
     | 
    
         
            +
                                [:last, 300] ], @row.to_a )
         
     | 
| 
      
 169 
     | 
    
         
            +
              end
         
     | 
| 
      
 170 
     | 
    
         
            +
              
         
     | 
| 
      
 171 
     | 
    
         
            +
              def test_delete
         
     | 
| 
      
 172 
     | 
    
         
            +
                # by index
         
     | 
| 
      
 173 
     | 
    
         
            +
                assert_equal(["B", 2], @row.delete(1))
         
     | 
| 
      
 174 
     | 
    
         
            +
             
     | 
| 
      
 175 
     | 
    
         
            +
                # by header
         
     | 
| 
      
 176 
     | 
    
         
            +
                assert_equal(["C", 3], @row.delete("C"))
         
     | 
| 
      
 177 
     | 
    
         
            +
                
         
     | 
| 
      
 178 
     | 
    
         
            +
                # using a block
         
     | 
| 
      
 179 
     | 
    
         
            +
                assert_equal(@row, @row.delete_if { |h, f| h == "A" and not f.nil? })
         
     | 
| 
      
 180 
     | 
    
         
            +
                assert_equal([["A", nil]], @row.to_a)
         
     | 
| 
      
 181 
     | 
    
         
            +
              end
         
     | 
| 
      
 182 
     | 
    
         
            +
              
         
     | 
| 
      
 183 
     | 
    
         
            +
              def test_fields
         
     | 
| 
      
 184 
     | 
    
         
            +
                # all fields
         
     | 
| 
      
 185 
     | 
    
         
            +
                assert_equal([1, 2, 3, 4, nil], @row.fields)
         
     | 
| 
      
 186 
     | 
    
         
            +
                
         
     | 
| 
      
 187 
     | 
    
         
            +
                # by header
         
     | 
| 
      
 188 
     | 
    
         
            +
                assert_equal([1, 3], @row.fields("A", "C"))
         
     | 
| 
      
 189 
     | 
    
         
            +
                
         
     | 
| 
      
 190 
     | 
    
         
            +
                # by index
         
     | 
| 
      
 191 
     | 
    
         
            +
                assert_equal([2, 3, nil], @row.fields(1, 2, 10))
         
     | 
| 
      
 192 
     | 
    
         
            +
                
         
     | 
| 
      
 193 
     | 
    
         
            +
                # by both
         
     | 
| 
      
 194 
     | 
    
         
            +
                assert_equal([2, 3, 4], @row.fields("B", "C", 3))
         
     | 
| 
      
 195 
     | 
    
         
            +
                
         
     | 
| 
      
 196 
     | 
    
         
            +
                # with minimum indices
         
     | 
| 
      
 197 
     | 
    
         
            +
                assert_equal([2, 3, 4], @row.fields("B", "C", ["A", 3]))
         
     | 
| 
      
 198 
     | 
    
         
            +
                
         
     | 
| 
      
 199 
     | 
    
         
            +
                # by header range
         
     | 
| 
      
 200 
     | 
    
         
            +
                assert_equal([2, 3], @row.values_at("B".."C"))
         
     | 
| 
      
 201 
     | 
    
         
            +
              end
         
     | 
| 
      
 202 
     | 
    
         
            +
              
         
     | 
| 
      
 203 
     | 
    
         
            +
              def test_index
         
     | 
| 
      
 204 
     | 
    
         
            +
                # basic usage
         
     | 
| 
      
 205 
     | 
    
         
            +
                assert_equal(0, @row.index("A"))
         
     | 
| 
      
 206 
     | 
    
         
            +
                assert_equal(1, @row.index("B"))
         
     | 
| 
      
 207 
     | 
    
         
            +
                assert_equal(2, @row.index("C"))
         
     | 
| 
      
 208 
     | 
    
         
            +
                assert_equal(nil, @row.index("Z"))
         
     | 
| 
      
 209 
     | 
    
         
            +
             
     | 
| 
      
 210 
     | 
    
         
            +
                # with minimum index
         
     | 
| 
      
 211 
     | 
    
         
            +
                assert_equal(0, @row.index("A"))
         
     | 
| 
      
 212 
     | 
    
         
            +
                assert_equal(0, @row.index("A", 0))
         
     | 
| 
      
 213 
     | 
    
         
            +
                assert_equal(3, @row.index("A", 1))
         
     | 
| 
      
 214 
     | 
    
         
            +
                assert_equal(3, @row.index("A", 2))
         
     | 
| 
      
 215 
     | 
    
         
            +
                assert_equal(3, @row.index("A", 3))
         
     | 
| 
      
 216 
     | 
    
         
            +
                assert_equal(4, @row.index("A", 4))
         
     | 
| 
      
 217 
     | 
    
         
            +
                assert_equal(nil, @row.index("A", 5))
         
     | 
| 
      
 218 
     | 
    
         
            +
              end
         
     | 
| 
      
 219 
     | 
    
         
            +
              
         
     | 
| 
      
 220 
     | 
    
         
            +
              def test_queries
         
     | 
| 
      
 221 
     | 
    
         
            +
                # headers
         
     | 
| 
      
 222 
     | 
    
         
            +
                assert(@row.header?("A"))
         
     | 
| 
      
 223 
     | 
    
         
            +
                assert(@row.header?("C"))
         
     | 
| 
      
 224 
     | 
    
         
            +
                assert(!@row.header?("Z"))
         
     | 
| 
      
 225 
     | 
    
         
            +
                assert(@row.include?("A"))  # alias
         
     | 
| 
      
 226 
     | 
    
         
            +
                
         
     | 
| 
      
 227 
     | 
    
         
            +
                # fields
         
     | 
| 
      
 228 
     | 
    
         
            +
                assert(@row.field?(4))
         
     | 
| 
      
 229 
     | 
    
         
            +
                assert(@row.field?(nil))
         
     | 
| 
      
 230 
     | 
    
         
            +
                assert(!@row.field?(10))
         
     | 
| 
      
 231 
     | 
    
         
            +
              end
         
     | 
| 
      
 232 
     | 
    
         
            +
              
         
     | 
| 
      
 233 
     | 
    
         
            +
              def test_each
         
     | 
| 
      
 234 
     | 
    
         
            +
                # array style
         
     | 
| 
      
 235 
     | 
    
         
            +
                ary = @row.to_a
         
     | 
| 
      
 236 
     | 
    
         
            +
                @row.each do |pair|
         
     | 
| 
      
 237 
     | 
    
         
            +
                  assert_equal(ary.first.first, pair.first)
         
     | 
| 
      
 238 
     | 
    
         
            +
                  assert_equal(ary.shift.last, pair.last)
         
     | 
| 
      
 239 
     | 
    
         
            +
                end
         
     | 
| 
      
 240 
     | 
    
         
            +
                
         
     | 
| 
      
 241 
     | 
    
         
            +
                # hash style
         
     | 
| 
      
 242 
     | 
    
         
            +
                ary = @row.to_a
         
     | 
| 
      
 243 
     | 
    
         
            +
                @row.each do |header, field|
         
     | 
| 
      
 244 
     | 
    
         
            +
                  assert_equal(ary.first.first, header)
         
     | 
| 
      
 245 
     | 
    
         
            +
                  assert_equal(ary.shift.last, field)
         
     | 
| 
      
 246 
     | 
    
         
            +
                end
         
     | 
| 
      
 247 
     | 
    
         
            +
                
         
     | 
| 
      
 248 
     | 
    
         
            +
                # verify that we can chain the call
         
     | 
| 
      
 249 
     | 
    
         
            +
                assert_equal(@row, @row.each { })
         
     | 
| 
      
 250 
     | 
    
         
            +
              end
         
     | 
| 
      
 251 
     | 
    
         
            +
              
         
     | 
| 
      
 252 
     | 
    
         
            +
              def test_enumerable
         
     | 
| 
      
 253 
     | 
    
         
            +
                assert_equal( [["A", 1], ["A", 4], ["A", nil]],
         
     | 
| 
      
 254 
     | 
    
         
            +
                              @row.select { |pair| pair.first == "A" } )
         
     | 
| 
      
 255 
     | 
    
         
            +
                
         
     | 
| 
      
 256 
     | 
    
         
            +
                assert_equal(10, @row.inject(0) { |sum, (header, n)| sum + (n || 0) })
         
     | 
| 
      
 257 
     | 
    
         
            +
              end
         
     | 
| 
      
 258 
     | 
    
         
            +
              
         
     | 
| 
      
 259 
     | 
    
         
            +
              def test_to_a
         
     | 
| 
      
 260 
     | 
    
         
            +
                row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3]).to_a
         
     | 
| 
      
 261 
     | 
    
         
            +
                assert_instance_of(Array, row)
         
     | 
| 
      
 262 
     | 
    
         
            +
                row.each do |pair|
         
     | 
| 
      
 263 
     | 
    
         
            +
                  assert_instance_of(Array, pair)
         
     | 
| 
      
 264 
     | 
    
         
            +
                  assert_equal(2, pair.size)
         
     | 
| 
      
 265 
     | 
    
         
            +
                end
         
     | 
| 
      
 266 
     | 
    
         
            +
                assert_equal([["A", 1], ["B", 2], ["C", 3]], row)
         
     | 
| 
      
 267 
     | 
    
         
            +
              end
         
     | 
| 
      
 268 
     | 
    
         
            +
              
         
     | 
| 
      
 269 
     | 
    
         
            +
              def test_to_hash
         
     | 
| 
      
 270 
     | 
    
         
            +
                assert_equal({"A" => nil, "B" => 2, "C" => 3}, @row.to_hash)
         
     | 
| 
      
 271 
     | 
    
         
            +
              end
         
     | 
| 
      
 272 
     | 
    
         
            +
              
         
     | 
| 
      
 273 
     | 
    
         
            +
              def test_to_csv
         
     | 
| 
      
 274 
     | 
    
         
            +
                # normal conversion
         
     | 
| 
      
 275 
     | 
    
         
            +
                assert_equal("1,2,3,4,\n", @row.to_csv)
         
     | 
| 
      
 276 
     | 
    
         
            +
                assert_equal("1,2,3,4,\n", @row.to_s)  # alias
         
     | 
| 
      
 277 
     | 
    
         
            +
                
         
     | 
| 
      
 278 
     | 
    
         
            +
                # with options
         
     | 
| 
      
 279 
     | 
    
         
            +
                assert_equal( "1|2|3|4|\r\n",
         
     | 
| 
      
 280 
     | 
    
         
            +
                              @row.to_csv(:col_sep => "|", :row_sep => "\r\n") )
         
     | 
| 
      
 281 
     | 
    
         
            +
              end
         
     | 
| 
      
 282 
     | 
    
         
            +
              
         
     | 
| 
      
 283 
     | 
    
         
            +
              def test_array_delegation
         
     | 
| 
      
 284 
     | 
    
         
            +
                assert(!@row.empty?, "Row was empty.")
         
     | 
| 
      
 285 
     | 
    
         
            +
                
         
     | 
| 
      
 286 
     | 
    
         
            +
                assert_equal([@row.headers.size, @row.fields.size].max, @row.size)
         
     | 
| 
      
 287 
     | 
    
         
            +
              end
         
     | 
| 
      
 288 
     | 
    
         
            +
              
         
     | 
| 
      
 289 
     | 
    
         
            +
              def test_inspect_shows_header_field_pairs
         
     | 
| 
      
 290 
     | 
    
         
            +
                str = @row.inspect
         
     | 
| 
      
 291 
     | 
    
         
            +
                @row.each do |header, field|
         
     | 
| 
      
 292 
     | 
    
         
            +
                  assert( str.include?("#{header.inspect}:#{field.inspect}"),
         
     | 
| 
      
 293 
     | 
    
         
            +
                          "Header field pair not found." )
         
     | 
| 
      
 294 
     | 
    
         
            +
                end
         
     | 
| 
      
 295 
     | 
    
         
            +
              end
         
     | 
| 
      
 296 
     | 
    
         
            +
              
         
     | 
| 
      
 297 
     | 
    
         
            +
              def test_inspect_shows_symbol_headers_as_bare_attributes
         
     | 
| 
      
 298 
     | 
    
         
            +
                str = FasterCSV::Row.new( @row.headers.map { |h| h.to_sym },
         
     | 
| 
      
 299 
     | 
    
         
            +
                                          @row.fields ).inspect
         
     | 
| 
      
 300 
     | 
    
         
            +
                @row.each do |header, field|
         
     | 
| 
      
 301 
     | 
    
         
            +
                  assert( str.include?("#{header}:#{field.inspect}"),
         
     | 
| 
      
 302 
     | 
    
         
            +
                          "Header field pair not found." )
         
     | 
| 
      
 303 
     | 
    
         
            +
                end
         
     | 
| 
      
 304 
     | 
    
         
            +
              end
         
     | 
| 
      
 305 
     | 
    
         
            +
            end
         
     |