/activesupport/test/core_ext/array/conversions_test.rb

https://github.com/rnaveiras/rails · Ruby · 240 lines · 193 code · 45 blank · 2 comment · 0 complexity · 10a8e85d9f59b4beab61e75e7362ba08 MD5 · raw file

  1. # frozen_string_literal: true
  2. require_relative "../../abstract_unit"
  3. require "active_support/core_ext/array"
  4. require "active_support/core_ext/big_decimal"
  5. require "active_support/core_ext/hash"
  6. require "active_support/core_ext/string"
  7. class ToSentenceTest < ActiveSupport::TestCase
  8. def test_plain_array_to_sentence
  9. assert_equal "", [].to_sentence
  10. assert_equal "one", ["one"].to_sentence
  11. assert_equal "one and two", ["one", "two"].to_sentence
  12. assert_equal "one, two, and three", ["one", "two", "three"].to_sentence
  13. end
  14. def test_to_sentence_with_words_connector
  15. assert_equal "one two, and three", ["one", "two", "three"].to_sentence(words_connector: " ")
  16. assert_equal "one & two, and three", ["one", "two", "three"].to_sentence(words_connector: " & ")
  17. assert_equal "onetwo, and three", ["one", "two", "three"].to_sentence(words_connector: nil)
  18. end
  19. def test_to_sentence_with_last_word_connector
  20. assert_equal "one, two, and also three", ["one", "two", "three"].to_sentence(last_word_connector: ", and also ")
  21. assert_equal "one, twothree", ["one", "two", "three"].to_sentence(last_word_connector: nil)
  22. assert_equal "one, two three", ["one", "two", "three"].to_sentence(last_word_connector: " ")
  23. assert_equal "one, two and three", ["one", "two", "three"].to_sentence(last_word_connector: " and ")
  24. end
  25. def test_two_elements
  26. assert_equal "one and two", ["one", "two"].to_sentence
  27. assert_equal "one two", ["one", "two"].to_sentence(two_words_connector: " ")
  28. end
  29. def test_one_element
  30. assert_equal "one", ["one"].to_sentence
  31. end
  32. def test_one_element_not_same_object
  33. elements = ["one"]
  34. assert_not_equal elements[0].object_id, elements.to_sentence.object_id
  35. end
  36. def test_one_non_string_element
  37. assert_equal "1", [1].to_sentence
  38. end
  39. def test_does_not_modify_given_hash
  40. options = { words_connector: " " }
  41. assert_equal "one two, and three", ["one", "two", "three"].to_sentence(options)
  42. assert_equal({ words_connector: " " }, options)
  43. end
  44. def test_with_blank_elements
  45. assert_equal ", one, , two, and three", [nil, "one", "", "two", "three"].to_sentence
  46. end
  47. def test_with_invalid_options
  48. exception = assert_raise ArgumentError do
  49. ["one", "two"].to_sentence(passing: "invalid option")
  50. end
  51. assert_equal "Unknown key: :passing. Valid keys are: :words_connector, :two_words_connector, :last_word_connector, :locale", exception.message
  52. end
  53. def test_always_returns_string
  54. assert_instance_of String, [ActiveSupport::SafeBuffer.new("one")].to_sentence
  55. assert_instance_of String, [ActiveSupport::SafeBuffer.new("one"), "two"].to_sentence
  56. assert_instance_of String, [ActiveSupport::SafeBuffer.new("one"), "two", "three"].to_sentence
  57. end
  58. def test_returns_no_frozen_string
  59. assert_not [].to_sentence.frozen?
  60. assert_not ["one"].to_sentence.frozen?
  61. assert_not ["one", "two"].to_sentence.frozen?
  62. assert_not ["one", "two", "three"].to_sentence.frozen?
  63. end
  64. end
  65. class ToSTest < ActiveSupport::TestCase
  66. class TestDB
  67. def self.reset
  68. @@counter = 0
  69. end
  70. reset
  71. def id
  72. @@counter += 1
  73. end
  74. end
  75. setup do
  76. TestDB.reset
  77. end
  78. def test_to_s_db
  79. collection = [TestDB.new, TestDB.new, TestDB.new]
  80. assert_deprecated do
  81. assert_equal "null", [].to_s(:db)
  82. end
  83. assert_deprecated do
  84. assert_equal "1,2,3", collection.to_s(:db)
  85. end
  86. end
  87. def test_to_s_not_existent
  88. assert_deprecated do
  89. assert_equal "[]", [].to_s(:not_existent)
  90. end
  91. end
  92. def test_to_fs_db
  93. collection = [TestDB.new, TestDB.new, TestDB.new]
  94. assert_equal "null", [].to_fs(:db)
  95. assert_equal "1,2,3", collection.to_fs(:db)
  96. assert_equal "null", [].to_formatted_s(:db)
  97. assert_equal "4,5,6", collection.to_formatted_s(:db)
  98. end
  99. end
  100. class ToXmlTest < ActiveSupport::TestCase
  101. def test_to_xml_with_hash_elements
  102. xml = [
  103. { name: "David", age: 26, age_in_millis: 820497600000 },
  104. { name: "Jason", age: 31, age_in_millis: BigDecimal("1.0") }
  105. ].to_xml(skip_instruct: true, indent: 0)
  106. assert_equal '<objects type="array"><object>', xml.first(30)
  107. assert_includes xml, %(<age type="integer">26</age>), xml
  108. assert_includes xml, %(<age-in-millis type="integer">820497600000</age-in-millis>), xml
  109. assert_includes xml, %(<name>David</name>), xml
  110. assert_includes xml, %(<age type="integer">31</age>), xml
  111. assert_includes xml, %(<age-in-millis type="decimal">1.0</age-in-millis>), xml
  112. assert_includes xml, %(<name>Jason</name>), xml
  113. end
  114. def test_to_xml_with_non_hash_elements
  115. xml = %w[1 2 3].to_xml(skip_instruct: true, indent: 0)
  116. assert_equal '<strings type="array"><string', xml.first(29)
  117. assert_includes xml, %(<string>2</string>), xml
  118. end
  119. def test_to_xml_with_non_hash_different_type_elements
  120. xml = [1, 2.0, "3"].to_xml(skip_instruct: true, indent: 0)
  121. assert_equal '<objects type="array"><object', xml.first(29)
  122. assert_includes xml, %(<object type="integer">1</object>), xml
  123. assert_includes xml, %(<object type="float">2.0</object>), xml
  124. assert_includes xml, %(object>3</object>), xml
  125. end
  126. def test_to_xml_with_dedicated_name
  127. xml = [
  128. { name: "David", age: 26, age_in_millis: 820497600000 }, { name: "Jason", age: 31 }
  129. ].to_xml(skip_instruct: true, indent: 0, root: "people")
  130. assert_equal '<people type="array"><person>', xml.first(29)
  131. end
  132. def test_to_xml_with_options
  133. xml = [
  134. { name: "David", street_address: "Paulina" }, { name: "Jason", street_address: "Evergreen" }
  135. ].to_xml(skip_instruct: true, skip_types: true, indent: 0)
  136. assert_equal "<objects><object>", xml.first(17)
  137. assert_includes xml, %(<street-address>Paulina</street-address>)
  138. assert_includes xml, %(<name>David</name>)
  139. assert_includes xml, %(<street-address>Evergreen</street-address>)
  140. assert_includes xml, %(<name>Jason</name>)
  141. end
  142. def test_to_xml_with_indent_set
  143. xml = [
  144. { name: "David", street_address: "Paulina" }, { name: "Jason", street_address: "Evergreen" }
  145. ].to_xml(skip_instruct: true, skip_types: true, indent: 4)
  146. assert_equal "<objects>\n <object>", xml.first(22)
  147. assert_includes xml, %(\n <street-address>Paulina</street-address>)
  148. assert_includes xml, %(\n <name>David</name>)
  149. assert_includes xml, %(\n <street-address>Evergreen</street-address>)
  150. assert_includes xml, %(\n <name>Jason</name>)
  151. end
  152. def test_to_xml_with_dasherize_false
  153. xml = [
  154. { name: "David", street_address: "Paulina" }, { name: "Jason", street_address: "Evergreen" }
  155. ].to_xml(skip_instruct: true, skip_types: true, indent: 0, dasherize: false)
  156. assert_equal "<objects><object>", xml.first(17)
  157. assert_includes xml, %(<street_address>Paulina</street_address>)
  158. assert_includes xml, %(<street_address>Evergreen</street_address>)
  159. end
  160. def test_to_xml_with_dasherize_true
  161. xml = [
  162. { name: "David", street_address: "Paulina" }, { name: "Jason", street_address: "Evergreen" }
  163. ].to_xml(skip_instruct: true, skip_types: true, indent: 0, dasherize: true)
  164. assert_equal "<objects><object>", xml.first(17)
  165. assert_includes xml, %(<street-address>Paulina</street-address>)
  166. assert_includes xml, %(<street-address>Evergreen</street-address>)
  167. end
  168. def test_to_xml_with_instruct
  169. xml = [
  170. { name: "David", age: 26, age_in_millis: 820497600000 },
  171. { name: "Jason", age: 31, age_in_millis: BigDecimal("1.0") }
  172. ].to_xml(skip_instruct: false, indent: 0)
  173. assert_match(/^<\?xml [^>]*/, xml)
  174. assert_equal 0, xml.rindex(/<\?xml /)
  175. end
  176. def test_to_xml_with_block
  177. xml = [
  178. { name: "David", age: 26, age_in_millis: 820497600000 },
  179. { name: "Jason", age: 31, age_in_millis: BigDecimal("1.0") }
  180. ].to_xml(skip_instruct: true, indent: 0) do |builder|
  181. builder.count 2
  182. end
  183. assert_includes xml, %(<count>2</count>), xml
  184. end
  185. def test_to_xml_with_empty
  186. xml = [].to_xml
  187. assert_match(/type="array"\/>/, xml)
  188. end
  189. def test_to_xml_dups_options
  190. options = { skip_instruct: true }
  191. [].to_xml(options)
  192. # :builder, etc, shouldn't be added to options
  193. assert_equal({ skip_instruct: true }, options)
  194. end
  195. end