From 5cfd7827165811f6e83e24931c9d14bd7279c632 Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Sun, 21 Oct 2012 22:41:54 +0200 Subject: [PATCH] refactor --- lib/git_stats/git_data/author.rb | 22 +++++------- lib/git_stats/git_data/repo.rb | 10 +++--- .../stats_view/charts/authors_charts.rb | 4 +-- spec/integration/activity_spec.rb | 5 +-- spec/integration/author_spec.rb | 25 +++++++++++-- spec/integration/file_spec.rb | 4 +-- spec/integration/repo_spec.rb | 36 +++++++++---------- spec/integration/shared.rb | 36 +++++++++++++++++++ 8 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 spec/integration/shared.rb diff --git a/lib/git_stats/git_data/author.rb b/lib/git_stats/git_data/author.rb index e6967f39b..aa90cc9fb 100644 --- a/lib/git_stats/git_data/author.rb +++ b/lib/git_stats/git_data/author.rb @@ -27,20 +27,14 @@ module GitStats } end - def lines_added_by_date - sum = 0 - commits.map { |commit| - sum += commit.short_stat.insertions - [commit.date, sum] - } - end - - def lines_deleted_by_date - sum = 0 - commits.map { |commit| - sum += commit.short_stat.deletions - [commit.date, sum] - } + [:insertions, :deletions].each do |method| + define_method "#{method}_by_date" do + sum = 0 + commits.map { |commit| + sum += commit.short_stat.send(method) + [commit.date, sum] + } + end end def short_stats diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index 8e7948f35..599a3969c 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -41,12 +41,10 @@ module GitStats Hash[authors.map { |author| [author, author.commits.size] }.sort_by { |author, commits| -commits }[0..limit]] end - def lines_added_by_author(limit = 4) - Hash[authors.map { |author| [author, author.lines_added] }.sort_by { |author, lines| -lines }[0..limit]] - end - - def lines_deleted_by_author(limit = 4) - Hash[authors.map { |author| [author, author.lines_deleted] }.sort_by { |author, lines| -lines }[0..limit]] + [:lines_added, :lines_deleted].each do |method| + define_method "#{method}_by_author" do |limit = 4| + Hash[authors.map { |author| [author, author.send(method)] }.sort_by { |author, lines| -lines }[0..limit]] + end end def files_count_by_date diff --git a/lib/git_stats/stats_view/charts/authors_charts.rb b/lib/git_stats/stats_view/charts/authors_charts.rb index c2c1488f9..e973d6156 100644 --- a/lib/git_stats/stats_view/charts/authors_charts.rb +++ b/lib/git_stats/stats_view/charts/authors_charts.rb @@ -19,7 +19,7 @@ module GitStats def lines_added_by_author_by_date(limit = 4) Chart.new do |f| f.multi_date_chart( - data: @authors.sort_by { |author| -author.lines_added }[0..limit].map { |author| {name: author.email, data: author.lines_added_by_date} }, + data: @authors.sort_by { |author| -author.lines_added }[0..limit].map { |author| {name: author.email, data: author.insertions_by_date} }, title: :lines_by_date.t, y_text: :lines.t ) @@ -29,7 +29,7 @@ module GitStats def lines_deleted_by_author_by_date(limit = 4) Chart.new do |f| f.multi_date_chart( - data: @authors.sort_by { |author| -author.lines_deleted }[0..limit].map { |author| {name: author.email, data: author.lines_deleted_by_date} }, + data: @authors.sort_by { |author| -author.lines_deleted }[0..limit].map { |author| {name: author.email, data: author.deletions_by_date} }, title: :lines_by_date.t, y_text: :lines.t ) diff --git a/spec/integration/activity_spec.rb b/spec/integration/activity_spec.rb index 5af77d778..b9a5b7506 100644 --- a/spec/integration/activity_spec.rb +++ b/spec/integration/activity_spec.rb @@ -1,7 +1,8 @@ -require 'spec_helper' +require 'integration/shared' describe GitStats::GitData::Activity do - let(:repo) { build(:test_repo, last_commit_hash: '872955c') } + include_context "shared" + let(:activity) { repo.activity } it 'should count commits by hour' do diff --git a/spec/integration/author_spec.rb b/spec/integration/author_spec.rb index 29afdefd7..94036fa55 100644 --- a/spec/integration/author_spec.rb +++ b/spec/integration/author_spec.rb @@ -1,7 +1,7 @@ -require 'spec_helper' +require 'integration/shared' describe GitStats::GitData::Activity do - let(:repo) { build(:test_repo, last_commit_hash: '872955c') } + include_context "shared" let(:tg) { repo.authors.by_email('tomasz.gieniusz@gmail.com') } let(:jd) { repo.authors.by_email('john.doe@gmail.com') } @@ -41,5 +41,26 @@ describe GitStats::GitData::Activity do tg.activity.by_year_month.should == {2012 => {10 => 8}} jd.activity.by_year_month.should == {2012 => {10 => 2}} end + + it 'should count commits_sum_by_date' do + tg.commits_sum_by_date.map { |d, s| d }.should == tg_commit_dates + tg.commits_sum_by_date.map { |d, s| s }.should == [1, 2, 3, 4, 5, 6, 7, 8] + jd.commits_sum_by_date.map { |d, s| d }.should == jd_commit_dates + jd.commits_sum_by_date.map { |d, s| s }.should == [1, 2] + end + + it 'should count lines_added_by_date' do + tg.insertions_by_date.map { |d, s| d }.should == tg_commit_dates + tg.insertions_by_date.map { |d, s| s }.should == [4, 9, 14, 15, 20, 1020, 1021, 1021] + jd.insertions_by_date.map { |d, s| d }.should == jd_commit_dates + jd.insertions_by_date.map { |d, s| s }.should == [3, 103] + end + + it 'should count lines_deleted_by_date' do + tg.deletions_by_date.map { |d, s| d }.should == tg_commit_dates + tg.deletions_by_date.map { |d, s| s }.should == [0, 0, 4, 4, 9, 9, 10, 10] + jd.deletions_by_date.map { |d, s| d }.should == jd_commit_dates + jd.deletions_by_date.map { |d, s| s }.should == [0, 0] + end end end \ No newline at end of file diff --git a/spec/integration/file_spec.rb b/spec/integration/file_spec.rb index 34c5e0681..e15e2b130 100644 --- a/spec/integration/file_spec.rb +++ b/spec/integration/file_spec.rb @@ -1,7 +1,7 @@ -require 'spec_helper' +require 'integration/shared' describe GitStats::GitData::Repo do - let(:repo) { build(:test_repo, last_commit_hash: '872955c') } + include_context "shared" it 'should gather all files in repo' do repo.files.map(&:filename).should =~ %w(long_second.haml long.txt second.txt test2.rb test.rb test.txt) diff --git a/spec/integration/repo_spec.rb b/spec/integration/repo_spec.rb index 39534bc21..6741e08e5 100644 --- a/spec/integration/repo_spec.rb +++ b/spec/integration/repo_spec.rb @@ -1,25 +1,10 @@ -require 'spec_helper' +require 'integration/shared' describe GitStats::GitData::Repo do - let(:repo) { build(:test_repo, last_commit_sha: '872955c') } - let(:commit_dates) { [ - DateTime.parse('2012-10-19 10:44:34 +0200'), - DateTime.parse('2012-10-19 10:46:10 +0200'), - DateTime.parse('2012-10-19 10:46:56 +0200'), - DateTime.parse('2012-10-19 10:47:35 +0200'), - DateTime.parse('2012-10-20 12:49:02 +0200'), - DateTime.parse('2012-10-21 12:49:02 +0200'), - DateTime.parse('2012-10-21 12:54:02 +0200'), - DateTime.parse('2012-10-21 13:20:00 +0200'), - DateTime.parse('2012-10-24 15:49:02 +0200'), - DateTime.parse('2012-10-26 17:05:25 +0200'), - ] } + include_context "shared" it 'should gather all authors' do - repo.authors.should =~ [ - build(:author, repo: repo, name: "Tomasz Gieniusz", email: "tomasz.gieniusz@gmail.com"), - build(:author, repo: repo, name: "John Doe", email: "john.doe@gmail.com"), - ] + repo.authors.should =~ expected_authors end it 'should calculate correct commits period' do @@ -64,4 +49,19 @@ describe GitStats::GitData::Repo do repo.lines_by_extension.should == {'.haml' => 100, '.txt' => 1008, '.rb' => 6} end + it 'should count commits_count_by_author' do + repo.commits_count_by_author.keys.should == expected_authors + repo.commits_count_by_author.values.should == [8, 2] + end + + it 'should count lines_added_by_author' do + repo.lines_added_by_author.keys.should == expected_authors + repo.lines_added_by_author.values.should == [1021, 103] + end + + it 'should count lines_deleted_by_author' do + repo.lines_deleted_by_author.keys.should == expected_authors + repo.lines_deleted_by_author.values.should == [10, 0] + end + end \ No newline at end of file diff --git a/spec/integration/shared.rb b/spec/integration/shared.rb new file mode 100644 index 000000000..3a8c7e464 --- /dev/null +++ b/spec/integration/shared.rb @@ -0,0 +1,36 @@ +require 'spec_helper' + +shared_context "shared" do + let(:repo) { build(:test_repo, last_commit_sha: '872955c') } + let(:commit_dates) { [ + DateTime.parse('2012-10-19 10:44:34 +0200'), + DateTime.parse('2012-10-19 10:46:10 +0200'), + DateTime.parse('2012-10-19 10:46:56 +0200'), + DateTime.parse('2012-10-19 10:47:35 +0200'), + DateTime.parse('2012-10-20 12:49:02 +0200'), + DateTime.parse('2012-10-21 12:49:02 +0200'), + DateTime.parse('2012-10-21 12:54:02 +0200'), + DateTime.parse('2012-10-21 13:20:00 +0200'), + DateTime.parse('2012-10-24 15:49:02 +0200'), + DateTime.parse('2012-10-26 17:05:25 +0200'), + ] } + let(:tg_commit_dates) { [ + DateTime.parse('2012-10-19 10:44:34 +0200'), + DateTime.parse('2012-10-19 10:46:10 +0200'), + DateTime.parse('2012-10-19 10:46:56 +0200'), + DateTime.parse('2012-10-19 10:47:35 +0200'), + DateTime.parse('2012-10-20 12:49:02 +0200'), + DateTime.parse('2012-10-21 12:49:02 +0200'), + DateTime.parse('2012-10-21 13:20:00 +0200'), + DateTime.parse('2012-10-26 17:05:25 +0200'), + ] } + let(:jd_commit_dates) { [ + DateTime.parse('2012-10-21 12:54:02 +0200'), + DateTime.parse('2012-10-24 15:49:02 +0200'), + ] } + + let(:expected_authors) { [ + build(:author, repo: repo, name: "Tomasz Gieniusz", email: "tomasz.gieniusz@gmail.com"), + build(:author, repo: repo, name: "John Doe", email: "john.doe@gmail.com"), + ] } +end \ No newline at end of file