PageRenderTime 42ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1# -*- coding:binary -*-
 2
 3shared_examples_for "an option" do |valid_values, invalid_values, type|
 4  subject do
 5    described_class.new("name")
 6  end
 7
 8  let(:required) { described_class.new('name', [true, 'A description here'])}
 9  let(:optional) { described_class.new('name', [false, 'A description here'])}
10
11  it "should return a type of #{type}"  do
12    subject.type.should == type
13  end
14
15  context 'when required' do
16    it 'should not be valid for nil' do
17      required.valid?(nil).should == false
18    end
19  end
20
21  context 'when not required' do
22    it 'it should be valid for nil' do
23      optional.valid?(nil).should == true
24    end
25  end
26
27  context "with valid values" do
28    valid_values.each do |vhash|
29      valid_value = vhash[:value]
30      normalized_value = vhash[:normalized]
31
32      it "should be valid and normalize appropriately: #{valid_value}" do
33        block = Proc.new {
34          subject.normalize(valid_value).should == normalized_value
35          subject.valid?(valid_value).should be_true
36        }
37        if vhash[:pending]
38          pending(vhash[:pending], &block)
39        else
40          block.call
41        end
42      end
43    end
44  end
45
46  context "with invalid values" do
47    invalid_values.each do |vhash|
48      invalid_value = vhash[:value]
49      it "should not be valid: #{invalid_value}" do
50        block = Proc.new { subject.valid?(invalid_value).should be_false }
51        if vhash[:pending]
52          pending(vhash[:pending], &block)
53        else
54          block.call
55        end
56      end
57    end
58  end
59
60end
61