2010-07-17 17:45:19 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'active_support/xml_mini'
|
2011-01-05 15:34:37 -05:00
|
|
|
require 'active_support/builder'
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
module XmlMiniTest
|
2012-01-05 20:12:46 -05:00
|
|
|
class RenameKeyTest < ActiveSupport::TestCase
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_dasherizes_by_default
|
|
|
|
assert_equal "my-key", ActiveSupport::XmlMini.rename_key("my_key")
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_does_nothing_with_dasherize_true
|
|
|
|
assert_equal "my-key", ActiveSupport::XmlMini.rename_key("my_key", :dasherize => true)
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_does_nothing_with_dasherize_false
|
|
|
|
assert_equal "my_key", ActiveSupport::XmlMini.rename_key("my_key", :dasherize => false)
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_camelizes_with_camelize_true
|
|
|
|
assert_equal "MyKey", ActiveSupport::XmlMini.rename_key("my_key", :camelize => true)
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_lower_camelizes_with_camelize_lower
|
|
|
|
assert_equal "myKey", ActiveSupport::XmlMini.rename_key("my_key", :camelize => :lower)
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_lower_camelizes_with_camelize_upper
|
|
|
|
assert_equal "MyKey", ActiveSupport::XmlMini.rename_key("my_key", :camelize => :upper)
|
|
|
|
end
|
2010-11-03 14:02:42 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_does_not_dasherize_leading_underscores
|
|
|
|
assert_equal "_id", ActiveSupport::XmlMini.rename_key("_id")
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_with_leading_underscore_dasherizes_interior_underscores
|
|
|
|
assert_equal "_my-key", ActiveSupport::XmlMini.rename_key("_my_key")
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_does_not_dasherize_trailing_underscores
|
|
|
|
assert_equal "id_", ActiveSupport::XmlMini.rename_key("id_")
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_with_trailing_underscore_dasherizes_interior_underscores
|
|
|
|
assert_equal "my-key_", ActiveSupport::XmlMini.rename_key("my_key_")
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
def test_rename_key_does_not_dasherize_multiple_leading_underscores
|
|
|
|
assert_equal "__id", ActiveSupport::XmlMini.rename_key("__id")
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
|
2011-05-07 19:02:42 -04:00
|
|
|
def test_rename_key_does_not_dasherize_multiple_trailing_underscores
|
2011-01-05 15:34:37 -05:00
|
|
|
assert_equal "id__", ActiveSupport::XmlMini.rename_key("id__")
|
2011-07-26 13:58:16 -04:00
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
end
|
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
class ToTagTest < ActiveSupport::TestCase
|
|
|
|
def assert_xml(xml)
|
|
|
|
assert_equal xml, @options[:builder].target!
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-07-26 14:02:41 -04:00
|
|
|
def setup
|
2011-01-05 15:34:37 -05:00
|
|
|
@xml = ActiveSupport::XmlMini
|
|
|
|
@options = {:skip_instruct => true, :builder => Builder::XmlMarkup.new}
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
test "#to_tag accepts a callable object and passes options with the builder" do
|
|
|
|
@xml.to_tag(:some_tag, lambda {|o| o[:builder].br }, @options)
|
|
|
|
assert_xml "<br/>"
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
test "#to_tag accepts a callable object and passes options and tag name" do
|
|
|
|
@xml.to_tag(:tag, lambda {|o, t| o[:builder].b(t) }, @options)
|
|
|
|
assert_xml "<b>tag</b>"
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
test "#to_tag accepts an object responding to #to_xml and passes the options, where :root is key" do
|
|
|
|
obj = Object.new
|
|
|
|
obj.instance_eval do
|
|
|
|
def to_xml(options) options[:builder].yo(options[:root].to_s) end
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
@xml.to_tag(:tag, obj, @options)
|
|
|
|
assert_xml "<yo>tag</yo>"
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-01-05 15:34:37 -05:00
|
|
|
test "#to_tag accepts arbitrary objects responding to #to_str" do
|
|
|
|
@xml.to_tag(:b, "Howdy", @options)
|
|
|
|
assert_xml "<b>Howdy</b>"
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-05-08 08:13:18 -04:00
|
|
|
test "#to_tag should dasherize the space when passed a string with spaces as a key" do
|
|
|
|
@xml.to_tag("New York", 33, @options)
|
|
|
|
assert_xml "<New---York type=\"integer\">33</New---York>"
|
|
|
|
end
|
2011-07-26 13:58:16 -04:00
|
|
|
|
2011-05-08 08:13:18 -04:00
|
|
|
test "#to_tag should dasherize the space when passed a symbol with spaces as a key" do
|
|
|
|
@xml.to_tag(:"New York", 33, @options)
|
|
|
|
assert_xml "<New---York type=\"integer\">33</New---York>"
|
|
|
|
end
|
2011-01-05 15:34:37 -05:00
|
|
|
# TODO: test the remaining functions hidden in #to_tag.
|
|
|
|
end
|
2010-07-17 17:45:19 -04:00
|
|
|
end
|