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] # @return [String]
# #
def inspect def inspect
return "Attribute(name: #{name.inspect} " \ segments = []
"namespace: #{namespace.inspect} value: #{value.inspect})"
[: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
end # Attribute end # Attribute
end # XML end # XML

View File

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

View File

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

View File

@ -114,11 +114,19 @@ module Oga
child_lines = children.map { |child| child.inspect(indent + 4) } child_lines = children.map { |child| child.inspect(indent + 4) }
.join("\n") .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 return <<-EOF.chomp
#{spacing} name: #{name.inspect} #{spacing} #{segments.join("\n#{spacing} ")}
#{spacing} namespace: #{namespace.inspect}
#{spacing} attributes: #{attributes.inspect}
#{spacing} children: [ #{spacing} children: [
#{child_lines} #{child_lines}
#{spacing}] #{spacing}]

View File

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

View File

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

View File

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

View File

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

View File

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