Решение на Пета задача от Мирослав Лалев

Обратно към всички решения

Към профила на Мирослав Лалев

Резултати

  • 0 точки от тестове
  • 0 бонус точки
  • 0 точки общо
  • 0 успешни тест(а)
  • 30 неуспешни тест(а)

Код

class ObjectStore
def self.init(&block)
this = self.new()
if not block.nil? then
this.instance_eval(&block)
end
this
end
attr_accessor :branch
def initialize()
@branch = BranchManager.new()
end
def add(name, object)
Response.new("Added #{name} to stage.",
@branch.current_branch.add(name, object))
end
def remove(name)
if @branch.current_branch.store.contains(name) then
Response.new("Added #{name} for removal.",
@branch.current_branch.remove(name))
else
Response.new("Object #{name} is not committed.")
end
end
def commit(message)
@branch.current_branch.commit(message)
end
def checkout(commit_hash)
@branch.current_branch.checkout(commit_hash)
end
def log()
if @branch.current_branch.commits.empty? then
Response.new("Branch #{@branch.current_branch.name}" +
"does not have any commits yet.")
else
Logger.log(@branch.current_branch.commits)
end
end
def head()
@branch.current_branch.head
end
def get(name)
@branch.current_branch.get(name)
end
end
class BranchManager
attr_accessor :current_branch
attr_accessor :branches
def initialize()
@current_branch = :master
@branches = { master: Branch.new() }
end
def current_branch()
@branches[@current_branch]
end
def create(name)
if @branches.has_key?(name.to_sym) then
Response.new("Branch #{name} already exists.")
else
@branches[name.to_sym] = Branch.new(name.to_sym)
Response.new("Created branch #{name}.")
end
end
def checkout(name)
if @branches.has_key?(name.to_sym) then
@current_branch = name.to_sym
Response.new("Switched to branch #{name}.")
else
Response.new("Branch #{name} does not exist.")
end
end
def remove(name)
if @branches.has_key?(name.to_sym) then
if @current_branch == name.to_sym
Response.new("Cannot remove current branch.")
else
@branches.delete(name.to_sym)
Response.new("Removed branch #{name}.")
end
else
Response.new("Branch #{name} does not exist.")
end
end
def list()
Logger.list(@branches.keys, @current_branch)
end
def get(name)
object = @branches[@current_branch].store.get(name)
if object.nil? then
Response.new("Object #{name} is not committed.")
else
Response.new("Found object #{name}.", object)
end
end
end
class Branch
attr_accessor :store
attr_accessor :name
attr_accessor :commits
def initialize(name = :master)
@name = name
@store = Store.new()
@commits = []
@uncommited_object = []
end
def commit(message)
if @uncommited_object.empty? then
Response.new("Nothing to commit, working directory clean.")
else
@store.update(@uncommited_object)
Response.new("#{message}\n\t#{@uncommited_object.size} objects changed",
@commits.unshift(Commit.new(message, @uncommited_object.shift(
@uncommited_object.size))))
end
end
def add(name, object)
@uncommited_object.unshift(Change.new(name, :add, object))[0]
end
def remove(name)
@uncommited_object.unshift(Change.new(name, :remove))[0]
end
def checkout(commit_hash)
index = @commits.find_index { |commit|
commit.hash == commit_hash
}
if index.nil? then
Response.new("Commit #{hash} does not exist.")
else
@commits.shift(index).each { |commit|
@store.revert(commit)
}
Response.new("HEAD is now at #{hash}.", @commits[0])
end
end
def head()
if commits[0].nil? then
"Branch #{@name} does not have any commits yet."
else
commits[0]
end
end
end
class Store
attr_accessor :store
def initialize()
@store = {}
end
def add(change)
@store[change.name] = change.object
end
def remove(change)
@store.delete(change.name)
end
def contains(name)
@store[name].nil? ? false : true
end
def get(name)
@store[name]
end
def revert(commit)
commit.objects.each { |change|
case change.type
when :add
remove(change)
when :remove
add(change)
end
}
end
def update(objects)
objects.each { |change|
case change.type
when :add
add(change)
when :remove
remove(change)
end
}
end
end
class Commit
require 'digest/sha1'
attr_accessor :message
attr_accessor :date
attr_accessor :hash
attr_accessor :objects
def initialize(message, objects)
@message = message
@date = Time.new.strftime("%a %b %d %H:%M %Y +0000")
@hash = Digest::SHA1.hexdigest "#{@date}#{@message}"
@objects = objects
end
def result()
"#{@message}\n\t#{@objects.size} objects changed"
end
end
class Change
attr_accessor :name
attr_accessor :object
attr_accessor :type
def initialize(name, type, object = nil)
@name = name
@object = object
@type = type
end
end
class Logger
def self.list(branches_keys, current_branch)
(branches_keys - [current_branch]).reduce("") { |result, name|
"\t" + result + name.to_s + "\n"
} + "\t*#{current_branch.to_s}"
end
def self.log(commits)
commits.reduce("") { |result, commit|
result + "Commit #{commit.hash}\nDate: #{commit.date}\n\n\t
#{commit.message}\n"
}.strip
end
end
class Response
attr_accessor :message
attr_accessor :result
def initialize(message, result = nil)
@message = message
@result = result
end
end

