spec/attribute_spec.rb in attributor-2.6.0 vs spec/attribute_spec.rb in attributor-2.6.1
- old
+ new
@@ -2,11 +2,11 @@
describe Attributor::Attribute do
let(:attribute_options) { Hash.new }
- let(:type) { AttributeType }
+ let(:type) { Attributor::String }
subject(:attribute) { Attributor::Attribute.new(type, attribute_options) }
let(:context) { ["context"] }
let(:value) { "one" }
@@ -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, id: type.id}}
+ h = {type: {name: 'String', id: type.id, family: type.family}}
common = attribute_options.select{|k,v| Attributor::Attribute::TOP_LEVEL_OPTIONS.include? k }
h.merge!( common )
h[:options] = {:min => 0 }
h
end
@@ -60,10 +60,20 @@
it 'should have the example value in the :example_definition key' do
subject.describe[:example_definition].should == "ex_def"
end
end
+ context 'with custom_data' do
+ let(:custom_data) { {loggable: true, visible_in_ui: false} }
+ let(:attribute_options) { {custom_data: custom_data} }
+ its(:describe) { should have_key(:custom_data) }
+
+ it 'keep the custom data attribute' do
+ subject.describe[:custom_data].should == custom_data
+ end
+ end
+
context 'for an anonymous type (aka: Struct)' do
let(:attribute_options) { Hash.new }
let(:attribute) do
Attributor::Attribute.new(Struct, attribute_options) do
attribute :id, Integer
@@ -108,10 +118,23 @@
expect {
Attributor::Attribute.new(Integer, default: "not an okay integer")
}.to raise_error(/Default value doesn't have the correct attribute type/)
end
+ context 'custom_data' do
+ it 'raises when not a hash' do
+ expect {
+ Attributor::Attribute.new(Integer, custom_data: 1)
+ }.to raise_error(/custom_data must be a Hash/)
+ end
+
+ it 'does not raise for hashes' do
+ expect {
+ Attributor::Attribute.new(Integer, custom_data: {loggable: true})
+ }.not_to raise_error
+ end
+ end
end
context 'example' do
let(:example) { nil }
@@ -143,11 +166,11 @@
example.should_receive(:gen).and_call_original
subject.example.should =~ example
end
context 'for a type with a non-String native_type' do
- let(:type) { IntegerAttributeType}
+ let(:type) { Attributor::Integer }
context 'using a regexp' do
let(:example) { /\d{5}/ }
it 'coerces the example value properly' do
example.should_receive(:gen).and_call_original
type.should_receive(:load).and_call_original
@@ -226,9 +249,10 @@
example_2 = subject.example(['different context'])
example_1.should_not eq example_2
end
+
end
end
context 'load' do
let(:context){ ['context'] }