PageRenderTime 189ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/activesupport/test/core_ext/array_ext_test.rb

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