/spec/support/shared/examples/options.rb
https://github.com/debbiemezyene/metasploit-framework · Ruby · 61 lines · 50 code · 10 blank · 1 comment · 6 complexity · 28f2636b4f4ad75bd8f0569218bc5cde MD5 · raw file
- # -*- coding:binary -*-
- shared_examples_for "an option" do |valid_values, invalid_values, type|
- 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'])}
- it "should return a type of #{type}" do
- subject.type.should == type
- end
- context 'when required' do
- it 'should not be valid for nil' do
- required.valid?(nil).should == false
- end
- end
- context 'when not required' do
- it 'it should be valid for nil' do
- optional.valid?(nil).should == true
- end
- end
- context "with valid values" do
- valid_values.each do |vhash|
- valid_value = vhash[:value]
- normalized_value = vhash[:normalized]
- it "should be valid and normalize appropriately: #{valid_value}" do
- block = Proc.new {
- subject.normalize(valid_value).should == normalized_value
- subject.valid?(valid_value).should be_true
- }
- if vhash[:pending]
- pending(vhash[:pending], &block)
- else
- block.call
- end
- end
- end
- end
- context "with invalid values" do
- invalid_values.each do |vhash|
- invalid_value = vhash[:value]
- it "should not be valid: #{invalid_value}" do
- block = Proc.new { subject.valid?(invalid_value).should be_false }
- if vhash[:pending]
- pending(vhash[:pending], &block)
- else
- block.call
- end
- end
- end
- end
- end