Trimmed XML inspect values.

This commit is contained in:
Yorick Peterse 2014-08-05 23:57:12 +02:00
parent 26d4bdc5b1
commit d7df908649
9 changed files with 57 additions and 74 deletions

View File

@ -42,8 +42,17 @@ module Oga
# @return [String]
#
def inspect
return "Attribute(name: #{name.inspect} " \
"namespace: #{namespace.inspect} value: #{value.inspect})"
segments = []
[:name, :namespace, :value].each do |attr|
value = send(attr)
if value and !value.empty?
segments << "#{attr}: #{value.inspect}"
end
end
return "Attribute(#{segments.join(' ')})"
end
end # Attribute
end # XML

View File

@ -64,22 +64,20 @@ module Oga
##
# Inspects the doctype.
#
# @param [Fixnum] indent The indentation level for each line.
# @return [String]
#
def inspect(indent = 0)
class_name = self.class.to_s.split('::').last
spacing = ' ' * indent
def inspect
segments = []
return <<-EOF.strip
#{class_name}(
#{spacing} name: #{name.inspect}
#{spacing} type: #{type.inspect}
#{spacing} public_id: #{public_id.inspect}
#{spacing} system_id: #{system_id.inspect}
#{spacing} inline_rules: #{inline_rules.inspect}
#{spacing})
EOF
[:name, :type, :public_id, :system_id, :inline_rules].each do |attr|
value = send(attr)
if value and !value.empty?
segments << "#{attr}: #{value.inspect}"
end
end
return "Doctype(#{segments.join(' ')})"
end
##

View File

@ -115,29 +115,20 @@ module Oga
# @return [String]
#
def inspect
class_name = self.class.to_s.split('::').last
segments = []
child_lines = children.map { |child| child.inspect(4) }.join("\n")
if doctype
dtd = doctype.inspect(2)
else
dtd = doctype.inspect
[:doctype, :xml_declaration].each do |attr|
value = send(attr)
if value
segments << "#{attr}: #{value.inspect}"
end
end
if xml_declaration
decl = xml_declaration.inspect(2)
else
decl = xml_declaration.inspect
end
segments << "children: [\n#{child_lines}\n]"
return <<-EOF.strip
#{class_name}(
doctype: #{dtd}
xml_declaration: #{decl}
children: [
#{child_lines}
])
EOF
return "Document(\n #{segments.join("\n ")})"
end
end # Document
end # XML

View File

@ -114,11 +114,19 @@ module Oga
child_lines = children.map { |child| child.inspect(indent + 4) }
.join("\n")
segments = []
[:name, :namespace, :attributes].each do |attr|
value = send(attr)
if value and !value.empty?
segments << "#{attr}: #{value.inspect}"
end
end
return <<-EOF.chomp
#{spacing} name: #{name.inspect}
#{spacing} namespace: #{namespace.inspect}
#{spacing} attributes: #{attributes.inspect}
#{spacing} #{segments.join("\n#{spacing} ")}
#{spacing} children: [
#{child_lines}
#{spacing}]

View File

@ -49,20 +49,20 @@ module Oga
end
##
# @param [Fixnum] indent
# @return [String]
#
def inspect(indent = 0)
class_name = self.class.to_s.split('::').last
spacing = ' ' * indent
def inspect
segments = []
return <<-EOF.strip
#{class_name}(
#{spacing} version: #{version.inspect}
#{spacing} encoding: #{encoding.inspect}
#{spacing} standalone: #{standalone.inspect}
#{spacing})
EOF
[:version, :encoding, :standalone].each do |attr|
value = send(attr)
if value and !value.empty?
segments << "#{attr}: #{value.inspect}"
end
end
return "XmlDeclaration(#{segments.join(' ')})"
end
##

View File

@ -67,13 +67,7 @@ describe Oga::XML::Doctype do
example 'pretty-print the node' do
@instance.inspect.should == <<-EOF.strip
Doctype(
name: "html"
type: "PUBLIC"
public_id: nil
system_id: nil
inline_rules: "<!ELEMENT foo>"
)
Doctype(name: "html" type: "PUBLIC" inline_rules: "<!ELEMENT foo>")
EOF
end
end

View File

@ -146,18 +146,8 @@ describe Oga::XML::Document do
example 'pretty-print the node' do
@instance.inspect.should == <<-EOF.strip
Document(
doctype: Doctype(
name: "html"
type: nil
public_id: nil
system_id: nil
inline_rules: nil
)
xml_declaration: XmlDeclaration(
version: "1.0"
encoding: "UTF-8"
standalone: nil
)
doctype: Doctype(name: "html")
xml_declaration: XmlDeclaration(version: "1.0" encoding: "UTF-8")
children: [
Comment(text: "foo")
])
@ -167,8 +157,6 @@ Document(
example 'pretty-print a document without a doctype and XML declaration' do
described_class.new.inspect.should == <<-EOF.strip
Document(
doctype: nil
xml_declaration: nil
children: [
])

View File

@ -141,7 +141,6 @@ describe Oga::XML::Element do
@instance.inspect.should == <<-EOF.strip
Element(
name: "p"
namespace: nil
attributes: {"class"=>"foo"}
children: [
Comment(text: "foo")

View File

@ -50,11 +50,7 @@ describe Oga::XML::XmlDeclaration do
example 'pretty-print the node' do
@instance.inspect.should == <<-EOF.strip
XmlDeclaration(
version: "1.0"
encoding: "UTF-8"
standalone: nil
)
XmlDeclaration(version: "1.0" encoding: "UTF-8")
EOF
end
end