spec/mixlib/config_spec.rb in mixlib-config-2.0.0 vs spec/mixlib/config_spec.rb in mixlib-config-2.1.0.rc.1

- old
+ new

@@ -273,19 +273,45 @@ it "should still default to that value after reset" do @klass.attr 5 @klass.reset @klass.attr.should == 4 end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => 4 } + end + + it "save should save the new value if it gets set" do + @klass.attr 5 + (saved = @klass.save).should == { :attr => 5 } + @klass.reset + @klass.attr.should == 4 + @klass.restore(saved) + @klass.attr.should == 5 + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr 4 + (saved = @klass.save).should == { :attr => 4 } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => 4 } + end end describe "When config has a default value block" do before :each do @klass = Class.new @klass.extend(::Mixlib::Config) @klass.class_eval do default :x, 4 - default(:attr) { x*2} + default(:attr) { x*2 } end end it "should default to that value" do @klass.attr.should == 8 @@ -315,10 +341,36 @@ it "should still default to that value after reset" do @klass.attr 5 @klass.reset @klass.attr.should == 8 end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => 8, :x => 4 } + end + + it "save should save the new value if it gets set" do + @klass.attr 5 + (saved = @klass.save).should == { :attr => 5 } + @klass.reset + @klass.attr.should == 8 + @klass.restore(saved) + @klass.attr.should == 5 + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr 8 + (saved = @klass.save).should == { :attr => 8 } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => 8 } + end end describe "When config has an array default value" do before :each do @klass = Class.new @@ -330,10 +382,36 @@ @klass.attr << 'x' @klass.attr.should == [ 'x' ] @klass.reset @klass.attr.should == [] end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => [] } + end + + it "save should save the new value if it gets set" do + @klass.attr << 'x' + (saved = @klass.save).should == { :attr => [ 'x' ] } + @klass.reset + @klass.attr.should == [] + @klass.restore(saved) + @klass.attr.should == [ 'x' ] + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr = [] + (saved = @klass.save).should == { :attr => [] } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => [] } + end end describe "When config has a hash default value" do before :each do @klass = Class.new @@ -345,10 +423,36 @@ @klass.attr[:x] = 10 @klass.attr[:x].should == 10 @klass.reset @klass.attr[:x].should == nil end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => {} } + end + + it "save should save the new value if it gets set" do + @klass.attr[:hi] = 'lo' + (saved = @klass.save).should == { :attr => { :hi => 'lo' } } + @klass.reset + @klass.attr.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => { :hi => 'lo' } } + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr = {} + (saved = @klass.save).should == { :attr => {} } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => {} } + end end describe "When config has a string default value" do before :each do @klass = Class.new @@ -360,10 +464,36 @@ @klass.attr << ' world' @klass.attr.should == 'hello world' @klass.reset @klass.attr.should == 'hello' end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => 'hello' } + end + + it "save should save the new value if it gets set" do + @klass.attr << ' world' + (saved = @klass.save).should == { :attr => 'hello world' } + @klass.reset + @klass.attr.should == 'hello' + @klass.restore(saved) + @klass.attr.should == 'hello world' + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr 'hello world' + (saved = @klass.save).should == { :attr => 'hello world' } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => 'hello world' } + end end describe "When config has a a default value block" do before :each do @klass = Class.new @@ -396,10 +526,36 @@ it "should still default to that value after reset" do @klass.attr 5 @klass.reset @klass.attr.should == 4 end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => 4 } + end + + it "save should save the new value if it gets set" do + @klass.attr 5 + (saved = @klass.save).should == { :attr => 5 } + @klass.reset + @klass.attr.should == 4 + @klass.restore(saved) + @klass.attr.should == 5 + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr 4 + (saved = @klass.save).should == { :attr => 4 } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => 4 } + end end describe "When a configurable exists with writer and default value" do before :each do @klass = Class.new @@ -447,10 +603,36 @@ it "should still default to that value after reset" do @klass.attr 5 @klass.reset @klass.attr.should == 4 end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => 4 } + end + + it "save should save the new value if it gets set" do + @klass.attr 5 + (saved = @klass.save).should == { :attr => 10 } + @klass.reset + @klass.attr.should == 4 + @klass.restore(saved) + @klass.attr.should == 10 + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr 4 + (saved = @klass.save).should == { :attr => 8 } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => 8 } + end end describe "When a configurable exists with writer and default value set in chained form" do before :each do @klass = Class.new @@ -495,10 +677,36 @@ it "should still default to that value after reset" do @klass.attr 5 @klass.reset @klass.attr.should == 4 end + + it "save should not save anything for it" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :attr => 4 } + end + + it "save should save the new value if it gets set" do + @klass.attr 5 + (saved = @klass.save).should == { :attr => 10 } + @klass.reset + @klass.attr.should == 4 + @klass.restore(saved) + @klass.attr.should == 10 + end + + it "save should save the new value even if it is set to its default value" do + @klass.attr 2 + (saved = @klass.save).should == { :attr => 4 } + @klass.reset + @klass.save.should == {} + @klass.restore(saved) + @klass.save.should == { :attr => 4 } + end end describe "When a configurable exists with a context" do before :each do @klass = Class.new @@ -524,16 +732,54 @@ @klass.x = 10 @klass.x.should == 10 @klass.blah.x.should == 5 end + it "setting the entire context to a hash with default value overridden sets the value" do + @klass.blah = { :x => 10 } + @klass.blah.x.should == 10 + end + + it "setting the entire context to a hash sets non-default values" do + @klass.blah = { :y => 10 } + @klass.blah.x.should == 5 + @klass.blah.y.should == 10 + end + + it "setting the entire context to a hash deletes any non-default values and resets default values" do + @klass.blah.x = 10 + @klass.blah.y = 10 + @klass.blah = { :z => 10 } + @klass.blah.x.should == 5 + @klass.blah.y.should == nil + @klass.blah.z.should == 10 + end + it "after reset of the parent class, children are reset" do @klass.blah.x = 10 @klass.blah.x.should == 10 @klass.reset @klass.blah.x.should == 5 end + + it "save should not save anything for it by default" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :blah => { :x => 5 } } + end + + it "save should save any new values that are set in the context" do + @klass.blah.x = 10 + (saved = @klass.save).should == { :blah => { :x => 10 } } + @klass.reset + @klass.blah.x.should == 5 + @klass.restore(saved) + @klass.blah.x.should == 10 + @klass.save.should == { :blah => { :x => 10 } } + end end describe "When a configurable exists with a nested context" do before :each do @klass = Class.new @@ -567,12 +813,68 @@ @klass.blah.yarr.x = 10 @klass.blah.yarr.x.should == 10 @klass.reset @klass.blah.yarr.x.should == 5 end + + it "save should not save anything for it by default" do + @klass.save.should == {} + end + + it "save with include_defaults should save all defaults" do + @klass.save(true).should == { :blah => { :yarr => { :x => 5 } } } + end + + it "save should save any new values that are set in the context" do + @klass.blah.yarr.x = 10 + (saved = @klass.save).should == { :blah => { :yarr => { :x => 10 } } } + @klass.reset + @klass.blah.yarr.x.should == 5 + @klass.restore(saved) + @klass.blah.yarr.x.should == 10 + @klass.save.should == { :blah => { :yarr => { :x => 10 } } } + end end + describe "When a config_context with no defaulted values exists" do + before :each do + @klass = Class.new + @klass.extend(::Mixlib::Config) + @klass.class_eval do + config_context(:blah) do + configurable(:x) + end + end + end + + it "save does not save the hash for the config_context" do + @klass.save.should == {} + end + + it "save with defaults saves the hash for the config_context" do + @klass.save(true).should == { :blah => {} } + end + end + + describe "When a config_context with no configurables exists" do + before :each do + @klass = Class.new + @klass.extend(::Mixlib::Config) + @klass.class_eval do + config_context(:blah) + end + end + + it "save does not save the hash for the config_context" do + @klass.save.should == {} + end + + it "save with defaults saves the hash for the config_context" do + @klass.save(true).should == { :blah => {} } + end + end + describe "When a nested context has strict mode on" do class StrictClass2 extend ::Mixlib::Config config_context :c do config_strict_mode true @@ -603,6 +905,53 @@ it "The nested class does not allow you to set arbitrary config options" do lambda { StrictClass3.y = 10 }.should raise_error(Mixlib::Config::UnknownConfigOptionError) end end + + describe "When a config_context is opened twice" do + before :each do + @klass = Class.new + @klass.extend(::Mixlib::Config) + @klass.class_eval do + config_context(:blah) do + default :x, 10 + end + config_context(:blah) do + default :y, 20 + end + end + end + + it "Both config_context blocks are honored" do + @klass.blah.x == 10 + @klass.blah.y == 20 + end + end + + it "When a config_context is opened in place of a regular configurable, an error is raised" do + klass = Class.new + klass.extend(::Mixlib::Config) + lambda do + klass.class_eval do + default :blah, 10 + config_context(:blah) do + default :y, 20 + end + end + end.should raise_error(Mixlib::Config::ReopenedConfigurableWithConfigContextError) + end + + it "When a config_context is opened in place of a regular configurable, an error is raised" do + klass = Class.new + klass.extend(::Mixlib::Config) + lambda do + klass.class_eval do + config_context(:blah) do + default :y, 20 + end + default :blah, 10 + end + end.should raise_error(Mixlib::Config::ReopenedConfigContextWithConfigurableError) + end + end