require 'spec_helper' describe Pump::Xml do describe ".new" do it "requires two parameters" do lambda{ Pump::Xml.new }.should raise_error(ArgumentError) lambda{ Pump::Xml.new('record') }.should raise_error(ArgumentError) lambda{ Pump::Xml.new('record', []) }.should_not raise_error end end describe "#encode" do let(:person) { Struct.new(:name, :age, :last_name).new('Benny', 9, 'Hellman') } let(:xml) { Pump::Xml.new('person', [{:name => :name}]) } it "requires one object" do lambda{ xml.encode }.should raise_error(ArgumentError) lambda{ xml.encode(person) }.should_not raise_error end it "returns xml string" do xml.encode(person).should eql("#{XML_INSTRUCT}\n Benny\n") end context "with array" do context "with one entry" do let(:people) { [person] } it "returns xml string" do xml.encode(people).should eql("#{XML_INSTRUCT}\n \n Benny\n \n") end end context "with multiple entries" do let(:people) { [person, Struct.new(:name, :age).new('Carlo', 5)] } it "returns xml string" do xml.encode(people).should eql("#{XML_INSTRUCT}\n \n Benny\n \n \n Carlo\n \n") end end context "with empty array" do let(:people) { [] } it "returns xml string" do xml.encode(people).should eql("#{XML_INSTRUCT}") end end context "with no instruct" do let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false) } let(:people) { [] } it "returns xml string" do xml.encode(people).should eql("") end end context "with extra_indent" do let(:people) { [person] } let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false, :extra_indent => 1) } it "returns xml string" do xml.encode(people).should eql(" \n \n Benny\n \n ") end end context "with array_root" do let(:people) { [person] } let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false, :array_root => "personas") } it "returns xml string" do xml.encode(people).should eql("\n \n Benny\n \n") end end end context "with no instruct" do let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false) } it "returns xml string" do xml.encode(person).should eql("\n Benny\n") end end context "with extra_indent" do let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false, :extra_indent => 1) } it "returns xml string" do xml.encode(person).should eql(" \n Benny\n ") end end context "with attribute" do let(:xml) do Pump::Xml.new('person', [ {:name => :name}, {:age => :age, :attributes => {:type => :integer}} ]) end it do xml.encode(person).should eql("#{XML_INSTRUCT}\n Benny\n 9\n") end end context "with blank name" do let(:person) { Struct.new(:name, :age).new('', 9) } it do xml.encode(person).should eql("#{XML_INSTRUCT}\n \n") end end context "with nil name" do let(:person) { Struct.new(:name, :age).new(nil, 9) } it do xml.encode(person).should eql("#{XML_INSTRUCT}\n \n") end context "and with :nil_check => true" do let(:xml) { Pump::Xml.new('person', [{:name => :name, :nil_check => true}]) } it do xml.encode(person).should eql("#{XML_INSTRUCT}\n \n") end end end context "with multiple attrubutes" do let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age}]) } it "returns xml string" do xml.encode(person).should eql("#{XML_INSTRUCT}\n Benny\n 9\n") end end context "with renamed attrubutes" do let(:xml) { Pump::Xml.new('person', [{"last-name" => :last_name}]) } it "returns xml string" do xml.encode(person).should eql("#{XML_INSTRUCT}\n Hellman\n") end end context "with date attribute" do let(:person) { Struct.new(:at).new(Date.new(2013, 2, 7)) } let(:xml) { Pump::Xml.new('person', [{:at => :at, :attributes => {:type => 'date'}}]) } it "returns xml string" do xml.encode(person).should eql("#{XML_INSTRUCT}\n 2013-02-07\n") end end context "with datetime attribute" do let(:person) { Struct.new(:at).new(Time.new(2013, 2, 7, 0, 0, 0)) } let(:xml) { Pump::Xml.new('person', [{:at => :at, :typecast => :xmlschema, :attributes => {:type => 'datetime'}}]) } it "returns xml string" do xml.encode(person).should eql("#{XML_INSTRUCT}\n 2013-02-07T00:00:00+01:00\n") end context "but nil" do let(:person) { Struct.new(:at).new(nil) } it "returns xml string" do xml.encode(person).should eql("#{XML_INSTRUCT}\n \n") end end end end end