diff options
author | Michael Bleigh <michael@intridea.com> | 2011-08-02 00:18:56 -0500 |
---|---|---|
committer | Michael Bleigh <michael@intridea.com> | 2011-08-02 00:18:56 -0500 |
commit | 3a53b51cd3b3107ff0b032ec03b58c85c3a66ee8 (patch) | |
tree | 1303d0d866126bf865653218701f2210fdd915a1 /spec/hashie/extensions/method_access_spec.rb | |
parent | 6f396272f51f7a7fb8f529c96197e4ac18c63925 (diff) | |
download | hashie-3a53b51cd3b3107ff0b032ec03b58c85c3a66ee8.tar.gz |
Adds MethodWriter and MethodQuery extensions.
Diffstat (limited to 'spec/hashie/extensions/method_access_spec.rb')
-rw-r--r-- | spec/hashie/extensions/method_access_spec.rb | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/spec/hashie/extensions/method_access_spec.rb b/spec/hashie/extensions/method_access_spec.rb index 9b3b542..f77fa00 100644 --- a/spec/hashie/extensions/method_access_spec.rb +++ b/spec/hashie/extensions/method_access_spec.rb @@ -39,3 +39,74 @@ describe Hashie::Extensions::MethodReader do end end end + +describe Hashie::Extensions::MethodWriter do + class WriterHash < Hash + include Hashie::Extensions::MethodWriter + end + subject{ WriterHash.new } + + it 'should write from a method call' do + subject.awesome = 'sauce' + subject['awesome'].should == 'sauce' + end + + it 'should convert the key using the #convert_key method' do + subject.stub!(:convert_key).and_return(:awesome) + subject.awesome = 'sauce' + subject[:awesome].should == 'sauce' + end + + it 'should still NoMethodError on non equals-ending methods' do + lambda{ subject.awesome }.should raise_error(NoMethodError) + end + + it 'should #respond_to? properly' do + subject.should be_respond_to(:abc=) + subject.should_not be_respond_to(:abc) + end +end + +describe Hashie::Extensions::MethodQuery do + class QueryHash < Hash + def initialize(hash = {}); self.update(hash) end + include Hashie::Extensions::MethodQuery + end + subject{ QueryHash } + + it 'should be true for non-nil string key values' do + subject.new('abc' => 123).should be_abc + end + + it 'should be true for non-nil symbol key values' do + subject.new(:abc => 123).should be_abc + end + + it 'should be false for nil key values' do + subject.new(:abc => false).should_not be_abc + end + + it 'should raise a NoMethodError for non-set keys' do + lambda{ subject.new.abc? }.should raise_error(NoMethodError) + end + + it 'should respond_to? for existing string keys' do + subject.new('abc' => 'def').should be_respond_to('abc?') + end + + it 'should respond_to? for existing symbol keys' do + subject.new(:abc => 'def').should be_respond_to(:abc?) + end + + it 'should not respond_to? for non-existent keys' do + subject.new.should_not be_respond_to('abc?') + end +end + +describe Hashie::Extensions::MethodAccess do + it 'should include all of the other method mixins' do + klass = Class.new(Hash) + klass.send :include, Hashie::Extensions::MethodAccess + (klass.ancestors & [Hashie::Extensions::MethodReader, Hashie::Extensions::MethodWriter, Hashie::Extensions::MethodQuery]).size.should == 3 + end +end |