Added spec for author_list.

This commit is contained in:
@@@No user configured@@@ 2014-09-25 08:05:05 +02:00
parent dc8f578396
commit 229fd0a42f
3 changed files with 67 additions and 14 deletions

View file

@ -4,8 +4,8 @@ require "bundler/gem_tasks"
require 'rake/testtask'
Rake::TestTask.new do |t|
t.warning = true
t.verbose = true
#t.warning = true
#t.verbose = true
t.libs << "spec"
t.test_files = FileList['spec/**/*_spec.rb']
end

View file

@ -1,24 +1,34 @@
require 'pp'
module TimeCost
class AuthorList
class UnknownAuthor < RuntimeError ; end
# Prepare an empty index (local)
def initialize
@count = 0
@store = {}
@author_to_id = {}
end
# Return local user id for git user
# FIXME: should handle multiple names for same user
def parse gitauthor
invert_store = @store.invert
result = 0
if invert_store.include? gitauthor then
result = invert_store[gitauthor]
def add author
if @author_to_id.include? author then
result = @author_to_id[author]
else
@store[gitauthor] = @count
@author_to_id[author] = @count
result = @count
@count += 1
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

View file

@ -3,8 +3,51 @@
require_relative 'spec_helper'
require 'minitest/spec'
describe "simple failing test" do
it "fails" do
assert 1 < 0
require 'timecost'
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