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

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

https://github.com/mislav/radiant
Ruby | 363 lines | 290 code | 72 blank | 1 comment | 4 complexity | bccb67bc2399f4f65ff0891b2004bec8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, MIT
  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..42).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[41], array.forty_two
  21. end
  22. end
  23. class ArrayExtToParamTests < Test::Unit::TestCase
  24. class ToParam < String
  25. def to_param
  26. "#{self}1"
  27. end
  28. end
  29. def test_string_array
  30. assert_equal '', %w().to_param
  31. assert_equal 'hello/world', %w(hello world).to_param
  32. assert_equal 'hello/10', %w(hello 10).to_param
  33. end
  34. def test_number_array
  35. assert_equal '10/20', [10, 20].to_param
  36. end
  37. def test_to_param_array
  38. assert_equal 'custom1/param1', [ToParam.new('custom'), ToParam.new('param')].to_param
  39. end
  40. end
  41. class ArrayExtToSentenceTests < Test::Unit::TestCase
  42. include ActiveSupport::Testing::Deprecation
  43. def test_plain_array_to_sentence
  44. assert_equal "", [].to_sentence
  45. assert_equal "one", ['one'].to_sentence
  46. assert_equal "one and two", ['one', 'two'].to_sentence
  47. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
  48. end
  49. def test_to_sentence_with_words_connector
  50. assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
  51. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
  52. end
  53. assert_deprecated(":connector has been deprecated. Use :words_connector instead") do
  54. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
  55. end
  56. assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
  57. assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
  58. assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
  59. end
  60. def test_to_sentence_with_last_word_connector
  61. assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
  62. assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true)
  63. end
  64. assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do
  65. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
  66. end
  67. assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
  68. assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
  69. assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
  70. assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' and ')
  71. end
  72. def test_two_elements
  73. assert_equal "one and two", ['one', 'two'].to_sentence
  74. assert_equal "one two", ['one', 'two'].to_sentence(:two_words_connector => ' ')
  75. end
  76. def test_one_element
  77. assert_equal "one", ['one'].to_sentence
  78. end
  79. def test_one_non_string_element
  80. assert_equal '1', [1].to_sentence
  81. end
  82. end
  83. class ArrayExtToSTests < Test::Unit::TestCase
  84. def test_to_s_db
  85. collection = [
  86. Class.new { def id() 1 end }.new,
  87. Class.new { def id() 2 end }.new,
  88. Class.new { def id() 3 end }.new
  89. ]
  90. assert_equal "null", [].to_s(:db)
  91. assert_equal "1,2,3", collection.to_s(:db)
  92. end
  93. end
  94. class ArrayExtGroupingTests < Test::Unit::TestCase
  95. def test_in_groups_of_with_perfect_fit
  96. groups = []
  97. ('a'..'i').to_a.in_groups_of(3) do |group|
  98. groups << group
  99. end
  100. assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
  101. assert_equal [%w(a b c), %w(d e f), %w(g h i)], ('a'..'i').to_a.in_groups_of(3)
  102. end
  103. def test_in_groups_of_with_padding
  104. groups = []
  105. ('a'..'g').to_a.in_groups_of(3) do |group|
  106. groups << group
  107. end
  108. assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups
  109. end
  110. def test_in_groups_of_pads_with_specified_values
  111. groups = []
  112. ('a'..'g').to_a.in_groups_of(3, 'foo') do |group|
  113. groups << group
  114. end
  115. assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups
  116. end
  117. def test_in_groups_of_without_padding
  118. groups = []
  119. ('a'..'g').to_a.in_groups_of(3, false) do |group|
  120. groups << group
  121. end
  122. assert_equal [%w(a b c), %w(d e f), ['g']], groups
  123. end
  124. def test_in_groups_returned_array_size
  125. array = (1..7).to_a
  126. 1.upto(array.size + 1) do |number|
  127. assert_equal number, array.in_groups(number).size
  128. end
  129. end
  130. def test_in_groups_with_empty_array
  131. assert_equal [[], [], []], [].in_groups(3)
  132. end
  133. def test_in_groups_with_block
  134. array = (1..9).to_a
  135. groups = []
  136. array.in_groups(3) do |group|
  137. groups << group
  138. end
  139. assert_equal array.in_groups(3), groups
  140. end
  141. def test_in_groups_with_perfect_fit
  142. assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  143. (1..9).to_a.in_groups(3)
  144. end
  145. def test_in_groups_with_padding
  146. array = (1..7).to_a
  147. assert_equal [[1, 2, 3], [4, 5, nil], [6, 7, nil]],
  148. array.in_groups(3)
  149. assert_equal [[1, 2, 3], [4, 5, 'foo'], [6, 7, 'foo']],
  150. array.in_groups(3, 'foo')
  151. end
  152. def test_in_groups_without_padding
  153. assert_equal [[1, 2, 3], [4, 5], [6, 7]],
  154. (1..7).to_a.in_groups(3, false)
  155. end
  156. end
  157. class ArraySplitTests < Test::Unit::TestCase
  158. def test_split_with_empty_array
  159. assert_equal [[]], [].split(0)
  160. end
  161. def test_split_with_argument
  162. assert_equal [[1, 2], [4, 5]], [1, 2, 3, 4, 5].split(3)
  163. assert_equal [[1, 2, 3, 4, 5]], [1, 2, 3, 4, 5].split(0)
  164. end
  165. def test_split_with_block
  166. assert_equal [[1, 2], [4, 5], [7, 8], [10]], (1..10).to_a.split { |i| i % 3 == 0 }
  167. end
  168. def test_split_with_edge_values
  169. assert_equal [[], [2, 3, 4, 5]], [1, 2, 3, 4, 5].split(1)
  170. assert_equal [[1, 2, 3, 4], []], [1, 2, 3, 4, 5].split(5)
  171. assert_equal [[], [2, 3, 4], []], [1, 2, 3, 4, 5].split { |i| i == 1 || i == 5 }
  172. end
  173. end
  174. class ArrayToXmlTests < Test::Unit::TestCase
  175. def test_to_xml
  176. xml = [
  177. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  178. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  179. ].to_xml(:skip_instruct => true, :indent => 0)
  180. assert_equal '<records type="array"><record>', xml.first(30)
  181. assert xml.include?(%(<age type="integer">26</age>)), xml
  182. assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml
  183. assert xml.include?(%(<name>David</name>)), xml
  184. assert xml.include?(%(<age type="integer">31</age>)), xml
  185. assert xml.include?(%(<age-in-millis type="decimal">1.0</age-in-millis>)), xml
  186. assert xml.include?(%(<name>Jason</name>)), xml
  187. end
  188. def test_to_xml_dups_options
  189. options = {:skip_instruct => true}
  190. [].to_xml(options)
  191. # :builder, etc, shouldn't be added to options
  192. assert_equal({:skip_instruct => true}, options)
  193. end
  194. def test_to_xml_with_dedicated_name
  195. xml = [
  196. { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
  197. ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
  198. assert_equal '<people type="array"><person>', xml.first(29)
  199. end
  200. def test_to_xml_with_options
  201. xml = [
  202. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  203. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
  204. assert_equal "<records><record>", xml.first(17)
  205. assert xml.include?(%(<street-address>Paulina</street-address>))
  206. assert xml.include?(%(<name>David</name>))
  207. assert xml.include?(%(<street-address>Evergreen</street-address>))
  208. assert xml.include?(%(<name>Jason</name>))
  209. end
  210. def test_to_xml_with_dasherize_false
  211. xml = [
  212. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  213. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => false)
  214. assert_equal "<records><record>", xml.first(17)
  215. assert xml.include?(%(<street_address>Paulina</street_address>))
  216. assert xml.include?(%(<street_address>Evergreen</street_address>))
  217. end
  218. def test_to_xml_with_dasherize_true
  219. xml = [
  220. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  221. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true)
  222. assert_equal "<records><record>", xml.first(17)
  223. assert xml.include?(%(<street-address>Paulina</street-address>))
  224. assert xml.include?(%(<street-address>Evergreen</street-address>))
  225. end
  226. def test_to_with_instruct
  227. xml = [
  228. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  229. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  230. ].to_xml(:skip_instruct => false, :indent => 0)
  231. assert_match(/^<\?xml [^>]*/, xml)
  232. assert_equal 0, xml.rindex(/<\?xml /)
  233. end
  234. def test_to_xml_with_block
  235. xml = [
  236. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  237. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  238. ].to_xml(:skip_instruct => true, :indent => 0) do |builder|
  239. builder.count 2
  240. end
  241. assert xml.include?(%(<count>2</count>)), xml
  242. end
  243. def test_to_xml_with_empty
  244. xml = [].to_xml
  245. assert_match(/type="array"\/>/, xml)
  246. end
  247. end
  248. class ArrayExtractOptionsTests < Test::Unit::TestCase
  249. def test_extract_options
  250. assert_equal({}, [].extract_options!)
  251. assert_equal({}, [1].extract_options!)
  252. assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
  253. assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
  254. end
  255. end
  256. class ArrayExtRandomTests < Test::Unit::TestCase
  257. def test_random_element_from_array
  258. assert_nil [].rand
  259. Kernel.expects(:rand).with(1).returns(0)
  260. assert_equal 'x', ['x'].rand
  261. Kernel.expects(:rand).with(3).returns(1)
  262. assert_equal 2, [1, 2, 3].rand
  263. end
  264. end
  265. class ArrayWrapperTests < Test::Unit::TestCase
  266. class FakeCollection
  267. def to_ary
  268. ["foo", "bar"]
  269. end
  270. end
  271. def test_array
  272. ary = %w(foo bar)
  273. assert_same ary, Array.wrap(ary)
  274. end
  275. def test_nil
  276. assert_equal [], Array.wrap(nil)
  277. end
  278. def test_object
  279. o = Object.new
  280. assert_equal [o], Array.wrap(o)
  281. end
  282. def test_string
  283. assert_equal ["foo"], Array.wrap("foo")
  284. end
  285. def test_string_with_newline
  286. assert_equal ["foo\nbar"], Array.wrap("foo\nbar")
  287. end
  288. def test_object_with_to_ary
  289. assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new)
  290. end
  291. end