CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/support/shared/examples/an_option.rb
Views: 11623
1
# -*- coding:binary -*-
2
3
RSpec.shared_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
expect(subject.type).to eq type
13
end
14
15
context 'when required' do
16
it 'should not be valid for nil' do
17
expect(required.valid?(nil)).to eq false
18
end
19
end
20
21
context 'when not required' do
22
it 'it should be valid for nil' do
23
expect(optional.valid?(nil)).to eq 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
expect(subject.normalize(valid_value)).to eq normalized_value
35
expect(subject.valid?(valid_value)).to be_truthy
36
}
37
if vhash[:skip]
38
skip(vhash[:skip], &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 { expect(subject.valid?(invalid_value)).to be_falsey }
51
if vhash[:skip]
52
skip(vhash[:skip], &block)
53
else
54
block.call
55
end
56
end
57
end
58
end
59
60
end
61
62
63