Лог от изпълнението

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

Failures:

  1) ObjectStore can add objects
     Failure/Error: expect(repo.add("object", "content")).to be_success("Added object to stage.", "content")
       expected #<Response:0x007fdc9340f598 @message="Added object to stage.", @result=#<Change:0x007fdc9340f5e8 @name="object", @object="content", @type=:add>> to be success "Added object to stage." and "content"
     # /tmp/d20160111-5693-7h360d/spec.rb:23:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) ObjectStore can commit objects
     Failure/Error: expect(repo.commit("So cool!")).to be_success("So cool!\n\t2 objects changed", repo.head.result)
       expected #<Response:0x007fdc93409760 @message="So cool!\n\t2 objects changed", @result=[#<Commit:0x007fdc93409a08 @message="So cool!", @date="Mon Jan 11 11:54 2016 +0000", @hash="8d716d10b8c311cefbd338ecb4c8b3dbe800c150", @objects=[#<Change:0x007fdc93409be8 @name="object2", @object="content2", @type=:add>, #<Change:0x007fdc93409cd8 @name="object1", @object="content1", @type=:add>]>]> to be success "So cool!\n\t2 objects changed" and "So cool!\n\t2 objects changed"
     # /tmp/d20160111-5693-7h360d/spec.rb:30:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) ObjectStore cannot commit without changed objects
     Failure/Error: result.success? == false &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc933ef298>
     # /tmp/d20160111-5693-7h360d/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:35:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) ObjectStore can remove objects
     Failure/Error: expect(repo.remove("object1")).to be_success("Added object1 for removal.", "content1")
       expected #<Response:0x007fdc9307fb58 @message="Added object1 for removal.", @result=#<Change:0x007fdc9307fbd0 @name="object1", @object=nil, @type=:remove>> to be success "Added object1 for removal." and "content1"
     # /tmp/d20160111-5693-7h360d/spec.rb:43:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) ObjectStore can commit changes which include only removed objects
     Failure/Error: expect(repo.commit("Removed object2")).to be_success("Removed object2\n\t1 objects changed", repo.head.result)
       expected #<Response:0x007fdc93065f50 @message="Removed object2\n\t1 objects changed", @result=[#<Commit:0x007fdc93067508 @message="Removed object2", @date="Mon Jan 11 11:54 2016 +0000", @hash="4b755f67ef71e686fd0bdaa7de55c4d9c52105a7", @objects=[#<Change:0x007fdc930678a0 @name="object2", @object=nil, @type=:remove>]>, #<Commit:0x007fdc930684a8 @message="So cool!", @date="Mon Jan 11 11:54 2016 +0000", @hash="8d716d10b8c311cefbd338ecb4c8b3dbe800c150", @objects=[#<Change:0x007fdc93068778 @name="object2", @object="content2", @type=:add>, #<Change:0x007fdc93068a48 @name="object1", @object="content1", @type=:add>]>]> to be success "Removed object2\n\t1 objects changed" and "Removed object2\n\t1 objects changed"
     # /tmp/d20160111-5693-7h360d/spec.rb:53:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) ObjectStore cannot remove objects that are not committed
     Failure/Error: result.success? == false &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc930440f8>
     # /tmp/d20160111-5693-7h360d/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:60:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) ObjectStore can show head
     Failure/Error: expect(repo.head).to be_success("There we go", last_commit)
       expected #<Commit:0x007fdc93021a80 @message="There we go", @date="Mon Jan 11 11:54 2016 +0000", @hash="05aaf1c110b12b6d9ec9ba2e5c90a7482fdf73a2", @objects=[#<Change:0x007fdc93021e90 @name="object2", @object="content2", @type=:add>]> to be success "There we go" and [#<Commit:0x007fdc93021a80 @message="There we go", @date="Mon Jan 11 11:54 2016 +0000", @hash="05aaf1c110b12b6d9ec9ba2e5c90a7482fdf73a2", @objects=[#<Change:0x007fdc93021e90 @name="object2", @object="content2", @type=:add>]>, #<Commit:0x007fdc93022908 @message="First part... more to come...", @date="Mon Jan 11 11:54 2016 +0000", @hash="4ea2b564fb99515666c3cbee4f89b08a5da211ce", @objects=[#<Change:0x007fdc93022c28 @name="object1", @object="content1", @type=:add>]>]
     # /tmp/d20160111-5693-7h360d/spec.rb:69:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  8) ObjectStore cannot show head for empty repository
     Failure/Error: result.message == message &&
     NoMethodError:
       undefined method `message' for "Branch master does not have any commits yet.":String
     # /tmp/d20160111-5693-7h360d/spec.rb:15:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:74:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  9) ObjectStore can show log of changes for a single commit
     Failure/Error: commit_hash = Digest::SHA1.hexdigest("#{commit.date.strftime("%a %b %d %H:%M %Y %z")}#{commit.message}")
     NoMethodError:
       undefined method `date' for #<Array:0x007fdc92fe9c48>
     # /tmp/d20160111-5693-7h360d/spec.rb:82:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  10) ObjectStore can show log of changes for a single commit
     Failure/Error: commit_hash = Digest::SHA1.hexdigest("#{commit.date.strftime("%a %b %d %H:%M %Y %z")}#{commit.message}")
     NoMethodError:
       undefined method `date' for #<Array:0x007fdc92fc12c0>
     # /tmp/d20160111-5693-7h360d/spec.rb:91:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  11) ObjectStore can show log of changes for multiple commits
     Failure/Error: commit1_hash = Digest::SHA1.hexdigest("#{commit1.date.strftime(time_format)}#{commit1.message}")
     NoMethodError:
       undefined method `date' for #<Array:0x007fdc92f8e2a8>
     # /tmp/d20160111-5693-7h360d/spec.rb:105:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  12) ObjectStore shows the log for the current branch only
     Failure/Error: commit1_hash = Digest::SHA1.hexdigest("#{commit1.date.strftime(time_format)}#{commit1.message}")
     NoMethodError:
       undefined method `date' for #<Array:0x007fdc92f78430>
     # /tmp/d20160111-5693-7h360d/spec.rb:126:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  13) ObjectStore cannot show log for empty repository
     Failure/Error: expect(repo.log).to be_error("Branch master does not have any commits yet.")
       expected #<Response:0x007fdc92f6fc90 @message="Branch masterdoes not have any commits yet.", @result=nil> to be error "Branch master does not have any commits yet."
     # /tmp/d20160111-5693-7h360d/spec.rb:133:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  14) ObjectStore can list branches
     Failure/Error: actual.message == message &&
     NoMethodError:
       undefined method `message' for "\t\tdevelop\nfeature\n\t*master":String
     # /tmp/d20160111-5693-7h360d/spec.rb:6:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:140:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  15) ObjectStore can create branches
     Failure/Error: actual.success? == true &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92eca038>
     # /tmp/d20160111-5693-7h360d/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:145:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  16) ObjectStore cannot create branch if already exists
     Failure/Error: result.success? == false &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92eb90a8>
     # /tmp/d20160111-5693-7h360d/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:150:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  17) ObjectStore can switch branches
     Failure/Error: actual.success? == true &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92e843f8>
     # /tmp/d20160111-5693-7h360d/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:158:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  18) ObjectStore cannot switch to nonexisting branch
     Failure/Error: result.success? == false &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92ca4128>
     # /tmp/d20160111-5693-7h360d/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:168:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  19) ObjectStore can remove branch
     Failure/Error: actual.success? == true &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92c8fd68>
     # /tmp/d20160111-5693-7h360d/spec.rb:8:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:174:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  20) ObjectStore cannot remove current branch
     Failure/Error: result.success? == false &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92c80bb0>
     # /tmp/d20160111-5693-7h360d/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:179:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  21) ObjectStore cannot remove nonexisting branch
     Failure/Error: result.success? == false &&
     NoMethodError:
       undefined method `success?' for #<Response:0x007fdc92c72d80>
     # /tmp/d20160111-5693-7h360d/spec.rb:16:in `block (3 levels) in <top (required)>'
     # /tmp/d20160111-5693-7h360d/spec.rb:184:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  22) ObjectStore can be initialized with block
     Failure/Error: expect(repo.head).to be_success("Second commit", $second_commit)
       expected #<Commit:0x007fdc93aac950 @message="Second commit", @date="Mon Jan 11 11:54 2016 +0000", @hash="a7b79fee10c0ac1228111d6d6474a3dc94082557", @objects=[#<Change:0x007fdc93aacd10 @name="object2", @object="content", @type=:add>]> to be success "Second commit" and [#<Commit:0x007fdc93aac950 @message="Second commit", @date="Mon Jan 11 11:54 2016 +0000", @hash="a7b79fee10c0ac1228111d6d6474a3dc94082557", @objects=[#<Change:0x007fdc93aacd10 @name="object2", @object="content", @type=:add>]>, #<Commit:0x007fdc93aadb48 @message="First commit", @date="Mon Jan 11 11:54 2016 +0000", @hash="62ed7556a4eb9bf5584cac4df619c5d0d007a107", @objects=[#<Change:0x007fdc93aadda0 @name="object1", @object="content1", @type=:add>]>]
     # /tmp/d20160111-5693-7h360d/spec.rb:195:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  23) ObjectStore can return objects
     Failure/Error: expect(repo.get("number")).to be_success("Found object number.", 21)
     NoMethodError:
       undefined method `get' for #<Branch:0x007fdc93b2f0f8>
     # /tmp/d20160111-5693-7h360d/solution.rb:53:in `get'
     # /tmp/d20160111-5693-7h360d/spec.rb:202:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  24) ObjectStore cannot return not committed objects
     Failure/Error: expect(repo.get("string")).to be_error("Object string is not committed.")
     NoMethodError:
       undefined method `get' for #<Branch:0x007fdc93b2cb00>
     # /tmp/d20160111-5693-7h360d/solution.rb:53:in `get'
     # /tmp/d20160111-5693-7h360d/spec.rb:209:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  25) ObjectStore cannot return objects when no commits
     Failure/Error: expect(repo.get("string")).to be_error("Object string is not committed.")
     NoMethodError:
       undefined method `get' for #<Branch:0x007fdc93b45cb8>
     # /tmp/d20160111-5693-7h360d/solution.rb:53:in `get'
     # /tmp/d20160111-5693-7h360d/spec.rb:214:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  26) ObjectStore can checkout commits
     Failure/Error: expect(repo.checkout(first_commit.hash)).to be_success("HEAD is now at #{first_commit.hash}.", first_commit)
     TypeError:
       no implicit conversion of String into Integer
     # /tmp/d20160111-5693-7h360d/spec.rb:223:in `hash'
     # /tmp/d20160111-5693-7h360d/spec.rb:223:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  27) ObjectStore cannot checkout commits with nonexisting hashes
     Failure/Error: expect(repo.checkout("[not-present]")).to be_error("Commit [not-present] does not exist.")
       expected #<Response:0x007fdc93b3caa0 @message="Commit -4535787171697950093 does not exist.", @result=nil> to be error "Commit [not-present] does not exist."
     # /tmp/d20160111-5693-7h360d/spec.rb:231:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  28) ObjectStore cannot checkout commits in empty repository
     Failure/Error: expect(repo.checkout("[not-present]")).to be_error("Commit [not-present] does not exist.")
       expected #<Response:0x007fdc93b345d0 @message="Commit 603881904901999047 does not exist.", @result=nil> to be error "Commit [not-present] does not exist."
     # /tmp/d20160111-5693-7h360d/spec.rb:236:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  29) ObjectStore can show the objects in a repo after overwriting an object
     Failure/Error: expect(first_commit.objects).to match_array(["content1"])
     NoMethodError:
       undefined method `objects' for #<Array:0x007fdc93b23bb8>
     # /tmp/d20160111-5693-7h360d/spec.rb:243:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  30) ObjectStore can show the objects of a repo after removing an object
     Failure/Error: expect(first_commit.objects).to match_array(["content1", "content2", "content3"])
     NoMethodError:
       undefined method `objects' for #<Array:0x007fdc93b21160>
     # /tmp/d20160111-5693-7h360d/spec.rb:259:in `block (2 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.02705 seconds
