require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require 'dionysus/configuration' describe Configuration do describe "basics" do before(:each) { @c = Configuration.new(false) } it "should be empty when no defaults" do @c.size.should == 0 @c.should be_empty end it "should be accessible by hash brackets" do @c[:something] = 'a value' @c['something-else'] = 'b value' @c.should_not be_empty @c[:something].should == 'a value' @c['something'].should == 'a value' @c[:'something-else'].should == 'b value' @c['something-else'].should == 'b value' end it "should be accessible by method name" do @c.something = 'a value' @c.should_not be_empty @c.something.should == 'a value' end it "should be accessible by get/set methods" do @c.set(:something, 'a value') @c.set('something-else', 'b value') @c.should_not be_empty @c.get(:something).should == 'a value' @c.get('something').should == 'a value' @c.get(:'something-else').should == 'b value' @c.get('something-else').should == 'b value' end it "should not allow keys with the same name as defined methods." do lambda { @c.set(:set, 'wowsers') }.should raise_error(ArgumentError, "Invalid key: set") @c.to_hash[:set].should be_nil lambda { @c[:set] = 'wowsers' }.should raise_error(ArgumentError, "Invalid key: set") @c.to_hash[:set].should be_nil lambda { @c.set = 'wowsers' }.should raise_error(ArgumentError, "Invalid key: set") @c.to_hash[:set].should be_nil end it "should not allow keys that do not conform to the allowed regex." do lambda { @c.set('^&^&*^&*', 'wowsers') }.should raise_error(ArgumentError, "Invalid key: ^&^&*^&*") lambda { @c.set('', 'wowsers') }.should raise_error(ArgumentError, "Invalid key: ") lambda { @c.set(nil, 'wowsers') }.should raise_error(ArgumentError, "Invalid key: ") lambda { @c.set(Object.new, 'wowsers') }.should raise_error(ArgumentError, /^Invalid key: #$/) end it "should revert to the normal method_missing with a block" do lambda { @c.not_a_key { 1+1 } }.should raise_error(NoMethodError) end it "should revert to the normal method_missing with any arguments" do lambda { @c.not_a_key('fooey') }.should raise_error(NoMethodError) lambda { @c.not_a_key(nil) }.should raise_error(NoMethodError) end it "should revert to normal method_missing with a setter and more than 1 argument" do lambda { @c.send(:not_a_key=, 'fooey', nil) }.should raise_error(NoMethodError) end end describe "with soft keys" do before(:each) { @c = Configuration.new(false) } it 'should be soft keys' do @c.should be_soft_keys end it 'should initialize empty' do @c.size.should == 0 @c.should be_empty end it "should take defaults" do @c = Configuration.new(false, :a_val => 'some value') @c.should be_soft_key @c.a_val.should == 'some value' @c.size.should == 1 @c.should_not be_empty end it "should remove the key on delete" do @c.foo = 'fooey' @c.keys.should == [:foo] @c.delete :foo @c.foo.should be_nil @c.keys.should be_empty end it "should not remove the key on set nil" do @c.foo = 'fooey' @c.keys.should == [:foo] @c.foo = nil @c.foo.should be_nil @c.keys.should == [:foo] end it "should allow any key" do @c.allowed_key?('fjdasfiodsaifo').should be_true @c.allowed_key?('^&*%^*9789').should be_true @c.allowed_key?(Object.new).should be_true end it "should not allow setting hard keys" do lambda { @c = Configurat.new(false, :a_hard_key, :a_val => 'some_value') }.should raise_error(ArgumentError, 'Cannot define hard keys in soft keys mode') end end describe "with hard keys" do before(:each) { @c = Configuration.new(:foo, :bar) } it "should be hard keys" do @c.should be_hard_keys end it "should initialize empty" do @c.size.should == 0 @c.should be_empty @c.keys.should == [:bar, :foo] end it "should take defaults" do @c = Configuration.new(:foo, :bar, {'wowsers' => 'wow', :bar => 'fooey'}) @c.keys.should == [:bar, :foo, :wowsers] @c.size.should == 2 @c.foo.should == nil @c.bar.should == 'fooey' @c[:wowsers].should == 'wow' end it "should not remove the key on delete" do @c.foo = 'fooey' @c.keys.should include(:foo) @c.size.should == 1 @c.delete :foo @c.foo.should be_nil @c.keys.should include(:foo) @c.size.should == 0 end it "should not remove the key on set nil" do @c.foo = 'fooey' @c.keys.should include(:foo) @c.size.should == 1 @c.foo = nil @c.foo.should be_nil @c.keys.should include(:foo) @c.size.should == 1 end it "should allow only the hard keys" do @c.allowed_key?('fjdasfiodsaifo').should be_false @c.allowed_key?('^&*%^*9789').should be_false @c.allowed_key?(Object.new).should be_false @c.allowed_key?(:foo).should be_true @c.allowed_key?('foo').should be_true end it "should revert to the normal method_missing an unallowed key" do lambda { @c.not_a_key }.should raise_error(NoMethodError) end end end