PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/bricooke/my-biz-expenses
Ruby | 182 lines | 146 code | 36 blank | 0 comment | 5 complexity | 3a521f34ed523a34d96e4061426c60d0 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. require 'bigdecimal'
  3. class ArrayExtToParamTests < Test::Unit::TestCase
  4. def test_string_array
  5. assert_equal '', %w().to_param
  6. assert_equal 'hello/world', %w(hello world).to_param
  7. assert_equal 'hello/10', %w(hello 10).to_param
  8. end
  9. def test_number_array
  10. assert_equal '10/20', [10, 20].to_param
  11. end
  12. end
  13. class ArrayExtToSentenceTests < Test::Unit::TestCase
  14. def test_plain_array_to_sentence
  15. assert_equal "", [].to_sentence
  16. assert_equal "one", ['one'].to_sentence
  17. assert_equal "one and two", ['one', 'two'].to_sentence
  18. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
  19. end
  20. def test_to_sentence_with_connector
  21. assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:connector => 'and also')
  22. end
  23. def test_to_sentence_with_skip_last_comma
  24. assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false)
  25. end
  26. def test_two_elements
  27. assert_equal "one and two", ['one', 'two'].to_sentence
  28. end
  29. def test_one_element
  30. assert_equal "one", ['one'].to_sentence
  31. end
  32. end
  33. class ArrayExtToSTests < Test::Unit::TestCase
  34. def test_to_s_db
  35. collection = [
  36. Class.new { def id() 1 end }.new,
  37. Class.new { def id() 2 end }.new,
  38. Class.new { def id() 3 end }.new
  39. ]
  40. assert_equal "null", [].to_s(:db)
  41. assert_equal "1,2,3", collection.to_s(:db)
  42. end
  43. end
  44. class ArrayExtGroupingTests < Test::Unit::TestCase
  45. def test_group_by_with_perfect_fit
  46. groups = []
  47. ('a'..'i').to_a.in_groups_of(3) do |group|
  48. groups << group
  49. end
  50. assert_equal [%w(a b c), %w(d e f), %w(g h i)], groups
  51. assert_equal [%w(a b c), %w(d e f), %w(g h i)], ('a'..'i').to_a.in_groups_of(3)
  52. end
  53. def test_group_by_with_padding
  54. groups = []
  55. ('a'..'g').to_a.in_groups_of(3) do |group|
  56. groups << group
  57. end
  58. assert_equal [%w(a b c), %w(d e f), ['g', nil, nil]], groups
  59. end
  60. def test_group_by_pads_with_specified_values
  61. groups = []
  62. ('a'..'g').to_a.in_groups_of(3, 'foo') do |group|
  63. groups << group
  64. end
  65. assert_equal [%w(a b c), %w(d e f), ['g', 'foo', 'foo']], groups
  66. end
  67. def test_group_without_padding
  68. groups = []
  69. ('a'..'g').to_a.in_groups_of(3, false) do |group|
  70. groups << group
  71. end
  72. assert_equal [%w(a b c), %w(d e f), ['g']], groups
  73. end
  74. end
  75. class ArraySplitTests < Test::Unit::TestCase
  76. def test_split_with_empty_array
  77. assert_equal [[]], [].split(0)
  78. end
  79. def test_split_with_argument
  80. assert_equal [[1, 2], [4, 5]], [1, 2, 3, 4, 5].split(3)
  81. assert_equal [[1, 2, 3, 4, 5]], [1, 2, 3, 4, 5].split(0)
  82. end
  83. def test_split_with_block
  84. assert_equal [[1, 2], [4, 5], [7, 8], [10]], (1..10).to_a.split { |i| i % 3 == 0 }
  85. end
  86. def test_split_with_edge_values
  87. assert_equal [[], [2, 3, 4, 5]], [1, 2, 3, 4, 5].split(1)
  88. assert_equal [[1, 2, 3, 4], []], [1, 2, 3, 4, 5].split(5)
  89. assert_equal [[], [2, 3, 4], []], [1, 2, 3, 4, 5].split { |i| i == 1 || i == 5 }
  90. end
  91. end
  92. class ArrayToXmlTests < Test::Unit::TestCase
  93. def test_to_xml
  94. xml = [
  95. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  96. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  97. ].to_xml(:skip_instruct => true, :indent => 0)
  98. assert_equal "<records><record>", xml.first(17), xml
  99. assert xml.include?(%(<age type="integer">26</age>)), xml
  100. assert xml.include?(%(<age-in-millis type="integer">820497600000</age-in-millis>)), xml
  101. assert xml.include?(%(<name>David</name>)), xml
  102. assert xml.include?(%(<age type="integer">31</age>)), xml
  103. assert xml.include?(%(<age-in-millis type="numeric">1.0</age-in-millis>)), xml
  104. assert xml.include?(%(<name>Jason</name>)), xml
  105. end
  106. def test_to_xml_with_dedicated_name
  107. xml = [
  108. { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31 }
  109. ].to_xml(:skip_instruct => true, :indent => 0, :root => "people")
  110. assert_equal "<people><person>", xml.first(16)
  111. end
  112. def test_to_xml_with_options
  113. xml = [
  114. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  115. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0)
  116. assert_equal "<records><record>", xml.first(17)
  117. assert xml.include?(%(<street-address>Paulina</street-address>))
  118. assert xml.include?(%(<name>David</name>))
  119. assert xml.include?(%(<street-address>Evergreen</street-address>))
  120. assert xml.include?(%(<name>Jason</name>))
  121. end
  122. def test_to_xml_with_dasherize_false
  123. xml = [
  124. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  125. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => false)
  126. assert_equal "<records><record>", xml.first(17)
  127. assert xml.include?(%(<street_address>Paulina</street_address>))
  128. assert xml.include?(%(<street_address>Evergreen</street_address>))
  129. end
  130. def test_to_xml_with_dasherize_true
  131. xml = [
  132. { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" }
  133. ].to_xml(:skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true)
  134. assert_equal "<records><record>", xml.first(17)
  135. assert xml.include?(%(<street-address>Paulina</street-address>))
  136. assert xml.include?(%(<street-address>Evergreen</street-address>))
  137. end
  138. def test_to_with_instruct
  139. xml = [
  140. { :name => "David", :age => 26, :age_in_millis => 820497600000 },
  141. { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') }
  142. ].to_xml(:skip_instruct => false, :indent => 0)
  143. assert(/^<\?xml [^>]*/.match(xml))
  144. assert xml.rindex(/<\?xml /) == 0
  145. end
  146. end