From 66177c58e3e12ec1304ada1292e1bb80c0bed6f2 Mon Sep 17 00:00:00 2001 From: Tom Duffield Date: Sat, 19 Nov 2016 19:25:57 -0600 Subject: [PATCH] DRY up the tests and address Rubocop feedback Signed-off-by: Tom Duffield --- .../generator/generator_tags.rb | 4 +- spec/unit/generator/generator_tags_spec.rb | 88 ++++++++++--------- 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/lib/github_changelog_generator/generator/generator_tags.rb b/lib/github_changelog_generator/generator/generator_tags.rb index 08d237b..51d0c2b 100644 --- a/lib/github_changelog_generator/generator/generator_tags.rb +++ b/lib/github_changelog_generator/generator/generator_tags.rb @@ -23,7 +23,8 @@ module GitHubChangelogGenerator # @param [Array] all_tags is the list of all tags ordered from newest -> oldest # @return [Hash] key is the tag to output, value is an array of [Left Tag, Right Tag] # PRs to include in this section will be >= [Left Tag Date] and <= [Right Tag Date] - def build_tag_section_mapping(filtered_tags, all_tags) + # rubocop:disable Style/For - for allows us to be more concise + def build_tag_section_mapping(filtered_tags, _all_tags) tag_mapping = {} for i in 0..(filtered_tags.length - 1) tag = filtered_tags[i] @@ -39,6 +40,7 @@ module GitHubChangelogGenerator end tag_mapping end + # rubocop:enable Style/For # Sort all tags by date, newest to oldest def sort_tags_by_date(tags) diff --git a/spec/unit/generator/generator_tags_spec.rb b/spec/unit/generator/generator_tags_spec.rb index 7ca342a..8791a05 100644 --- a/spec/unit/generator/generator_tags_spec.rb +++ b/spec/unit/generator/generator_tags_spec.rb @@ -16,108 +16,109 @@ describe GitHubChangelogGenerator::Generator do describe "#build_tag_section_mapping" do let(:sorted_tags) { tags_from_strings(%w(8 7 6 5 4 3 2 1)) } let(:filtered_tags) { generator.get_filtered_tags(sorted_tags) } + let(:options) { {} } + let(:generator) { GitHubChangelogGenerator::Generator.new(options) } subject do generator.build_tag_section_mapping(filtered_tags, sorted_tags) end - context "with no constraints" do - let(:generator) { GitHubChangelogGenerator::Generator.new() } + shared_examples_for "a section mapping" do + it { is_expected.to be_a(Hash) } + it { is_expected.to eq(expected_mapping) } + end + shared_examples_for "a full changelog" do let(:expected_mapping) do { - tag_with_name('8') => [tag_with_name('7'), tag_with_name('8')], - tag_with_name('7') => [tag_with_name('6'), tag_with_name('7')], - tag_with_name('6') => [tag_with_name('5'), tag_with_name('6')], - tag_with_name('5') => [tag_with_name('4'), tag_with_name('5')], - tag_with_name('4') => [tag_with_name('3'), tag_with_name('4')], - tag_with_name('3') => [tag_with_name('2'), tag_with_name('3')], - tag_with_name('2') => [tag_with_name('1'), tag_with_name('2')], - tag_with_name('1') => [nil, tag_with_name('1')] + tag_with_name("8") => [tag_with_name("7"), tag_with_name("8")], + tag_with_name("7") => [tag_with_name("6"), tag_with_name("7")], + tag_with_name("6") => [tag_with_name("5"), tag_with_name("6")], + tag_with_name("5") => [tag_with_name("4"), tag_with_name("5")], + tag_with_name("4") => [tag_with_name("3"), tag_with_name("4")], + tag_with_name("3") => [tag_with_name("2"), tag_with_name("3")], + tag_with_name("2") => [tag_with_name("1"), tag_with_name("2")], + tag_with_name("1") => [nil, tag_with_name("1")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" + end + + context "with no constraints" do + it_behaves_like "a full changelog" end context "with between tags only" do - let(:generator) { GitHubChangelogGenerator::Generator.new(between_tags: %w(3 5 8)) } - + let(:options) { { between_tags: %w(3 5 8) } } let(:expected_mapping) do { - tag_with_name('8') => [tag_with_name('5'), tag_with_name('8')], - tag_with_name('5') => [tag_with_name('3'), tag_with_name('5')] + tag_with_name("8") => [tag_with_name("5"), tag_with_name("8")], + tag_with_name("5") => [tag_with_name("3"), tag_with_name("5")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" end context "with since only" do - let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: '6') } + let(:options) { { since_tag: "6" } } let(:expected_mapping) do { - tag_with_name('8') => [tag_with_name('7'), tag_with_name('8')], - tag_with_name('7') => [tag_with_name('6'), tag_with_name('7')] + tag_with_name("8") => [tag_with_name("7"), tag_with_name("8")], + tag_with_name("7") => [tag_with_name("6"), tag_with_name("7")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" end context "with due only" do - let(:generator) { GitHubChangelogGenerator::Generator.new(due_tag: '4') } + let(:options) { { due_tag: "4" } } let(:expected_mapping) do { - tag_with_name('3') => [tag_with_name('2'), tag_with_name('3')], - tag_with_name('2') => [tag_with_name('1'), tag_with_name('2')], - tag_with_name('1') => [nil, tag_with_name('1')] + tag_with_name("3") => [tag_with_name("2"), tag_with_name("3")], + tag_with_name("2") => [tag_with_name("1"), tag_with_name("2")], + tag_with_name("1") => [nil, tag_with_name("1")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" end context "with since and due" do - let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: '2', due_tag: '5') } + let(:options) { { since_tag: "2", due_tag: "5" } } let(:expected_mapping) do { - tag_with_name('4') => [tag_with_name('3'), tag_with_name('4')], - tag_with_name('3') => [tag_with_name('2'), tag_with_name('3')] + tag_with_name("4") => [tag_with_name("3"), tag_with_name("4")], + tag_with_name("3") => [tag_with_name("2"), tag_with_name("3")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" end context "with since, due, and between_tags" do - let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: '2', due_tag: '7', between_tags: %w(3 5 6)) } + let(:options) { { since_tag: "2", due_tag: "7", between_tags: %w(3 5 6) } } let(:expected_mapping) do { - tag_with_name('6') => [tag_with_name('5'), tag_with_name('6')], - tag_with_name('5') => [tag_with_name('3'), tag_with_name('5')] + tag_with_name("6") => [tag_with_name("5"), tag_with_name("6")], + tag_with_name("5") => [tag_with_name("3"), tag_with_name("5")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" end context "with conflicting since/due/between_tags" do - let(:generator) { GitHubChangelogGenerator::Generator.new(since_tag: '2', due_tag: '7', between_tags: %w(1 3 6 8)) } + let(:options) { { since_tag: "2", due_tag: "7", between_tags: %w(1 3 6 8) } } let(:expected_mapping) do { - tag_with_name('6') => [tag_with_name('3'), tag_with_name('6')] + tag_with_name("6") => [tag_with_name("3"), tag_with_name("6")] } end - it { is_expected.to be_a(Hash) } - it { is_expected.to eq(expected_mapping) } + it_behaves_like "a section mapping" end end @@ -133,6 +134,7 @@ describe GitHubChangelogGenerator::Generator do it { is_expected.to be_a(Array) } it { is_expected.to match_array(tags_from_strings(%w(1 2 3))) } end + context "when between_tags same as input array" do before do @generator = GitHubChangelogGenerator::Generator.new(between_tags: %w(1 2 3))