short_stat spec and factory_girl

This commit is contained in:
Tomasz Gieniusz 2012-10-16 20:52:22 +02:00
parent 3a29083414
commit d26c261a18
7 changed files with 71 additions and 9 deletions

View File

@ -27,5 +27,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency('rake')
gem.add_development_dependency('pry')
gem.add_development_dependency('rspec')
gem.add_development_dependency('factory_girl')
gem.add_development_dependency('simplecov')
end

View File

@ -8,6 +8,11 @@ module GitStats
calculate_stat
end
def to_s
"#{self.class} #@commit"
end
private
def calculate_stat
stat_line = Command.new(commit.repo, "git show --shortstat --oneline #{commit.hash}").run.lines.to_a[1]
if stat_line.blank?
@ -18,10 +23,6 @@ module GitStats
@deletions = stat_line[/(\d+) deletions?/, 1].to_i
end
end
def to_s
"#{self.class} #@commit"
end
end
end
end

View File

@ -1,9 +1,9 @@
require 'spec_helper'
describe GitStats::GitData::Author do
let(:repo) { GitStats::GitData::Repo.new(path: "repo_path") }
let(:author) { GitStats::GitData::Author.new(repo: repo, name: "author1", email: "author1@gmail.com") }
let(:other_author) { GitStats::GitData::Author.new(repo: repo, name: "author2", email: "author2@gmail.com") }
let(:repo) { build(:repo) }
let(:author) { build(:author, repo: repo) }
let(:other_author) { build(:author, repo: repo) }
let(:my_commits) { 10.times.map { |i| double("my_commit #{i}", :author => author) } }
let(:other_commits) { 10.times.map { |i| double("other_commit #{i}", :author => other_author) } }

21
spec/factories.rb Normal file
View File

@ -0,0 +1,21 @@
FactoryGirl.define do
initialize_with { new(attributes) }
factory :repo, class: GitStats::GitData::Repo do
path "repo_path"
end
factory :author, class: GitStats::GitData::Author do
sequence(:name) { |i| "author#{i}" }
sequence(:email) { |i| "author#{i}@gmail.com" }
association :repo, strategy: :build
end
factory :commit, class: GitStats::GitData::Commit do
sequence(:hash) { |i| i }
sequence(:stamp) { |i| i }
sequence(:date) { |i| Date.new(i) }
association :repo, strategy: :build
association :author, strategy: :build
end
end

View File

@ -1,7 +1,7 @@
require 'spec_helper'
describe GitStats::GitData::Repo do
let(:repo) { GitStats::GitData::Repo.new(path: "repo_path") }
let(:repo) { build(:repo) }
describe 'git output parsing' do
before {

31
spec/short_stat_spec.rb Normal file
View File

@ -0,0 +1,31 @@
require 'spec_helper'
describe GitStats::GitData::ShortStat do
let(:commit) { build(:commit, hash: 'abc') }
describe 'git output parsing' do
context 'parsing git show output' do
[
{content: '2 files changed, 17 insertions(+), 3 deletions(-)', expect: [2, 17, 3]},
{content: '1 file changed, 1 insertion(+), 1 deletion(-)', expect: [1, 1, 1]},
{content: '2 files changed, 3 deletions(-)', expect: [2, 0, 3]},
{content: '2 files changed, 5 insertions(+)', expect: [2, 5, 0]},
{content: '', expect: [0, 0, 0]},
].each do |test|
it "#{test[:content]} parsing" do
GitStats::GitData::Command.should_receive(:new).with(
commit.repo, 'git show --shortstat --oneline abc').and_return(
double(:run => "abc some commit
#{test[:content]}
"))
commit.short_stat.should be_a(GitStats::GitData::ShortStat)
commit.short_stat.files_changed.should == test[:expect][0]
commit.short_stat.insertions.should == test[:expect][1]
commit.short_stat.deletions.should == test[:expect][2]
end
end
end
end
end

View File

@ -1,4 +1,12 @@
require 'simplecov'
SimpleCov.start
require 'git_stats'
require 'git_stats'
require 'factory_girl'
FactoryGirl.find_definitions
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end