Решение на Пета задача от Адриана Стефанова

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

Към профила на Адриана Стефанова

Резултати

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

Код

# ObjectStore is a git repo for ruby objects
class ObjectStore
attr_reader :added, :commit, :to_be_removed, :old, :branch
def self.init()
ObjectStore.new
end
def initialize
@added = Hash.new
@to_be_removed = Array.new
@old = Hash.new
end
def add(name, object)
previous = @added[name]
if previous == nil
previous = Array.new
end
previous << object
@added[name] = previous
Result.new("Added #{name} to stage.", true, object)
end
def commit(message)
changes?()
commit_map = create_commit_map
date = Time.now.strftime "%a %b %d %H:%M %z"
commit = Commit.new(message, commit_map, date)
commit_hash = calculate_commit_hash(date, message)
@old[commit_hash] = commit
@commit = commit
Result.new("#{message}\n\t#{commit_map.size} objects changed", true, nil)
end
def remove(name)
if(@commit.objects.include?(name))
@to_be_removed << name
Result.new("Added #{name} for removal", true, @commit.objects[name])
else
Result.new("Object #{name} is not committed.", false, nil)
end
end
def checkout(commit_hash)
if @old.include?commit_hash
@commit = @old[commit_hash]
Result.new("HEAD is now at #{commit_hash}", true, @committed)
else
Result.new("Commit #{commit_hash} does not exist.", false, nil)
end
end
def calculate_commit_hash(date, message)
require 'digest/sha1'
Digest::SHA1.hexdigest(date.to_s + message)
end
private
def changes?()
if @added.size == 0 && @to_be_removed.size == 0
result_message = "Nothing to commit, working directory clean."
return Result.new(result_message, true, nil)
end
end
def create_commit_map
commit_map = Hash.new
commit_map = commit_added(commit_map)
if @commit != nil
commit_map = commit_to_be_removed(commit_map)
end
commit_map
end
def commit_added(commit_map)
@added.each{ |key, value|
commit_map[key] = value.last
@added.delete(key)
}
commit_map
end
def commit_to_be_removed(commit_map)
@commit.objects.each{ |key, value|
if @to_be_removed.include?(key)
commit_map.delete(key)
end
@to_be_removed.clear
}
commit_map
end
end
class Commit
attr_accessor :objects, :message, :date
def initialize(message, objects = Hash.new, date)
@message = message
@objects = objects
@date = date
end
end
# Result is an object that contains info about the execution of a command
class Result
attr_accessor :message, :success, :result
def initialize(message, success, result)
@message, @success, @result = message, success, result
end
def success?
@success
end
def error?
! @success
end
def to_s
result_string = "message: #{@message}\n"
result_string = result_string + "result: #{result}" if result != nil
result_string
end
end

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

.FFFF.FFFFFFFFFFFFFFFFFFFF..FF

Failures:

  1) ObjectStore can commit objects
     Failure/Error: expect(repo.commit("So cool!")).to be_success("So cool!\n\t2 objects changed", repo.head.result)
     NoMethodError:
       undefined method `head' for #<ObjectStore:0x007fae678598d8>
     # /tmp/d20160111-5693-u6wju4/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)>'

  2) ObjectStore cannot commit without changed objects
     Failure/Error: expect(repo.commit("So cool!!")).to be_error("Nothing to commit, working directory clean.")
       expected #<Result:0x007fae6784e578 @message="So cool!!\n\t0 objects changed", @success=true, @result=nil> to be error "Nothing to commit, working directory clean."
     # /tmp/d20160111-5693-u6wju4/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)>'

  3) ObjectStore can remove objects
     Failure/Error: expect(repo.remove("object1")).to be_success("Added object1 for removal.", "content1")
       expected #<Result:0x007fae67843830 @message="Added object1 for removal", @success=true, @result="content1"> to be success "Added object1 for removal." and "content1"
     # /tmp/d20160111-5693-u6wju4/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)>'

  4) 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)
     NoMethodError:
       undefined method `head' for #<ObjectStore:0x007fae6783ee98>
     # /tmp/d20160111-5693-u6wju4/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)>'

  5) ObjectStore can show head
     Failure/Error: expect(repo.head).to be_success("There we go", last_commit)
     NoMethodError:
       undefined method `head' for #<ObjectStore:0x007fae674bfb50>
     # /tmp/d20160111-5693-u6wju4/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)>'

  6) ObjectStore cannot show head for empty repository
     Failure/Error: expect(repo.head).to be_error("Branch master does not have any commits yet.")
     NoMethodError:
       undefined method `head' for #<ObjectStore:0x007fae674b9b60>
     # /tmp/d20160111-5693-u6wju4/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)>'

  7) 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 nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  8) 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 nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  9) 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 nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  10) ObjectStore shows the log for the current branch only
     Failure/Error: repo.branch.create("develop")
     NoMethodError:
       undefined method `create' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/spec.rb:116: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 cannot show log for empty repository
     Failure/Error: expect(repo.log).to be_error("Branch master does not have any commits yet.")
     NoMethodError:
       undefined method `log' for #<ObjectStore:0x007fae6745f7c8>
     # /tmp/d20160111-5693-u6wju4/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)>'

  12) ObjectStore can list branches
     Failure/Error: repo.branch.create("develop")
     NoMethodError:
       undefined method `create' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/spec.rb:138: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 can create branches
     Failure/Error: expect(repo.branch.create("develop")).to be_success("Created branch develop.")
     NoMethodError:
       undefined method `create' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  14) ObjectStore cannot create branch if already exists
     Failure/Error: expect(repo.branch.create("master")).to be_error("Branch master already exists.")
     NoMethodError:
       undefined method `create' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  15) ObjectStore can switch branches
     Failure/Error: repo.branch.create("develop")
     NoMethodError:
       undefined method `create' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/spec.rb:157: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 switch to nonexisting branch
     Failure/Error: expect(repo.branch.checkout("develop")).to be_error("Branch develop does not exist.")
     NoMethodError:
       undefined method `checkout' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  17) ObjectStore can remove branch
     Failure/Error: repo.branch.create("develop")
     NoMethodError:
       undefined method `create' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/spec.rb:173: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 remove current branch
     Failure/Error: expect(repo.branch.remove("master")).to be_error("Cannot remove current branch.")
     NoMethodError:
       undefined method `remove' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  19) ObjectStore cannot remove nonexisting branch
     Failure/Error: expect(repo.branch.remove("develop")).to be_error("Branch develop does not exist.")
     NoMethodError:
       undefined method `remove' for nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  20) ObjectStore can be initialized with block
     Failure/Error: expect(repo.head).to be_success("Second commit", $second_commit)
     NoMethodError:
       undefined method `head' for #<ObjectStore:0x007fae673c3490>
     # /tmp/d20160111-5693-u6wju4/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)>'

  21) ObjectStore can return objects
     Failure/Error: expect(repo.get("number")).to be_success("Found object number.", 21)
     NoMethodError:
       undefined method `get' for #<ObjectStore:0x007fae673bb420>
     # /tmp/d20160111-5693-u6wju4/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)>'

  22) 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 #<ObjectStore:0x007fae673af468>
     # /tmp/d20160111-5693-u6wju4/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)>'

  23) 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 #<ObjectStore:0x007fae673ab9f8>
     # /tmp/d20160111-5693-u6wju4/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)>'

  24) ObjectStore can checkout commits
     Failure/Error: expect(repo.checkout(first_commit.hash)).to be_success("HEAD is now at #{first_commit.hash}.", first_commit)
       expected #<Result:0x007fae67396ff8 @message="Commit 3598570045133737684 does not exist.", @success=false, @result=nil> to be success "HEAD is now at 3598570045133737684." and nil
     # /tmp/d20160111-5693-u6wju4/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)>'

  25) 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 nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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)>'

  26) 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 nil:NilClass
     # /tmp/d20160111-5693-u6wju4/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.04636 seconds
