Permalink
Browse files

Merge branch 'specs-cleanup'

  • Loading branch information...
2 parents b55c624 + 89c412d commit 4d15bb3394642393a7340e0f0d9e319fc7f70283 @burtlo burtlo committed Mar 19, 2013
View
@@ -7,3 +7,4 @@ Gemfile.lock
*.swp
*.swo
.idea
+tmp
View
@@ -4,7 +4,6 @@ gemspec
group :development do
gem "rake", "0.9.2"
- gem "rspec", "2.8"
- gem "guard"
gem "guard-rspec"
+ gem 'rb-fsevent', '~> 0.9'
end
@@ -0,0 +1,50 @@
+require 'spec_helper'
+
+describe "Attribute Default Value" do
+
+ context "when given a default value" do
+
+ class Meal
+ include HappyMapper
+ tag 'meal'
+ attribute :type, String, :default => 'omnivore'
+ end
+
+ let(:subject) { Meal }
+ let(:default_meal_type) { 'omnivore' }
+
+ context "when no value has been specified" do
+ it "returns the default value" do
+ meal = subject.parse('<meal />')
+ expect(meal.type).to eq default_meal_type
+ end
+ end
+
+ context "when saving to xml" do
+
+ let(:expected_xml) { %{<?xml version="1.0"?>\n<meal/>\n} }
+
+ it "the default value is not included" do
+ meal = subject.new
+ expect(meal.to_xml).to eq expected_xml
+ end
+ end
+
+ context "when a new, non-nil value has been set" do
+ it "returns the new value" do
+ meal = subject.parse('<meal />')
+ meal.type = 'vegan'
+
+ expect(meal.type).to_not eq default_meal_type
+ end
+
+ let(:expected_xml) { %{<?xml version="1.0"?>\n<meal type="kosher"/>\n} }
+
+ it "saves the new value to the xml" do
+ meal = subject.new
+ meal.type = 'kosher'
+ expect(meal.to_xml).to eq expected_xml
+ end
+ end
+ end
+end
@@ -1,32 +0,0 @@
-require 'spec_helper'
-
-describe "The default option of attributes" do
-
- class WithDefault
- include HappyMapper
- tag 'foo'
- attribute :bar, String, :default => 'baz'
- end
-
- it 'should return the default value after parsing a string without it' do
- foo = WithDefault.parse('<foo />')
- foo.bar.should == 'baz'
- end
-
- it 'should not include the default value in the produced xml' do
- foo = WithDefault.new
- foo.to_xml.should == %{<?xml version="1.0"?>\n<foo/>\n}
- end
-
- it 'should not return the default value when a non-nil value has been set' do
- foo = WithDefault.parse('<foo />')
- foo.bar = 'not-baz'
- foo.bar.should_not == 'baz'
- end
-
- it 'should include a non-nil value in the XML' do
- foo = WithDefault.new
- foo.bar = 'not-baz'
- foo.to_xml.should == %{<?xml version="1.0"?>\n<foo bar="not-baz"/>\n}
- end
-end
@@ -1,10 +1,12 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
+require 'spec_helper'
describe HappyMapper::Attribute do
+
describe "initialization" do
- it 'should accept :default as an option' do
+ it 'accepts :default option' do
attr = described_class.new(:foo, String, :default => 'foobar')
attr.default.should == 'foobar'
end
end
+
end
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
+require 'spec_helper'
describe HappyMapper::Element do
describe "initialization" do
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
+require 'spec_helper'
module Foo
class Bar; end
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
+require 'spec_helper'
describe HappyMapper::Attribute do
describe "initialization" do
@@ -1,92 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
-
-
-generic_class_xml = %{
- <root>
- <description>some description</description>
- <blarg name='blargname1' href='http://blarg.com'/>
- <blarg name='blargname2' href='http://blarg.com'/>
- <jello name='jelloname' href='http://jello.com'/>
- <subelement>
- <jello name='subjelloname' href='http://ohnojello.com' other='othertext'/>
- </subelement>
- </root>
-}
-
-module GenericBase
- class Base
- include HappyMapper
- tag '*'
- attribute :name, String
- attribute :href, String
- attribute :other, String
- end
- class Sub
- include HappyMapper
- tag 'subelement'
- has_one :jello, Base, :tag => 'jello'
- end
- class Root
- include HappyMapper
- tag 'root'
- element :description, String
- has_many :blargs, Base, :tag => 'blarg', :xpath => '.'
- has_many :jellos, Base, :tag => 'jello', :xpath => '.'
- has_many :subjellos, Base, :tag => 'jello', :xpath => 'subelement/.', :read_only => true
- has_one :sub_element, Sub
- end
-end
-
-
-describe HappyMapper do
- describe "can have generic classes using tag '*'" do
-
- before(:all) do
- @root = GenericBase::Root.parse(generic_class_xml)
- @xml = Nokogiri::XML(@root.to_xml)
- end
-
- it 'should map different elements to same class' do
- @root.blargs.should_not be_nil
- @root.jellos.should_not be_nil
- end
-
- it 'should filter on xpath appropriately' do
- @root.blargs.should have(2).items
- @root.jellos.should have(1).items
- @root.subjellos.should have(1).items
- end
-
- it 'should parse correct values onto generic class' do
- @root.blargs[0].name.should == 'blargname1'
- @root.blargs[0].href.should == 'http://blarg.com'
- @root.blargs[0].other.should be_nil
- @root.blargs[1].name.should == 'blargname2'
- @root.blargs[1].href.should == 'http://blarg.com'
- @root.blargs[1].other.should be_nil
- @root.jellos[0].name.should == 'jelloname'
- @root.jellos[0].href.should == 'http://jello.com'
- @root.jellos[0].other.should be_nil
- @root.subjellos[0].name.should == 'subjelloname'
- @root.subjellos[0].href.should == 'http://ohnojello.com'
- @root.subjellos[0].other.should == 'othertext'
- end
-
- it 'should #to_xml using parent element tag name' do
- @xml.xpath('/root/description').text.should == 'some description'
- @xml.xpath('/root/blarg[1]/@name').text.should == 'blargname1'
- @xml.xpath('/root/blarg[1]/@href').text.should == 'http://blarg.com'
- @xml.xpath('/root/blarg[1]/@other').text.should be_empty
- @xml.xpath('/root/blarg[2]/@name').text.should == 'blargname2'
- @xml.xpath('/root/blarg[2]/@href').text.should == 'http://blarg.com'
- @xml.xpath('/root/blarg[2]/@other').text.should be_empty
- @xml.xpath('/root/jello[1]/@name').text.should == 'jelloname'
- @xml.xpath('/root/jello[1]/@href').text.should == 'http://jello.com'
- @xml.xpath('/root/jello[1]/@other').text.should be_empty
- end
-
- it "should properly respect child HappyMapper tags if tag isn't provided on the element defintion" do
- @xml.xpath('root/subelement').should have(1).item
- end
- end
-end
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
+require 'spec_helper'
require 'pp'
require 'uri'
Oops, something went wrong.

0 comments on commit 4d15bb3

Please sign in to comment.