spec/configuration_spec.rb in loquacious-1.7.1 vs spec/configuration_spec.rb in loquacious-1.8.0
- old
+ new
@@ -1,13 +1,9 @@
require File.expand_path('spec_helper', File.dirname(__FILE__))
describe Loquacious::Configuration do
- before(:all) do
- @respond_to = Object.instance_method(:respond_to?)
- end
-
before(:each) do
@obj = Loquacious::Configuration.new
end
it 'should initialize from a block' do
@@ -28,17 +24,16 @@
@obj.second = 'bar'
@obj.second.should == 'bar'
end
it 'should deine attribute accessors when first used' do
- m = @respond_to.bind(@obj)
- m.call(:foo).should == false
- m.call(:foo=).should == false
+ @obj.respond_to?(:foo).should == false
+ @obj.respond_to?(:foo=).should == false
@obj.foo
- m.call(:foo).should == true
- m.call(:foo=).should == true
+ @obj.respond_to?(:foo).should == true
+ @obj.respond_to?(:foo=).should == true
end
it 'should provide a hash object for storing method descriptions' do
h = @obj.__desc
@obj.__desc.should equal(h)
@@ -95,10 +90,11 @@
cfg.puts.should == 'not what you think'
end
it 'should not be affected by loading other modules like timeout' do
require 'timeout'
+ Loquacious.remove :timeout
cfg = Loquacious::Configuration.new {
timeout 10
foo 'bar'
baz 'buz'
}
@@ -133,11 +129,11 @@
}
obj.first.should == 'foo'
obj.second.bar.should be_nil
- Loquacious::Configuration::DSL.new(obj) {
+ Loquacious::Configuration::DSL.evaluate(:config => obj) {
first 'bar'
second.bar 'no longer nil'
}
obj.first.should == 'bar'
@@ -159,11 +155,11 @@
obj.__desc[:first].should == 'the first value'
obj.__desc[:second].should == 'the second value'
obj.second.__desc[:bar].should == 'time to go drinking'
- Loquacious::Configuration::DSL.new(obj) {
+ Loquacious::Configuration::DSL.evaluate(:config => obj) {
first 'bar'
second.bar 'no longer nil'
}
obj.first.should == 'bar'
@@ -273,10 +269,151 @@
}
other.__desc[:first].should == " This is the first thing we are defining in this config.\n It has a multiline comment."
other.__desc[:second].should == "This is a short explanation\n\nExample:\n do this then that\n followed by this line"
end
+ end
+ # -----------------------------------------------------------------------
+ describe 'when working with defaults' do
+ before :each do
+ Loquacious::Configuration.instance_variable_get(:@table).clear
+ end
+
+ it 'returns default values when no other value exists' do
+ Loquacious::Configuration.defaults_for('test') {
+ first 'foo', :desc => 'the first value'
+ desc 'the second value'
+ second {
+ bar nil, :desc => 'time to go drinking'
+ }
+ }
+
+ c = Loquacious::Configuration.for 'test'
+ c.first.should == 'foo'
+ c.second.bar.should be_nil
+ end
+
+ it 'does not overwrite existing configuration values' do
+ c = Loquacious::Configuration.for('test') {
+ first 1
+ thrid 3
+ }
+
+ Loquacious::Configuration.defaults_for('test') {
+ first 'foo', :desc => 'the first value'
+ desc 'the second value'
+ second {
+ bar nil, :desc => 'time to go drinking'
+ }
+ }
+
+ c.first.should == 1
+ c.third.should == 3
+ c.second.bar.should be_nil
+
+ c.__desc[:first].should == 'the first value'
+ c.__desc[:second].should == 'the second value'
+ c.second.__desc[:bar].should == 'time to go drinking'
+ c.__desc[:thrid].should be_nil
+ end
+
+ it 'does not overwrite nested configuration values' do
+ c = Loquacious::Configuration.for('test') {
+ first 1
+ second {
+ bar 'pub'
+ baz {
+ buz 'random text'
+ boo 'who'
+ }
+ }
+ thrid 3
+ }
+
+ Loquacious::Configuration.defaults_for('test') {
+ first 'foo', :desc => 'the first value'
+ desc 'the second value'
+ second {
+ bar 'h-bar', :desc => 'time to go drinking'
+ desc 'getting weird'
+ baz {
+ buz 'buz', :desc => 'post drinking feeling'
+ boo nil, :desc => 'no need to cry about it'
+ }
+ }
+ }
+
+ c.first.should == 1
+ c.third.should == 3
+ c.second.bar.should == 'pub'
+ c.second.baz.buz.should == 'random text'
+ c.second.baz.boo.should == 'who'
+
+ c.second.bar = Loquacious::Undefined.new('second.bar')
+ c.second.bar.should == 'h-bar'
+
+ c.__desc[:first].should == 'the first value'
+ c.__desc[:second].should == 'the second value'
+ c.second.__desc[:bar].should == 'time to go drinking'
+ c.second.__desc[:baz].should == 'getting weird'
+ c.second.baz.__desc[:buz].should == 'post drinking feeling'
+ c.second.baz.__desc[:boo].should == 'no need to cry about it'
+ c.__desc[:thrid].should be_nil
+ end
+
+ it 'supports differing default type' do
+ c = Loquacious::Configuration.for('test') {
+ first 1
+ second {
+ bar 'pub'
+ desc 'overwrite me'
+ baz {
+ buz 'random text'
+ boo 'who'
+ }
+ }
+ thrid 3
+ }
+
+ Loquacious::Configuration.defaults_for('test') {
+ first 'foo', :desc => 'the first value'
+ desc 'the second value'
+ second {
+ bar 'h-bar', :desc => 'time to go drinking'
+ baz nil, :desc => 'deprecated'
+ }
+ }
+
+ c.second.baz.buz.should == 'random text'
+ c.second.baz.boo.should == 'who'
+
+ c.second.baz = Loquacious::Undefined.new('second.bar')
+ c.second.baz.should be_nil
+ c.second.__desc[:baz].should == 'deprecated'
+ end
+
+ it 'properly handles Proc default values' do
+ c = Loquacious::Configuration.for('test') {
+ first 1
+ second {
+ bar 'pub'
+ }
+ thrid 3
+ }
+
+ Loquacious::Configuration.defaults_for('test') {
+ first 'foo', :desc => 'the first value'
+ desc 'the second value'
+ second {
+ bar 'h-bar', :desc => 'time to go drinking'
+ baz(Proc.new { c.third * 12 }, :desc => 'proc will be evaluated')
+ }
+ }
+
+ c.second.baz.should == 36
+ c.second.__desc[:baz].should == 'proc will be evaluated'
+ end
end
+
end
-# EOF