30 examples, 26 failures

Failed examples:

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

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

Адриана обнови решението на 23.11.2015 11:34 (преди над 8 години)

+# ObjectStore is a git repo for ruby objects
+class ObjectStore
+ attr_reader :added, :commit, :to_be_removed, :old, :branch
+
+ def self.init()
+ ObjectStore.new
+ end
+
+ def initialize
+ @added = Hash.new
+ @to_be_removed = Array.new
+ @old = Hash.new
+ end
+
+ def add(name, object)
+ previous = @added[name]
+
+ if previous == nil
+ previous = Array.new
+ end
+
+ previous << object
+ @added[name] = previous
+ Result.new("Added #{name} to stage.", true, object)
+ end
+
+ def commit(message)
+ changes?()
+ commit_map = create_commit_map
+
+ date = Time.now.strftime "%a %b %d %H:%M %z"
+ commit = Commit.new(message, commit_map, date)
+
+ commit_hash = calculate_commit_hash(date, message)
+ @old[commit_hash] = commit
+ @commit = commit
+ Result.new("#{message}\n\t#{commit_map.size} objects changed", true, nil)
+ end
+
+ def remove(name)
+ if(@commit.objects.include?(name))
+ @to_be_removed << name
+ Result.new("Added #{name} for removal", true, @commit.objects[name])
+ else
+ Result.new("Object #{name} is not committed.", false, nil)
+ end
+ end
+
+ def checkout(commit_hash)
+
+ if @old.include?commit_hash
+ @commit = @old[commit_hash]
+ Result.new("HEAD is now at #{commit_hash}", true, @committed)
+ else
+ Result.new("Commit #{commit_hash} does not exist.", false, nil)
+ end
+ end
+
+ def calculate_commit_hash(date, message)
+ require 'digest/sha1'
+ Digest::SHA1.hexdigest(date.to_s + message)
+ end
+
+ private
+
+ def changes?()
+ if @added.size == 0 && @to_be_removed.size == 0
+ result_message = "Nothing to commit, working directory clean."
+ return Result.new(result_message, true, nil)
+ end
+ end
+
+ def create_commit_map
+ commit_map = Hash.new
+ commit_map = commit_added(commit_map)
+
+ if @commit != nil
+ commit_map = commit_to_be_removed(commit_map)
+ end
+ commit_map
+ end
+
+ def commit_added(commit_map)
+ @added.each{ |key, value|
+ commit_map[key] = value.last
+ @added.delete(key)
+ }
+ commit_map
+ end
+
+ def commit_to_be_removed(commit_map)
+
+ @commit.objects.each{ |key, value|
+ if @to_be_removed.include?(key)
+ commit_map.delete(key)
+ end
+ @to_be_removed.clear
+ }
+ commit_map
+ end
+
+end
+
+class Commit
+ attr_accessor :objects, :message, :date
+
+ def initialize(message, objects = Hash.new, date)
+ @message = message
+ @objects = objects
+ @date = date
+ end
+end
+
+# Result is an object that contains info about the execution of a command
+class Result
+ attr_accessor :message, :success, :result
+
+ def initialize(message, success, result)
+ @message, @success, @result = message, success, result
+ end
+
+ def success?
+ @success
+ end
+
+ def error?
+ ! @success
+ end
+
+ def to_s
+ result_string = "message: #{@message}\n"
+ result_string = result_string + "result: #{result}" if result != nil
+ result_string
+ end
+end