This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
/
property_spec.rb
132 lines (111 loc) · 3.67 KB
/
property_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
share_examples_for 'A semipublic Property' do
before :all do
%w[ @type @name @value @other_value ].each do |ivar|
raise "+#{ivar}+ should be defined in before block" unless instance_variable_defined?(ivar)
end
module ::Blog
class Article
include DataMapper::Resource
property :id, Serial
end
end
@model = Blog::Article
@options ||= {}
@property = @type.new(@model, @name, @options)
end
describe '.new' do
describe 'when provided no options' do
it 'should return a Property' do
@property.should be_kind_of(@type)
end
it 'should set the load_as' do
@property.load_as.should be(@type.load_as)
end
it 'should set the model' do
@property.model.should equal(@model)
end
it 'should set the options to the default' do
@property.options.should == @type.options.merge(@options)
end
end
[ :index, :unique_index, :unique, :lazy ].each do |attribute|
[ true, false, :title, [ :title ] ].each do |value|
describe "when provided #{(options = { attribute => value }).inspect}" do
before :all do
@property = @type.new(@model, @name, @options.merge(options))
end
it 'should return a Property' do
@property.should be_kind_of(@type)
end
it 'should set the model' do
@property.model.should equal(@model)
end
it 'should set the load_as' do
@property.load_as.should be(@type.load_as)
end
it "should set the options to #{options.inspect}" do
@property.options.should == @type.options.merge(@options.merge(options))
end
end
end
[ [], nil ].each do |value|
describe "when provided #{(invalid_options = { attribute => value }).inspect}" do
it 'should raise an exception' do
lambda {
@type.new(@model, @name, @options.merge(invalid_options))
}.should raise_error(ArgumentError, "options[#{attribute.inspect}] must be either true, false, a Symbol or an Array of Symbols")
end
end
end
end
end
describe '#load' do
before :all do
@value = mock(@value)
end
subject { @property.load(@value) }
describe 'with a property' do
it 'should delegate to #typecast' do
return_value = mock(@other_value)
@property.should_receive(:typecast).with(@value).and_return(return_value)
should == return_value
end
end
end
describe "#typecast" do
describe 'when value is nil' do
it 'returns value unchanged' do
@property.typecast(nil).should be(nil)
end
describe 'when value is a Ruby primitive' do
it 'returns value unchanged' do
@property.typecast(@value).should == @value
end
end
end
end
describe '#valid?' do
describe 'when provided a valid value' do
it 'should return true' do
@property.valid?(@value).should be(true)
end
end
describe 'when provide an invalid value' do
it 'should return false' do
@property.valid?(@invalid_value).should be(false)
end
end
describe 'when provide a nil value when required' do
it 'should return false' do
@property = @type.new(@model, @name, @options.merge(:required => true))
@property.valid?(nil).should be(false)
end
end
describe 'when provide a nil value when not required' do
it 'should return false' do
@property = @type.new(@model, @name, @options.merge(:required => false))
@property.valid?(nil).should be(true)
end
end
end
end