Add tests to verify sorting algorithm #244

This commit is contained in:
Petr Korolev 2015-06-11 13:21:18 +03:00
parent 22d1657fd4
commit 19eb03c54e
5 changed files with 99 additions and 24 deletions

View File

@ -2,6 +2,8 @@
## [0.0.4](https://github.com/skywinder/changelog_test/tree/0.0.4) (2015-05-22)
[Full Changelog](https://github.com/skywinder/changelog_test/compare/v0.0.3...0.0.4)
**Closed issues:**
- Test issue, that should appear in 0.0.4 [\#3](https://github.com/skywinder/changelog_test/issues/3)
@ -10,10 +12,24 @@
- Add automatically generated change log file. [\#5](https://github.com/skywinder/changelog_test/pull/5) ([skywinder](https://github.com/skywinder))
## [v0.0.3](https://github.com/skywinder/changelog_test/tree/v0.0.3) (2015-03-04)
[Full Changelog](https://github.com/skywinder/changelog_test/compare/v0.0.2...v0.0.3)
**Merged pull requests:**
- fix \#3. hotfix. Should appear in v0.0.3 [\#4](https://github.com/skywinder/changelog_test/pull/4) ([skywinder](https://github.com/skywinder))
## [v0.0.2](https://github.com/skywinder/changelog_test/tree/v0.0.2) (2015-03-04)
[Full Changelog](https://github.com/skywinder/changelog_test/compare/v0.0.1...v0.0.2)
**Merged pull requests:**
- Here is a test hotfix should appear in v.0.0.2 [\#2](https://github.com/skywinder/changelog_test/pull/2) ([skywinder](https://github.com/skywinder))
## [v0.0.1](https://github.com/skywinder/changelog_test/tree/v0.0.1) (2015-03-02)
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*

View File

@ -5,6 +5,7 @@ module GitHubChangelogGenerator
# @return [String] Generated change log file
def compound_changelog
fetch_and_filter_tags
sort_tags_by_date(@filtered_tags)
fetch_issues_and_pr
log = "# Change Log\n\n"
@ -129,7 +130,7 @@ module GitHubChangelogGenerator
log = generate_unreleased_section
(1...filtered_tags.size).each do |index|
log += generate_log_between_tags(all_tags[index], all_tags[index - 1])
log += generate_log_between_tags(filtered_tags[index], filtered_tags[index - 1])
end
if @filtered_tags.count != 0
log += generate_log_between_tags(nil, filtered_tags.last)

View File

@ -4,7 +4,6 @@ module GitHubChangelogGenerator
def fetch_and_filter_tags
@filtered_tags = get_filtered_tags(@fetcher.get_all_tags)
fetch_tags_dates
sort_tags_by_date(@filtered_tags)
end
# Sort all tags by date
@ -22,11 +21,13 @@ module GitHubChangelogGenerator
def get_time_of_tag(tag_name)
fail ChangelogGeneratorError, "tag_name is nil".red if tag_name.nil?
if @tag_times_hash[tag_name["name"]]
@tag_times_hash[tag_name["name"]]
name_of_tag = tag_name["name"]
time_for_name = @tag_times_hash[name_of_tag]
if !time_for_name.nil?
time_for_name
else
time_string = @fetcher.fetch_date_of_tag tag_name
@tag_times_hash[tag_name["name"]] = time_string
@tag_times_hash[name_of_tag] = time_string
time_string
end
end

View File

@ -1,3 +1,18 @@
def tag_mash_with_name(tag)
mash_tag = Hashie::Mash.new
mash_tag.name = tag
mash_tag
end
def tags_mash_from_strings(tags_strings)
mash_array = []
tags_strings.each do |tag|
mash_tag = tag_mash_with_name(tag)
mash_array << mash_tag
end
mash_array
end
describe GitHubChangelogGenerator::Generator do
describe "#filter_between_tags" do
context "when between_tags nil" do
@ -46,16 +61,6 @@ describe GitHubChangelogGenerator::Generator do
end
end
def tags_mash_from_strings(tags_strings)
mash_array = []
tags_strings.each do |tag|
mash_tag = Hashie::Mash.new
mash_tag.name = tag
mash_array << mash_tag
end
mash_array
end
describe "#get_filtered_tags" do
subject do
generator.get_filtered_tags(tags_mash_from_strings(%w(1 2 3 4 5)))
@ -76,19 +81,71 @@ describe GitHubChangelogGenerator::Generator do
it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2))) }
end
context "with invalid excluded tags" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags: %w(invalid tags)) }
it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) }
end
end
describe "#get_time_of_tag" do
before(:all) do
@generator = GitHubChangelogGenerator::Generator.new
puts "blah"
current_time = Time.now
before(:all) { @generator = GitHubChangelogGenerator::Generator.new }
context "run with nil parameter" do
it "should raise ChangelogGeneratorError" do
expect { @generator.get_time_of_tag nil }.to raise_error(GitHubChangelogGenerator::ChangelogGeneratorError)
end
end
it "should raise ChangelogGeneratorError" do
expect { @generator.get_time_of_tag nil }.to raise_error(GitHubChangelogGenerator::ChangelogGeneratorError)
context "fetch already filled tag" do
before { @generator.instance_variable_set :@tag_times_hash, "valid_tag" => current_time }
subject { @generator.get_time_of_tag tag_mash_with_name("valid_tag") }
it { is_expected.to be_a_kind_of(Time) }
it { is_expected.to eq(current_time) }
end
context "fetch not filled tag" do
before do
mock = double("fake fetcher")
allow(mock).to receive_messages(fetch_date_of_tag: current_time)
@generator.instance_variable_set :@fetcher, mock
end
subject do
of_tag = @generator.get_time_of_tag tag_mash_with_name("valid_tag")
of_tag
end
it { is_expected.to be_a_kind_of(Time) }
it { is_expected.to eq(current_time) }
end
end
describe "#sort_tags_by_date" do
subject { generator.sort_tags_by_date }
time1 = Time.now
time2 = Time.now
time3 = Time.now
before(:all) do
@generator = GitHubChangelogGenerator::Generator.new
end
context "sort unsorted tags" do
tags = tags_mash_from_strings %w(valid_tag1 valid_tag2 valid_tag3)
before do
@generator.instance_variable_set :@tag_times_hash, "valid_tag1" => time1, "valid_tag2" => time2, "valid_tag3" => time3
end
subject do
@generator.sort_tags_by_date tags
end
it { is_expected.to be_a_kind_of(Array) }
it { is_expected.to match_array(tags.reverse!) }
end
context "sort sorted tags" do
tags = tags_mash_from_strings %w(valid_tag3 valid_tag2 valid_tag1)
before do
@generator.instance_variable_set :@tag_times_hash, "valid_tag1" => time1, "valid_tag2" => time2, "valid_tag3" => time3
end
subject do
@generator.sort_tags_by_date tags
end
it { is_expected.to be_a_kind_of(Array) }
it { is_expected.to match_array(tags) }
end
end
end

View File

@ -27,9 +27,9 @@ describe GitHubChangelogGenerator::Parser do
end
end
describe ".user_project_from_option" do
# context "when option is invalid" do
# it("should exit") { expect { GitHubChangelogGenerator::Parser.user_project_from_option("blah", nil) }.to raise_error(SystemExit) }
# end
context "when option is invalid" do
it("should exit") { expect { GitHubChangelogGenerator::Parser.user_project_from_option("blah", nil) }.to raise_error(SystemExit) }
end
context "when option is valid" do
subject { GitHubChangelogGenerator::Parser.user_project_from_option("skywinder/ActionSheetPicker-3.0", nil) }