metasploit-framework/spec/support/shared/examples/an_option.rb

62 lines
1.5 KiB
Ruby
Raw Normal View History

# -*- coding:binary -*-
2012-11-29 20:40:25 +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
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
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
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
end
end
2012-11-29 20:40:25 +00:00
context "with valid values" do
valid_values.each do |vhash|
valid_value = vhash[:value]
normalized_value = vhash[:normalized]
2012-11-29 20:40:25 +00:00
it "should be valid and normalize appropriately: #{valid_value}" do
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
}
2014-08-26 20:17:00 +00:00
if vhash[:skip]
skip(vhash[:skip], &block)
else
block.call
end
2012-11-29 20:40:25 +00:00
end
end
end
context "with invalid values" do
invalid_values.each do |vhash|
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)
else
block.call
end
2012-11-29 20:40:25 +00:00
end
end
end
end