Skip to content

Commit

Permalink
Added basic specs for Brice::History.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Aug 11, 2011
1 parent f1fc759 commit 448de6d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--colour
--debug
--require spec/spec_helper
97 changes: 97 additions & 0 deletions spec/brice/history_spec.rb
@@ -0,0 +1,97 @@
require 'brice/history'

describe Brice::History do

def hist
@hist_store ||= []
end

def init_hist(opt = {})
@hist = Brice::History.new(opt.merge(:path => @path), hist)
end

def saved_hist
@hist.send(:save_history)
File.read(@path)
end

def compare_hist(input, result = input, saved = result)
input.each { |i| hist.push(i) }
hist.should == result
saved_hist.should == saved.map { |i| "#{i}\n" }.join
end

describe 'no uniq, no merge' do

before :each do
init_hist(:uniq => false, :merge => false)
end

example { compare_hist(%w[]) }
example { compare_hist(%w[1]) }
example { compare_hist(%w[1 2 3]) }
example { compare_hist(%w[1] * 3, %w[1 1 1]) }
example { compare_hist(%w[1 2 3 1], %w[1 2 3 1]) }

end

describe 'uniq, no merge' do

before :each do
init_hist(:uniq => true, :merge => false)
end

example { compare_hist(%w[]) }
example { compare_hist(%w[1]) }
example { compare_hist(%w[1 2 3]) }
example { compare_hist(%w[1] * 3, %w[1 1 1], %w[1]) }
example { compare_hist(%w[1 2 3 1], %w[1 2 3 1], %w[1 2 3]) }

end

describe 'uniq, merge' do

before :each do
init_hist(:uniq => true, :merge => true)
end

example { compare_hist(%w[]) }
example { compare_hist(%w[1]) }
example { compare_hist(%w[1 2 3]) }
example { compare_hist(%w[1] * 3, %w[1]) }

# TODO: describe merging

end

describe 'reverse uniq, no merge' do

before :each do
init_hist(:uniq => :reverse, :merge => false)
end

example { compare_hist(%w[]) }
example { compare_hist(%w[1]) }
example { compare_hist(%w[1 2 3]) }
example { compare_hist(%w[1] * 3, %w[1 1 1], %w[1]) }
example { compare_hist(%w[1 2 3 1], %w[1 2 3 1], %w[2 3 1]) }

end

describe 'reverse uniq, merge' do

before :each do
init_hist(:uniq => :reverse, :merge => true)
end

example { compare_hist(%w[]) }
example { compare_hist(%w[1]) }
example { compare_hist(%w[1 2 3]) }
example { compare_hist(%w[1] * 3, %w[1]) }
example { compare_hist(%w[1 2 3 1], %w[2 3 1]) }

# TODO: describe merging

end

end
21 changes: 21 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,21 @@
$:.unshift('lib') unless $:.first == 'lib'

require 'tempfile'

RSpec.configure { |config|
config.before(:each) { open_tempfile }
config.after(:each) { close_tempfile }

config.include(Module.new {
def open_tempfile
@temp = Tempfile.open("wirble_spec_#{object_id}_temp")
@path = path = @temp.path

Kernel.at_exit { File.unlink(path) if File.file?(path) }
end

def close_tempfile
@temp.close(true) if @temp
end
})
}

0 comments on commit 448de6d

Please sign in to comment.