oval 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,146 @@
1
+ require 'spec_helper'
2
+ require 'oval/hash_item'
3
+ require 'oval/instance_of'
4
+ require 'oval/kind_of'
5
+
6
+ describe Oval::HashItem do
7
+ context 'the class' do
8
+ it { described_class.should respond_to :[] }
9
+ it { described_class.should respond_to :validate_item_decl }
10
+ end
11
+ context 'an instance' do
12
+ let(:subject) { described_class[{:key_decl0 => :val_decl0}] }
13
+ it { should respond_to :validate }
14
+ it { should respond_to :key_decl }
15
+ it { should respond_to :val_decl }
16
+ it { subject.private_methods.map{|m| m.is_a?(Symbol) ? m : m.intern}.should include :item_decl= }
17
+ end
18
+
19
+ describe "[]" do
20
+ context "#{described_class.name}[{:key_decl0 => :val_decl0}]" do
21
+ it "should == new({:key_decl0 => :val_decl0})" do
22
+ described_class.stubs(:new).once.with({:key_decl0 => :val_decl0}).returns :ok
23
+ described_class[{:key_decl0 => :val_decl0}].should be :ok
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "validate_item_decl" do
29
+ [ :foo, [], [:a, :b], {:a => :A, :b => :B}, {}, nil ].each do |decl|
30
+ context "validate_item_decl(#{decl.inspect})" do
31
+ let(:decl) { decl }
32
+ let(:msg) { "Invalid item declaration #{decl.inspect}. Should be one-element Hash of type { key_decl => value_decl }"}
33
+ it { expect { described_class.validate_item_decl(decl) }.to raise_error Oval::DeclError, msg }
34
+ end
35
+ end
36
+ [ {:a => :A}, {Oval::InstanceOf[String] => Oval::KindOf[Symbol] } ].each do |decl|
37
+ context "validate_item_decl(#{decl.inspect})" do
38
+ let(:decl) { decl }
39
+ it { expect { described_class.validate_item_decl(decl) }.to_not raise_error }
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "#key_decl" do
45
+ let(:subject) { described_class[{:key_decl0 => :val_decl0}] }
46
+ context "#{described_class.name}[{:key_decl0 => :val_decl0}].key_decl" do
47
+ it { subject.key_decl.should be :key_decl0 }
48
+ end
49
+ context "when @key_decl = :key_decl1" do
50
+ before { subject.instance_variable_set(:@key_decl, :key_decl1) }
51
+ it { subject.key_decl.should be :key_decl1 }
52
+ end
53
+ end
54
+
55
+ describe "#val_decl" do
56
+ let(:subject) { described_class[:val_decl0] }
57
+ let(:subject) { described_class[{:key_decl0 => :val_decl0}] }
58
+ context "#{described_class.name}[{:key_decl0 => :val_decl0}].key_decl" do
59
+ it { subject.val_decl.should be :val_decl0 }
60
+ end
61
+ context "when @val_decl = :val_decl1" do
62
+ before { subject.instance_variable_set(:@val_decl, :val_decl1) }
63
+ it { subject.val_decl.should be :val_decl1 }
64
+ end
65
+ end
66
+
67
+ describe "#item_decl=" do
68
+ let(:subject) { described_class[:item_decl0] }
69
+ before do
70
+ described_class.any_instance.stubs(:item_decl=).once.with(:item_decl0)
71
+ subject
72
+ described_class.any_instance.unstub(:item_decl=)
73
+ end
74
+ context "item_decl = {:key_decl1 => :val_decl1}" do
75
+ before do
76
+ subject
77
+ described_class.expects(:validate_item_decl).once.with({:key_decl1 => :val_decl1})
78
+ subject.send(:item_decl=, {:key_decl1 => :val_decl1})
79
+ end
80
+ it "should assign @key_decl = :key_decl1" do
81
+ subject.instance_variable_get(:@key_decl).should be :key_decl1
82
+ end
83
+ it "should assign @val_decl = :val_decl1" do
84
+ subject.instance_variable_get(:@val_decl).should be :val_decl1
85
+ end
86
+ end
87
+ context "item_decl = Oval::Anything" do
88
+ it "should not invoke validate_item_decl" do
89
+ described_class.stubs(:validate_item_decl).never
90
+ subject.send(:item_decl=, Oval::Anything)
91
+ end
92
+ it "should assign key_decl=Oval::Anything[]" do
93
+ subject.send(:item_decl=, Oval::Anything)
94
+ subject.key_decl.should be Oval::Anything[]
95
+ end
96
+ it "should assign val_decl=Oval::Anything[]" do
97
+ subject.send(:item_decl=, Oval::Anything)
98
+ subject.val_decl.should be Oval::Anything[]
99
+ end
100
+ end
101
+ context "item_decl = Oval::Anything[]" do
102
+ it "should not invoke validate_item_decl" do
103
+ described_class.stubs(:validate_item_decl).never
104
+ subject.send(:item_decl=, Oval::Anything[])
105
+ end
106
+ it "should assign key_decl=Oval::Anything[]" do
107
+ subject.send(:item_decl=, Oval::Anything[])
108
+ subject.key_decl.should be Oval::Anything[]
109
+ end
110
+ it "should assign val_decl=Oval::Anything[]" do
111
+ subject.send(:item_decl=, Oval::Anything[])
112
+ subject.val_decl.should be Oval::Anything[]
113
+ end
114
+ end
115
+ end
116
+
117
+ describe "#validate" do
118
+ # Valid items
119
+ [
120
+ [ {:foo => :bar},[[:foo,:bar], 0] ],
121
+ [ {:foo => :bar},[[:foo,:bar], 0, 'sh'] ],
122
+ [ {Oval::InstanceOf[String] => Oval::InstanceOf[Symbol]},[['foo',:bar], 0, 'hash'] ],
123
+ ].each do |item_decl,args|
124
+ context "#{described_class.name}[#{item_decl.inspect}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
125
+ let(:subject) { described_class[item_decl] }
126
+ let(:args) { args }
127
+ it { expect { subject.validate(*args) }.to_not raise_error }
128
+ end
129
+ end
130
+ [
131
+ [ {:foo => :bar},[[:geez,:bar], 0], "Invalid value :geez. Should be equal :foo" ],
132
+ [ {:foo => :bar},[[:foo,:geez], 1], "Invalid value :geez. Should be equal :bar" ],
133
+ [ {:foo => :bar},[[:geez,:bar], 0,'hash'], "Invalid value :geez for hash key. Should be equal :foo" ],
134
+ [ {:foo => :bar},[[:foo,:geez], 1,'hash'], "Invalid value :geez for hash[:foo]. Should be equal :bar" ],
135
+ [ {Oval::InstanceOf[String] => Oval::InstanceOf[Symbol]},[[:foo,:bar], 0, 'hash'], "Invalid object :foo of type Symbol for hash key. Should be an instance of String"],
136
+ [ {Oval::InstanceOf[String] => Oval::InstanceOf[Symbol]},[['foo','bar'], 0, 'hash'], "Invalid object \"bar\" of type String for hash[\"foo\"]. Should be an instance of Symbol"],
137
+ ].each do |item_decl,args,msg|
138
+ context "#{described_class.name}[#{item_decl.inspect}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
139
+ let(:subject) { described_class[item_decl] }
140
+ let(:args) { args }
141
+ let(:msg) { msg }
142
+ it { expect { subject.validate(*args) }.to raise_error Oval::ValueError, msg }
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+ require 'oval/instance_of'
3
+
4
+ describe Oval::InstanceOf do
5
+ describe 'an instance' do
6
+ let(:subject) { described_class[:klass] }
7
+ before { described_class.any_instance.stubs(:validate_class) }
8
+ it { should respond_to :validate }
9
+ end
10
+
11
+ context "#{described_class.name}[:symbol1]" do
12
+ let(:subject) { described_class[:symbol1] }
13
+ let(:msg) { "Invalid class :symbol1 for InstanceOf" }
14
+ it { expect { subject }.to raise_error Oval::DeclError, msg }
15
+ end
16
+
17
+ describe "#validate" do
18
+ [
19
+ [Array, []],
20
+ [String, "hello"],
21
+ [Fixnum, 1],
22
+ ].each do |klass,object|
23
+ context "#{described_class.name}[#{klass.name}].validate(#{object.inspect})" do
24
+ let(:subject) { described_class[klass] }
25
+ let(:object) { object }
26
+ it { expect { subject.validate(object) }.to_not raise_error }
27
+ end
28
+ end
29
+ [
30
+ [Array, {}],
31
+ [String, :symbol1],
32
+ [Fixnum, "hello"],
33
+ [Integer, 1],
34
+ [Object, :obj]
35
+ ].each do |klass,object|
36
+ context "#{described_class.name}[#{klass.name}].validate(#{object.inspect})" do
37
+ let(:subject) { described_class[klass] }
38
+ let(:object) { object }
39
+ let(:msg) { "Invalid object #{object.inspect} of type #{object.class.name}. Should be an instance of #{klass.name}" }
40
+ it { expect { subject.validate(object) }.to raise_error msg }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'oval/kind_of'
3
+
4
+ describe Oval::KindOf do
5
+ describe 'an instance' do
6
+ let(:subject) { described_class[:klass] }
7
+ before { described_class.any_instance.stubs(:validate_class) }
8
+ it { should respond_to :validate }
9
+ end
10
+
11
+ context "#{described_class.name}[:symbol1]" do
12
+ let(:subject) { described_class[:symbol1] }
13
+ let(:msg) { "Invalid class :symbol1 for KindOf" }
14
+ it { expect { subject }.to raise_error Oval::DeclError, msg }
15
+ end
16
+
17
+ describe "#validate" do
18
+ [
19
+ [Array, []],
20
+ [String, "hello"],
21
+ [Fixnum, 1],
22
+ [Integer, 1],
23
+ [Object, :symbol]
24
+ ].each do |klass,object|
25
+ context "#{described_class.name}[#{klass.name}].validate(#{object.inspect})" do
26
+ let(:subject) { described_class[klass] }
27
+ let(:object) { object }
28
+ it { expect { subject.validate(object) }.to_not raise_error }
29
+ end
30
+ end
31
+ [
32
+ [Array, {}],
33
+ [String, :symbol1],
34
+ [Fixnum, "hello"],
35
+ [Integer, []],
36
+ ].each do |klass,object|
37
+ context "#{described_class.name}[#{klass.name}].validate(#{object.inspect})" do
38
+ let(:subject) { described_class[klass] }
39
+ let(:object) { object }
40
+ let(:msg) { "Invalid object #{object.inspect} of type #{object.class.name}. Should be a kind of #{klass.name}" }
41
+ it { expect { subject.validate(object) }.to raise_error msg }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'oval/one_of'
3
+
4
+ describe Oval::OneOf do
5
+ context 'the class' do
6
+ it { described_class.should respond_to :[] }
7
+ end
8
+ context 'an instance' do
9
+ it { should respond_to :validate }
10
+ it { should respond_to :decls }
11
+ end
12
+
13
+ describe "[]" do
14
+ [[], [:arg1], [:arg1, :arg2], [:arg1, :arg2, :arg3] ].each do |args|
15
+ context "[#{args.map{|x| x.inspect}.join(', ')}]" do
16
+ let(:args) { args }
17
+ it "should == new(#{args.map{|x| x.inspect}.join(', ')})" do
18
+ described_class.expects(:new).once.with(*args).returns :ok
19
+ described_class[*args].should be :ok
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "new" do
26
+ [[], [:arg1], [:arg1, :arg2], [:arg1, :arg2, :arg3] ].each do |args|
27
+ context "new(#{args.map{|x| x.inspect}.join(', ')})" do
28
+ let(:args) { args }
29
+ it "should invoke decls = #{args.inspect}" do
30
+ described_class.any_instance.expects(:decls=).once.with(args)
31
+ expect { described_class.new(*args) }.to_not raise_error
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "#validate" do
38
+ [
39
+ [ [:foo ], [:foo] ],
40
+ [ [:foo, :bar], [:bar] ],
41
+ [ [:foo, :bar], [:bar,'subj'] ],
42
+ ].each do |decls,args|
43
+ context "#{described_class}[#{decls.map{|x| x.inspect}.join(', ')}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
44
+ let(:args) { args }
45
+ let(:subject) { described_class[*decls] }
46
+ it { expect { subject.validate(*args) }.to_not raise_error }
47
+ end
48
+ end
49
+ [
50
+ [ [:foo ], [:bar], "Invalid value :bar" ],
51
+ [ [:foo, :bar], [:geez], "Invalid value :geez" ],
52
+ [ [:foo, :bar], [:geez,'subj'], "Invalid value :geez for subj" ],
53
+ ].each do |decls,args,msg|
54
+ context "#{described_class}[#{decls.map{|x| x.inspect}.join(', ')}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
55
+ let(:args) { args }
56
+ let(:subject) { described_class[*decls] }
57
+ let(:msg) { msg }
58
+ it { expect { subject.validate(*args) }.to raise_error Oval::ValueError, msg }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,215 @@
1
+ require 'spec_helper'
2
+ require 'oval/options'
3
+
4
+ describe Oval::Options do
5
+ context "the class" do
6
+ [
7
+ :validate_decl,
8
+ :validate_option_name_decl,
9
+ ].each do |method|
10
+ it { described_class.should respond_to method }
11
+ end
12
+ end
13
+
14
+ context "an instance" do
15
+ let(:subject) { described_class[:decl0] }
16
+ before { described_class.stubs(:validate_decl).once.with(:decl0) }
17
+ [
18
+ :validate,
19
+ :validate_option,
20
+ :validate_option_name,
21
+ :validate_option_value,
22
+ ].each do |method|
23
+ it { should respond_to method }
24
+ end
25
+ end
26
+
27
+ describe "#validate" do
28
+ context "validate(:symbol)" do
29
+ let(:subject) { described_class[:decl0] }
30
+ let(:msg) { "Invalid options :symbol of type Symbol. Should be a Hash" }
31
+ before do
32
+ described_class.stubs(:validate_decl).once.with(:decl0)
33
+ subject.stubs(:validate_option).never
34
+ end
35
+ it { expect { subject.validate(:symbol) }.to raise_error Oval::ValueError, msg }
36
+ end
37
+ [
38
+ {},
39
+ {:a => :A},
40
+ {:a => :A, :b => :B},
41
+ ].each do |options|
42
+ context "validate(#{options.inspect},:subj1)" do
43
+ let(:options) { options }
44
+ let(:subject) { described_class[:decl1] }
45
+ before do
46
+ described_class.expects(:validate_decl).once.with(:decl1)
47
+ options.each do |name,value|
48
+ subject.expects(:validate_option).once.with(name, value, :subj1)
49
+ end
50
+ end
51
+ it "should invoke validate_option(key,value,:subj1) for each key,value in #{options.inspect}" do
52
+ expect { subject.validate(options,:subj1) }.to_not raise_error
53
+ end
54
+ end
55
+ end
56
+ context "validate({:a => :A})" do
57
+ let(:subject) { described_class[:decl1] }
58
+ before { described_class.expects(:validate_decl).once.with(:decl1) }
59
+ it "should invoke validate_option(:a,:A,nil) once" do
60
+ subject.expects(:validate_option).once.with(:a, :A, nil)
61
+ expect { subject.validate({:a => :A}) }.to_not raise_error
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#validate_option" do
67
+ before { described_class.expects(:validate_decl).once.with(:decl1) }
68
+ let(:subject) { described_class[:decl1] }
69
+ context "validate_option(:name1,:value1,:subj1)" do
70
+ it "should invoke validate_option_name(name1,:subj1) and validate_option_value(:value,:name1,:subj1) once" do
71
+ subject.expects(:validate_option_name).once.with(:name1,:subj1)
72
+ subject.expects(:validate_option_value).once.with(:value1,:name1,:subj1)
73
+ expect { subject.send(:validate_option,:name1,:value1,:subj1) }.to_not raise_error
74
+ end
75
+ end
76
+ context "validate_option(:name1,:value1)" do
77
+ it "should invoke validate_option_name(name1,nil) and validate_option_value(:value,:name1,nil) once" do
78
+ subject.expects(:validate_option_name).once.with(:name1,nil)
79
+ subject.expects(:validate_option_value).once.with(:value1,:name1,nil)
80
+ expect { subject.send(:validate_option,:name1,:value1) }.to_not raise_error
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#validate_option_name" do
86
+ [
87
+ [
88
+ {:foo => :X, :bar => :Y},
89
+ ":bar and :foo",
90
+ [ :foo, :bar ],
91
+ [ :geez ],
92
+ ],
93
+ [
94
+ {:foo => :X, :bar => :Y, :geez => :Z},
95
+ ":bar, :foo and :geez",
96
+ [ :foo, :bar, :geez ],
97
+ [ :dood ],
98
+ ],
99
+ ].each do |decl, enumerated, valid, invalid|
100
+ context "on Options[#{decl.inspect}]" do
101
+ before { described_class.expects(:validate_decl).once.with(decl) }
102
+ let(:subject) { described_class[decl] }
103
+ let(:msg1) { "Invalid option #{name.inspect}. Allowed options are #{enumerated}"}
104
+ let(:msg2) { "Invalid option #{name.inspect} for subj1. Allowed options are #{enumerated}"}
105
+ valid.each do |name|
106
+ context "validate_option_name(#{name.inspect})" do
107
+ let(:name) { name }
108
+ it { expect { subject.send(:validate_option_name, name) }.to_not raise_error }
109
+ end
110
+ end
111
+ invalid.each do |name|
112
+ context "validate_option_name(#{name.inspect})" do
113
+ let(:name) { name }
114
+ it { expect { subject.send(:validate_option_name, name) }.to raise_error Oval::ValueError, msg1}
115
+ end
116
+ context "validate_option_name(#{name.inspect},'subj1')" do
117
+ let(:name) { name }
118
+ it { expect { subject.send(:validate_option_name, name, 'subj1') }.to raise_error Oval::ValueError, msg2}
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ describe "#validate_option_value" do
126
+ [
127
+ [
128
+ {:foo => :X, :bar => :Y},
129
+ [ :V, :bar ],
130
+ [ :V, :Y, nil ],
131
+ ],
132
+ [
133
+ {:foo => :X, :bar => :Y, :geez => :Z},
134
+ [ :V, :geez, 'subj1'],
135
+ [ :V, :Z, 'subj1[:geez]'],
136
+ ],
137
+ ].each do |decl, args, args2|
138
+ context "on Options[#{decl.inspect}]" do
139
+ before { described_class.expects(:validate_decl).once.with(decl) }
140
+ let(:subject) { described_class[decl] }
141
+ context "validate_option_value(#{args.map{|x| x.inspect}.join(', ')})" do
142
+ let(:name) { name }
143
+ it "should invoke self.class.ensure_match(#{args2.map{|x| x.inspect}.join(', ')}) once" do
144
+ described_class.stubs(:ensure_match).once.with(*args2)
145
+ expect { subject.send(:validate_option_value,*args) }.to_not raise_error
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ describe "#validate_decl" do
153
+ context "validate_decl({:foo => :F, :bar => 4})" do
154
+ it { expect { described_class.validate_decl({:foo => :F, :bar => 4}) }.to_not raise_error }
155
+ end
156
+ context "validate_decl(:decl1)" do
157
+ let(:msg) { "Invalid declaration :decl1 of type Symbol. Should be a Hash" }
158
+ it { expect { described_class.validate_decl(:decl1) }.to raise_error Oval::DeclError, msg }
159
+ end
160
+ [
161
+ {},
162
+ {:one => 1},
163
+ {:one => 1, :two => "Two"},
164
+ {:one => "One", :two => 2, :three => [1,2,3]}
165
+ ].each do |decl|
166
+ context "validate_decl(#{decl.inspect})" do
167
+ let(:decl) { decl }
168
+ before { decl.keys.each {|key| described_class.expects(:validate_option_name_decl).once.with(key) } }
169
+ it "calls validate_option_name_decl(key) once for each key from #{decl.inspect} " do
170
+ expect { described_class.validate_decl(decl) }.to_not raise_error
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "validate_option_name_decl" do
177
+ [ :one, 'two', 3 ].each do |decl|
178
+ context "validate_option_name_decl(#{decl.inspect})" do
179
+ let(:decl) { decl }
180
+ it { expect { described_class.validate_option_name_decl(decl) }.to_not raise_error }
181
+ end
182
+ end
183
+ end
184
+
185
+
186
+ describe "decl" do
187
+ before { described_class.stubs(:validate_decl) }
188
+ context "new(:decl0).decl" do
189
+ it { described_class.new(:decl0).decl.should be :decl0 }
190
+ end
191
+ context "when @decl == :decl1" do
192
+ let(:subject) { described_class.new(:decl0) }
193
+ before { subject.instance_variable_set(:@decl,:decl1) }
194
+ it { subject.decl.should be :decl1 }
195
+ end
196
+ end
197
+
198
+ describe "#decl=" do
199
+ # first, stub validate_decl to create dummy object
200
+ before { described_class.stubs(:validate_decl) }
201
+ let(:subject) { described_class.new(:decl0) }
202
+ context "#decl = :decl1" do
203
+ it "should call self.class.validate_decl(:decl1) once" do
204
+ subject # reference before re-stubbing validate_decl
205
+ described_class.stubs(:validate_decl).never
206
+ described_class.stubs(:validate_decl).once.with(:decl1)
207
+ expect { subject.send(:decl=,:decl1) }.to_not raise_error
208
+ end
209
+ it "should assign @decl = :decl1" do
210
+ subject.send(:decl=, :decl1)
211
+ subject.instance_variable_get(:@decl).should be :decl1
212
+ end
213
+ end
214
+ end
215
+ end