Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP: get arrays and nil working correctly

  • Loading branch information...
commit 511060f7d441ac448066d57c4c162c0605d2cb2c 1 parent ecfb1f9
@joshuaclayton joshuaclayton authored
View
24 lib/hoptoad_notifier/notice.rb
@@ -240,12 +240,12 @@ def clean_unserializable_data(data, stack = [])
data.to_hash.inject({}) do |result, (key, value)|
result.merge(key => clean_unserializable_data(value, stack + [data.object_id]))
end
- elsif data.respond_to?(:to_ary)
- data.collect do |value|
- clean_unserializable_data(value, stack + [data.object_id])
- end
else
- data.to_s
+ if serializable?(data)
+ data
+ else
+ data.to_s
+ end
end
end
@@ -310,11 +310,23 @@ def xml_vars_for(builder, hash)
if value.respond_to?(:to_hash)
builder.var(:key => key){|b| xml_vars_for(b, value.to_hash) }
else
- builder.var(value.to_s, :key => key)
+ builder.var(xml_value_for(value), :key => key)
end
end
end
+ def xml_value_for(value)
+ if serializable?(value)
+ value.inspect
+ else
+ value.to_s
+ end
+ end
+
+ def serializable?(value)
+ value.nil? || value.is_a?(Array)
+ end
+
def rack_env(method)
rack_request.send(method) if rack_request
end
View
12 test/catcher_test.rb
@@ -37,16 +37,14 @@ def assert_sent_hash(hash, xpath)
end
def assert_sent_element(value, xpath)
- assert_valid_node last_sent_notice_document, xpath, stringify_array_elements(value).to_s
+ assert_valid_node last_sent_notice_document, xpath, xml_value_for(value)
end
- def stringify_array_elements(data)
- if data.respond_to?(:to_ary)
- data.collect do |value|
- stringify_array_elements(value)
- end
+ def xml_value_for(value)
+ if value.nil? || value.is_a?(Array)
+ value.inspect
else
- data.to_s
+ value.to_s
end
end
View
2  test/helper.rb
@@ -174,7 +174,7 @@ def assert_valid_node(document, xpath, content)
nodes = document.xpath(xpath)
assert nodes.any?{|node| node.content == content },
"Expected xpath #{xpath} to have content #{content}, " +
- "but found #{nodes.map { |n| n.content }} in #{nodes.size} matching nodes." +
+ "but found #{nodes.map { |n| n.content }} in #{nodes.size} matching nodes.\n" +
"Document:\n#{document.to_s}"
end
end
View
13 test/notice_test.rb
@@ -200,6 +200,10 @@ def stub_request(attrs = {})
:action => "action",
:url => "http://url.com",
:parameters => { "paramskey" => "paramsvalue",
+ "to_s_differences" => {
+ "nil_value_in_array" => [nil],
+ "array_of_strings" => %w(one two three four five)
+ },
"nestparentkey" => { "nestkey" => "nestvalue" } },
:session_data => { "sessionkey" => "sessionvalue" },
:cgi_data => { "cgikey" => "cgivalue" },
@@ -239,6 +243,13 @@ def stub_request(attrs = {})
assert_valid_node(@document, "//request/params/var/@key", "nestparentkey")
assert_valid_node(@document, "//request/params/var/var/@key", "nestkey")
assert_valid_node(@document, "//request/params/var/var", "nestvalue")
+
+ assert_valid_node(@document, "//request/params/var/@key", "to_s_differences")
+ assert_valid_node(@document, "//request/params/var/var/@key", "nil_value_in_array")
+ assert_valid_node(@document, "//request/params/var/var", "[nil]")
+ assert_valid_node(@document, "//request/params/var/var/@key", "array_of_strings")
+ assert_valid_node(@document, "//request/params/var/var", %{["one", "two", "three", "four", "five"]})
+
assert_valid_node(@document, "//request/session/var/@key", "sessionkey")
assert_valid_node(@document, "//request/session/var", "sessionvalue")
assert_valid_node(@document, "//request/cgi-data/var/@key", "cgikey")
@@ -422,7 +433,7 @@ def assert_serializes_hash(attribute)
assert_kind_of Hash, hash[:sub_hash], "subhashes should be kept"
assert_equal object.to_s, hash[:sub_hash][:sub_object], "subhash members should be serialized"
assert_kind_of Array, hash[:array], "arrays should be kept"
- assert_equal object.to_s, hash[:array].first, "array members should be serialized"
+ assert_equal object, hash[:array].first, "array members should not be serialized"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.