require 'spec_helper'
describe Socialcastr::SAX::ActiveResource do
describe "#parse" do
before do
@active_resource = Socialcastr::SAX::ActiveResource.new
@parser = Nokogiri::XML::SAX::Parser.new(@active_resource)
end
context "an empty document with a root 'message' element" do
it "should return a nil object" do
xml = ""
@parser.parse(xml)
@active_resource.data.should be_nil
end
end
context "a document with a nil element" do
it "should make the nil element accessible" do
xml = "1"
@parser.parse(xml)
@active_resource.data.rating.should be_nil
end
end
context "a document with a string element" do
it "should make the element accessible as a string" do
xml = "Hello World!"
@parser.parse(xml)
@active_resource.data.title.class.should == String
@active_resource.data.title.should == "Hello World!"
end
end
context "a document with a true boolean element" do
it "should make the element accessible as a boolean" do
xml = "true"
@parser.parse(xml)
@active_resource.data.ratable.should be_true
end
end
context "a document with a false boolean element" do
it "should make the element accessible as a boolean" do
xml = "false"
@parser.parse(xml)
@active_resource.data.ratable.should be_false
end
end
context "a document with an array of 2 string elements" do
before do
xml = "firstsecond"
@parser.parse(xml)
end
it "should make the array accessible through the collection name" do
@active_resource.data.tags.class.should == Array
end
it "should have an array containing 2 elements" do
@active_resource.data.tags.size.should == 2
end
it "should have an array of objects of class string" do
@active_resource.data.tags.first.class.should == String
end
end
context "a document with an array of 2 complex elements" do
before do
xml = "firstsecond"
@parser.parse(xml)
end
it "should make the array accessible through the c ollection name" do
@active_resource.data.tags.class.should == Array
end
it "should have an array containing 2 elements" do
@active_resource.data.tags.size.should == 2
end
it "should have an array of objects of class derived from the element name" do
@active_resource.data.tags.first.class.should == Socialcastr::Tag
end
end
context "a document with an array as the root element" do
before do
xml = "helloworld"
@parser.parse(xml)
end
it "should return an array" do
@active_resource.data.class.should == Array
end
it "should grant access to each element" do
@active_resource.data.first.should == "hello"
end
end
context "a document with a complex element" do
before do
xml = "johndoeJohn Doe"
@parser.parse(xml)
end
it "should grant access to the inside element as an object of class derived from the element name" do
@active_resource.data.user.class.should == Socialcastr::User
end
it "should grant access to nested elements as methods of the parent class" do
@active_resource.data.user.username.should == "johndoe"
end
end
end
end