PageRenderTime 82ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ruby-1.9.1-p0/test/csv/test_row.rb

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