30 examples, 30 failures

Failed examples:

rspec /tmp/d20160111-5693-7h360d/spec.rb:21 # ObjectStore can add objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:26 # ObjectStore can commit objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:33 # ObjectStore cannot commit without changed objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:38 # ObjectStore can remove objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:46 # ObjectStore can commit changes which include only removed objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:56 # ObjectStore cannot remove objects that are not committed
rspec /tmp/d20160111-5693-7h360d/spec.rb:63 # ObjectStore can show head
rspec /tmp/d20160111-5693-7h360d/spec.rb:72 # ObjectStore cannot show head for empty repository
rspec /tmp/d20160111-5693-7h360d/spec.rb:77 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-7h360d/spec.rb:86 # ObjectStore can show log of changes for a single commit
rspec /tmp/d20160111-5693-7h360d/spec.rb:95 # ObjectStore can show log of changes for multiple commits
rspec /tmp/d20160111-5693-7h360d/spec.rb:111 # ObjectStore shows the log for the current branch only
rspec /tmp/d20160111-5693-7h360d/spec.rb:131 # ObjectStore cannot show log for empty repository
rspec /tmp/d20160111-5693-7h360d/spec.rb:136 # ObjectStore can list branches
rspec /tmp/d20160111-5693-7h360d/spec.rb:143 # ObjectStore can create branches
rspec /tmp/d20160111-5693-7h360d/spec.rb:148 # ObjectStore cannot create branch if already exists
rspec /tmp/d20160111-5693-7h360d/spec.rb:153 # ObjectStore can switch branches
rspec /tmp/d20160111-5693-7h360d/spec.rb:166 # ObjectStore cannot switch to nonexisting branch
rspec /tmp/d20160111-5693-7h360d/spec.rb:171 # ObjectStore can remove branch
rspec /tmp/d20160111-5693-7h360d/spec.rb:177 # ObjectStore cannot remove current branch
rspec /tmp/d20160111-5693-7h360d/spec.rb:182 # ObjectStore cannot remove nonexisting branch
rspec /tmp/d20160111-5693-7h360d/spec.rb:187 # ObjectStore can be initialized with block
rspec /tmp/d20160111-5693-7h360d/spec.rb:198 # ObjectStore can return objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:205 # ObjectStore cannot return not committed objects
rspec /tmp/d20160111-5693-7h360d/spec.rb:212 # ObjectStore cannot return objects when no commits
rspec /tmp/d20160111-5693-7h360d/spec.rb:217 # ObjectStore can checkout commits
rspec /tmp/d20160111-5693-7h360d/spec.rb:227 # ObjectStore cannot checkout commits with nonexisting hashes
rspec /tmp/d20160111-5693-7h360d/spec.rb:234 # ObjectStore cannot checkout commits in empty repository
rspec /tmp/d20160111-5693-7h360d/spec.rb:239 # ObjectStore can show the objects in a repo after overwriting an object
rspec /tmp/d20160111-5693-7h360d/spec.rb:253 # ObjectStore can show the objects of a repo after removing an object

