spec/helpers_spec.rb in bikepoa-tools-0.3.1 vs spec/helpers_spec.rb in bikepoa-tools-0.4.0

- old
+ new

@@ -1,65 +1,61 @@ +require 'spec_helper' describe BikePOA::Helpers::ForceField do - class DummySubject < Hashie::Dash - include BikePOA::Helpers::ForceField - property :normal - property :number - property :name + describe 'enforcer installation' do + before :each do + module BikePOA::Helpers::ForceField::Enforcers + def self.force_something(value) + "#{value}, forced" + end + end + class DummySubject < Hashie::Dash + include BikePOA::Helpers::ForceField + property :forced + force_something :forced + end + end - force_integer :number - force_utf8 :name - end + context 'when setting property directly' do + let(:dummy) { DummySubject.new } - describe 'force utf8' do - let(:dummy) { DummySubject.new } - - describe 'when setting property directly' do - it 'encodes a iso-8859-1 value to utf-8' do - dummy.name = "Jo\xE3o da Silva" - dummy.name.should == "Jo\xC3\xA3o da Silva" + it 'applies the enforcer' do + dummy.forced = 'a value' + dummy.forced.should == 'a value, forced' end + end + context 'when creating the object from a hash' do + let(:dummy) { DummySubject.new(forced: 'a value') } - it 'does not try to over-convert an utf-8 value' do - dummy.name = "Jo\xC3\xA3o da Silva" - dummy.name.should == "Jo\xC3\xA3o da Silva" + it 'forces the value to become something else' do + dummy = DummySubject.new(forced: 'a value') + dummy.forced.should == 'a value, forced' end end - describe "when loading the Dash from a hash" do - it 'encodes an iso-8859-1 value to utf-8' do - expect { DummySubject.new(number: '678') }.to_not raise_error(ArgumentError) - end - end end - describe 'force integer' do - let(:dummy) { DummySubject.new } + describe :Enforcers do + let(:mod) { BikePOA::Helpers::ForceField::Enforcers } - describe "when setting property directly" do + describe :force_utf8 do + it 'encodes a iso-8859-1 value to utf-8' do + mod.force_utf8("Jo\xE3o da Silva").bytes.to_a.should == "Jo\xC3\xA3o da Silva".bytes.to_a + end - it "accepts any value that is castable to integer" do - expect { dummy.number = '42' }.to_not raise_error(ArgumentError) - expect { dummy.number = 42 }.to_not raise_error(ArgumentError) - expect { dummy.number = 42.0 }.to_not raise_error(ArgumentError) + it 'does not try to over-convert an utf-8 value' do + mod.force_utf8("Jo\xC3\xA3o da Silva").bytes.to_a.should == "Jo\xC3\xA3o da Silva".bytes.to_a end - it "raises when the value is not castable to integer" do - expect { dummy.number = "abc" }.to raise_error(ArgumentError) - expect { dummy.number = "a12" }.to raise_error(ArgumentError) - end - it 'lets you put anything into non-forced fields' do - expect { DummySubject.new(normal: 'abc1123') }.to_not raise_error - end end - describe "when loading the Dash from a hash" do - it 'accepts values castable to integer' do - expect { DummySubject.new(number: '678') }.to_not raise_error(ArgumentError) + describe 'force integer' do + it "accepts any value that is castable to integer" do + expect { mod.force_integer('42') }.to_not raise_error(ArgumentError) + expect { mod.force_integer(42) }.to_not raise_error(ArgumentError) + expect { mod.force_integer(42.0) }.to_not raise_error(ArgumentError) end - it "raises when one of the forced-fields is invalid" do - expect { DummySubject.new(number: "abc") }.to raise_error(ArgumentError) - end - it 'lets you put anything into non-forced fields' do - expect { DummySubject.new(normal: 'abc1123') }.to_not raise_error + it "raises when the value is not castable to integer" do + expect { mod.force_integer("abc") }.to raise_error(ArgumentError) + expect { mod.force_integer("a12") }.to raise_error(ArgumentError) end end end end