PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/skdaksh/authlogic_example
Ruby | 356 lines | 285 code | 71 blank | 0 comment | 4 complexity | 48b2eaf88a208725ce3d848aa7c3b8ab 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..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_with_dedicated_name
  189. xml = [
  190. { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
  191. ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
  192. assert_equal '<people type="array"><person>', xml.first(29)
  193. end
  194. def test_to_xml_with_options
  195. xml = [
  196. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  197. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
  198. assert_equal "<records><record>", xml.first(17)
  199. assert xml.include?(%(<street-address>Paulina</street-address>))
  200. assert xml.include?(%(<name>David</name>))
  201. assert xml.include?(%(<street-address>Evergreen</street-address>))
  202. assert xml.include?(%(<name>Jason</name>))
  203. end
  204. def test_to_xml_with_dasherize_false
  205. xml = [
  206. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  207. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => false)
  208. assert_equal "<records><record>", xml.first(17)
  209. assert xml.include?(%(<street_address>Paulina</street_address>))
  210. assert xml.include?(%(<street_address>Evergreen</street_address>))
  211. end
  212. def test_to_xml_with_dasherize_true
  213. xml = [
  214. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  215. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true)
  216. assert_equal "<records><record>", xml.first(17)
  217. assert xml.include?(%(<street-address>Paulina</street-address>))
  218. assert xml.include?(%(<street-address>Evergreen</street-address>))
  219. end
  220. def test_to_with_instruct
  221. xml = [
  222. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  223. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  224. ].to_xml(:skip_instruct => false, :indent => 0)
  225. assert_match(/^<\?xml [^>]*/, xml)
  226. assert_equal 0, xml.rindex(/<\?xml /)
  227. end
  228. def test_to_xml_with_block
  229. xml = [
  230. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  231. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  232. ].to_xml(:skip_instruct => true, :indent => 0) do |builder|
  233. builder.count 2
  234. end
  235. assert xml.include?(%(<count>2</count>)), xml
  236. end
  237. def test_to_xml_with_empty
  238. xml = [].to_xml
  239. assert_match(/type="array"\/>/, xml)
  240. end
  241. end
  242. class ArrayExtractOptionsTests < Test::Unit::TestCase
  243. def test_extract_options
  244. assert_equal({}, [].extract_options!)
  245. assert_equal({}, [1].extract_options!)
  246. assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
  247. assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
  248. end
  249. end
  250. class ArrayExtRandomTests < Test::Unit::TestCase
  251. def test_random_element_from_array
  252. assert_nil [].rand
  253. Kernel.expects(:rand).with(1).returns(0)
  254. assert_equal 'x', ['x'].rand
  255. Kernel.expects(:rand).with(3).returns(1)
  256. assert_equal 2, [1, 2, 3].rand
  257. end
  258. end
  259. class ArrayWrapperTests < Test::Unit::TestCase
  260. class FakeCollection
  261. def to_ary
  262. ["foo", "bar"]
  263. end
  264. end
  265. def test_array
  266. ary = %w(foo bar)
  267. assert_same ary, Array.wrap(ary)
  268. end
  269. def test_nil
  270. assert_equal [], Array.wrap(nil)
  271. end
  272. def test_object
  273. o = Object.new
  274. assert_equal [o], Array.wrap(o)
  275. end
  276. def test_string
  277. assert_equal ["foo"], Array.wrap("foo")
  278. end
  279. def test_string_with_newline
  280. assert_equal ["foo\nbar"], Array.wrap("foo\nbar")
  281. end
  282. def test_object_with_to_ary
  283. assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new)
  284. end
  285. end