From 229fd0a42fed5fb5340996beaed4130db2bcc369 Mon Sep 17 00:00:00 2001 From: "@@@No user configured@@@" <@@@No user configured@@@> Date: Thu, 25 Sep 2014 08:05:05 +0200 Subject: [PATCH] Added spec for author_list. --- Rakefile | 4 +-- lib/timecost/author_list.rb | 28 ++++++++++++++------- spec/author_list_spec.rb | 49 ++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/Rakefile b/Rakefile index 1c2876c..d4b7f59 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/lib/timecost/author_list.rb b/lib/timecost/author_list.rb index 06ba002..355952e 100644 --- a/lib/timecost/author_list.rb +++ b/lib/timecost/author_list.rb @@ -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 diff --git a/spec/author_list_spec.rb b/spec/author_list_spec.rb index ed525b1..0f26e61 100644 --- a/spec/author_list_spec.rb +++ b/spec/author_list_spec.rb @@ -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