Options: add error class, refactor
- improve readability of specs - extract methods
This commit is contained in:
parent
70aaba9aa2
commit
c1f6fce86c
|
@ -2,6 +2,8 @@
|
||||||
require 'delegate'
|
require 'delegate'
|
||||||
module GitHubChangelogGenerator
|
module GitHubChangelogGenerator
|
||||||
class Options < SimpleDelegator
|
class Options < SimpleDelegator
|
||||||
|
UnsupportedOptionError = Class.new(ArgumentError)
|
||||||
|
|
||||||
KNOWN_OPTIONS = [
|
KNOWN_OPTIONS = [
|
||||||
:add_issues_wo_labels,
|
:add_issues_wo_labels,
|
||||||
:add_pr_wo_labels,
|
:add_pr_wo_labels,
|
||||||
|
@ -56,9 +58,12 @@ module GitHubChangelogGenerator
|
||||||
|
|
||||||
def initialize(values)
|
def initialize(values)
|
||||||
super(values)
|
super(values)
|
||||||
if unsupported_options.any?
|
unsupported_options.any? && raise(UnsupportedOptionError, unsupported_options.inspect)
|
||||||
raise ArgumentError, "Unsupported options #{unsupported_options}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def []=(key, val)
|
||||||
|
supported_option?(key) || raise(UnsupportedOptionError, key.inspect)
|
||||||
|
values[key] = val
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
|
@ -72,7 +77,15 @@ module GitHubChangelogGenerator
|
||||||
end
|
end
|
||||||
|
|
||||||
def unsupported_options
|
def unsupported_options
|
||||||
values.keys - (KNOWN_OPTIONS + THESE_ARE_DIFFERENT)
|
values.keys - supported_options
|
||||||
|
end
|
||||||
|
|
||||||
|
def supported_option?(key)
|
||||||
|
supported_options.include?(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def supported_options
|
||||||
|
KNOWN_OPTIONS + THESE_ARE_DIFFERENT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,16 +1,42 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
describe GitHubChangelogGenerator::Options do
|
RSpec.describe GitHubChangelogGenerator::Options do
|
||||||
it 'can be instantiated with defaults' do
|
describe "#initialize" do
|
||||||
expect(described_class.new(user: 'olle')[:user]).to eq('olle')
|
context "with known options" do
|
||||||
|
it "instantiates successfully" do
|
||||||
|
expect(described_class.new(user: "olle")[:user]).to eq("olle")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'disallows unknown option names' do
|
context "with unknown options" do
|
||||||
|
it "raises an error" do
|
||||||
expect {
|
expect {
|
||||||
described_class.new(
|
described_class.new(
|
||||||
git_remote: 'origin',
|
git_remote: "origin",
|
||||||
strength: 'super-strength',
|
strength: "super-strength",
|
||||||
wisdom: 'deep'
|
wisdom: "deep"
|
||||||
)
|
)
|
||||||
}.to raise_error(ArgumentError, "Unsupported options [:strength, :wisdom]")
|
}.to raise_error("[:strength, :wisdom]")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#[]=(key, value)" do
|
||||||
|
let(:options) { described_class.new(git_remote: "origin") }
|
||||||
|
|
||||||
|
context "with known options" do
|
||||||
|
it "sets the option value" do
|
||||||
|
expect {
|
||||||
|
options[:git_remote] = "in the cloud"
|
||||||
|
}.to change { options[:git_remote] }.to "in the cloud"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with unknown options" do
|
||||||
|
it "raises an error" do
|
||||||
|
expect {
|
||||||
|
options[:charisma] = 8
|
||||||
|
}.to raise_error(":charisma")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user