PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activesupport/test/core_ext/array_ext_test.rb

https://github.com/port49/lwdg_rails
Ruby | 309 lines | 250 code | 59 blank | 0 comment | 4 complexity | 87146631e7bd2022a3e78fe394c22a99 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'bigdecimal'
  3. class ArrayExtAccessTests < Test::Unit::TestCase
  4. def test_from
  5. assert_equal %w( a b c d ), %w( a b c d ).from(0)
  6. assert_equal %w( c d ), %w( a b c d ).from(2)
  7. assert_nil %w( a b c d ).from(10)
  8. end
  9. def test_to
  10. assert_equal %w( a ), %w( a b c d ).to(0)
  11. assert_equal %w( a b c ), %w( a b c d ).to(2)
  12. assert_equal %w( a b c d ), %w( a b c d ).to(10)
  13. end
  14. def test_second_through_tenth
  15. array = (1..10).to_a
  16. assert_equal array[1], array.second
  17. assert_equal array[2], array.third
  18. assert_equal array[3], array.fourth
  19. assert_equal array[4], array.fifth
  20. assert_equal array[5], array.sixth
  21. assert_equal array[6], array.seventh
  22. assert_equal array[7], array.eighth
  23. assert_equal array[8], array.ninth
  24. assert_equal array[9], array.tenth
  25. end
  26. end
  27. class ArrayExtToParamTests < Test::Unit::TestCase
  28. class ToParam < String
  29. def to_param
  30. "#{self}1"
  31. end
  32. end
  33. def test_string_array
  34. assert_equal '', %w().to_param
  35. assert_equal 'hello/world', %w(hello world).to_param
  36. assert_equal 'hello/10', %w(hello 10).to_param
  37. end
  38. def test_number_array
  39. assert_equal '10/20', [10, 20].to_param
  40. end
  41. def test_to_param_array
  42. assert_equal 'custom1/param1', [ToParam.new('custom'), ToParam.new('param')].to_param
  43. end
  44. end
  45. class ArrayExtToSentenceTests < Test::Unit::TestCase
  46. def test_plain_array_to_sentence
  47. assert_equal "", [].to_sentence
  48. assert_equal "one", ['one'].to_sentence
  49. assert_equal "one and two", ['one', 'two'].to_sentence
  50. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
  51. end
  52. def test_to_sentence_with_connector
  53. assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:connector => 'and also')
  54. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
  55. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => nil)
  56. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => ' ')
  57. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
  58. end
  59. def test_to_sentence_with_skip_last_comma
  60. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
  61. end
  62. def test_two_elements
  63. assert_equal "one and two", ['one', 'two'].to_sentence
  64. assert_equal "one two", ['one', 'two'].to_sentence(:connector => '')
  65. end
  66. def test_one_element
  67. assert_equal "one", ['one'].to_sentence
  68. end
  69. def test_one_non_string_element
  70. assert_equal '1', [1].to_sentence
  71. end
  72. end
  73. class ArrayExtToSTests < Test::Unit::TestCase
  74. def test_to_s_db
  75. collection = [
  76. Class.new { def id() 1 end }.new,
  77. Class.new { def id() 2 end }.new,
  78. Class.new { def id() 3 end }.new
  79. ]
  80. assert_equal "null", [].to_s(:db)
  81. assert_equal "1,2,3", collection.to_s(:db)
  82. end
  83. end
  84. class ArrayExtGroupingTests < Test::Unit::TestCase
  85. def test_in_groups_of_with_perfect_fit
  86. groups = []
  87. ('a'..'i').to_a.in_groups_of(3) do |group|
  88. groups << group
  89. end
  90. assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
  91. assert_equal [%w(a b c), %w(d e f), %w(g h i)], ('a'..'i').to_a.in_groups_of(3)
  92. end
  93. def test_in_groups_of_with_padding
  94. groups = []
  95. ('a'..'g').to_a.in_groups_of(3) do |group|
  96. groups << group
  97. end
  98. assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups
  99. end
  100. def test_in_groups_of_pads_with_specified_values
  101. groups = []
  102. ('a'..'g').to_a.in_groups_of(3, 'foo') do |group|
  103. groups << group
  104. end
  105. assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups
  106. end
  107. def test_in_groups_of_without_padding
  108. groups = []
  109. ('a'..'g').to_a.in_groups_of(3, false) do |group|
  110. groups << group
  111. end
  112. assert_equal [%w(a b c), %w(d e f), ['g']], groups
  113. end
  114. def test_in_groups_returned_array_size
  115. array = (1..7).to_a
  116. 1.upto(array.size + 1) do |number|
  117. assert_equal number, array.in_groups(number).size
  118. end
  119. end
  120. def test_in_groups_with_empty_array
  121. assert_equal [[], [], []], [].in_groups(3)
  122. end
  123. def test_in_groups_with_block
  124. array = (1..9).to_a
  125. groups = []
  126. array.in_groups(3) do |group|
  127. groups << group
  128. end
  129. assert_equal array.in_groups(3), groups
  130. end
  131. def test_in_groups_with_perfect_fit
  132. assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  133. (1..9).to_a.in_groups(3)
  134. end
  135. def test_in_groups_with_padding
  136. array = (1..7).to_a
  137. assert_equal [[1, 2, 3], [4, 5, nil], [6, 7, nil]],
  138. array.in_groups(3)
  139. assert_equal [[1, 2, 3], [4, 5, 'foo'], [6, 7, 'foo']],
  140. array.in_groups(3, 'foo')
  141. end
  142. def test_in_groups_without_padding
  143. assert_equal [[1, 2, 3], [4, 5], [6, 7]],
  144. (1..7).to_a.in_groups(3, false)
  145. end
  146. end
  147. class ArraySplitTests < Test::Unit::TestCase
  148. def test_split_with_empty_array
  149. assert_equal [[]], [].split(0)
  150. end
  151. def test_split_with_argument
  152. assert_equal [[1, 2], [4, 5]], [1, 2, 3, 4, 5].split(3)
  153. assert_equal [[1, 2, 3, 4, 5]], [1, 2, 3, 4, 5].split(0)
  154. end
  155. def test_split_with_block
  156. assert_equal [[1, 2], [4, 5], [7, 8], [10]], (1..10).to_a.split { |i| i % 3 == 0 }
  157. end
  158. def test_split_with_edge_values
  159. assert_equal [[], [2, 3, 4, 5]], [1, 2, 3, 4, 5].split(1)
  160. assert_equal [[1, 2, 3, 4], []], [1, 2, 3, 4, 5].split(5)
  161. assert_equal [[], [2, 3, 4], []], [1, 2, 3, 4, 5].split { |i| i == 1 || i == 5 }
  162. end
  163. end
  164. class ArrayToXmlTests < Test::Unit::TestCase
  165. def test_to_xml
  166. xml = [
  167. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  168. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  169. ].to_xml(:skip_instruct => true, :indent => 0)
  170. assert_equal '<records type="array"><record>', xml.first(30)
  171. assert xml.include?(%(<age type="integer">26</age>)), xml
  172. assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml
  173. assert xml.include?(%(<name>David</name>)), xml
  174. assert xml.include?(%(<age type="integer">31</age>)), xml
  175. assert xml.include?(%(<age-in-millis type="decimal">1.0</age-in-millis>)), xml
  176. assert xml.include?(%(<name>Jason</name>)), xml
  177. end
  178. def test_to_xml_with_dedicated_name
  179. xml = [
  180. { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
  181. ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
  182. assert_equal '<people type="array"><person>', xml.first(29)
  183. end
  184. def test_to_xml_with_options
  185. xml = [
  186. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  187. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
  188. assert_equal "<records><record>", xml.first(17)
  189. assert xml.include?(%(<street-address>Paulina</street-address>))
  190. assert xml.include?(%(<name>David</name>))
  191. assert xml.include?(%(<street-address>Evergreen</street-address>))
  192. assert xml.include?(%(<name>Jason</name>))
  193. end
  194. def test_to_xml_with_dasherize_false
  195. xml = [
  196. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  197. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => false)
  198. assert_equal "<records><record>", xml.first(17)
  199. assert xml.include?(%(<street_address>Paulina</street_address>))
  200. assert xml.include?(%(<street_address>Evergreen</street_address>))
  201. end
  202. def test_to_xml_with_dasherize_true
  203. xml = [
  204. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  205. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true)
  206. assert_equal "<records><record>", xml.first(17)
  207. assert xml.include?(%(<street-address>Paulina</street-address>))
  208. assert xml.include?(%(<street-address>Evergreen</street-address>))
  209. end
  210. def test_to_with_instruct
  211. xml = [
  212. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  213. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  214. ].to_xml(:skip_instruct => false, :indent => 0)
  215. assert_match(/^<\?xml [^>]*/, xml)
  216. assert_equal 0, xml.rindex(/<\?xml /)
  217. end
  218. def test_to_xml_with_block
  219. xml = [
  220. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  221. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  222. ].to_xml(:skip_instruct => true, :indent => 0) do |builder|
  223. builder.count 2
  224. end
  225. assert xml.include?(%(<count>2</count>)), xml
  226. end
  227. def test_to_xml_with_empty
  228. xml = [].to_xml
  229. assert_match(/type="array"\/>/, xml)
  230. end
  231. end
  232. class ArrayExtractOptionsTests < Test::Unit::TestCase
  233. def test_extract_options
  234. assert_equal({}, [].extract_options!)
  235. assert_equal({}, [1].extract_options!)
  236. assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
  237. assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
  238. end
  239. end
  240. uses_mocha "ArrayExtRandomTests" do
  241. class ArrayExtRandomTests < Test::Unit::TestCase
  242. def test_random_element_from_array
  243. assert_nil [].rand
  244. Kernel.expects(:rand).with(1).returns(0)
  245. assert_equal 'x', ['x'].rand
  246. Kernel.expects(:rand).with(3).returns(1)
  247. assert_equal 2, [1, 2, 3].rand
  248. end
  249. end
  250. end