require_relative './../spec_helper.rb' describe ROXML, "with namespaces" do describe "for writing" do before do @xml = < allowInOut true OhNo! Another! gronk EOS end class NetworkConfig include ROXML xml_namespace :gronk xml_name 'NetworkConfig' xml_reader :name, :from => '@name' xml_reader :errors, :as => [], :from => 'gronk:error' xml_accessor :fence_mode, :from => 'vmw:FenceMode' xml_accessor :dhcp?, :from => 'vmw:Dhcp' end class VApp include ROXML xml_namespace :gronk xml_name "VApp" xml_reader :name, :from => '@name' xml_reader :status, :from => '@status' xml_reader :href, :from => '@href' xml_reader :type, :from => '@type' xml_accessor :foo, :from => 'foo', :namespace => false xml_accessor :bar, :from => 'bar', :namespace => false xml_accessor :network_configs, :as => [NetworkConfig], :namespace => :gronk end describe "#to_xml" do it "should reproduce the input xml" do output = ROXML::XML::Document.new output.root = VApp.from_xml(@xml).to_xml(:namespaces => {'vmw' => "http://foo.example.com", 'gronk' => "http://gronk.example.com"}) expect(ROXML::XML.parse_string(output.to_s)).to be_equivalent_to(ROXML::XML.parse_string(@xml)) end end end shared_examples_for "roxml namespacey declaration" do context "with a namespacey :from" do context "and an explicit :namespace" do it "should raise" do expect do Class.new do include ROXML xml_reader :default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespacey_from', :namespace => 'explicit' end end.to raise_error(ROXML::ContradictoryNamespaces) end end context "and :namespace => false" do it "should raise" do expect do Class.new do include ROXML xml_reader :default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespacey_from', :namespace => false end end.to raise_error(ROXML::ContradictoryNamespaces) end end end end shared_examples_for "roxml namespacey declaration with default" do it_should_behave_like "roxml namespacey declaration" it "should use the default namespace" do expect(@instance.default_namespace).to eq('default namespace node') end context "and :namespace => false" do it "should find the namespace-less node" do expect(@instance.default_namespace_with_namespace_false).to eq('namespaceless node') end end context "with an explicit :namespace" do it "should use the explicit namespace" do @instance.default_and_explicit_namespace == 'explicit namespace node' end end context "with a namespace-less :from" do it "should use the default namespace" do expect(@instance.default_namespace_with_namespaceless_from).to eq('default namespace node') end context "and :namespace => false" do it "should find the namespace-less node" do expect(@instance.default_namespace_with_namespaceless_from_and_namespace_false).to eq('namespaceless node') end end context "and an explicit :namespace" do it "should use the explicit namespace" do expect(@instance.default_namespace_with_namespaceless_from_and_explicit_namespace).to eq('explicit namespace node') end end end context "with a namespacey :from" do it "should use the :from namespace" do expect(@instance.default_namespace_with_namespacey_from).to eq('namespacey node') end end context "with an namespacey XPath :from" do it "should use the given namespace" do expect(@instance.default_namespace_with_namespacey_xpath_from).to eq('namespacey xpath node') end end context "with an XPath :from" do it "should use the default namespace" do expect(@instance.default_namespace_with_xpath_from).to eq('default namespace with xpath node') end end end context "with a default namespace declared" do class DefaultNamespaceyObject include ROXML xml_namespace :default_declared xml_reader :default_namespace xml_reader :default_namespace_with_namespace_false, :namespace => false xml_reader :default_and_explicit_namespace, :namespace => 'explicit' xml_reader :default_namespace_with_namespaceless_from, :from => 'with_namespaceless_from' xml_reader :default_namespace_with_namespaceless_from_and_explicit_namespace, :from => 'with_namespaceless_from', :namespace => 'explicit' xml_reader :default_namespace_with_namespaceless_from_and_namespace_false, :from => 'with_namespaceless_from', :namespace => false xml_reader :default_namespace_with_namespacey_from, :from => 'namespacey:with_namespacey_from' xml_reader :default_namespace_with_namespacey_xpath_from, :from => 'xpath/namespacey:default_namespace' xml_reader :default_namespace_with_xpath_from, :from => 'xpath/default_namespace' # These are handled in the "roxml namespacey declaration" shared spec # xml_reader :default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespaceless_from', :namespace => false # xml_reader :default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespaceless_from', :namespace => 'explicit' end before do @instance = DefaultNamespaceyObject.from_xml(%{ default namespace node namespacey node explicit namespace node namespaceless node default namespace node explicit namespace node namespaceless node namespacey xpath node default namespace with xpath node }) end it_should_behave_like "roxml namespacey declaration with default" end context "with a default namespace on the root node" do class XmlDefaultNamespaceyObject include ROXML xml_reader :default_namespace xml_reader :default_namespace_with_namespace_false, :namespace => false xml_reader :default_and_explicit_namespace, :namespace => 'explicit' xml_reader :default_namespace_with_namespaceless_from, :from => 'with_namespaceless_from' xml_reader :default_namespace_with_namespaceless_from_and_explicit_namespace, :from => 'with_namespaceless_from', :namespace => 'explicit' xml_reader :default_namespace_with_namespaceless_from_and_namespace_false, :from => 'with_namespaceless_from', :namespace => false xml_reader :default_namespace_with_namespacey_from, :from => 'namespacey:with_namespacey_from' xml_reader :default_namespace_with_namespacey_xpath_from, :from => 'xpath/namespacey:default_namespace' xml_reader :default_namespace_with_xpath_from, :from => 'xpath/default_namespace' # These are handled in the "roxml namespacey declaration" shared spec # xml_reader :default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespaceless_from', :namespace => false # xml_reader :default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespaceless_from', :namespace => 'explicit' end before do @instance = XmlDefaultNamespaceyObject.from_xml(%{ default namespace node namespacey node explicit namespace node namespaceless node default namespace node explicit namespace node namespaceless node namespacey xpath node default namespace with xpath node }) end it_should_behave_like "roxml namespacey declaration with default" end context "without a default namespace" do class NamespaceyObject include ROXML xml_reader :no_default_namespace xml_reader :no_default_namespace_with_namespace_false, :namespace => false xml_reader :no_default_but_an_explicit_namespace, :namespace => 'explicit' xml_reader :no_default_namespace_with_namespaceless_from, :from => 'with_namespaceless_from' xml_reader :no_default_namespace_with_namespaceless_from_and_explicit_namespace, :from => 'with_namespaceless_from', :namespace => 'explicit' xml_reader :no_default_namespace_with_namespaceless_from_and_namespace_false, :from => 'with_namespaceless_from', :namespace => false xml_reader :no_default_namespace_with_namespacey_from, :from => 'namespacey:with_namespacey_from' # These are handled in the "roxml namespacey declaration" shared spec # xml_reader :no_default_namespace_with_namespacey_from_and_explicit_namespace, :from => 'namespacey:with_namespacey_from', :namespace => 'explicit' # xml_reader :no_default_namespace_with_namespacey_from_and_namespace_false, :from => 'namespacey:with_namespacey_from', :namespace => false end before do @instance = NamespaceyObject.from_xml(%{ namespacey node explicit namespace node namespaceless node explicit namespace node namespaceless node namespaceless node }) end it_should_behave_like "roxml namespacey declaration" it "should find the namespace-less node" do expect(@instance.no_default_namespace).to eq('namespaceless node') end context "with :namespace => false" do it "should find the namespace-less node" do expect(@instance.no_default_namespace_with_namespace_false).to eq('namespaceless node') end end context "with an explicit :namespace" do it "should use the explicit namespace" do expect(@instance.no_default_but_an_explicit_namespace).to eq('explicit namespace node') end end context "with a namespace-less :from" do it "should find the namespace-less node" do expect(@instance.no_default_namespace_with_namespaceless_from).to eq('namespaceless node') end context "and an explicit :namespace" do it "should use the explicit namespace" do expect(@instance.no_default_namespace_with_namespaceless_from_and_explicit_namespace).to eq('explicit namespace node') end end context "with :namespace => false" do it "should find the namespace-less node" do expect(@instance.no_default_namespace_with_namespaceless_from_and_namespace_false).to eq('namespaceless node') end end end context "with a namespacey :from" do it "should use the :from namespace" do expect(@instance.no_default_namespace_with_namespacey_from).to eq('namespacey node') end end end end