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

/activesupport/test/core_ext/array_ext_test.rb

https://github.com/zachinglis/docrails
Ruby | 250 lines | 202 code | 48 blank | 0 comment | 4 complexity | 8e0722a330ca49792b767fc4cc450710 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. end
  15. class ArrayExtToParamTests < Test::Unit::TestCase
  16. class ToParam < String
  17. def to_param
  18. "#{self}1"
  19. end
  20. end
  21. def test_string_array
  22. assert_equal '', %w().to_param
  23. assert_equal 'hello/world', %w(hello world).to_param
  24. assert_equal 'hello/10', %w(hello 10).to_param
  25. end
  26. def test_number_array
  27. assert_equal '10/20', [10, 20].to_param
  28. end
  29. def test_to_param_array
  30. assert_equal 'custom1/param1', [ToParam.new('custom'), ToParam.new('param')].to_param
  31. end
  32. end
  33. class ArrayExtToSentenceTests < Test::Unit::TestCase
  34. def test_plain_array_to_sentence
  35. assert_equal "", [].to_sentence
  36. assert_equal "one", ['one'].to_sentence
  37. assert_equal "one and two", ['one', 'two'].to_sentence
  38. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
  39. end
  40. def test_to_sentence_with_connector
  41. assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:connector => 'and also')
  42. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '')
  43. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => nil)
  44. assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => ' ')
  45. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ')
  46. end
  47. def test_to_sentence_with_skip_last_comma
  48. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
  49. end
  50. def test_two_elements
  51. assert_equal "one and two", ['one', 'two'].to_sentence
  52. assert_equal "one two", ['one', 'two'].to_sentence(:connector => '')
  53. end
  54. def test_one_element
  55. assert_equal "one", ['one'].to_sentence
  56. end
  57. def test_one_non_string_element
  58. assert_equal '1', [1].to_sentence
  59. end
  60. end
  61. class ArrayExtToSTests < Test::Unit::TestCase
  62. def test_to_s_db
  63. collection = [
  64. Class.new { def id() 1 end }.new,
  65. Class.new { def id() 2 end }.new,
  66. Class.new { def id() 3 end }.new
  67. ]
  68. assert_equal "null", [].to_s(:db)
  69. assert_equal "1,2,3", collection.to_s(:db)
  70. end
  71. end
  72. class ArrayExtGroupingTests < Test::Unit::TestCase
  73. def test_group_by_with_perfect_fit
  74. groups = []
  75. ('a'..'i').to_a.in_groups_of(3) do |group|
  76. groups << group
  77. end
  78. assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
  79. assert_equal [%w(a b c), %w(d e f), %w(g h i)], ('a'..'i').to_a.in_groups_of(3)
  80. end
  81. def test_group_by_with_padding
  82. groups = []
  83. ('a'..'g').to_a.in_groups_of(3) do |group|
  84. groups << group
  85. end
  86. assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups
  87. end
  88. def test_group_by_pads_with_specified_values
  89. groups = []
  90. ('a'..'g').to_a.in_groups_of(3, 'foo') do |group|
  91. groups << group
  92. end
  93. assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups
  94. end
  95. def test_group_without_padding
  96. groups = []
  97. ('a'..'g').to_a.in_groups_of(3, false) do |group|
  98. groups << group
  99. end
  100. assert_equal [%w(a b c), %w(d e f), ['g']], groups
  101. end
  102. end
  103. class ArraySplitTests < Test::Unit::TestCase
  104. def test_split_with_empty_array
  105. assert_equal [[]], [].split(0)
  106. end
  107. def test_split_with_argument
  108. assert_equal [[1, 2], [4, 5]], [1, 2, 3, 4, 5].split(3)
  109. assert_equal [[1, 2, 3, 4, 5]], [1, 2, 3, 4, 5].split(0)
  110. end
  111. def test_split_with_block
  112. assert_equal [[1, 2], [4, 5], [7, 8], [10]], (1..10).to_a.split { |i| i % 3 == 0 }
  113. end
  114. def test_split_with_edge_values
  115. assert_equal [[], [2, 3, 4, 5]], [1, 2, 3, 4, 5].split(1)
  116. assert_equal [[1, 2, 3, 4], []], [1, 2, 3, 4, 5].split(5)
  117. assert_equal [[], [2, 3, 4], []], [1, 2, 3, 4, 5].split { |i| i == 1 || i == 5 }
  118. end
  119. end
  120. class ArrayToXmlTests < Test::Unit::TestCase
  121. def test_to_xml
  122. xml = [
  123. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  124. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  125. ].to_xml(:skip_instruct => true, :indent => 0)
  126. assert_equal '<records type="array"><record>', xml.first(30)
  127. assert xml.include?(%(<age type="integer">26</age>)), xml
  128. assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml
  129. assert xml.include?(%(<name>David</name>)), xml
  130. assert xml.include?(%(<age type="integer">31</age>)), xml
  131. assert xml.include?(%(<age-in-millis type="decimal">1.0</age-in-millis>)), xml
  132. assert xml.include?(%(<name>Jason</name>)), xml
  133. end
  134. def test_to_xml_with_dedicated_name
  135. xml = [
  136. { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
  137. ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
  138. assert_equal '<people type="array"><person>', xml.first(29)
  139. end
  140. def test_to_xml_with_options
  141. xml = [
  142. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  143. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
  144. assert_equal "<records><record>", xml.first(17)
  145. assert xml.include?(%(<street-address>Paulina</street-address>))
  146. assert xml.include?(%(<name>David</name>))
  147. assert xml.include?(%(<street-address>Evergreen</street-address>))
  148. assert xml.include?(%(<name>Jason</name>))
  149. end
  150. def test_to_xml_with_dasherize_false
  151. xml = [
  152. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  153. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => false)
  154. assert_equal "<records><record>", xml.first(17)
  155. assert xml.include?(%(<street_address>Paulina</street_address>))
  156. assert xml.include?(%(<street_address>Evergreen</street_address>))
  157. end
  158. def test_to_xml_with_dasherize_true
  159. xml = [
  160. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  161. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true)
  162. assert_equal "<records><record>", xml.first(17)
  163. assert xml.include?(%(<street-address>Paulina</street-address>))
  164. assert xml.include?(%(<street-address>Evergreen</street-address>))
  165. end
  166. def test_to_with_instruct
  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 => false, :indent => 0)
  171. assert_match(/^<\?xml [^>]*/, xml)
  172. assert_equal 0, xml.rindex(/<\?xml /)
  173. end
  174. def test_to_xml_with_block
  175. xml = [
  176. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  177. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  178. ].to_xml(:skip_instruct => true, :indent => 0) do |builder|
  179. builder.count 2
  180. end
  181. assert xml.include?(%(<count>2</count>)), xml
  182. end
  183. end
  184. class ArrayExtractOptionsTests < Test::Unit::TestCase
  185. def test_extract_options
  186. assert_equal({}, [].extract_options!)
  187. assert_equal({}, [1].extract_options!)
  188. assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
  189. assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
  190. end
  191. end
  192. uses_mocha "ArrayExtRandomTests" do
  193. class ArrayExtRandomTests < Test::Unit::TestCase
  194. def test_random_element_from_array
  195. assert_nil [].rand
  196. Kernel.expects(:rand).with(1).returns(0)
  197. assert_equal 'x', ['x'].rand
  198. Kernel.expects(:rand).with(3).returns(1)
  199. assert_equal 2, [1, 2, 3].rand
  200. end
  201. end
  202. end