PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/fastercsv-1.4.0/test/tc_headers.rb

https://github.com/nkokkos/spot-us
Ruby | 277 lines | 199 code | 46 blank | 32 comment | 0 complexity | 33bfd6a32415ec0daca3e81fd78bd419 MD5 | raw file
  1. #!/usr/local/bin/ruby -w
  2. # tc_headers.rb
  3. #
  4. # Created by James Edward Gray II on 2006-02-25.
  5. # Copyright 2006 Gray Productions. All rights reserved.
  6. require "test/unit"
  7. require "faster_csv"
  8. class TestFasterCSVHeaders < Test::Unit::TestCase
  9. def setup
  10. @data = <<-END_CSV.gsub(/^\s+/, "")
  11. first,second,third
  12. A,B,C
  13. 1,2,3
  14. END_CSV
  15. end
  16. def test_first_row
  17. [:first_row, true].each do |setting| # two names for the same setting
  18. # activate headers
  19. csv = nil
  20. assert_nothing_raised(Exception) do
  21. csv = FasterCSV.parse(@data, :headers => setting)
  22. end
  23. # first data row - skipping headers
  24. row = csv[0]
  25. assert_not_nil(row)
  26. assert_instance_of(FasterCSV::Row, row)
  27. assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
  28. # second data row
  29. row = csv[1]
  30. assert_not_nil(row)
  31. assert_instance_of(FasterCSV::Row, row)
  32. assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
  33. # empty
  34. assert_nil(csv[2])
  35. end
  36. end
  37. def test_array_of_headers
  38. # activate headers
  39. csv = nil
  40. assert_nothing_raised(Exception) do
  41. csv = FasterCSV.parse(@data, :headers => [:my, :new, :headers])
  42. end
  43. # first data row - skipping headers
  44. row = csv[0]
  45. assert_not_nil(row)
  46. assert_instance_of(FasterCSV::Row, row)
  47. assert_equal( [[:my, "first"], [:new, "second"], [:headers, "third"]],
  48. row.to_a )
  49. # second data row
  50. row = csv[1]
  51. assert_not_nil(row)
  52. assert_instance_of(FasterCSV::Row, row)
  53. assert_equal([[:my, "A"], [:new, "B"], [:headers, "C"]], row.to_a)
  54. # third data row
  55. row = csv[2]
  56. assert_not_nil(row)
  57. assert_instance_of(FasterCSV::Row, row)
  58. assert_equal([[:my, "1"], [:new, "2"], [:headers, "3"]], row.to_a)
  59. # empty
  60. assert_nil(csv[3])
  61. # with return and convert
  62. assert_nothing_raised(Exception) do
  63. csv = FasterCSV.parse(@data, :headers => [:my, :new, :headers],
  64. :return_headers => true,
  65. :header_converters => lambda { |h| h.to_s } )
  66. end
  67. row = csv[0]
  68. assert_not_nil(row)
  69. assert_instance_of(FasterCSV::Row, row)
  70. assert_equal( [["my", :my], ["new", :new], ["headers", :headers]],
  71. row.to_a )
  72. assert(row.header_row?)
  73. assert(!row.field_row?)
  74. end
  75. def test_csv_header_string
  76. # activate headers
  77. csv = nil
  78. assert_nothing_raised(Exception) do
  79. csv = FasterCSV.parse(@data, :headers => "my,new,headers")
  80. end
  81. # first data row - skipping headers
  82. row = csv[0]
  83. assert_not_nil(row)
  84. assert_instance_of(FasterCSV::Row, row)
  85. assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a)
  86. # second data row
  87. row = csv[1]
  88. assert_not_nil(row)
  89. assert_instance_of(FasterCSV::Row, row)
  90. assert_equal([%w{my A}, %w{new B}, %w{headers C}], row.to_a)
  91. # third data row
  92. row = csv[2]
  93. assert_not_nil(row)
  94. assert_instance_of(FasterCSV::Row, row)
  95. assert_equal([%w{my 1}, %w{new 2}, %w{headers 3}], row.to_a)
  96. # empty
  97. assert_nil(csv[3])
  98. # with return and convert
  99. assert_nothing_raised(Exception) do
  100. csv = FasterCSV.parse(@data, :headers => "my,new,headers",
  101. :return_headers => true,
  102. :header_converters => :symbol )
  103. end
  104. row = csv[0]
  105. assert_not_nil(row)
  106. assert_instance_of(FasterCSV::Row, row)
  107. assert_equal( [[:my, "my"], [:new, "new"], [:headers, "headers"]],
  108. row.to_a )
  109. assert(row.header_row?)
  110. assert(!row.field_row?)
  111. end
  112. def test_csv_header_string_inherits_separators
  113. # parse with custom col_sep
  114. csv = nil
  115. assert_nothing_raised(Exception) do
  116. csv = FasterCSV.parse( @data.tr(",", "|"), :col_sep => "|",
  117. :headers => "my|new|headers" )
  118. end
  119. # verify headers were recognized
  120. row = csv[0]
  121. assert_not_nil(row)
  122. assert_instance_of(FasterCSV::Row, row)
  123. assert_equal([%w{my first}, %w{new second}, %w{headers third}], row.to_a)
  124. end
  125. def test_return_headers
  126. # activate headers and request they are returned
  127. csv = nil
  128. assert_nothing_raised(Exception) do
  129. csv = FasterCSV.parse(@data, :headers => true, :return_headers => true)
  130. end
  131. # header row
  132. row = csv[0]
  133. assert_not_nil(row)
  134. assert_instance_of(FasterCSV::Row, row)
  135. assert_equal( [%w{first first}, %w{second second}, %w{third third}],
  136. row.to_a )
  137. assert(row.header_row?)
  138. assert(!row.field_row?)
  139. # first data row - skipping headers
  140. row = csv[1]
  141. assert_not_nil(row)
  142. assert_instance_of(FasterCSV::Row, row)
  143. assert_equal([%w{first A}, %w{second B}, %w{third C}], row.to_a)
  144. assert(!row.header_row?)
  145. assert(row.field_row?)
  146. # second data row
  147. row = csv[2]
  148. assert_not_nil(row)
  149. assert_instance_of(FasterCSV::Row, row)
  150. assert_equal([%w{first 1}, %w{second 2}, %w{third 3}], row.to_a)
  151. assert(!row.header_row?)
  152. assert(row.field_row?)
  153. # empty
  154. assert_nil(csv[3])
  155. end
  156. def test_converters
  157. # create test data where headers and fields look alike
  158. data = <<-END_MATCHING_CSV.gsub(/^\s+/, "")
  159. 1,2,3
  160. 1,2,3
  161. END_MATCHING_CSV
  162. # normal converters do not affect headers
  163. csv = FasterCSV.parse( data, :headers => true,
  164. :return_headers => true,
  165. :converters => :numeric )
  166. assert_equal([%w{1 1}, %w{2 2}, %w{3 3}], csv[0].to_a)
  167. assert_equal([["1", 1], ["2", 2], ["3", 3]], csv[1].to_a)
  168. assert_nil(csv[2])
  169. # header converters do affect headers (only)
  170. assert_nothing_raised(Exception) do
  171. csv = FasterCSV.parse( data, :headers => true,
  172. :return_headers => true,
  173. :converters => :numeric,
  174. :header_converters => :symbol )
  175. end
  176. assert_equal([[:"1", "1"], [:"2", "2"], [:"3", "3"]], csv[0].to_a)
  177. assert_equal([[:"1", 1], [:"2", 2], [:"3", 3]], csv[1].to_a)
  178. assert_nil(csv[2])
  179. end
  180. def test_builtin_downcase_converter
  181. csv = FasterCSV.parse( "One,TWO Three", :headers => true,
  182. :return_headers => true,
  183. :header_converters => :downcase )
  184. assert_equal(%w{one two\ three}, csv.headers)
  185. end
  186. def test_builtin_symbol_converter
  187. csv = FasterCSV.parse( "One,TWO Three", :headers => true,
  188. :return_headers => true,
  189. :header_converters => :symbol )
  190. assert_equal([:one, :two_three], csv.headers)
  191. end
  192. def test_custom_converter
  193. converter = lambda { |header| header.tr(" ", "_") }
  194. csv = FasterCSV.parse( "One,TWO Three",
  195. :headers => true,
  196. :return_headers => true,
  197. :header_converters => converter )
  198. assert_equal(%w{One TWO_Three}, csv.headers)
  199. end
  200. def test_table_support
  201. csv = nil
  202. assert_nothing_raised(Exception) do
  203. csv = FasterCSV.parse(@data, :headers => true)
  204. end
  205. assert_instance_of(FasterCSV::Table, csv)
  206. end
  207. def test_skip_blanks
  208. @data = <<-END_CSV.gsub(/^ +/, "")
  209. A,B,C
  210. 1,2,3
  211. END_CSV
  212. expected = [%w[1 2 3]]
  213. FasterCSV.parse(@data, :headers => true, :skip_blanks => true) do |row|
  214. assert_equal(expected.shift, row.fields)
  215. end
  216. expected = [%w[A B C], %w[1 2 3]]
  217. FasterCSV.parse( @data,
  218. :headers => true,
  219. :return_headers => true,
  220. :skip_blanks => true ) do |row|
  221. assert_equal(expected.shift, row.fields)
  222. end
  223. end
  224. def test_blank_row_bug_fix
  225. @data += "\n#{@data}" # add a blank row
  226. # ensure that everything returned is a Row object
  227. FasterCSV.parse(@data, :headers => true) do |row|
  228. assert_instance_of(FasterCSV::Row, row)
  229. end
  230. end
  231. end