PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/array_ext_test.rb

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