tc_row.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/usr/local/bin/ruby -w
  2. # tc_row.rb
  3. #
  4. # Created by James Edward Gray II on 2006-02-24.
  5. # Copyright 2012 Gray Productions. All rights reserved.
  6. require "test/unit"
  7. require "faster_csv"
  8. class TestFasterCSVRow < Test::Unit::TestCase
  9. def setup
  10. @row = FasterCSV::Row.new(%w{A B C A A}, [1, 2, 3, 4])
  11. end
  12. def test_initialize
  13. # basic
  14. row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3])
  15. assert_not_nil(row)
  16. assert_instance_of(FasterCSV::Row, row)
  17. assert_equal([["A", 1], ["B", 2], ["C", 3]], row.to_a)
  18. # missing headers
  19. row = FasterCSV::Row.new(%w{A}, [1, 2, 3])
  20. assert_not_nil(row)
  21. assert_instance_of(FasterCSV::Row, row)
  22. assert_equal([["A", 1], [nil, 2], [nil, 3]], row.to_a)
  23. # missing fields
  24. row = FasterCSV::Row.new(%w{A B C}, [1, 2])
  25. assert_not_nil(row)
  26. assert_instance_of(FasterCSV::Row, row)
  27. assert_equal([["A", 1], ["B", 2], ["C", nil]], row.to_a)
  28. end
  29. def test_row_type
  30. # field rows
  31. row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3]) # implicit
  32. assert(!row.header_row?)
  33. assert(row.field_row?)
  34. row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3], false) # explicit
  35. assert(!row.header_row?)
  36. assert(row.field_row?)
  37. # header row
  38. row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3], true)
  39. assert(row.header_row?)
  40. assert(!row.field_row?)
  41. end
  42. def test_headers
  43. assert_equal(%w{A B C A A}, @row.headers)
  44. end
  45. def test_field
  46. # by name
  47. assert_equal(2, @row.field("B"))
  48. assert_equal(2, @row["B"]) # alias
  49. # by index
  50. assert_equal(3, @row.field(2))
  51. # missing
  52. assert_nil(@row.field("Missing"))
  53. assert_nil(@row.field(10))
  54. # minimum index
  55. assert_equal(1, @row.field("A"))
  56. assert_equal(1, @row.field("A", 0))
  57. assert_equal(4, @row.field("A", 1))
  58. assert_equal(4, @row.field("A", 2))
  59. assert_equal(4, @row.field("A", 3))
  60. assert_equal(nil, @row.field("A", 4))
  61. assert_equal(nil, @row.field("A", 5))
  62. end
  63. def test_set_field
  64. # set field by name
  65. assert_equal(100, @row["A"] = 100)
  66. # set field by index
  67. assert_equal(300, @row[3] = 300)
  68. # set field by name and minimum index
  69. assert_equal([:a, :b, :c], @row["A", 4] = [:a, :b, :c])
  70. # verify the changes
  71. assert_equal( [ ["A", 100],
  72. ["B", 2],
  73. ["C", 3],
  74. ["A", 300],
  75. ["A", [:a, :b, :c]] ], @row.to_a )
  76. # assigning an index past the end
  77. assert_equal("End", @row[10] = "End")
  78. assert_equal( [ ["A", 100],
  79. ["B", 2],
  80. ["C", 3],
  81. ["A", 300],
  82. ["A", [:a, :b, :c]],
  83. [nil, nil],
  84. [nil, nil],
  85. [nil, nil],
  86. [nil, nil],
  87. [nil, nil],
  88. [nil, "End"] ], @row.to_a )
  89. # assigning a new field by header
  90. assert_equal("New", @row[:new] = "New")
  91. assert_equal( [ ["A", 100],
  92. ["B", 2],
  93. ["C", 3],
  94. ["A", 300],
  95. ["A", [:a, :b, :c]],
  96. [nil, nil],
  97. [nil, nil],
  98. [nil, nil],
  99. [nil, nil],
  100. [nil, nil],
  101. [nil, "End"],
  102. [:new, "New"] ], @row.to_a )
  103. end
  104. def test_append
  105. # add a value
  106. assert_equal(@row, @row << "Value")
  107. assert_equal( [ ["A", 1],
  108. ["B", 2],
  109. ["C", 3],
  110. ["A", 4],
  111. ["A", nil],
  112. [nil, "Value"] ], @row.to_a )
  113. # add a pair
  114. assert_equal(@row, @row << %w{Header Field})
  115. assert_equal( [ ["A", 1],
  116. ["B", 2],
  117. ["C", 3],
  118. ["A", 4],
  119. ["A", nil],
  120. [nil, "Value"],
  121. %w{Header Field} ], @row.to_a )
  122. # a pair with Hash syntax
  123. assert_equal(@row, @row << {:key => :value})
  124. assert_equal( [ ["A", 1],
  125. ["B", 2],
  126. ["C", 3],
  127. ["A", 4],
  128. ["A", nil],
  129. [nil, "Value"],
  130. %w{Header Field},
  131. [:key, :value] ], @row.to_a )
  132. # multiple fields at once
  133. assert_equal(@row, @row.push(100, 200, [:last, 300]))
  134. assert_equal( [ ["A", 1],
  135. ["B", 2],
  136. ["C", 3],
  137. ["A", 4],
  138. ["A", nil],
  139. [nil, "Value"],
  140. %w{Header Field},
  141. [:key, :value],
  142. [nil, 100],
  143. [nil, 200],
  144. [:last, 300] ], @row.to_a )
  145. end
  146. def test_delete
  147. # by index
  148. assert_equal(["B", 2], @row.delete(1))
  149. # by header
  150. assert_equal(["C", 3], @row.delete("C"))
  151. # using a block
  152. assert_equal(@row, @row.delete_if { |h, f| h == "A" and not f.nil? })
  153. assert_equal([["A", nil]], @row.to_a)
  154. end
  155. def test_fields
  156. # all fields
  157. assert_equal([1, 2, 3, 4, nil], @row.fields)
  158. # by header
  159. assert_equal([1, 3], @row.fields("A", "C"))
  160. # by index
  161. assert_equal([2, 3, nil], @row.fields(1, 2, 10))
  162. # by both
  163. assert_equal([2, 3, 4], @row.fields("B", "C", 3))
  164. # with minimum indices
  165. assert_equal([2, 3, 4], @row.fields("B", "C", ["A", 3]))
  166. # by header range
  167. assert_equal([2, 3], @row.values_at("B".."C"))
  168. end
  169. def test_index
  170. # basic usage
  171. assert_equal(0, @row.index("A"))
  172. assert_equal(1, @row.index("B"))
  173. assert_equal(2, @row.index("C"))
  174. assert_equal(nil, @row.index("Z"))
  175. # with minimum index
  176. assert_equal(0, @row.index("A"))
  177. assert_equal(0, @row.index("A", 0))
  178. assert_equal(3, @row.index("A", 1))
  179. assert_equal(3, @row.index("A", 2))
  180. assert_equal(3, @row.index("A", 3))
  181. assert_equal(4, @row.index("A", 4))
  182. assert_equal(nil, @row.index("A", 5))
  183. end
  184. def test_queries
  185. # headers
  186. assert(@row.header?("A"))
  187. assert(@row.header?("C"))
  188. assert(!@row.header?("Z"))
  189. assert(@row.include?("A")) # alias
  190. # fields
  191. assert(@row.field?(4))
  192. assert(@row.field?(nil))
  193. assert(!@row.field?(10))
  194. end
  195. def test_each
  196. # array style
  197. ary = @row.to_a
  198. @row.each do |pair|
  199. assert_equal(ary.first.first, pair.first)
  200. assert_equal(ary.shift.last, pair.last)
  201. end
  202. # hash style
  203. ary = @row.to_a
  204. @row.each do |header, field|
  205. assert_equal(ary.first.first, header)
  206. assert_equal(ary.shift.last, field)
  207. end
  208. # verify that we can chain the call
  209. assert_equal(@row, @row.each { })
  210. end
  211. def test_enumerable
  212. assert_equal( [["A", 1], ["A", 4], ["A", nil]],
  213. @row.select { |pair| pair.first == "A" } )
  214. assert_equal(10, @row.inject(0) { |sum, (header, n)| sum + (n || 0) })
  215. end
  216. def test_to_a
  217. row = FasterCSV::Row.new(%w{A B C}, [1, 2, 3]).to_a
  218. assert_instance_of(Array, row)
  219. row.each do |pair|
  220. assert_instance_of(Array, pair)
  221. assert_equal(2, pair.size)
  222. end
  223. assert_equal([["A", 1], ["B", 2], ["C", 3]], row)
  224. end
  225. def test_to_hash
  226. assert_equal({"A" => nil, "B" => 2, "C" => 3}, @row.to_hash)
  227. end
  228. def test_to_csv
  229. # normal conversion
  230. assert_equal("1,2,3,4,\n", @row.to_csv)
  231. assert_equal("1,2,3,4,\n", @row.to_s) # alias
  232. # with options
  233. assert_equal( "1|2|3|4|\r\n",
  234. @row.to_csv(:col_sep => "|", :row_sep => "\r\n") )
  235. end
  236. def test_array_delegation
  237. assert(!@row.empty?, "Row was empty.")
  238. assert_equal([@row.headers.size, @row.fields.size].max, @row.size)
  239. end
  240. def test_inspect_shows_header_field_pairs
  241. str = @row.inspect
  242. @row.each do |header, field|
  243. assert( str.include?("#{header.inspect}:#{field.inspect}"),
  244. "Header field pair not found." )
  245. end
  246. end
  247. def test_inspect_shows_symbol_headers_as_bare_attributes
  248. str = FasterCSV::Row.new( @row.headers.map { |h| h.to_sym },
  249. @row.fields ).inspect
  250. @row.each do |header, field|
  251. assert( str.include?("#{header}:#{field.inspect}"),
  252. "Header field pair not found." )
  253. end
  254. end
  255. end