История (2 версии и 0 коментара)

Мирослав обнови решението на 23.11.2015 13:53 (преди над 8 години)

+class ObjectStore
+
+ def self.init(&block)
+ this = self.new()
+ if not block.nil? then
+ this.instance_eval(&block)
+ end
+ this
+ end
+
+ attr_accessor :branch
+
+ def initialize()
+ @branch = BranchManager.new()
+ end
+
+ def add(name, object)
+ Response.new("Added #{name} to stage.",
+ @branch.current_branch.add(name, object))
+ end
+
+ def remove(name)
+ if @branch.current_branch.store.contains(name) then
+ Response.new("Added #{name} for removal.",
+ @branch.current_branch.remove(name))
+ else
+ Response.new("Object #{name} is not committed.")
+ end
+ end
+
+ def commit(message)
+ @branch.current_branch.commit(message)
+ end
+
+ def checkout(commit_hash)
+ @branch.current_branch.checkout(commit_hash)
+ end
+
+ def log()
+ if @branch.current_branch.commits.empty? then
+ Response.new("Branch #{@branch.current_branch.name}" +
+ "does not have any commits yet.")
+ else
+ Logger.log(@branch.current_branch.commits)
+ end
+ end
+
+ def head()
+ @branch.current_branch.head
+ end
+
+ def get(name)
+ @branch.current_branch.get(name)
+ end
+end
+
+class BranchManager
+
+ attr_accessor :current_branch
+ attr_accessor :branches
+
+ def initialize()
+ @current_branch = :master
+ @branches = { master: Branch.new() }
+ end
+
+ def current_branch()
+ @branches[@current_branch]
+ end
+
+ def create(name)
+ if @branches.has_key?(name.to_sym) then
+ Response.new("Branch #{name} already exists.")
+ else
+ @branches[name.to_sym] = Branch.new(name.to_sym)
+ Response.new("Created branch #{name}.")
+ end
+ end
+
+ def checkout(name)
+ if @branches.has_key?(name.to_sym) then
+ @current_branch = name.to_sym
+ Response.new("Switched to branch #{name}.")
+ else
+ Response.new("Branch #{name} does not exist.")
+ end
+ end
+
+ def remove(name)
+ if @branches.has_key?(name.to_sym) then
+ if @current_branch == name.to_sym
+ Response.new("Cannot remove current branch.")
+ else
+ @branches.delete(name.to_sym)
+ Response.new("Removed branch #{name}.")
+ end
+ else
+ Response.new("Branch #{name} does not exist.")
+ end
+ end
+
+ def list()
+ Logger.list(@branches.keys, @current_branch)
+ end
+
+ def get(name)
+ object = @branches[@current_branch].store.get(name)
+ if object.nil? then
+ Response.new("Object #{name} is not committed.")
+ else
+ Response.new("Found object #{name}.", object)
+ end
+ end
+end
+
+class Branch
+
+ attr_accessor :store
+ attr_accessor :name
+ attr_accessor :commits
+
+ def initialize(name = :master)
+ @name = name
+ @store = Store.new()
+ @commits = []
+ @uncommited_object = []
+ end
+
+ def commit(message)
+ if @uncommited_object.empty? then
+ Response.new("Nothing to commit, working directory clean.")
+ else
+ @store.update(@uncommited_object)
+ Response.new("#{message}\n\t#{@uncommited_object.size} objects changed",
+ @commits.unshift(Commit.new(message, @uncommited_object.shift(
+ @uncommited_object.size))))
+ end
+ end
+
+ def add(name, object)
+ @uncommited_object.unshift(Change.new(name, :add, object))[0]
+ end
+
+ def remove(name)
+ @uncommited_object.unshift(Change.new(name, :remove))[0]
+ end
+
+ def checkout(commit_hash)
+ index = @commits.find_index { |commit|
+ commit.hash == commit_hash
+ }
+ if index.nil? then
+ Response.new("Commit #{hash} does not exist.")
+ else
+ @store.revert(@commits.shift(index))
+ @commits.shift(index).each { |commit|
+ @store.revert(commit)
+ }
+ Response.new("HEAD is now at #{hash}.", @commits[0])
+ end
+ end
+
+ def head()
+ if commits[0].nil? then
+ "Branch #{@name} does not have any commits yet."
+ else
+ commits[0]
+ end
+ end
+end
+
+class Store
+
+ attr_accessor :store
+
+ def initialize()
+ @store = {}
+ end
+
+ def add(change)
+ @store[change.name] = change.object
+ end
+
+ def remove(change)
+ @store.delete(change.name)
+ end
+
+ def contains(name)
+ @store[name].nil? ? false : true
+ end
+
+ def get(name)
+ @store[name]
+ end
+
+ def revert(commit)
+ commit.objects.each { |change|
+ case change.type
+ when :add
+ remove(change)
+ when :remove
+ add(change)
+ end
+ }
+ end
+
+ def update(objects)
+ objects.each { |change|
+ case change.type
+ when :add
+ add(change)
+ when :remove
+ remove(change)
+ end
+ }
+ end
+end
+
+class Commit
+
+ require 'digest/sha1'
+
+ attr_accessor :message
+ attr_accessor :date
+ attr_accessor :hash
+ attr_accessor :objects
+
+ def initialize(message, objects)
+ @message = message
+ @date = Time.new.strftime("%a %b %d %H:%M %Y +0000")
+ @hash = Digest::SHA1.hexdigest "#{@date}#{@message}"
+ @objects = objects
+ end
+
+ def result()
+ "#{@message}\n\t#{@objects.size} objects changed"
+ end
+end
+
+class Change
+
+ attr_accessor :name
+ attr_accessor :object
+ attr_accessor :type
+
+ def initialize(name, type, object = nil)
+ @name = name
+ @object = object
+ @type = type
+ end
+end
+
+class Logger
+
+ def self.list(branches_keys, current_branch)
+ (branches_keys - [current_branch]).reduce("") { |result, name|
+ "\t" + result + name.to_s + "\n"
+ } + "\t*#{current_branch.to_s}"
+ end
+
+ def self.log(commits)
+ commits.reduce("") { |result, commit|
+ result + "Commit #{commit.hash}\nDate: #{commit.date}\n\n\t
+ #{commit.message}\n"
+ }.strip
+ end
+end
+
+class Response
+
+ attr_accessor :message
+ attr_accessor :result
+
+ def initialize(message, result = nil)
+ @message = message
+ @result = result
+ end
+end

