spec/attribute_spec.rb in attributor-2.4.0 vs spec/attribute_spec.rb in attributor-2.5.0
- old
+ new
@@ -41,11 +41,11 @@
end
context 'describe' do
let(:attribute_options) { {:required => true, :values => ["one"], :description => "something", :min => 0} }
let(:expected) do
- h = {:type => {:name => type.name} }
+ h = {type: {name: type.name, id: type.id}}
common = attribute_options.select{|k,v| Attributor::Attribute::TOP_LEVEL_OPTIONS.include? k }
h.merge!( common )
h[:options] = {:min => 0 }
h
end
@@ -235,25 +235,58 @@
type.should_receive(:load).with(value, context, foo: 'bar')
attribute.load(value, context, foo: 'bar')
end
context 'applying default values' do
+ let(:value) { nil }
let(:default_value) { "default value" }
let(:attribute_options) { {:default => default_value} }
subject(:result) { attribute.load(value) }
context 'for nil' do
- let(:value) { nil }
it { should == default_value}
end
context 'for false' do
let(:type) { Attributor::Boolean }
let(:default_value) { false }
- let(:value) { nil }
it { should == default_value}
+ end
+
+ context 'for a Proc-based default value' do
+ let(:context){ ["$"] }
+ subject(:result){ attribute.load(value,context) }
+
+
+ context 'with no arguments arguments' do
+ let(:default_value) { proc { "no_params" } }
+ it { should == default_value.call }
+ end
+
+ context 'with 1 argument (the parent)' do
+ let(:default_value) { proc {|parent| "parent is fake: #{parent.class}" } }
+ it { should == "parent is fake: Attributor::FakeParent" }
+ end
+
+ context 'with 2 argument (the parent and the contents)' do
+ let(:default_value) { proc {|parent,context| "parent is fake: #{parent.class} and context is: #{context}" } }
+ it { should == "parent is fake: Attributor::FakeParent and context is: [\"$\"]"}
+ end
+
+ context 'which attempts to use the parent (which is not supported for the moment)' do
+ let(:default_value) { proc {|parent| "any parent method should spit out warning: [#{parent.something}]" } }
+ it "should output a warning" do
+ begin
+ old_verbose, $VERBOSE = $VERBOSE, nil
+ Kernel.should_receive(:warn).and_call_original
+ attribute.load(value,context).should == "any parent method should spit out warning: []"
+ ensure
+ $VERBOSE = old_verbose
+ end
+ end
+ end
end
end
context 'validating a value' do