Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

switched to bacon for testing, trie fixes, add trie specs

  • Loading branch information...
commit a1c43c9b21281430a2634fedf074445798284fe6 1 parent bb0313f
@minad authored
View
24 Rakefile
@@ -1,24 +1,11 @@
require 'rake'
require 'rake/rdoctask'
-begin
- require 'spec/rake/spectask'
-rescue LoadError
- puts %{To use rspec for testing you must install the rspec gem:
- gem install rspec}
- exit 0
-end
-
-desc "Run all specs"
-Spec::Rake::SpecTask.new(:spec) do |t|
- t.spec_opts = ['-cfs', '--backtrace']
- t.spec_files = FileList['test/**/*_spec.rb']
-end
+task :default => :test
-desc "Print SpecDocs"
-Spec::Rake::SpecTask.new(:doc) do |t|
- t.spec_opts = ["--format", "specdoc"]
- t.spec_files = FileList['test/*_spec.rb']
+desc 'Run tests with bacon'
+task :test => FileList['test/*_test.rb'] do |t|
+ sh "bacon -q -Ilib:test #{t.prerequisites.join(' ')}"
end
desc "Generate the RDoc"
@@ -28,6 +15,3 @@ Rake::RDocTask.new do |rdoc|
rdoc.main = "README.md"
rdoc.title = "Gitrb"
end
-
-desc "Run the rspec"
-task :default => :spec
View
20 gitrb.gemspec
@@ -6,12 +6,10 @@ Gem::Specification.new do |s|
s.email = 'mail@daniel-mendler.de'
s.homepage = 'https://github.com/minad/gitrb'
s.rubyforge_project = %q{gitrb}
- s.description = <<END
-Pure ruby git implementation similar to grit.
-END
+ s.description = 'Fast and lightweight ruby git implementation'
s.require_path = 'lib'
s.has_rdoc = true
- s.extra_rdoc_files = ['README.md']
+ s.extra_rdoc_files = ['README.md']
s.files = %w{
LICENSE
README.md
@@ -30,12 +28,14 @@ lib/gitrb/tree.rb
lib/gitrb/trie.rb
lib/gitrb/user.rb
lib/gitrb/util.rb
-test/bare_repository_spec.rb
+test/bare_repository_test.rb
test/benchmark.rb
-test/commit_spec.rb
-test/repository_spec.rb
-test/trie_spec.rb
-test/tree_spec.rb
+test/commit_test.rb
+test/helper.rb
+test/profile.rb
+test/repository_test.rb
+test/trie_test.rb
+test/tree_test.rb
}
+ s.add_development_dependency('bacon')
end
-
View
48 lib/gitrb/repository.rb
@@ -83,7 +83,14 @@ def in_transaction?
end
# Diff
- def diff(from, to, path = nil)
+ # Options:
+ # :to - Required target commit
+ # :from - Optional source commit (otherwise comparision with empty tree)
+ # :path - Restrict to path
+ # :detect_renames - Detect renames O(n^2)
+ # :detect_copies - Detect copies O(n^2), very slow
+ def diff(opts)
+ from, to = opts[:from], opts[:to]
if from && !(Commit === from)
raise ArgumentError, "Invalid sha: #{from}" if from !~ SHA_PATTERN
from = Reference.new(:repository => self, :id => from)
@@ -92,7 +99,10 @@ def diff(from, to, path = nil)
raise ArgumentError, "Invalid sha: #{to}" if to !~ SHA_PATTERN
to = Reference.new(:repository => self, :id => to)
end
- Diff.new(from, to, git_diff_tree('--root', '-u', '--full-index', from && from.id, to.id, '--', path))
+ Diff.new(from, to, git_diff_tree('--root', '--full-index', '-u',
+ opts[:detect_renames] ? '-M' : nil,
+ opts[:detect_copies] ? '-C' : nil,
+ from ? from.id : nil, to.id, '--', opts[:path]))
end
# All changes made inside a transaction are atomic. If some
@@ -145,12 +155,11 @@ def commit(message = '', author = nil, committer = nil)
def log(limit = 10, start = nil, path = nil)
limit = limit.to_s
start = start.to_s
+ path = path.to_s
raise ArgumentError, "Invalid limit: #{limit}" if limit !~ /^\d+$/
raise ArgumentError, "Invalid commit: #{start}" if start =~ /^\-/
- args = ['--pretty=tformat:%H%n%P%n%T%n%an%n%ae%n%at%n%cn%n%ce%n%ct%n%x00%s%n%b%x00', "-#{limit}" ]
- args << start if !start.empty?
- args << '--' << path if path && !path.empty?
- log = git_log(*args).split(/\n*\x00\n*/)
+ log = git_log('--pretty=tformat:%H%n%P%n%T%n%an%n%ae%n%at%n%cn%n%ce%n%ct%n%x00%s%n%b%x00',
+ "-#{limit}", start.empty? ? nil : start, '--', path.empty? ? nil : path).split(/\n*\x00\n*/)
commits = []
log.each_slice(2) do |data, message|
data = data.split("\n")
@@ -176,15 +185,20 @@ def get(id)
raise ArgumentError, 'No id given' if !(String === id)
if id =~ SHA_PATTERN
- raise NotFound, "Sha too short: #{id}" if id.length < 5
- list = @objects.find(id).to_a
- raise NotFound, "Sha is ambiguous: #{id}" if list.size > 1
- return list.first if list.size == 1
+ raise ArgumentError, "Sha too short: #{id}" if id.length < 5
+
+ trie = @objects.find(id)
+ raise NotFound, "Sha is ambiguous: #{id}" if trie.size > 1
+ return trie.value if !trie.empty?
elsif id =~ REVISION_PATTERN
list = git_rev_parse(id).split("\n") rescue nil
raise NotFound, "Revision not found: #{id}" if !list || list.empty?
raise NotFound, "Revision is ambiguous: #{id}" if list.size > 1
id = list.first
+
+ trie = @objects.find(id)
+ raise NotFound, "Sha is ambiguous: #{id}" if trie.size > 1
+ return trie.value if !trie.empty?
end
@logger.debug "gitrb: Loading #{id}"
@@ -207,14 +221,10 @@ def get(id)
raise NotFound, "Bad object: #{id}" if content.length != size.to_i
else
trie = @packs.find(id)
- raise NotFound, "Object not found: #{id}" if !trie
-
- id += trie.key[-(41 - id.length)...-1]
-
- list = trie.to_a
- raise NotFound, "Sha is ambiguous: #{id}" if list.size > 1
-
- pack, offset = list.first
+ raise NotFound, "Object not found: #{id}" if trie.empty?
+ raise NotFound, "Sha is ambiguous: #{id}" if trie.size > 1
+ id = trie.key
+ pack, offset = trie.value
content, type = pack.get_object(offset)
end
@@ -365,7 +375,7 @@ def get_type(id, expected)
def load_packs
@packs = Trie.new
- @objects = Synchronized.new(Trie.new)
+ @objects = Util::Synchronized.new(Trie.new)
packs_path = "#{@path}/objects/pack"
if File.directory?(packs_path)
View
99 lib/gitrb/trie.rb
@@ -4,72 +4,91 @@ class Trie
attr_reader :key, :value
- def initialize(key = '', value = nil, children = [])
+ def initialize(key = nil, value = nil, children = [])
@key = key
@value = value
@children = children
end
+ def children
+ @children.compact
+ end
+
def clear
- @key = ''
- @value = nil
- @children.clear
+ @key = @value = nil
+ @children = []
+ end
+
+ def empty?
+ self.size == 0
+ end
+
+ def each(&block)
+ yield(@value) if @value
+ children.each {|child| child.each(&block) }
+ end
+
+ def size
+ children.inject(@value ? 1 : 0) {|sum, child| sum + child.size }
end
def find(key)
- if key.empty?
- self
- else
- child = @children[key[0].ord]
- if child && key[0...child.key.length] == child.key
- child.find(key[child.key.length..-1])
+ if @key
+ if @key.index(key) == 0
+ self
+ elsif key.index(@key) == 0
+ child = @children[index(key)]
+ child ? child.find(key) : Trie.new
else
- nil
+ Trie.new
end
+ else
+ self
end
end
def insert(key, value)
- if key.empty?
+ if !@key
+ @key = key
@value = value
- self
- else
- idx = key[0].ord
- child = @children[idx]
- if child
- child.split(key) if key[0...child.key.length] != child.key
- child.insert(key[child.key.length..-1], value)
+ elsif @key == key
+ @value = value
+ elsif @key.index(key) == 0
+ child = Trie.new(@key, @value, @children)
+ @children = []
+ @children[@key[key.length].ord] = child
+ @key = key
+ @value = value
+ elsif key.index(@key) == 0
+ i = index(key)
+ if @children[i]
+ @children[i].insert(key, value)
else
- @children[idx] = Trie.new(key, value)
+ @children[i] = Trie.new(key, value)
end
- end
- end
-
- def each(&block)
- yield(@value) if !@key.empty?
- @children.compact.each {|c| c.each(&block) }
- end
+ else
+ n = 0
+ n += 1 while key[n] == @key[n]
- def values
- to_a
- end
+ child1 = Trie.new(@key, @value, @children)
+ child2 = Trie.new(key, value)
- def dup
- Trie.new(@key.dup, @value ? @value.dup : nil, @children.map {|c| c ? c.dup : nil })
+ @value = nil
+ @key = @key[0...n]
+ @children = []
+ @children[index(child1.key)] = child1
+ @children[index(child2.key)] = child2
+ end
end
def inspect
"#<Gitrb::Trie @key=#{@key.inspect}, @value=#{@value.inspect}, @children=#{@children.compact.inspect}>"
end
- def split(key)
- prefix = 0
- prefix += 1 while key[prefix] == @key[prefix]
- child = Trie.new(@key[prefix..-1], @value, @children)
- @children = []
- @children[@key[prefix].ord] = child
- @key = @key[0...prefix]
- @value = nil
+ private
+
+ def index(key)
+ key[@key.length].ord
end
end
end
View
16 lib/gitrb/util.rb
@@ -17,16 +17,16 @@ def self.read_bytes_until(io, char)
str
end
end
- end
- class Synchronized
- def initialize(obj)
- @obj = obj
- @mutex = Mutex.new
- end
+ class Synchronized
+ def initialize(obj)
+ @obj = obj
+ @mutex = Mutex.new
+ end
- def method_missing(*args)
- @mutex.synchronize { @obj.send(*args) }
+ def method_missing(*args)
+ @mutex.synchronize { @obj.send(*args) }
+ end
end
end
end
View
12 test/bare_repository_spec.rb → test/bare_repository_test.rb
@@ -1,14 +1,10 @@
-require "#{File.dirname(__FILE__)}/../lib/gitrb"
-require "#{File.dirname(__FILE__)}/helper"
-require 'pp'
+require 'helper'
describe Gitrb do
REPO = '/tmp/gitrb_test.git'
- attr_reader :repo
-
- before(:each) do
+ before do
FileUtils.rm_rf REPO
Dir.mkdir REPO
@@ -18,13 +14,13 @@
it 'should fail to initialize without a valid git repository' do
lambda {
Gitrb::Repository.new('/foo', 'master', true)
- }.should raise_error(ArgumentError)
+ }.should.raise(ArgumentError)
end
it 'should save and load entries' do
repo.root['a'] = Gitrb::Blob.new(:data => 'Hello')
repo.commit
- repo.root['a'].data.should == 'Hello'
+ repo.root['a'].data.should.equal 'Hello'
end
end
View
2  test/benchmark.rb
@@ -1,5 +1,3 @@
-Gem.activate 'diff-lcs'
-
require 'gitrb'
require 'grit'
require 'benchmark'
View
41 test/commit_spec.rb → test/commit_test.rb
@@ -1,13 +1,10 @@
-require "#{File.dirname(__FILE__)}/../lib/gitrb"
-require 'pp'
+require 'helper'
describe Gitrb::Commit do
REPO = '/tmp/gitrb_test'
- attr_reader :repo
-
- before(:each) do
+ before do
FileUtils.rm_rf REPO
Dir.mkdir REPO
@@ -25,7 +22,7 @@
content = commit.dump
- content.should == "tree #{@repo.root.id}
+ content.should.equal "tree #{@repo.root.id}
author #{user.dump}
committer #{user.dump}
@@ -40,34 +37,12 @@
commit = repo.commit("Commit Message", author, author)
IO.popen("git log") do |io|
- io.gets.should == "commit #{commit.id}\n"
- io.gets.should == "Author: hans <hans@email.de>\n"
- io.gets.should == "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
- io.gets.should == "\n"
- io.gets.should == " Commit Message\n"
+ io.gets.should.equal "commit #{commit.id}\n"
+ io.gets.should.equal "Author: hans <hans@email.de>\n"
+ io.gets.should.equal "Date: Mon Apr 20 00:00:00 2009 #{Time.now.strftime('%z')}\n"
+ io.gets.should.equal "\n"
+ io.gets.should.equal " Commit Message\n"
end
end
- it "should diff 2 commits" do
- repo.root['x'] = Gitrb::Blob.new(:data => 'a')
- repo.root['y'] = Gitrb::Blob.new(:data => "
-First Line.
-Second Line.
-Last Line.
-")
- a = repo.commit
-
- repo.root.delete('x')
- repo.root['y'] = Gitrb::Blob.new(:data => "
-First Line.
-Last Line.
-Another Line.
-")
- repo.root['z'] = Gitrb::Blob.new(:data => 'c')
-
- b = repo.commit
-
- diff = repo.diff(a, b)
- end
-
end
View
9 test/helper.rb
@@ -1,4 +1,6 @@
-module Helper
+require 'gitrb'
+
+module TestHelper
def ls_tree(id)
repo.git_ls_tree(id).split("\n").map {|line| line.split(" ") }
end
@@ -14,3 +16,8 @@ def file(file, data)
end
end
end
+
+class Bacon::Context
+ include TestHelper
+ attr_reader :repo
+end
View
1  test/profile.rb
@@ -1,4 +1,3 @@
-Gem.activate 'diff-lcs'
require 'gitrb'
require 'fileutils'
require 'grit'
View
159 test/repository_spec.rb → test/repository_test.rb
@@ -1,16 +1,10 @@
-require "#{File.dirname(__FILE__)}/../lib/gitrb"
-require "#{File.dirname(__FILE__)}/helper"
-require 'pp'
+require 'helper'
describe Gitrb do
- include Helper
-
REPO = '/tmp/gitrb_test'
- attr_reader :repo
-
- before(:each) do
+ before do
FileUtils.rm_rf REPO
Dir.mkdir REPO
@repo = Gitrb::Repository.new(:path => REPO, :create => true)
@@ -19,52 +13,52 @@
it 'should fail to initialize without a valid git repository' do
lambda do
Gitrb::Repository.new(:path => '/')
- end.should raise_error(ArgumentError)
+ end.should.raise(ArgumentError)
end
it 'should put and get objects by sha' do
blob1 = repo.put(Gitrb::Blob.new(:data => 'Hello'))
blob2 = repo.put(Gitrb::Blob.new(:data => 'World'))
- repo.get(blob1.id).should === blob1
- repo.get(blob1.id[0..4]).should === blob1
- repo.get(blob1.id[0..10]).should === blob1
+ repo.get(blob1.id).should.be.identical_to blob1
+ repo.get(blob1.id[0..4]).should.be.identical_to blob1
+ repo.get(blob1.id[0..10]).should.be.identical_to blob1
- repo.get(blob2.id).should === blob2
- repo.get(blob2.id[0..4]).should === blob2
- repo.get(blob2.id[0..10]).should === blob2
+ repo.get(blob2.id).should.be.identical_to blob2
+ repo.get(blob2.id[0..4]).should.be.identical_to blob2
+ repo.get(blob2.id[0..10]).should.be.identical_to blob2
end
it 'should find commits by revision' do
repo.root['a'] = Gitrb::Blob.new(:data => 'Hello')
commit1 = repo.commit
- repo.get('HEAD').should === commit1
- repo.get('master').should === commit1
- lambda { repo.get('HEAD^') }.should raise_error(Gitrb::NotFound)
+ repo.get('HEAD').should.be.identical_to commit1
+ repo.get('master').should.be.identical_to commit1
+ lambda { repo.get('HEAD^') }.should.raise(Gitrb::NotFound)
repo.root['a'] = Gitrb::Blob.new(:data => 'World')
commit2 = repo.commit
- repo.get('master').should === commit2
- repo.get('HEAD').should === commit2
- repo.get('HEAD^').should === commit1
- repo.get('HEAD~').should === commit1
- lambda { repo.get('HEAD^^') }.should raise_error(Gitrb::NotFound)
+ repo.get('master').should.be.identical_to commit2
+ repo.get('HEAD').should.be.identical_to commit2
+ repo.get('HEAD^').should.be.identical_to commit1
+ repo.get('HEAD~').should.be.identical_to commit1
+ lambda { repo.get('HEAD^^') }.should.raise(Gitrb::NotFound)
end
it 'should find modified entries' do
repo.root['a'] = Gitrb::Blob.new(:data => 'Hello')
- repo.root.should be_modified
+ repo.root.should.be.modified
repo.commit
- repo.root.should_not be_modified
+ repo.root.should.not.be.modified
repo.root['a'] = Gitrb::Blob.new(:data => 'Bello')
- repo.root.should be_modified
+ repo.root.should.be.modified
end
it 'should load a repo' do
@@ -73,8 +67,8 @@
repo.refresh
- repo.root['a'].data.should == 'Hello'
- repo.root['b'].data.should == 'World'
+ repo.root['a'].data.should.equal 'Hello'
+ repo.root['b'].data.should.equal 'World'
end
it 'should load folders' do
@@ -83,11 +77,11 @@
repo.refresh
- repo.root['x'].object.should be_kind_of(Gitrb::Tree)
- repo.root['y'].object.should be_kind_of(Gitrb::Tree)
+ repo.root['x'].object.should.be.kind_of(Gitrb::Tree)
+ repo.root['y'].object.should.be.kind_of(Gitrb::Tree)
- repo.root['x']['a'].data.should == 'Hello'
- repo.root['y']['b'].data.should == 'World'
+ repo.root['x']['a'].data.should.equal 'Hello'
+ repo.root['y']['b'].data.should.equal 'World'
end
it 'should detect modification' do
@@ -97,17 +91,17 @@
repo.refresh
- repo.root['x/a'].data.should == 'a'
+ repo.root['x/a'].data.should.equal 'a'
repo.transaction do
repo.root['x/a'] = Gitrb::Blob.new(:data => 'b')
- repo.root['x'].should be_modified
- repo.root.should be_modified
+ repo.root['x'].should.be.modified
+ repo.root.should.be.modified
end
repo.refresh
- repo.root['x/a'].data.should == 'b'
+ repo.root['x/a'].data.should.equal 'b'
end
it 'should resolve paths' do
@@ -116,39 +110,39 @@
repo.refresh
- repo.root['x/a'].data.should == 'Hello'
- repo.root['y/b'].data.should == 'World'
+ repo.root['x/a'].data.should.equal 'Hello'
+ repo.root['y/b'].data.should.equal 'World'
repo.root['y/b'] = Gitrb::Blob.new(:data => 'Now this')
- repo.root['y']['b'].data.should == 'Now this'
+ repo.root['y']['b'].data.should.equal 'Now this'
end
it 'should create new trees' do
repo.root['new/tree'] = Gitrb::Blob.new(:data => 'This tree')
- repo.root['new/tree'].data.should == 'This tree'
+ repo.root['new/tree'].data.should.equal 'This tree'
end
it 'should delete entries' do
repo.root['a'] = Gitrb::Blob.new(:data => 'Hello')
repo.root.delete('a')
- repo.root['a'].should be_nil
+ repo.root['a'].should.be.nil
end
it 'should move entries' do
repo.root['a/b/c'] = Gitrb::Blob.new(:data => 'Hello')
- repo.root['a/b/c'].data.should == 'Hello'
+ repo.root['a/b/c'].data.should.equal 'Hello'
repo.root.move('a/b/c', 'x/y/z')
- repo.root['a/b/c'].should be_nil
- repo.root['x/y/z'].data.should == 'Hello'
+ repo.root['a/b/c'].should.be.nil
+ repo.root['x/y/z'].data.should.equal 'Hello'
end
it 'should have a head commit' do
file 'a', 'Hello'
repo.refresh
- repo.head.should_not be_nil
+ repo.head.should.not.be.nil
end
it 'should rollback a transaction' do
@@ -164,9 +158,9 @@
rescue
end
- repo.root['a/b'].data.should == 'Hello'
- repo.root['c/d'].data.should == 'World'
- repo.root['x/a'].should be_nil
+ repo.root['a/b'].data.should.equal 'Hello'
+ repo.root['c/d'].data.should.equal 'World'
+ repo.root['x/a'].should.be.nil
end
it 'should commit a transaction' do
@@ -181,11 +175,11 @@
a = ls_tree(repo.root['a'].object.id)
x = ls_tree(repo.root['x'].object.id)
- a.should == [["100644", "blob", "b653cf27cef08de46da49a11fa5016421e9e3b32", "b"]]
- x.should == [["100644", "blob", "87d2b203800386b1cc8735a7d540a33e246357fa", "a"]]
+ a.should.equal [["100644", "blob", "b653cf27cef08de46da49a11fa5016421e9e3b32", "b"]]
+ x.should.equal [["100644", "blob", "87d2b203800386b1cc8735a7d540a33e246357fa", "a"]]
- repo.git_show(a[0][2]).should == 'Changed'
- repo.git_show(x[0][2]).should == 'Added'
+ repo.git_show(a[0][2]).should.equal 'Changed'
+ repo.git_show(x[0][2]).should.equal 'Added'
end
it "should save blobs" do
@@ -199,9 +193,9 @@
b = repo.root['b'].id
c = repo.root['c'].id
- repo.git_show(a).should == 'a'
- repo.git_show(b).should == 'b'
- repo.git_show(c).should == 'c'
+ repo.git_show(a).should.equal 'a'
+ repo.git_show(b).should.equal 'b'
+ repo.git_show(c).should.equal 'c'
end
it 'should allow only one transaction' do
@@ -223,7 +217,7 @@
repo.refresh
- repo.root['a/b'].data.should == 'Changed by second thread'
+ repo.root['a/b'].data.should.equal 'Changed by second thread'
end
it 'should find all objects' do
@@ -232,8 +226,8 @@
repo.root['d'] = Gitrb::Blob.new(:data => 'World')
repo.commit
- repo.root.to_a[0][1].data.should == 'Hello'
- repo.root.to_a[1][1].data.should == 'World'
+ repo.root.to_a[0][1].data.should.equal 'Hello'
+ repo.root.to_a[1][1].data.should.equal 'World'
end
it "should load log" do
@@ -243,8 +237,8 @@
repo.root['b'] = Gitrb::Blob.new(:data => 'b')
repo.commit 'added b'
- repo.log[0].message.should == 'added b'
- repo.log[1].message.should == 'added a'
+ repo.log[0].message.should.equal 'added b'
+ repo.log[1].message.should.equal 'added a'
end
it "should load tags" do
@@ -258,54 +252,67 @@
id = File.read(repo.path + '/refs/tags/0.1')
tag = repo.get(id)
- tag.tagtype.should == 'commit'
- tag.object.object.should == repo.head
- tag.tagger.name.should == user.name
- tag.tagger.email.should == user.email
+ tag.tagtype.should.equal 'commit'
+ tag.object.object.should.equal repo.head
+ tag.tagger.name.should.equal user.name
+ tag.tagger.email.should.equal user.email
tag.message.should =~ /message/
end
it 'should detect changes and refresh' do
file 'a', 'data'
- repo.root['a'].should be_nil
- repo.should be_changed
+ repo.root['a'].should.be.nil
+ repo.should.be.changed
repo.refresh
- repo.should_not be_changed
- repo.root['a'].data.should == 'data'
+ repo.should.not.be.changed
+ repo.root['a'].data.should.equal 'data'
end
it 'should clear cache' do
file 'a', 'data'
repo.refresh
- repo.root['a'].data.should == 'data'
+ repo.root['a'].data.should.equal 'data'
repo.clear
- repo.root['a'].data.should == 'data'
+ repo.root['a'].data.should.equal 'data'
end
it 'should forbid branch switching from within transaction' do
repo.transaction do
- lambda { repo.branch = 'test' }.should raise_error(ThreadError)
+ lambda { repo.branch = 'test' }.should.raise(ThreadError)
end
end
it 'should forbid clearing from within transaction' do
repo.transaction do
- lambda { repo.clear }.should raise_error(ThreadError)
+ lambda { repo.clear }.should.raise(ThreadError)
end
end
it 'should forbid nested transactions' do
repo.transaction do
- lambda { repo.transaction {} }.should raise_error(ThreadError)
+ lambda { repo.transaction {} }.should.raise(ThreadError)
end
end
it 'should be in transaction' do
- repo.should_not be_in_transaction
+ repo.should.not.be.in_transaction
repo.transaction do
- repo.should be_in_transaction
+ repo.should.be.in_transaction
end
- repo.should_not be_in_transaction
+ repo.should.not.be.in_transaction
+ end
+
+ it "should diff 2 commits" do
+ repo.root['x'] = Gitrb::Blob.new(:data => 'a')
+ repo.root['y'] = Gitrb::Blob.new(:data => "\nFirst Line.\nSecond Line.\nLast Line.\n")
+ a = repo.commit
+
+ repo.diff(:to => a).patch.should.include "+First Line.\n+Second Line.\n+Last Line."
+ repo.root['y'] = Gitrb::Blob.new(:data => "\nFirst Line.\nLast Line.\nAnother Line.\n")
+ b = repo.commit
+
+ repo.diff(:from => a, :to => b).patch.should.include '+Another Line.'
+ repo.diff(:from => a, :to => b).patch.should.include '-Second Line.'
end
end
View
24 test/tree_spec.rb → test/tree_test.rb
@@ -1,15 +1,9 @@
-require "#{File.dirname(__FILE__)}/../lib/gitrb"
-require "#{File.dirname(__FILE__)}/helper"
-require 'pp'
+require 'helper'
describe Gitrb::Tree do
REPO = '/tmp/gitrb_test'
- include Helper
-
- attr_reader :repo, :tree
-
- before(:each) do
+ before do
FileUtils.rm_rf REPO
Dir.mkdir REPO
@@ -34,10 +28,10 @@
"100644 b\0#{b}" +
"100644 c\0#{c}"
- repo.get(id).should be_a(Gitrb::Tree)
- repo.get(id).names.should include('a')
- repo.get(id).names.should include('b')
- repo.get(id).names.should include('c')
+ repo.get(id).should.be.instance_of(Gitrb::Tree)
+ repo.get(id).names.should.include('a')
+ repo.get(id).names.should.include('b')
+ repo.get(id).names.should.include('c')
end
it "should save trees" do
@@ -49,7 +43,7 @@
tree.save
- ls_tree(tree.id).should ==
+ ls_tree(tree.id).should.equal\
[["100644", "blob", "2e65efe2a145dda7ee51d1741299f848e5bf752e", "a"],
["100644", "blob", "63d8dbd40c23542e740659a7168a0ce3138ea748", "b"],
["100644", "blob", "3410062ba67c5ed59b854387a8bc0ec012479368", "c"]]
@@ -64,10 +58,10 @@
tree.save
- ls_tree(tree.id).should ==
+ ls_tree(tree.id).should.equal\
[["040000", "tree", "24e88cb96c396400000ef706d1ca1ed9a88251aa", "x"]]
- ls_tree("24e88cb96c396400000ef706d1ca1ed9a88251aa").should ==
+ ls_tree("24e88cb96c396400000ef706d1ca1ed9a88251aa").should.equal\
[["100644", "blob", "2e65efe2a145dda7ee51d1741299f848e5bf752e", "a"],
["100644", "blob", "63d8dbd40c23542e740659a7168a0ce3138ea748", "b"],
["100644", "blob", "3410062ba67c5ed59b854387a8bc0ec012479368", "c"]]
View
26 test/trie_spec.rb
@@ -1,26 +0,0 @@
-require "#{File.dirname(__FILE__)}/../lib/gitrb"
-require 'pp'
-
-describe Gitrb::Trie do
-
- it "should add children" do
- trie = Gitrb::Trie.new
- 0.upto(100) do |i|
- trie.insert('a' * i, i)
- end
- trie.find('').key.should == ''
- trie.find('').value.should == 0
- 1.upto(100) do |i|
- trie.find('a' * i).key.should == 'a'
- trie.find('a' * i).value.should == i
- end
- end
-
- it "should split node" do
- trie = Gitrb::Trie.new
- trie.insert("abc", 1)
- trie.insert("ab", 2)
- trie.insert("ac", 3)
- end
-
-end
View
96 test/trie_test.rb
@@ -0,0 +1,96 @@
+require 'gitrb'
+
+describe Gitrb::Trie do
+
+ before do
+ @trie = Gitrb::Trie.new
+ end
+
+ it 'should be empty' do
+ @trie.should.be.empty
+ @trie.to_a.should.be.empty
+ @trie.size.should.equal 0
+ @trie.find('').should.be.identical_to @trie
+ @trie.find('abc').should.be.identical_to @trie
+ end
+
+ it 'should find by prefix' do
+ @trie.insert('abcdef', 1)
+ @trie.find('a').should.be.identical_to @trie.find('abcdef')
+ @trie.find('abc').should.be.identical_to @trie.find('abcdef')
+ @trie.find('abc').key.should.equal 'abcdef'
+ end
+
+ it 'should have clear' do
+ @trie.insert('abc', 1)
+ @trie.should.not.be.empty
+ @trie.clear
+ @trie.should.be.empty
+ end
+
+ it 'should split shorter node' do
+ @trie.insert('a', 1)
+ @trie.key.should.equal 'a'
+ @trie.value.should.equal 1
+ @trie.size.should.equal 1
+ @trie.find('a').should.be.identical_to @trie
+
+ @trie.insert('b', 2)
+ @trie.children.size.should.equal 2
+ @trie.key.should.equal ''
+ @trie.value.should.be.nil
+ @trie.size.should.equal 2
+ @trie.find('a').key.should.equal 'a'
+ @trie.find('b').key.should.equal 'b'
+ @trie.size.should.equal 2
+ end
+
+ it 'should add child' do
+ @trie.insert('a', 1)
+
+ @trie.insert('ab', 2)
+ @trie.size.should.equal 2
+ @trie.children.size.should.equal 1
+ @trie.find('a').children.size.should.equal 1
+ @trie.find('a').key.should.equal 'a'
+ @trie.find('a').value.should.equal 1
+ @trie.find('ab').key.should.equal 'ab'
+ @trie.find('ab').value.should.equal 2
+ end
+
+ it 'should overwrite value' do
+ @trie.insert('a', 1)
+ @trie.insert('a', 2)
+ @trie.find('a').value.should.equal 2
+ @trie.size.should.equal 1
+ end
+
+ it 'should add second-level child' do
+ @trie.insert('a', 1)
+ @trie.insert('ab', 2)
+ @trie.insert('abc', 3)
+
+ @trie.find('a').children.size.should.equal 1
+ @trie.find('ab').children.size.should.equal 1
+ @trie.find('abc').value.should.equal 3
+ @trie.size.should.equal 3
+ end
+
+ it 'set value of empty node' do
+ @trie.insert('a', 1)
+ @trie.insert('b', 2)
+ @trie.size.should.equal 2
+ @trie.insert('', 3)
+ @trie.value.should.equal 3
+ @trie.size.should.equal 3
+ end
+
+ it 'should split longer node' do
+ @trie.insert('ab', 1)
+ @trie.size.should.equal 1
+ @trie.insert('a', 2)
+ @trie.size.should.equal 2
+ @trie.find('ab').value.should.equal 1
+ @trie.find('a').value.should.equal 2
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.