1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
RSpec.shared_examples "a basic configuration" do
subject(:config) do
Class.new(described_class) do
option_accessor :use_test, type: Contracts::Bool,
default: false
end.new
end
it { expect(config).not_to be_nil }
describe ".option_reader" do
subject(:config) { config_klass.new }
let(:config_klass) { Class.new(described_class) }
before do
config_klass.option_reader :new_opt, type: Contracts::Num,
default: 1
end
context "when value is read" do
it { expect(config.new_opt).to eq 1 }
end
context "when one tries to write a value" do
it { expect { config.new_opt = 1 }.to raise_error NoMethodError }
end
context "when block is defined" do
before do
config_klass.option_reader(
:new_opt2,
type: Contracts::Num
) { |c| c.new_opt.value + 1 }
end
it "uses the block to set the value" do
expect(config.new_opt2).to eq 2
end
end
context "when block and default value is defined" do
it "complains that only one or the other can be specified" do
expect do
config_klass
.option_accessor(:new_opt2, type: Contracts::Num,
default: 2) { |c| c.new_opt.value + 1 }
end.to raise_error ArgumentError, "Either use block or default value"
end
end
end
describe ".option_accessor" do
subject(:config) { config_klass.new }
let(:config_klass) { Class.new(described_class) }
before do
config_klass.option_accessor :new_opt, type: Contracts::Num,
default: 1
end
context "when default is used" do
it { expect(config.new_opt).to eq 1 }
end
context "when is modified" do
before { config.new_opt = 2 }
it { expect(config.new_opt).to eq 2 }
end
context "when block is defined" do
before do
config_klass.option_accessor(
:new_opt2, type: Contracts::Num
) do |c|
c.new_opt.value + 1
end
end
it "uses the block to set the default value" do
expect(config.new_opt2).to eq 2
end
end
context "when block and default value is defined" do
it "complains that only one or the other can be specified" do
expect do
config_klass.option_accessor(:new_opt2,
type: Contracts::Num,
default: 2) { |c| c.new_opt1 + 1 }
end.to raise_error ArgumentError, "Either use block or default value"
end
end
end
describe "option?" do
let(:name) { :use_test }
it "returns true when passed a valid option name" do
expect(config.option?(:use_test)).to be_truthy
end
it "returns false when passed an invalid option name" do
expect(config.option?(:blub)).to be_falsy
end
end
describe "set_if_option" do
let(:name) { :use_test }
let(:value) { true }
it "sets the option to the given value if a valid option name is passed" do
expect { config.set_if_option(:use_test, true) }
.to change(config, :use_test).from(false).to(true)
end
it "raises no error if an invalid option name is passed" do
expect { config.set_if_option(:blub, true) }.not_to raise_error
end
end
end
|