PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/array_ext_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 477 lines | 385 code | 91 blank | 1 comment | 4 complexity | 8bf5e8ef49d77020411cf7a7b416926b MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/core_ext/array'
  3. require 'active_support/core_ext/big_decimal'
  4. require 'active_support/core_ext/object/conversions'
  5. require 'active_support/core_ext' # FIXME: pulling in all to_xml extensions
  6. require 'active_support/hash_with_indifferent_access'
  7. class ArrayExtAccessTests < Test::Unit::TestCase
  8. def test_from
  9. assert_equal %w( a b c d ), %w( a b c d ).from(0)
  10. assert_equal %w( c d ), %w( a b c d ).from(2)
  11. assert_equal %w(), %w( a b c d ).from(10)
  12. end
  13. def test_to
  14. assert_equal %w( a ), %w( a b c d ).to(0)
  15. assert_equal %w( a b c ), %w( a b c d ).to(2)
  16. assert_equal %w( a b c d ), %w( a b c d ).to(10)
  17. end
  18. def test_second_through_tenth
  19. array = (1..42).to_a
  20. assert_equal array[1], array.second
  21. assert_equal array[2], array.third
  22. assert_equal array[3], array.fourth
  23. assert_equal array[4], array.fifth
  24. assert_equal array[41], array.forty_two
  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_words_connector
  53. assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ')
  54. assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ')
  55. assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil)
  56. end
  57. def test_to_sentence_with_last_word_connector
  58. assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ')
  59. assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil)
  60. assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ')
  61. assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' and ')
  62. end
  63. def test_two_elements
  64. assert_equal "one and two", ['one', 'two'].to_sentence
  65. assert_equal "one two", ['one', 'two'].to_sentence(:two_words_connector => ' ')
  66. end
  67. def test_one_element
  68. assert_equal "one", ['one'].to_sentence
  69. end
  70. def test_one_element_not_same_object
  71. elements = ["one"]
  72. assert_not_equal elements[0].object_id, elements.to_sentence.object_id
  73. end
  74. def test_one_non_string_element
  75. assert_equal '1', [1].to_sentence
  76. end
  77. end
  78. class ArrayExtToSTests < Test::Unit::TestCase
  79. def test_to_s_db
  80. collection = [
  81. Class.new { def id() 1 end }.new,
  82. Class.new { def id() 2 end }.new,
  83. Class.new { def id() 3 end }.new
  84. ]
  85. assert_equal "null", [].to_s(:db)
  86. assert_equal "1,2,3", collection.to_s(:db)
  87. end
  88. end
  89. class ArrayExtGroupingTests < Test::Unit::TestCase
  90. def test_in_groups_of_with_perfect_fit
  91. groups = []
  92. ('a'..'i').to_a.in_groups_of(3) do |group|
  93. groups << group
  94. end
  95. assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
  96. assert_equal [%w(a b c), %w(d e f), %w(g h i)], ('a'..'i').to_a.in_groups_of(3)
  97. end
  98. def test_in_groups_of_with_padding
  99. groups = []
  100. ('a'..'g').to_a.in_groups_of(3) do |group|
  101. groups << group
  102. end
  103. assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups
  104. end
  105. def test_in_groups_of_pads_with_specified_values
  106. groups = []
  107. ('a'..'g').to_a.in_groups_of(3, 'foo') do |group|
  108. groups << group
  109. end
  110. assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups
  111. end
  112. def test_in_groups_of_without_padding
  113. groups = []
  114. ('a'..'g').to_a.in_groups_of(3, false) do |group|
  115. groups << group
  116. end
  117. assert_equal [%w(a b c), %w(d e f), ['g']], groups
  118. end
  119. def test_in_groups_returned_array_size
  120. array = (1..7).to_a
  121. 1.upto(array.size + 1) do |number|
  122. assert_equal number, array.in_groups(number).size
  123. end
  124. end
  125. def test_in_groups_with_empty_array
  126. assert_equal [[], [], []], [].in_groups(3)
  127. end
  128. def test_in_groups_with_block
  129. array = (1..9).to_a
  130. groups = []
  131. array.in_groups(3) do |group|
  132. groups << group
  133. end
  134. assert_equal array.in_groups(3), groups
  135. end
  136. def test_in_groups_with_perfect_fit
  137. assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  138. (1..9).to_a.in_groups(3)
  139. end
  140. def test_in_groups_with_padding
  141. array = (1..7).to_a
  142. assert_equal [[1, 2, 3], [4, 5, nil], [6, 7, nil]],
  143. array.in_groups(3)
  144. assert_equal [[1, 2, 3], [4, 5, 'foo'], [6, 7, 'foo']],
  145. array.in_groups(3, 'foo')
  146. end
  147. def test_in_groups_without_padding
  148. assert_equal [[1, 2, 3], [4, 5], [6, 7]],
  149. (1..7).to_a.in_groups(3, false)
  150. end
  151. end
  152. class ArraySplitTests < Test::Unit::TestCase
  153. def test_split_with_empty_array
  154. assert_equal [[]], [].split(0)
  155. end
  156. def test_split_with_argument
  157. assert_equal [[1, 2], [4, 5]], [1, 2, 3, 4, 5].split(3)
  158. assert_equal [[1, 2, 3, 4, 5]], [1, 2, 3, 4, 5].split(0)
  159. end
  160. def test_split_with_block
  161. assert_equal [[1, 2], [4, 5], [7, 8], [10]], (1..10).to_a.split { |i| i % 3 == 0 }
  162. end
  163. def test_split_with_edge_values
  164. assert_equal [[], [2, 3, 4, 5]], [1, 2, 3, 4, 5].split(1)
  165. assert_equal [[1, 2, 3, 4], []], [1, 2, 3, 4, 5].split(5)
  166. assert_equal [[], [2, 3, 4], []], [1, 2, 3, 4, 5].split { |i| i == 1 || i == 5 }
  167. end
  168. end
  169. class ArrayToXmlTests < Test::Unit::TestCase
  170. def test_to_xml
  171. xml = [
  172. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  173. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  174. ].to_xml(:skip_instruct => true, :indent => 0)
  175. assert_equal '<objects type="array"><object>', xml.first(30)
  176. assert xml.include?(%(<age type="integer">26</age>)), xml
  177. assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml
  178. assert xml.include?(%(<name>David</name>)), xml
  179. assert xml.include?(%(<age type="integer">31</age>)), xml
  180. assert xml.include?(%(<age-in-millis type="decimal">1.0</age-in-millis>)), xml
  181. assert xml.include?(%(<name>Jason</name>)), xml
  182. end
  183. def test_to_xml_with_dedicated_name
  184. xml = [
  185. { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
  186. ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
  187. assert_equal '<people type="array"><person>', xml.first(29)
  188. end
  189. def test_to_xml_with_options
  190. xml = [
  191. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  192. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
  193. assert_equal "<objects><object>", xml.first(17)
  194. assert xml.include?(%(<street-address>Paulina</street-address>))
  195. assert xml.include?(%(<name>David</name>))
  196. assert xml.include?(%(<street-address>Evergreen</street-address>))
  197. assert xml.include?(%(<name>Jason</name>))
  198. end
  199. def test_to_xml_with_dasherize_false
  200. xml = [
  201. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  202. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => false)
  203. assert_equal "<objects><object>", xml.first(17)
  204. assert xml.include?(%(<street_address>Paulina</street_address>))
  205. assert xml.include?(%(<street_address>Evergreen</street_address>))
  206. end
  207. def test_to_xml_with_dasherize_true
  208. xml = [
  209. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  210. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true)
  211. assert_equal "<objects><object>", xml.first(17)
  212. assert xml.include?(%(<street-address>Paulina</street-address>))
  213. assert xml.include?(%(<street-address>Evergreen</street-address>))
  214. end
  215. def test_to_with_instruct
  216. xml = [
  217. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  218. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  219. ].to_xml(:skip_instruct => false, :indent => 0)
  220. assert_match(/^<\?xml [^>]*/, xml)
  221. assert_equal 0, xml.rindex(/<\?xml /)
  222. end
  223. def test_to_xml_with_block
  224. xml = [
  225. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  226. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  227. ].to_xml(:skip_instruct => true, :indent => 0) do |builder|
  228. builder.count 2
  229. end
  230. assert xml.include?(%(<count>2</count>)), xml
  231. end
  232. def test_to_xml_with_empty
  233. xml = [].to_xml
  234. assert_match(/type="array"\/>/, xml)
  235. end
  236. def test_to_xml_dups_options
  237. options = {:skip_instruct => true}
  238. [].to_xml(options)
  239. # :builder, etc, shouldn't be added to options
  240. assert_equal({:skip_instruct => true}, options)
  241. end
  242. end
  243. class ArrayExtractOptionsTests < Test::Unit::TestCase
  244. class HashSubclass < Hash
  245. end
  246. class ExtractableHashSubclass < Hash
  247. def extractable_options?
  248. true
  249. end
  250. end
  251. def test_extract_options
  252. assert_equal({}, [].extract_options!)
  253. assert_equal({}, [1].extract_options!)
  254. assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
  255. assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
  256. end
  257. def test_extract_options_doesnt_extract_hash_subclasses
  258. hash = HashSubclass.new
  259. hash[:foo] = 1
  260. array = [hash]
  261. options = array.extract_options!
  262. assert_equal({}, options)
  263. assert_equal [hash], array
  264. end
  265. def test_extract_options_extracts_extractable_subclass
  266. hash = ExtractableHashSubclass.new
  267. hash[:foo] = 1
  268. array = [hash]
  269. options = array.extract_options!
  270. assert_equal({:foo => 1}, options)
  271. assert_equal [], array
  272. end
  273. def test_extract_options_extracts_hwia
  274. hash = [{:foo => 1}.with_indifferent_access]
  275. options = hash.extract_options!
  276. assert_equal 1, options[:foo]
  277. end
  278. end
  279. class ArrayUniqByTests < Test::Unit::TestCase
  280. def test_uniq_by
  281. assert_equal [1,2], [1,2,3,4].uniq_by { |i| i.odd? }
  282. assert_equal [1,2], [1,2,3,4].uniq_by(&:even?)
  283. assert_equal((-5..0).to_a, (-5..5).to_a.uniq_by{ |i| i**2 })
  284. end
  285. def test_uniq_by!
  286. a = [1,2,3,4]
  287. a.uniq_by! { |i| i.odd? }
  288. assert_equal [1,2], a
  289. a = [1,2,3,4]
  290. a.uniq_by! { |i| i.even? }
  291. assert_equal [1,2], a
  292. a = (-5..5).to_a
  293. a.uniq_by! { |i| i**2 }
  294. assert_equal((-5..0).to_a, a)
  295. end
  296. end
  297. class ArrayExtRandomTests < ActiveSupport::TestCase
  298. def test_sample_from_array
  299. assert_nil [].sample
  300. assert_equal [], [].sample(5)
  301. assert_equal 42, [42].sample
  302. assert_equal [42], [42].sample(5)
  303. a = [:foo, :bar, 42]
  304. s = a.sample(2)
  305. assert_equal 2, s.size
  306. assert_equal 1, (a-s).size
  307. assert_equal [], a-(0..20).sum{a.sample(2)}
  308. o = Object.new
  309. def o.to_int; 1; end
  310. assert_equal [0], [0].sample(o)
  311. o = Object.new
  312. assert_raises(TypeError) { [0].sample(o) }
  313. o = Object.new
  314. def o.to_int; ''; end
  315. assert_raises(TypeError) { [0].sample(o) }
  316. assert_raises(ArgumentError) { [0].sample(-7) }
  317. end
  318. end
  319. class ArrayWrapperTests < Test::Unit::TestCase
  320. class FakeCollection
  321. def to_ary
  322. ["foo", "bar"]
  323. end
  324. end
  325. class Proxy
  326. def initialize(target) @target = target end
  327. def method_missing(*a) @target.send(*a) end
  328. end
  329. class DoubtfulToAry
  330. def to_ary
  331. :not_an_array
  332. end
  333. end
  334. class NilToAry
  335. def to_ary
  336. nil
  337. end
  338. end
  339. def test_array
  340. ary = %w(foo bar)
  341. assert_same ary, Array.wrap(ary)
  342. end
  343. def test_nil
  344. assert_equal [], Array.wrap(nil)
  345. end
  346. def test_object
  347. o = Object.new
  348. assert_equal [o], Array.wrap(o)
  349. end
  350. def test_string
  351. assert_equal ["foo"], Array.wrap("foo")
  352. end
  353. def test_string_with_newline
  354. assert_equal ["foo\nbar"], Array.wrap("foo\nbar")
  355. end
  356. def test_object_with_to_ary
  357. assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new)
  358. end
  359. def test_proxy_object
  360. p = Proxy.new(Object.new)
  361. assert_equal [p], Array.wrap(p)
  362. end
  363. def test_proxy_to_object_with_to_ary
  364. p = Proxy.new(FakeCollection.new)
  365. assert_equal [p], Array.wrap(p)
  366. end
  367. def test_struct
  368. o = Struct.new(:foo).new(123)
  369. assert_equal [o], Array.wrap(o)
  370. end
  371. def test_wrap_returns_wrapped_if_to_ary_returns_nil
  372. o = NilToAry.new
  373. assert_equal [o], Array.wrap(o)
  374. end
  375. def test_wrap_does_not_complain_if_to_ary_does_not_return_an_array
  376. assert_equal DoubtfulToAry.new.to_ary, Array.wrap(DoubtfulToAry.new)
  377. end
  378. end
  379. class ArrayPrependAppendTest < Test::Unit::TestCase
  380. def test_append
  381. assert_equal [1, 2], [1].append(2)
  382. end
  383. def test_prepend
  384. assert_equal [2, 1], [1].prepend(2)
  385. end
  386. end