Added spec for author_list.
This commit is contained in:
parent
dc8f578396
commit
229fd0a42f
3 changed files with 67 additions and 14 deletions
4
Rakefile
4
Rakefile
|
@ -4,8 +4,8 @@ require "bundler/gem_tasks"
|
||||||
require 'rake/testtask'
|
require 'rake/testtask'
|
||||||
|
|
||||||
Rake::TestTask.new do |t|
|
Rake::TestTask.new do |t|
|
||||||
t.warning = true
|
#t.warning = true
|
||||||
t.verbose = true
|
#t.verbose = true
|
||||||
t.libs << "spec"
|
t.libs << "spec"
|
||||||
t.test_files = FileList['spec/**/*_spec.rb']
|
t.test_files = FileList['spec/**/*_spec.rb']
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,24 +1,34 @@
|
||||||
|
|
||||||
|
require 'pp'
|
||||||
|
|
||||||
module TimeCost
|
module TimeCost
|
||||||
class AuthorList
|
class AuthorList
|
||||||
|
class UnknownAuthor < RuntimeError ; end
|
||||||
|
|
||||||
# Prepare an empty index (local)
|
# Prepare an empty index (local)
|
||||||
def initialize
|
def initialize
|
||||||
@count = 0
|
@count = 0
|
||||||
@store = {}
|
@author_to_id = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
# Return local user id for git user
|
def add author
|
||||||
# FIXME: should handle multiple names for same user
|
if @author_to_id.include? author then
|
||||||
def parse gitauthor
|
result = @author_to_id[author]
|
||||||
invert_store = @store.invert
|
|
||||||
result = 0
|
|
||||||
if invert_store.include? gitauthor then
|
|
||||||
result = invert_store[gitauthor]
|
|
||||||
else
|
else
|
||||||
@store[gitauthor] = @count
|
@author_to_id[author] = @count
|
||||||
result = @count
|
result = @count
|
||||||
@count += 1
|
@count += 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def alias author_ref, author_new
|
||||||
|
raise UnknownAuthor unless @author_to_id.include? author_ref
|
||||||
|
end
|
||||||
|
|
||||||
|
# Return local user id for git user
|
||||||
|
# FIXME: should handle multiple names for same user
|
||||||
|
def parse author
|
||||||
|
return @author_to_id[author]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,8 +3,51 @@
|
||||||
require_relative 'spec_helper'
|
require_relative 'spec_helper'
|
||||||
require 'minitest/spec'
|
require 'minitest/spec'
|
||||||
|
|
||||||
describe "simple failing test" do
|
require 'timecost'
|
||||||
it "fails" do
|
|
||||||
assert 1 < 0
|
describe TimeCost::AuthorList do
|
||||||
|
let(:author_list) { TimeCost::AuthorList.new }
|
||||||
|
let(:author_first) { "foo@example.com" }
|
||||||
|
let(:author_second) { "bar@example.com" }
|
||||||
|
|
||||||
|
describe '.new' do
|
||||||
|
it "can be created without arguments" do
|
||||||
|
assert_instance_of TimeCost::AuthorList, author_list
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.add' do
|
||||||
|
it "must accept adding authors" do
|
||||||
|
assert_respond_to author_list, :add
|
||||||
|
|
||||||
|
author_list.add "foo@example.com"
|
||||||
|
author_list.add "bar@example.com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "must assign a different id to different authors" do
|
||||||
|
author_list.add "foo@example.com"
|
||||||
|
author_list.add "bar@example.com"
|
||||||
|
id_foo = author_list.parse "foo@example.com"
|
||||||
|
id_bar = author_list.parse "bar@example.com"
|
||||||
|
refute_equal id_foo, id_bar
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.alias' do
|
||||||
|
it "must accept aliases for authors" do
|
||||||
|
assert_respond_to author_list, :alias
|
||||||
|
|
||||||
|
author_list.add author_first
|
||||||
|
author_list.alias author_first, author_second
|
||||||
|
end
|
||||||
|
|
||||||
|
it "must assign the same id to aliases authors" do
|
||||||
|
author_list.add author_first
|
||||||
|
author_list.alias author_first, author_second
|
||||||
|
|
||||||
|
id_foo = author_list.parse author_first
|
||||||
|
id_bar = author_list.parse author_second
|
||||||
|
refute_equal id_foo, id_bar
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue