oval 0.0.4 → 0.0.5
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.
- data/CHANGELOG +5 -0
- data/Modulefile +1 -1
- data/README.md +20 -0
- data/lib/oval.rb +7 -0
- data/lib/oval/anything.rb +1 -0
- data/lib/oval/array_item.rb +5 -1
- data/lib/oval/base.rb +16 -1
- data/lib/oval/collection.rb +5 -2
- data/lib/oval/hash_item.rb +7 -2
- data/lib/oval/instance_of.rb +3 -0
- data/lib/oval/kind_of.rb +3 -0
- data/lib/oval/match.rb +40 -0
- data/lib/oval/one_of.rb +14 -2
- data/lib/oval/options.rb +1 -1
- data/lib/oval/subclass_of.rb +3 -0
- data/oval.gemspec +1 -1
- data/spec/unit/oval/anything_spec.rb +14 -0
- data/spec/unit/oval/array_item_spec.rb +34 -6
- data/spec/unit/oval/base_spec.rb +10 -10
- data/spec/unit/oval/class_decl_base_spec.rb +5 -6
- data/spec/unit/oval/collection_spec.rb +27 -27
- data/spec/unit/oval/hash_item_spec.rb +41 -0
- data/spec/unit/oval/instance_of_spec.rb +14 -0
- data/spec/unit/oval/kind_of_spec.rb +14 -0
- data/spec/unit/oval/match_spec.rb +105 -0
- data/spec/unit/oval/one_of_spec.rb +24 -3
- data/spec/unit/oval/options_spec.rb +6 -2
- data/spec/unit/oval/subclass_of_spec.rb +10 -0
- data/spec/unit/oval_spec.rb +17 -0
- metadata +6 -3
data/CHANGELOG
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
2014-02-03 Pawel Tomulik <ptomulik@meil.pw.edu.pl>
|
2
|
+
* release 0.0.5
|
3
|
+
* refactored ensure_match -> validate
|
4
|
+
* added Oval::Match declarator
|
5
|
+
* fixed error message in OneOf to be more verbose
|
6
|
+
* added it_should method to declarators
|
2
7
|
* release 0.0.4
|
3
8
|
2014-02-02 Pawel Tomulik <ptomulik@meil.pw.edu.pl>
|
4
9
|
* corrections to README.md
|
data/Modulefile
CHANGED
data/README.md
CHANGED
@@ -248,6 +248,26 @@ document all the core non-terminal declarators implemented in **Oval**.
|
|
248
248
|
end
|
249
249
|
```
|
250
250
|
|
251
|
+
####<a id="ov\_match"></a>ov\_match
|
252
|
+
|
253
|
+
- Declaration
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
ov_match[re]
|
257
|
+
```
|
258
|
+
|
259
|
+
- Validation - permits only values matching regular expression **re**,
|
260
|
+
- Allowed values for **re** - must be a kind of `Regexp`.
|
261
|
+
- Example
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
# Only valid identifiers are allowed as :bar option
|
265
|
+
ov = ov_options[ :bar => ov_match[/^[a-z_]\w+$/] ]
|
266
|
+
def foo(ops = {})
|
267
|
+
ov.validate(ops,'ops')
|
268
|
+
end
|
269
|
+
```
|
270
|
+
|
251
271
|
####<a id="ov\_one\_of"></a>ov\_one\_of
|
252
272
|
|
253
273
|
- Declaration
|
data/lib/oval.rb
CHANGED
@@ -3,6 +3,7 @@ module Oval
|
|
3
3
|
require 'oval/collection'
|
4
4
|
require 'oval/instance_of'
|
5
5
|
require 'oval/kind_of'
|
6
|
+
require 'oval/match'
|
6
7
|
require 'oval/one_of'
|
7
8
|
require 'oval/options'
|
8
9
|
require 'oval/subclass_of'
|
@@ -12,7 +13,13 @@ module Oval
|
|
12
13
|
def ov_collection; Oval::Collection; end
|
13
14
|
def ov_instance_of; Oval::InstanceOf; end
|
14
15
|
def ov_kind_of; Oval::KindOf; end
|
16
|
+
def ov_match; Oval::Match; end
|
15
17
|
def ov_one_of; Oval::OneOf; end
|
16
18
|
def ov_options; Oval::Options; end
|
17
19
|
def ov_subclass_of; Oval::SubclassOf; end
|
20
|
+
|
21
|
+
module_function
|
22
|
+
def validate(thing, decl, subject = nil)
|
23
|
+
Oval::Base.validate(thing, decl, subject)
|
24
|
+
end
|
18
25
|
end
|
data/lib/oval/anything.rb
CHANGED
data/lib/oval/array_item.rb
CHANGED
@@ -4,7 +4,11 @@ class Oval::ArrayItem < Oval::Base
|
|
4
4
|
|
5
5
|
def validate(item, i, subject = nil)
|
6
6
|
item_subject = subject.nil? ? nil : "#{subject}[#{i}]"
|
7
|
-
self.class.
|
7
|
+
self.class.validate(item,item_decl,item_subject)
|
8
|
+
end
|
9
|
+
|
10
|
+
def it_should
|
11
|
+
self.class.it_should(item_decl)
|
8
12
|
end
|
9
13
|
|
10
14
|
def self.[](item_decl)
|
data/lib/oval/base.rb
CHANGED
@@ -13,7 +13,7 @@ class Oval::Base
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.
|
16
|
+
def self.validate(thing, decl, subject = nil)
|
17
17
|
if decl.is_a? Oval::Base
|
18
18
|
decl.validate(thing,subject)
|
19
19
|
else
|
@@ -22,6 +22,17 @@ class Oval::Base
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
def self.it_should(decl)
|
26
|
+
if decl.is_a? Oval::Base
|
27
|
+
decl.it_should
|
28
|
+
elsif decl == Oval::Anything
|
29
|
+
Oval::Anything[].it_should
|
30
|
+
else
|
31
|
+
# "terminal symbol"
|
32
|
+
"be equal #{decl.inspect}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
25
36
|
def self.[](*args)#,subject = default_subject)
|
26
37
|
return new(*args)
|
27
38
|
end
|
@@ -30,6 +41,10 @@ class Oval::Base
|
|
30
41
|
raise NotImplementedError, "This method should be overwritten by a subclass"
|
31
42
|
end
|
32
43
|
|
44
|
+
def it_should()
|
45
|
+
raise NotImplementedError, "This method should be overwritten by a subclass"
|
46
|
+
end
|
47
|
+
|
33
48
|
def initialize(*args)
|
34
49
|
end
|
35
50
|
|
data/lib/oval/collection.rb
CHANGED
@@ -43,14 +43,17 @@ require 'oval/hash_item'
|
|
43
43
|
# ```
|
44
44
|
#
|
45
45
|
class Oval::Collection < Oval::Base
|
46
|
-
|
47
46
|
def validate(collection, subject = nil)
|
48
47
|
class_subject = subject.nil? ? nil : "#{subject}.class"
|
49
|
-
self.class.
|
48
|
+
self.class.validate(collection.class, class_decl, class_subject)
|
50
49
|
i = 0
|
51
50
|
collection.each { |item| item_validator.validate(item, i, subject); i+= 1}
|
52
51
|
end
|
53
52
|
|
53
|
+
def it_should
|
54
|
+
"be #{klass} whose item should #{self.class.it_should(item_validator)}"
|
55
|
+
end
|
56
|
+
|
54
57
|
def self.[](class_decl,item_decl = Oval::Anything[])
|
55
58
|
new(class_decl,item_decl)
|
56
59
|
end
|
data/lib/oval/hash_item.rb
CHANGED
@@ -6,8 +6,13 @@ class Oval::HashItem < Oval::Base
|
|
6
6
|
def validate(item, i, subject = nil)
|
7
7
|
key_subject = subject.nil? ? nil: "#{subject} key"
|
8
8
|
val_subject = subject.nil? ? nil: "#{subject}[#{item[0].inspect}]"
|
9
|
-
self.class.
|
10
|
-
self.class.
|
9
|
+
self.class.validate(item[0],key_decl,key_subject)
|
10
|
+
self.class.validate(item[1],val_decl,val_subject)
|
11
|
+
end
|
12
|
+
|
13
|
+
def it_should
|
14
|
+
"be {key => value} where key should #{self.class.it_should(key_decl)} " +
|
15
|
+
"and value should #{self.class.it_should(val_decl)}"
|
11
16
|
end
|
12
17
|
|
13
18
|
def self.[](item_decl)
|
data/lib/oval/instance_of.rb
CHANGED
data/lib/oval/kind_of.rb
CHANGED
data/lib/oval/match.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'oval/base'
|
2
|
+
|
3
|
+
class Oval::Match < Oval::Base
|
4
|
+
|
5
|
+
def validate(thing, subject = nil)
|
6
|
+
unless re.match(thing)
|
7
|
+
raise Oval::ValueError,
|
8
|
+
"Invalid value #{thing.inspect}#{for_subject(subject)}. " +
|
9
|
+
"Should #{it_should}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def it_should
|
14
|
+
"match #{re.inspect}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.[](re)
|
18
|
+
new(re)
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(re)
|
22
|
+
self.re = re
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :re
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def re=(re)
|
30
|
+
self.class.validate_re(re)
|
31
|
+
@re = re
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.validate_re(re)
|
35
|
+
unless re.is_a?(Regexp)
|
36
|
+
raise Oval::DeclError, "Invalid regular expression #{re.inspect}. " +
|
37
|
+
"Should be an instance of Regexp"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/oval/one_of.rb
CHANGED
@@ -12,14 +12,26 @@ class Oval::OneOf < Oval::Base
|
|
12
12
|
ok = false
|
13
13
|
self.decls.each do |decl|
|
14
14
|
begin
|
15
|
-
self.class.
|
15
|
+
self.class.validate(thing, decl)
|
16
16
|
ok = true
|
17
17
|
break
|
18
18
|
rescue Oval::ValueError
|
19
19
|
end
|
20
20
|
end
|
21
21
|
unless ok
|
22
|
-
raise Oval::ValueError,
|
22
|
+
raise Oval::ValueError,
|
23
|
+
"Invalid value #{thing.inspect}#{for_subject(subject)}. " +
|
24
|
+
"Should #{self.it_should}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def it_should
|
29
|
+
if decls.empty?
|
30
|
+
"be absent"
|
31
|
+
else
|
32
|
+
output = decls[0..-2].map{|k| self.class.it_should(k)}.join(', ')
|
33
|
+
output.empty? ? self.class.it_should(decls[0]) :
|
34
|
+
[output, self.class.it_should(decls[-1])].join(' or ')
|
23
35
|
end
|
24
36
|
end
|
25
37
|
|
data/lib/oval/options.rb
CHANGED
@@ -39,7 +39,7 @@ class Oval::Options < Oval::Base
|
|
39
39
|
|
40
40
|
def validate_option_value(value, name, subject = nil)
|
41
41
|
subject = "#{subject}[#{name.inspect}]" if subject
|
42
|
-
self.class.
|
42
|
+
self.class.validate(value, decl[name], subject)
|
43
43
|
end
|
44
44
|
|
45
45
|
def self.validate_decl(decl)
|
data/lib/oval/subclass_of.rb
CHANGED
data/oval.gemspec
CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |gem|
|
5
5
|
gem.name = 'oval'
|
6
|
-
gem.version = '0.0.
|
6
|
+
gem.version = '0.0.5'
|
7
7
|
gem.authors = ["Pawel Tomulik"]
|
8
8
|
gem.email = ["ptomulik@meil.pw.edu.pl"]
|
9
9
|
gem.description = %q{Validate options when passed to methods}
|
@@ -2,26 +2,36 @@ require 'spec_helper'
|
|
2
2
|
require 'oval/anything'
|
3
3
|
describe Oval::Anything do
|
4
4
|
let(:subject) { described_class.instance }
|
5
|
+
|
6
|
+
it "should be subclass of Oval::Base" do
|
7
|
+
described_class.should < Oval::Base
|
8
|
+
end
|
9
|
+
|
5
10
|
context 'the class' do
|
6
11
|
it { described_class.should respond_to :instance }
|
7
12
|
it { described_class.should respond_to :[] }
|
8
13
|
it { described_class.should_not respond_to :new }
|
9
14
|
end
|
15
|
+
|
10
16
|
context 'an instance' do
|
11
17
|
it { should respond_to :validate }
|
18
|
+
it { should respond_to :it_should }
|
12
19
|
end
|
20
|
+
|
13
21
|
describe "instance (i.e. class method called instance)" do
|
14
22
|
it "should return singleton" do
|
15
23
|
previous = described_class.instance
|
16
24
|
described_class.instance.should be previous
|
17
25
|
end
|
18
26
|
end
|
27
|
+
|
19
28
|
describe "[]" do
|
20
29
|
it "should == #{described_class}.instance" do
|
21
30
|
described_class.stubs(:instance).once.with().returns :ok
|
22
31
|
described_class[].should be :ok
|
23
32
|
end
|
24
33
|
end
|
34
|
+
|
25
35
|
describe "#validate" do
|
26
36
|
# It should accept anything, which is quite hard to test, so I put here
|
27
37
|
# some random stuff ...
|
@@ -35,4 +45,8 @@ describe Oval::Anything do
|
|
35
45
|
end
|
36
46
|
end
|
37
47
|
end
|
48
|
+
|
49
|
+
describe "#it_should" do
|
50
|
+
its(:it_should) { should == "be anything" }
|
51
|
+
end
|
38
52
|
end
|
@@ -1,14 +1,20 @@
|
|
1
|
-
require 'spec_helper
|
2
|
-
require 'oval/array_item
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'oval/array_item'
|
3
3
|
|
4
4
|
describe Oval::ArrayItem do
|
5
|
+
it "should be subclass of Oval::Base" do
|
6
|
+
described_class.should < Oval::Base
|
7
|
+
end
|
8
|
+
|
5
9
|
context 'the class' do
|
6
10
|
it { described_class.should respond_to :[] }
|
7
11
|
end
|
12
|
+
|
8
13
|
context 'an instance' do
|
9
14
|
let(:subject) { described_class[:item_decl0] }
|
10
15
|
it { should respond_to :validate }
|
11
16
|
it { should respond_to :item_decl }
|
17
|
+
it { should respond_to :it_should }
|
12
18
|
end
|
13
19
|
|
14
20
|
describe "[]" do
|
@@ -44,17 +50,39 @@ describe Oval::ArrayItem do
|
|
44
50
|
describe "#validate" do
|
45
51
|
let(:subject) { described_class[:item_decl0] }
|
46
52
|
context "validate(:item1,0)" do
|
47
|
-
it 'should invoke self.class.
|
48
|
-
described_class.expects(:
|
53
|
+
it 'should invoke self.class.validate(:item1,:item_decl0,nil) once' do
|
54
|
+
described_class.expects(:validate).once.with(:item1,:item_decl0,nil)
|
49
55
|
subject.validate(:item1,0)
|
50
56
|
end
|
51
57
|
end
|
52
58
|
context "validate(:item1,0,'foo')" do
|
53
|
-
it 'should invoke self.class.
|
54
|
-
described_class.expects(:
|
59
|
+
it 'should invoke self.class.validate(:item1,:item_decl0,"foo[0]") once' do
|
60
|
+
described_class.expects(:validate).once.with(:item1,:item_decl0,"foo[0]")
|
55
61
|
subject.validate(:item1,0,'foo')
|
56
62
|
end
|
57
63
|
end
|
58
64
|
end
|
65
|
+
|
66
|
+
describe "#it_should" do
|
67
|
+
[
|
68
|
+
[:foo, "be equal :foo"],
|
69
|
+
[10, "be equal 10"],
|
70
|
+
[Class.new(Oval::Base) do
|
71
|
+
def it_should
|
72
|
+
"be a test value"
|
73
|
+
end
|
74
|
+
def inspect
|
75
|
+
'<Oval::Test>'
|
76
|
+
end
|
77
|
+
end.new,
|
78
|
+
"be a test value"]
|
79
|
+
].each do |item_decl,msg|
|
80
|
+
context "#{described_class}[#{item_decl.inspect}].it_should" do
|
81
|
+
let(:item_decl) { item_decl }
|
82
|
+
let(:msg) { msg }
|
83
|
+
it { described_class[item_decl].it_should.should == msg }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
59
87
|
end
|
60
88
|
|
data/spec/unit/oval/base_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe Oval::Base do
|
|
5
5
|
context "the class" do
|
6
6
|
[
|
7
7
|
:ensure_equal,
|
8
|
-
:
|
8
|
+
:validate,
|
9
9
|
:[],
|
10
10
|
:for_subject,
|
11
11
|
:enumerate,
|
@@ -23,31 +23,31 @@ describe Oval::Base do
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
describe "
|
27
|
-
context "
|
26
|
+
describe "validate" do
|
27
|
+
context "validate(:foo,:bar)" do
|
28
28
|
it "should invoke ensure_equal(:foo,:bar,nil) once" do
|
29
29
|
described_class.expects(:ensure_equal).once.with(:foo,:bar,nil)
|
30
|
-
expect { described_class.
|
30
|
+
expect { described_class.validate(:foo,:bar) }.to_not raise_error
|
31
31
|
end
|
32
32
|
end
|
33
|
-
context "
|
33
|
+
context "validate(:foo,:bar,'subj1')" do
|
34
34
|
it "should invoke ensure_equal(:foo,:bar,'subj1') once" do
|
35
35
|
described_class.expects(:ensure_equal).once.with(:foo,:bar,'subj1')
|
36
|
-
expect { described_class.
|
36
|
+
expect { described_class.validate(:foo,:bar,'subj1') }.to_not raise_error
|
37
37
|
end
|
38
38
|
end
|
39
|
-
context "
|
39
|
+
context "validate(:foo,shape)" do
|
40
40
|
let(:shape) { described_class[:shape0] }
|
41
41
|
it "should invoke shape.validate(:foo,nil) once" do
|
42
42
|
shape.expects(:validate).once.with(:foo,nil)
|
43
|
-
expect { described_class.
|
43
|
+
expect { described_class.validate(:foo,shape) }.to_not raise_error
|
44
44
|
end
|
45
45
|
end
|
46
|
-
context "
|
46
|
+
context "validate(:foo,shape,'subj1')" do
|
47
47
|
let(:shape) { described_class[:shape0] }
|
48
48
|
it "should invoke shape.validate(:foo,'subj1') once" do
|
49
49
|
shape.expects(:validate).once.with(:foo,'subj1')
|
50
|
-
expect { described_class.
|
50
|
+
expect { described_class.validate(:foo,shape,'subj1') }.to_not raise_error
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
@@ -2,6 +2,11 @@ require 'spec_helper'
|
|
2
2
|
require 'oval/class_decl_base'
|
3
3
|
|
4
4
|
describe Oval::ClassDeclBase do
|
5
|
+
|
6
|
+
it "should be subclass of Oval::Base" do
|
7
|
+
described_class.should < Oval::Base
|
8
|
+
end
|
9
|
+
|
5
10
|
context "the class" do
|
6
11
|
[
|
7
12
|
:[],
|
@@ -22,12 +27,6 @@ describe Oval::ClassDeclBase do
|
|
22
27
|
end
|
23
28
|
|
24
29
|
describe "#validate_class" do
|
25
|
-
## let(:subject) { described_class.new(:klass0) }
|
26
|
-
## before do
|
27
|
-
## described_class.stubs(:validate_class)
|
28
|
-
## subject
|
29
|
-
## described_class.unstub(:validate_class)
|
30
|
-
## end
|
31
30
|
context "validate_class(:symbol1)" do
|
32
31
|
let(:msg) { "Invalid class :symbol1 for ClassDeclBase" }
|
33
32
|
it { expect { described_class.send(:validate_class,:symbol1,Oval::ClassDeclBase) }.to raise_error Oval::DeclError, msg }
|
@@ -5,16 +5,22 @@ require 'oval/instance_of'
|
|
5
5
|
require 'oval/kind_of'
|
6
6
|
|
7
7
|
describe Oval::Collection do
|
8
|
+
it "should be subclass of Oval::Base" do
|
9
|
+
described_class.should < Oval::Base
|
10
|
+
end
|
11
|
+
|
8
12
|
describe 'the class' do
|
9
13
|
it { described_class.should respond_to :[] }
|
10
14
|
it { described_class.should respond_to :new }
|
11
15
|
it { described_class.should respond_to :validate_class_decl }
|
12
16
|
end
|
17
|
+
|
13
18
|
describe 'an instance' do
|
14
19
|
let(:subject) { described_class[Array,nil] }
|
15
20
|
it { should respond_to :validate }
|
16
21
|
it { should respond_to :class_decl }
|
17
22
|
it { should respond_to :item_decl }
|
23
|
+
it { should respond_to :it_should }
|
18
24
|
end
|
19
25
|
|
20
26
|
describe "[]" do
|
@@ -74,33 +80,6 @@ describe Oval::Collection do
|
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
77
|
-
## describe "validate_item_decl" do
|
78
|
-
## [
|
79
|
-
## [Hash, "Hash"],
|
80
|
-
## [Class.new(Hash), "Class.new(Hash)" ],
|
81
|
-
## ].each do |class_decl,class_desc|
|
82
|
-
## context "validate_item_decl(:decl1, #{class_desc})" do
|
83
|
-
## let(:class_decl) { class_decl }
|
84
|
-
## it "should invoke validate_hash_item_decl(:decl1) once" do
|
85
|
-
## described_class.stubs(:validate_hash_item_decl).once.with(:decl1)
|
86
|
-
## expect { described_class.validate_item_decl(:decl1,class_decl) }.to_not raise_error
|
87
|
-
## end
|
88
|
-
## end
|
89
|
-
## end
|
90
|
-
## [
|
91
|
-
## [Array, "Array"],
|
92
|
-
## [Class.new(Array), "Class.new(Array)" ],
|
93
|
-
## ].each do |class_decl,class_desc|
|
94
|
-
## context "validate_item_decl(:decl1, #{class_desc})" do
|
95
|
-
## let(:class_decl) { class_decl }
|
96
|
-
## it "should not invoke validate_hash_item_decl(:decl1)" do
|
97
|
-
## described_class.stubs(:validate_hash_item_decl).never
|
98
|
-
## expect { described_class.validate_item_decl(:decl1,class_decl) }.to_not raise_error
|
99
|
-
## end
|
100
|
-
## end
|
101
|
-
## end
|
102
|
-
## end
|
103
|
-
|
104
83
|
describe "#class_decl" do
|
105
84
|
before do
|
106
85
|
described_class.stubs(:validate_class_decl)
|
@@ -254,4 +233,25 @@ describe Oval::Collection do
|
|
254
233
|
end
|
255
234
|
end
|
256
235
|
end
|
236
|
+
|
237
|
+
describe "#it_should" do
|
238
|
+
[
|
239
|
+
[ [Array,Oval::Anything],
|
240
|
+
"be Array whose item should be anything" ],
|
241
|
+
[ [Array,nil],
|
242
|
+
"be Array whose item should be equal nil"],
|
243
|
+
[ [Array, Oval::InstanceOf[Fixnum]],
|
244
|
+
"be Array whose item should be an instance of Fixnum"],
|
245
|
+
[ [Hash, {:foo => :bar}],
|
246
|
+
"be Hash whose item should be {key => value} where key should be equal :foo and value should be equal :bar"],
|
247
|
+
[ [Hash, {Oval::InstanceOf[Symbol] => Oval::KindOf[Numeric]}],
|
248
|
+
"be Hash whose item should be {key => value} where key should be an instance of Symbol and value should be a kind of Numeric"],
|
249
|
+
].each do |decls,msg|
|
250
|
+
context "#{described_class.name}[#{decls.map{|x| x.inspect}.join(', ')}].it_should" do
|
251
|
+
let(:decls) { decls }
|
252
|
+
let(:msg) { msg }
|
253
|
+
it { described_class[*decls].it_should.should == msg}
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
257
|
end
|
@@ -4,16 +4,22 @@ require 'oval/instance_of'
|
|
4
4
|
require 'oval/kind_of'
|
5
5
|
|
6
6
|
describe Oval::HashItem do
|
7
|
+
it "should be subclass of Oval::Base" do
|
8
|
+
described_class.should < Oval::Base
|
9
|
+
end
|
10
|
+
|
7
11
|
context 'the class' do
|
8
12
|
it { described_class.should respond_to :[] }
|
9
13
|
it { described_class.should respond_to :validate_item_decl }
|
10
14
|
end
|
15
|
+
|
11
16
|
context 'an instance' do
|
12
17
|
let(:subject) { described_class[{:key_decl0 => :val_decl0}] }
|
13
18
|
it { should respond_to :validate }
|
14
19
|
it { should respond_to :key_decl }
|
15
20
|
it { should respond_to :val_decl }
|
16
21
|
it { subject.private_methods.map{|m| m.is_a?(Symbol) ? m : m.intern}.should include :item_decl= }
|
22
|
+
it { should respond_to :it_should }
|
17
23
|
end
|
18
24
|
|
19
25
|
describe "[]" do
|
@@ -143,4 +149,39 @@ describe Oval::HashItem do
|
|
143
149
|
end
|
144
150
|
end
|
145
151
|
end
|
152
|
+
|
153
|
+
describe "#it_should" do
|
154
|
+
[
|
155
|
+
[{:foo => :bar}, 'be equal :foo', 'be equal :bar' ],
|
156
|
+
[{'x' => 10}, 'be equal "x"', 'be equal 10' ],
|
157
|
+
[
|
158
|
+
{
|
159
|
+
Class.new(Oval::Base) do
|
160
|
+
def it_should
|
161
|
+
'be the test1 value'
|
162
|
+
end
|
163
|
+
def inspect
|
164
|
+
'Oval::Test1'
|
165
|
+
end
|
166
|
+
end.new =>
|
167
|
+
Class.new(Oval::Base) do
|
168
|
+
def it_should
|
169
|
+
'be the test2 value'
|
170
|
+
end
|
171
|
+
def inspect
|
172
|
+
'Oval::Test2'
|
173
|
+
end
|
174
|
+
end.new
|
175
|
+
},
|
176
|
+
'be the test1 value',
|
177
|
+
'be the test2 value',
|
178
|
+
]
|
179
|
+
].each do |item_decl,key_should, val_should|
|
180
|
+
context "#{described_class}[#{item_decl.inspect}].it_should" do
|
181
|
+
let(:item_decl) { item_decl }
|
182
|
+
let(:msg) { "be {key => value} where key should #{key_should} and value should #{val_should}" }
|
183
|
+
it { described_class[item_decl].it_should.should == msg }
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
146
187
|
end
|
@@ -2,10 +2,15 @@ require 'spec_helper'
|
|
2
2
|
require 'oval/instance_of'
|
3
3
|
|
4
4
|
describe Oval::InstanceOf do
|
5
|
+
it "should be subclass of Oval::ClassDeclBase" do
|
6
|
+
described_class.should < Oval::ClassDeclBase
|
7
|
+
end
|
8
|
+
|
5
9
|
describe 'an instance' do
|
6
10
|
let(:subject) { described_class[:klass] }
|
7
11
|
before { described_class.stubs(:validate_class) }
|
8
12
|
it { should respond_to :validate }
|
13
|
+
it { should respond_to :it_should }
|
9
14
|
end
|
10
15
|
|
11
16
|
context "#{described_class.name}[:symbol1]" do
|
@@ -41,4 +46,13 @@ describe Oval::InstanceOf do
|
|
41
46
|
end
|
42
47
|
end
|
43
48
|
end
|
49
|
+
|
50
|
+
describe "#it_should" do
|
51
|
+
[ String, NilClass, Numeric ].each do |klass|
|
52
|
+
context "#{described_class.name}[#{klass.name}].it_should" do
|
53
|
+
let(:klass) { klass }
|
54
|
+
it { described_class[klass].it_should.should == "be an instance of #{klass.name}"}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
44
58
|
end
|
@@ -2,10 +2,15 @@ require 'spec_helper'
|
|
2
2
|
require 'oval/kind_of'
|
3
3
|
|
4
4
|
describe Oval::KindOf do
|
5
|
+
it "should be subclass of Oval::ClassDeclBase" do
|
6
|
+
described_class.should < Oval::ClassDeclBase
|
7
|
+
end
|
8
|
+
|
5
9
|
describe 'an instance' do
|
6
10
|
let(:subject) { described_class[:klass] }
|
7
11
|
before { described_class.stubs(:validate_class) }
|
8
12
|
it { should respond_to :validate }
|
13
|
+
it { should respond_to :it_should }
|
9
14
|
end
|
10
15
|
|
11
16
|
context "#{described_class.name}[:symbol1]" do
|
@@ -42,4 +47,13 @@ describe Oval::KindOf do
|
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
50
|
+
|
51
|
+
describe "#it_should" do
|
52
|
+
[ String, NilClass, Numeric ].each do |klass|
|
53
|
+
context "#{described_class.name}[#{klass.name}].it_should" do
|
54
|
+
let(:klass) { klass }
|
55
|
+
it { described_class[klass].it_should.should == "be a kind of #{klass.name}"}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
45
59
|
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'oval/match'
|
3
|
+
|
4
|
+
describe Oval::Match do
|
5
|
+
it "should be subclass of Oval::Base" do
|
6
|
+
described_class.should < Oval::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'the class' do
|
10
|
+
it { described_class.should respond_to :[] }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'an instance' do
|
14
|
+
let(:subject) { described_class[:re0] }
|
15
|
+
before { described_class.stubs(:validate_re).with(:re0) }
|
16
|
+
it { should respond_to :validate }
|
17
|
+
it { should respond_to :re }
|
18
|
+
it { should respond_to :it_should }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "[]" do
|
22
|
+
context "#{described_class.name}[:re0]" do
|
23
|
+
it "should == new(:re0)" do
|
24
|
+
described_class.stubs(:new).once.with(:re0).returns :ok
|
25
|
+
described_class[:re0].should be :ok
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "re" do
|
31
|
+
let(:subject) { described_class[:re0] }
|
32
|
+
before { described_class.stubs(:validate_re).with(:re0) }
|
33
|
+
context "#{described_class.name}[:re0].re" do
|
34
|
+
it { subject.re.should be :re0 }
|
35
|
+
end
|
36
|
+
context "when @re = :re1" do
|
37
|
+
before { subject.instance_variable_set(:@re, :re1) }
|
38
|
+
it { subject.re.should be :re1 }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "re=" do
|
43
|
+
let(:subject) { described_class[:re0] }
|
44
|
+
before { described_class.stubs(:validate_re).with(:re0) }
|
45
|
+
context "re = :re1" do
|
46
|
+
it "should invoke self.class.validate_re(:re1) once" do
|
47
|
+
described_class.expects(:validate_re).once.with(:re1)
|
48
|
+
expect { subject.send(:re=, :re1) }.to_not raise_error
|
49
|
+
end
|
50
|
+
it "should assign @re = :re1" do
|
51
|
+
described_class.stubs(:validate_re).with(:re1)
|
52
|
+
subject.send(:re=, :re1)
|
53
|
+
subject.instance_variable_get(:@re).should be :re1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "validate_re" do
|
59
|
+
context "#{described_class.name}.validate_re(:re1)" do
|
60
|
+
let(:msg) { "Invalid regular expression :re1. Should be an instance of Regexp" }
|
61
|
+
it { expect { described_class.validate_re(:re1) }.to raise_error Oval::DeclError, msg }
|
62
|
+
end
|
63
|
+
context "#{described_class.name}.validate_re(/^.*$/)" do
|
64
|
+
it { expect { described_class.validate_re(/^.*$/) }.to_not raise_error }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#validate" do
|
69
|
+
# Valid
|
70
|
+
[
|
71
|
+
[/.*/, ['foo']],
|
72
|
+
[/.*/, ['foo','subj']],
|
73
|
+
[/^[a-z_][a-z0-9_]+/, ['_123a','subj']]
|
74
|
+
].each do |re,args|
|
75
|
+
context "#{described_class.name}[#{re.inspect}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
|
76
|
+
let(:re) { re }
|
77
|
+
let(:args) { args }
|
78
|
+
it { expect { described_class[re].validate(*args) }.to_not raise_error }
|
79
|
+
end
|
80
|
+
end
|
81
|
+
# Validation error
|
82
|
+
[
|
83
|
+
[ /^[a-z_][a-z0-9_]+/, ['5643'], "Invalid value \"5643\". Should match #{(/^[a-z_][a-z0-9_]+/).inspect}" ],
|
84
|
+
[ /^[a-z_][a-z0-9_]+/, ['5643','subj'], "Invalid value \"5643\" for subj. Should match #{(/^[a-z_][a-z0-9_]+/).inspect}" ],
|
85
|
+
[ /^[a-z_][a-z0-9_]+/, [nil,'subj'], "Invalid value nil for subj. Should match #{(/^[a-z_][a-z0-9_]+/).inspect}" ]
|
86
|
+
].each do |re,args,msg|
|
87
|
+
context "#{described_class.name}[#{re.inspect}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
|
88
|
+
let(:re) { re }
|
89
|
+
let(:args) { args }
|
90
|
+
let(:msg) { msg }
|
91
|
+
it { expect { described_class[re].validate(*args) }.to raise_error Oval::ValueError, msg }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#it_should" do
|
97
|
+
[ //, /^.*$/, /^[a-z_][a-z0-9_]+$/].each do |re|
|
98
|
+
context "#{described_class}[#{re.inspect}].it_should" do
|
99
|
+
let(:re) { re }
|
100
|
+
let(:msg) { "match #{re.inspect}" }
|
101
|
+
it { described_class[re].it_should.should == msg }
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -2,11 +2,17 @@ require 'spec_helper'
|
|
2
2
|
require 'oval/one_of'
|
3
3
|
|
4
4
|
describe Oval::OneOf do
|
5
|
+
it "should be subclass of Oval::Base" do
|
6
|
+
described_class.should < Oval::Base
|
7
|
+
end
|
8
|
+
|
5
9
|
context 'the class' do
|
6
10
|
it { described_class.should respond_to :[] }
|
7
11
|
end
|
12
|
+
|
8
13
|
context 'an instance' do
|
9
14
|
it { should respond_to :validate }
|
15
|
+
it { should respond_to :it_should }
|
10
16
|
it { should respond_to :decls }
|
11
17
|
end
|
12
18
|
|
@@ -47,9 +53,9 @@ describe Oval::OneOf do
|
|
47
53
|
end
|
48
54
|
end
|
49
55
|
[
|
50
|
-
[ [:foo ], [:bar], "Invalid value :bar" ],
|
51
|
-
[ [:foo, :bar], [:geez], "Invalid value :geez" ],
|
52
|
-
[ [:foo, :bar], [:geez,'subj'], "Invalid value :geez for subj" ],
|
56
|
+
[ [:foo ], [:bar], "Invalid value :bar. Should be equal :foo" ],
|
57
|
+
[ [:foo, :bar], [:geez], "Invalid value :geez. Should be equal :foo or be equal :bar" ],
|
58
|
+
[ [:foo, :bar], [:geez,'subj'], "Invalid value :geez for subj. Should be equal :foo or be equal :bar" ],
|
53
59
|
].each do |decls,args,msg|
|
54
60
|
context "#{described_class}[#{decls.map{|x| x.inspect}.join(', ')}].validate(#{args.map{|x| x.inspect}.join(', ')})" do
|
55
61
|
let(:args) { args }
|
@@ -59,4 +65,19 @@ describe Oval::OneOf do
|
|
59
65
|
end
|
60
66
|
end
|
61
67
|
end
|
68
|
+
|
69
|
+
describe "#it_should" do
|
70
|
+
[
|
71
|
+
[ [], "be absent" ],
|
72
|
+
[ [:foo], "be equal :foo"],
|
73
|
+
[ [:foo, :bar], "be equal :foo or be equal :bar"],
|
74
|
+
[ [:foo, :bar, :geez], "be equal :foo, be equal :bar or be equal :geez"],
|
75
|
+
].each do |decls,msg|
|
76
|
+
context "#{described_class.name}[#{decls.map{|x| x.inspect}.join(', ')}].it_should" do
|
77
|
+
let(:decls) { decls }
|
78
|
+
let(:msg) { msg }
|
79
|
+
it { described_class[*decls].it_should.should == msg}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
62
83
|
end
|
@@ -2,6 +2,10 @@ require 'spec_helper'
|
|
2
2
|
require 'oval/options'
|
3
3
|
|
4
4
|
describe Oval::Options do
|
5
|
+
it "should be subclass of Oval::Base" do
|
6
|
+
described_class.should < Oval::Base
|
7
|
+
end
|
8
|
+
|
5
9
|
context "the class" do
|
6
10
|
[
|
7
11
|
:validate_decl,
|
@@ -140,8 +144,8 @@ describe Oval::Options do
|
|
140
144
|
let(:subject) { described_class[decl] }
|
141
145
|
context "validate_option_value(#{args.map{|x| x.inspect}.join(', ')})" do
|
142
146
|
let(:name) { name }
|
143
|
-
it "should invoke self.class.
|
144
|
-
described_class.stubs(:
|
147
|
+
it "should invoke self.class.validate(#{args2.map{|x| x.inspect}.join(', ')}) once" do
|
148
|
+
described_class.stubs(:validate).once.with(*args2)
|
145
149
|
expect { subject.send(:validate_option_value,*args) }.to_not raise_error
|
146
150
|
end
|
147
151
|
end
|
@@ -6,6 +6,7 @@ describe Oval::SubclassOf do
|
|
6
6
|
it "should be subclass of Oval::ClassDeclBase" do
|
7
7
|
described_class.should < Oval::ClassDeclBase
|
8
8
|
end
|
9
|
+
|
9
10
|
describe "#validate" do
|
10
11
|
[
|
11
12
|
[Integer,[Fixnum]],
|
@@ -31,4 +32,13 @@ describe Oval::SubclassOf do
|
|
31
32
|
end
|
32
33
|
end
|
33
34
|
end
|
35
|
+
|
36
|
+
describe "#it_should" do
|
37
|
+
[ String, NilClass, Numeric ].each do |klass|
|
38
|
+
context "#{described_class.name}[#{klass.name}].it_should" do
|
39
|
+
let(:klass) { klass }
|
40
|
+
it { described_class[klass].it_should.should == "be a subclass of #{klass.name}"}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
34
44
|
end
|
data/spec/unit/oval_spec.rb
CHANGED
@@ -9,6 +9,7 @@ describe Oval do
|
|
9
9
|
it { should respond_to :ov_collection }
|
10
10
|
it { should respond_to :ov_instance_of }
|
11
11
|
it { should respond_to :ov_kind_of }
|
12
|
+
it { should respond_to :ov_match }
|
12
13
|
it { should respond_to :ov_one_of }
|
13
14
|
it { should respond_to :ov_options }
|
14
15
|
it { should respond_to :ov_subclass_of }
|
@@ -18,9 +19,25 @@ describe Oval do
|
|
18
19
|
its(:ov_collection) { should be Oval::Collection }
|
19
20
|
its(:ov_instance_of) { should be Oval::InstanceOf }
|
20
21
|
its(:ov_kind_of) { should be Oval::KindOf }
|
22
|
+
its(:ov_match) { should be Oval::Match }
|
21
23
|
its(:ov_one_of) { should be Oval::OneOf }
|
22
24
|
its(:ov_options) { should be Oval::Options }
|
23
25
|
its(:ov_subclass_of) { should be Oval::SubclassOf }
|
26
|
+
|
27
|
+
describe "validate" do
|
28
|
+
context "#{described_class.name}.validate(:foo,:bar)" do
|
29
|
+
it "should invoke Oval::Base.validate(:foo,:bar,nil) once" do
|
30
|
+
Oval::Base.expects(:validate).once.with(:foo,:bar,nil)
|
31
|
+
expect { described_class.validate(:foo,:bar) }.to_not raise_error
|
32
|
+
end
|
33
|
+
end
|
34
|
+
context "#{described_class.name}.validate(:foo,:bar,'subj')" do
|
35
|
+
it "should invoke Oval::Base.validate(:foo,:bar,'subj') once" do
|
36
|
+
Oval::Base.expects(:validate).once.with(:foo,:bar,'subj')
|
37
|
+
expect { described_class.validate(:foo,:bar,'subj') }.to_not raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
24
41
|
end
|
25
42
|
|
26
43
|
# examples from README.md and other sources
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oval
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -168,6 +168,7 @@ files:
|
|
168
168
|
- lib/oval/hash_item.rb
|
169
169
|
- lib/oval/instance_of.rb
|
170
170
|
- lib/oval/kind_of.rb
|
171
|
+
- lib/oval/match.rb
|
171
172
|
- lib/oval/one_of.rb
|
172
173
|
- lib/oval/options.rb
|
173
174
|
- lib/oval/subclass_of.rb
|
@@ -181,6 +182,7 @@ files:
|
|
181
182
|
- spec/unit/oval/hash_item_spec.rb
|
182
183
|
- spec/unit/oval/instance_of_spec.rb
|
183
184
|
- spec/unit/oval/kind_of_spec.rb
|
185
|
+
- spec/unit/oval/match_spec.rb
|
184
186
|
- spec/unit/oval/one_of_spec.rb
|
185
187
|
- spec/unit/oval/options_spec.rb
|
186
188
|
- spec/unit/oval/subclass_of_spec.rb
|
@@ -200,7 +202,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
200
202
|
version: '0'
|
201
203
|
segments:
|
202
204
|
- 0
|
203
|
-
hash:
|
205
|
+
hash: 2837400115143885778
|
204
206
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
205
207
|
none: false
|
206
208
|
requirements:
|
@@ -209,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
209
211
|
version: '0'
|
210
212
|
segments:
|
211
213
|
- 0
|
212
|
-
hash:
|
214
|
+
hash: 2837400115143885778
|
213
215
|
requirements: []
|
214
216
|
rubyforge_project:
|
215
217
|
rubygems_version: 1.8.23
|
@@ -228,6 +230,7 @@ test_files:
|
|
228
230
|
- spec/unit/oval/hash_item_spec.rb
|
229
231
|
- spec/unit/oval/instance_of_spec.rb
|
230
232
|
- spec/unit/oval/kind_of_spec.rb
|
233
|
+
- spec/unit/oval/match_spec.rb
|
231
234
|
- spec/unit/oval/one_of_spec.rb
|
232
235
|
- spec/unit/oval/options_spec.rb
|
233
236
|
- spec/unit/oval/subclass_of_spec.rb
|