From 06ae1503d4b78a38ae3d84408b74608f11169e87 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 28 Jul 2015 19:01:11 +0200 Subject: [PATCH] nodes/attributes support in to_compatible_types This extends XPath::Conversion.to_compatible_types so that it can also take XML::Node and XML::Attribute objects as input. --- lib/oga/xpath/conversion.rb | 4 ++-- spec/oga/xpath/conversion_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/oga/xpath/conversion.rb b/lib/oga/xpath/conversion.rb index cd7477b..fd72f54 100644 --- a/lib/oga/xpath/conversion.rb +++ b/lib/oga/xpath/conversion.rb @@ -10,11 +10,11 @@ module Oga # @return [Array] # def self.to_compatible_types(left, right) - if left.is_a?(XML::NodeSet) + if left.is_a?(XML::NodeSet) or left.respond_to?(:text) left = to_string(left) end - if right.is_a?(XML::NodeSet) + if right.is_a?(XML::NodeSet) or right.respond_to?(:text) right = to_string(right) end diff --git a/spec/oga/xpath/conversion_spec.rb b/spec/oga/xpath/conversion_spec.rb index 66dfa5a..f69c260 100644 --- a/spec/oga/xpath/conversion_spec.rb +++ b/spec/oga/xpath/conversion_spec.rb @@ -12,6 +12,26 @@ describe Oga::XPath::Conversion do right.should == 'bar' end + it 'returns two Strings when using two Nodes' do + n1 = Oga::XML::Text.new(:text => 'foo') + n2 = Oga::XML::Text.new(:text => 'bar') + + left, right = described_class.to_compatible_types(n1, n2) + + left.should == 'foo' + right.should == 'bar' + end + + it 'returns two Strings when using two Attributes' do + n1 = Oga::XML::Attribute.new(:value => 'foo') + n2 = Oga::XML::Attribute.new(:value => 'bar') + + left, right = described_class.to_compatible_types(n1, n2) + + left.should == 'foo' + right.should == 'bar' + end + it 'returns two Strings when using a NodeSet and Float' do set = node_set(Oga::XML::Text.new(:text => 'foo'))