Options: a class to wrap Hash-accessing options choices

- protects against bad names
This commit is contained in:
Olle Jonsson 2016-10-05 19:35:55 +00:00
parent d41a818676
commit 43a6e49055
5 changed files with 97 additions and 6 deletions

View File

@ -10,6 +10,7 @@ require "multi_json"
require "benchmark"
require_relative "github_changelog_generator/helper"
require_relative "github_changelog_generator/options"
require_relative "github_changelog_generator/parser"
require_relative "github_changelog_generator/parser_file"
require_relative "github_changelog_generator/generator/generator"

View File

@ -0,0 +1,78 @@
# frozen_string_literal: true
require 'delegate'
module GitHubChangelogGenerator
class Options < SimpleDelegator
KNOWN_OPTIONS = [
:add_issues_wo_labels,
:add_pr_wo_labels,
:author,
:base,
:between_tags,
:bug_labels,
:bug_prefix,
:cache_file,
:cache_log,
:compare_link,
:date_format,
:due_tag,
:enhancement_labels,
:enhancement_prefix,
:exclude_labels,
:exclude_tags,
:exclude_tags_regex,
:filter_issues_by_milestone,
:frontmatter,
:future_release,
:git_remote,
:github_endpoint,
:github_site,
:header,
:http_cache,
:include_labels,
:issue_prefix,
:issues,
:max_issues,
:merge_prefix,
:output,
:project,
:pulls,
:release_branch,
:release_url,
:simple_list,
:since_tag,
:token,
:unreleased,
:unreleased_label,
:unreleased_only,
:user,
:usernames_as_github_logins,
:verbose,
]
THESE_ARE_DIFFERENT = [
:tag1,
:tag2,
]
def initialize(values)
super(values)
if unsupported_options.any?
raise ArgumentError, "Unsupported options #{unsupported_options}"
end
end
def to_hash
values
end
private
def values
__getobj__
end
def unsupported_options
values.keys - (KNOWN_OPTIONS + THESE_ARE_DIFFERENT)
end
end
end

View File

@ -187,7 +187,7 @@ module GitHubChangelogGenerator
# @return [Hash] Default options
def self.default_options
{
Options.new(
tag1: nil,
tag2: nil,
date_format: "%Y-%m-%d",
@ -217,7 +217,7 @@ module GitHubChangelogGenerator
http_cache: true,
cache_file: "/tmp/github-changelog-http-cache",
cache_log: "/tmp/github-changelog-logger.log"
}
)
end
# If `:user` or `:project` not set in options, try setting them

16
spec/unit/options_spec.rb Normal file
View File

@ -0,0 +1,16 @@
# frozen_string_literal: true
describe GitHubChangelogGenerator::Options do
it 'can be instantiated with defaults' do
expect(described_class.new(user: 'olle')[:user]).to eq('olle')
end
it 'disallows unknown option names' do
expect {
described_class.new(
git_remote: 'origin',
strength: 'super-strength',
wisdom: 'deep'
)
}.to raise_error(ArgumentError, "Unsupported options [:strength, :wisdom]")
end
end

View File

@ -85,10 +85,6 @@ describe GitHubChangelogGenerator::Reader do
context "angular.js.md" do
it { is_expected.to be_an(Array) }
it { is_expected.not_to be_empty }
it do
pending("Implement heading_level for parser.")
expect(subject.count).to eq(134)
end
# it do
# pending('Implement heading_level for parser.')
# expect(subject.first).to include('version' => '1.4.0-beta.6 cookie-liberation')