2013-04-17 19:26:16 +00:00
|
|
|
# -*- coding:binary -*-
|
2012-11-29 20:40:25 +00:00
|
|
|
|
2015-12-31 22:56:13 +00:00
|
|
|
RSpec.shared_examples_for "an option" do |valid_values, invalid_values, type|
|
2012-11-29 20:40:25 +00:00
|
|
|
subject do
|
|
|
|
described_class.new("name")
|
|
|
|
end
|
|
|
|
|
2013-07-20 21:46:16 +00:00
|
|
|
let(:required) { described_class.new('name', [true, 'A description here'])}
|
|
|
|
let(:optional) { described_class.new('name', [false, 'A description here'])}
|
|
|
|
|
2013-07-20 22:37:39 +00:00
|
|
|
it "should return a type of #{type}" do
|
2015-10-20 16:30:38 +00:00
|
|
|
expect(subject.type).to eq type
|
2013-07-20 22:37:39 +00:00
|
|
|
end
|
|
|
|
|
2013-07-20 21:46:16 +00:00
|
|
|
context 'when required' do
|
|
|
|
it 'should not be valid for nil' do
|
2015-10-20 16:30:38 +00:00
|
|
|
expect(required.valid?(nil)).to eq false
|
2013-07-20 21:46:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not required' do
|
|
|
|
it 'it should be valid for nil' do
|
2015-10-20 16:30:38 +00:00
|
|
|
expect(optional.valid?(nil)).to eq true
|
2013-07-20 21:46:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 20:40:25 +00:00
|
|
|
context "with valid values" do
|
2012-11-29 22:35:50 +00:00
|
|
|
valid_values.each do |vhash|
|
2013-09-30 18:47:53 +00:00
|
|
|
valid_value = vhash[:value]
|
|
|
|
normalized_value = vhash[:normalized]
|
2012-11-29 22:35:50 +00:00
|
|
|
|
2012-11-29 20:40:25 +00:00
|
|
|
it "should be valid and normalize appropriately: #{valid_value}" do
|
2013-09-30 18:47:53 +00:00
|
|
|
block = Proc.new {
|
2015-10-20 16:30:38 +00:00
|
|
|
expect(subject.normalize(valid_value)).to eq normalized_value
|
2015-10-20 19:37:18 +00:00
|
|
|
expect(subject.valid?(valid_value)).to be_truthy
|
2013-09-30 18:47:53 +00:00
|
|
|
}
|
2014-08-26 20:17:00 +00:00
|
|
|
if vhash[:skip]
|
|
|
|
skip(vhash[:skip], &block)
|
2013-09-30 18:47:53 +00:00
|
|
|
else
|
|
|
|
block.call
|
|
|
|
end
|
2012-11-29 20:40:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with invalid values" do
|
2012-11-29 22:35:50 +00:00
|
|
|
invalid_values.each do |vhash|
|
2013-09-30 18:47:53 +00:00
|
|
|
invalid_value = vhash[:value]
|
2012-11-29 20:40:25 +00:00
|
|
|
it "should not be valid: #{invalid_value}" do
|
2015-10-20 19:37:18 +00:00
|
|
|
block = Proc.new { expect(subject.valid?(invalid_value)).to be_falsey }
|
2014-08-26 20:17:00 +00:00
|
|
|
if vhash[:skip]
|
|
|
|
skip(vhash[:skip], &block)
|
2013-09-30 18:47:53 +00:00
|
|
|
else
|
|
|
|
block.call
|
|
|
|
end
|
2012-11-29 20:40:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|