Skip to content

Commit

Permalink
Checking files in the ConversionObserver::Checker.
Browse files Browse the repository at this point in the history
  • Loading branch information
ymendel committed Jun 27, 2008
1 parent 2b2bd42 commit 3212066
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 17 deletions.
23 changes: 23 additions & 0 deletions lib/conversion_observer/checker.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,5 +15,28 @@ def run
true true
end end
end end

def check_files(files)
files.select { |file| check_file(file) }
end

def check_file(file)
size = File.size(file)

if files[file] == size
files.delete(file)
true
else
files[file] = size
false
end
end


private

def clear_files
@files = {}
end
end end
end end
118 changes: 101 additions & 17 deletions spec/checker_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


describe 'when run' do describe 'when run' do
before :each do before :each do
@checker.stubs(:check_files)
@checker.stubs(:approve_files) @checker.stubs(:approve_files)
@checker.stubs(:check_file)
end end


it 'should get files to check' do it 'should get files to check' do
Expand Down Expand Up @@ -64,33 +64,117 @@
end end
end end


it 'should check files' it 'should check files' do
@checker.should respond_to(:check_files)
end


describe 'when checking files' do describe 'when checking files' do
it 'should accept files' before :each do
it 'should require files' @files = Array.new(3) { |i| stub("file #{i+1}") }
end

it 'should accept files' do
lambda { @checker.check_files(@files) }.should_not raise_error(ArgumentError)
end

it 'should require files' do
lambda { @checker.check_files }.should raise_error(ArgumentError)
end


it 'should check each file' it 'should check each file' do
it 'should return the files that checked out okay' @files.each do |file|
@checker.expects(:check_file).with(file)
end

@checker.check_files(@files)
end

it 'should return the files that checked out okay' do
@files.each_with_index do |file, i|
@checker.stubs(:check_file).with(file).returns((i%2).zero?)
end

@checker.check_files(@files).should == @files.values_at(0,2)
end
end end


it 'should check a file' it 'should check a file' do
@checker.should respond_to(:check_file)
end


describe 'when checking a file' do describe 'when checking a file' do
it 'should accept a file' before :each do
it 'should require a file' @file = stub('file')
File.stubs(:size)
end


it 'should get the file size' it 'should accept a file' do
it 'should compare the file size to the cached size' lambda { @checker.check_file(@file) }.should_not raise_error(ArgumentError)
end


describe 'when the file size is the same' do it 'should require a file' do
it 'should return true' lambda { @checker.check_file }.should raise_error(ArgumentError)
it 'should remove the cached size'
end end


describe 'when the file size is different' do it 'should get the file size' do
it 'should return false' File.expects(:size).with(@file)
it 'should leave the cached size' @checker.check_file(@file)
end

describe 'when the file size has not been cached' do
before :each do
@checker.send(:clear_files)
@size = stub('size')
File.stubs(:size).with(@file).returns(@size)
end

it 'should return false' do
@checker.check_file(@file).should be(false)
end

it 'should add the size to the cache' do
@checker.check_file(@file)
@checker.files[@file].should == @size
end
end

describe 'when the file size has been cached' do
before :each do
@checker.send(:clear_files)
@size = stub('size')
@checker.files[@file] = @size
end

describe 'and the new file size is different' do
before :each do
@other_size = stub('other size')
File.stubs(:size).with(@file).returns(@other_size)
end

it 'should return false' do
@checker.check_file(@file).should be(false)
end

it 'should modify the cached size' do
@checker.check_file(@file)
@checker.files[@file].should == @other_size
end
end

describe 'and the new file size is the same' do
before :each do
File.stubs(:size).with(@file).returns(@size)
end

it 'should return true' do
@checker.check_file(@file).should be(true)
end

it 'should remove the cached size' do
@checker.check_file(@file)
@checker.files.should_not include(@file)
end
end
end end
end end


Expand Down

0 comments on commit 3212066

Please sign in to comment.