Skip to content
Browse files

Added basic specs for Brice::History.

  • Loading branch information...
1 parent f1fc759 commit 448de6d1c85e1446b99094a54755f43c0895020c @blackwinter committed Aug 11, 2011
Showing with 121 additions and 0 deletions.
  1. +3 −0 .rspec
  2. +97 −0 spec/brice/history_spec.rb
  3. +21 −0 spec/spec_helper.rb
View
3 .rspec
@@ -0,0 +1,3 @@
+--colour
+--debug
+--require spec/spec_helper
View
97 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
View
21 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.
Something went wrong with that request. Please try again.