spec/base_spec.rb in fs-0.2.0 vs spec/base_spec.rb in fs-0.2.1

- old
+ new

@@ -35,11 +35,11 @@ File.directory?('foo').should be_true File.directory?('bar').should be_true end it 'fails if a parent dir is missing' do - lambda {FS.makedir('foo/bar')}.should raise_error + ->{ FS.makedir('foo/bar') }.should raise_error end end describe '::makedirs' do it 'creates all missing parent dirs' do @@ -66,11 +66,11 @@ end it 'fails if dir not empty' do FS.makedirs('foo/dir') FS.touch('foo/file') - lambda {FS.removedir('foo')}.should raise_error + ->{ FS.removedir('foo') }.should raise_error(Errno::ENOTEMPTY) end end describe '::removedirs' do it 'removes a dir' do @@ -267,18 +267,18 @@ FS.list.should be_empty end it 'fails on dirs' do FS.makedir('dir') - lambda {FS.remove('dir')}.should raise_error + ->{ FS.remove('dir') }.should raise_error end # FIXME: fakefs # it 'fails if the dir is not empty' do # FS.makedir('/foo') # FS.touch('/foo/bar') - # lambda {FS.remove('/foo')}.should raise_error + # ->{ FS.remove('/foo') }.should raise_error # end end describe '::write' do it 'writes content from a string' do @@ -291,21 +291,55 @@ File.open('foo.txt').read.should eql('bar') end end describe '::read' do - it 'reads the content to a string' do + before do File.open('foo.txt', 'w') {|f| f.write 'bar' } + end + + it 'reads the content to a string' do FS.read('foo.txt').should eql('bar') end - it 'reads the content to a block' do - File.open('foo.txt', 'w') {|f| f.write 'bar' } - FS.read('foo.txt') {|f| f.read.should eql('bar')} + it 'yields the content to a block' do + FS.read('foo.txt') {|content| content.should eql('bar')} end end + describe '::read_lines' do + before do + File.open('foo.txt', 'w') {|f| f.write "bar\nbaz\nqux" } + end + + it 'reads all lines to an array' do + FS.read_lines('foo.txt').should eql(['bar', 'baz', 'qux']) + end + + it 'yields all lines to a block' do + lines = [] + FS.read_lines('foo.txt') {|line| lines << line} + lines.should eql(['bar', 'baz', 'qux']) + end + end + + describe '::grep' do + before do + File.open('foo.txt', 'w') {|f| f.write "this\nis\nthe\ntest" } + end + + it 'greps matching lines to an array' do + FS.grep('foo.txt', /is/).should eql(['this', 'is']) + end + + it 'yields matching lines to a block' do + lines = [] + FS.grep('foo.txt', /is/) {|line| lines << line} + lines.should eql(['this', 'is']) + end + end + describe '::root' do it 'always returns /' do FS.root.should eql('/') end end @@ -413,10 +447,10 @@ end describe '::empty?' do it 'returns nil if the path does not exist' do FS.exist?('foobar').should be_false - FS.empty?('foobar').should be_nil + ->{ FS.empty?('foobar') }.should raise_error(Errno::ENOENT) end it 'returns if a file is empty' do FS.touch('empty.file') FS.write('content.file', 'something')