Skip to content

Commit

Permalink
fixed bug in tests which inverted sense of #added_methods, and added …
Browse files Browse the repository at this point in the history
…#added_complexity
  • Loading branch information
michaelfeathers committed Jun 2, 2011
1 parent b053311 commit 88c0790
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
14 changes: 13 additions & 1 deletion commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ module RepoDepot
class Commit < CodeHistory

def added_methods
Repository.new('', @events.select {|e| e.previous_state }).methods
Repository.new('', events.reject {|e| e.previous_state }).methods
end

def added_complexity
events.reduce(0.0) do |sum,e|
if not e.previous_state
sum += e.complexity
elsif e.previous_state.complexity >= e.complexity
sum += 0.0
else
sum += e.complexity - e.previous_state.complexity
end
end
end

end
Expand Down
38 changes: 32 additions & 6 deletions commit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,41 @@
RepoDepot::Commit.new('', []).added_methods.should be_empty
end

it "has zero methods when the commit has only events with no previous state" do
it "has one method when the commit has an event with no previous state" do
(events ||= []) << CodeEvent.new({})
RepoDepot::Commit.new('', events).added_methods.should be_empty
RepoDepot::Commit.new('', events).added_methods.count.should eq(1)
end

it "has one method when the commit has an event with a previous state" do
events = [CodeEvent.new({}), CodeEvent.new({})]
weave(events)
RepoDepot::Commit.new('', events).added_methods.count.should eq(1)
it "has no method when the commit has an event with a previous state" do
(events ||= []) << CodeEvent.new(previous_state: CodeEvent.new({}))
RepoDepot::Commit.new('', events).added_methods.count.should eq(0)
end
end

context "#added_complexity" do
it "has the complexity of added methods when there are no previous events" do
events = [CodeEvent.new(complexity: 0.5), CodeEvent.new(complexity: 0.5)]
RepoDepot::Commit.new('', events).added_complexity.should eq(1.0)
end

it "has the difference of current and previous complexities for non-added methods" do
previous = CodeEvent.new(complexity: 0.3)
current = CodeEvent.new(complexity: 1.0, previous_state: previous)
RepoDepot::Commit.new('', [current]).added_complexity.should eq(0.7)
end

it "reports zero for added complexity when a method reduces in complexity" do
previous = CodeEvent.new(complexity: 0.3)
current = CodeEvent.new(complexity: 0.1, previous_state: previous)
RepoDepot::Commit.new('', [current]).added_complexity.should eq(0.0)
end

it "dances grand coupe den finalen" do
previous = CodeEvent.new(complexity: 0.1)
events = [CodeEvent.new(complexity: 0.5),
CodeEvent.new(complexity: 0.3),
CodeEvent.new(complexity: 0.2, previous_state: previous)]
RepoDepot::Commit.new('', events).added_complexity.should eq(0.9)
end
end
end
Expand Down

0 comments on commit 88c0790

Please sign in to comment.