Мирослав обнови решението на 23.11.2015 15:29 (преди над 8 години)

class ObjectStore
def self.init(&block)
this = self.new()
if not block.nil? then
this.instance_eval(&block)
end
this
end
attr_accessor :branch
def initialize()
@branch = BranchManager.new()
end
def add(name, object)
Response.new("Added #{name} to stage.",
@branch.current_branch.add(name, object))
end
def remove(name)
if @branch.current_branch.store.contains(name) then
Response.new("Added #{name} for removal.",
@branch.current_branch.remove(name))
else
Response.new("Object #{name} is not committed.")
end
end
def commit(message)
@branch.current_branch.commit(message)
end
def checkout(commit_hash)
@branch.current_branch.checkout(commit_hash)
end
def log()
if @branch.current_branch.commits.empty? then
Response.new("Branch #{@branch.current_branch.name}" +
"does not have any commits yet.")
else
Logger.log(@branch.current_branch.commits)
end
end
def head()
@branch.current_branch.head
end
def get(name)
@branch.current_branch.get(name)
end
end
class BranchManager
attr_accessor :current_branch
attr_accessor :branches
def initialize()
@current_branch = :master
@branches = { master: Branch.new() }
end
def current_branch()
@branches[@current_branch]
end
def create(name)
if @branches.has_key?(name.to_sym) then
Response.new("Branch #{name} already exists.")
else
@branches[name.to_sym] = Branch.new(name.to_sym)
Response.new("Created branch #{name}.")
end
end
def checkout(name)
if @branches.has_key?(name.to_sym) then
@current_branch = name.to_sym
Response.new("Switched to branch #{name}.")
else
Response.new("Branch #{name} does not exist.")
end
end
def remove(name)
if @branches.has_key?(name.to_sym) then
if @current_branch == name.to_sym
Response.new("Cannot remove current branch.")
else
@branches.delete(name.to_sym)
Response.new("Removed branch #{name}.")
end
else
Response.new("Branch #{name} does not exist.")
end
end
def list()
Logger.list(@branches.keys, @current_branch)
end
def get(name)
object = @branches[@current_branch].store.get(name)
if object.nil? then
Response.new("Object #{name} is not committed.")
else
Response.new("Found object #{name}.", object)
end
end
end
class Branch
attr_accessor :store
attr_accessor :name
attr_accessor :commits
def initialize(name = :master)
@name = name
@store = Store.new()
@commits = []
@uncommited_object = []
end
def commit(message)
if @uncommited_object.empty? then
Response.new("Nothing to commit, working directory clean.")
else
@store.update(@uncommited_object)
Response.new("#{message}\n\t#{@uncommited_object.size} objects changed",
@commits.unshift(Commit.new(message, @uncommited_object.shift(
@uncommited_object.size))))
end
end
def add(name, object)
@uncommited_object.unshift(Change.new(name, :add, object))[0]
end
def remove(name)
@uncommited_object.unshift(Change.new(name, :remove))[0]
end
def checkout(commit_hash)
index = @commits.find_index { |commit|
commit.hash == commit_hash
}
if index.nil? then
Response.new("Commit #{hash} does not exist.")
else
- @store.revert(@commits.shift(index))
@commits.shift(index).each { |commit|
@store.revert(commit)
}
Response.new("HEAD is now at #{hash}.", @commits[0])
end
end
def head()
if commits[0].nil? then
"Branch #{@name} does not have any commits yet."
else
commits[0]
end
end
end
class Store
attr_accessor :store
def initialize()
@store = {}
end
def add(change)
@store[change.name] = change.object
end
def remove(change)
@store.delete(change.name)
end
def contains(name)
@store[name].nil? ? false : true
end
def get(name)
@store[name]
end
def revert(commit)
commit.objects.each { |change|
case change.type
when :add
remove(change)
when :remove
add(change)
end
}
end
def update(objects)
objects.each { |change|
case change.type
when :add
add(change)
when :remove
remove(change)
end
}
end
end
class Commit
require 'digest/sha1'
attr_accessor :message
attr_accessor :date
attr_accessor :hash
attr_accessor :objects
def initialize(message, objects)
@message = message
@date = Time.new.strftime("%a %b %d %H:%M %Y +0000")
@hash = Digest::SHA1.hexdigest "#{@date}#{@message}"
@objects = objects
end
def result()
"#{@message}\n\t#{@objects.size} objects changed"
end
end
class Change
attr_accessor :name
attr_accessor :object
attr_accessor :type
def initialize(name, type, object = nil)
@name = name
@object = object
@type = type
end
end
class Logger
def self.list(branches_keys, current_branch)
(branches_keys - [current_branch]).reduce("") { |result, name|
"\t" + result + name.to_s + "\n"
} + "\t*#{current_branch.to_s}"
end
def self.log(commits)
commits.reduce("") { |result, commit|
result + "Commit #{commit.hash}\nDate: #{commit.date}\n\n\t
#{commit.message}\n"
}.strip
end
end
class Response
attr_accessor :message
attr_accessor :result
def initialize(message, result = nil)
@message = message
@result = result
end
end