Implemented File#<< and File#close

This commit is contained in:
Pat Nakajima 2009-07-16 16:19:03 -04:00
parent d711edc590
commit 2d31ba9ebb
2 changed files with 28 additions and 1 deletions

View File

@ -193,9 +193,15 @@ module FakeFS
@path = path
@mode = mode
@file = FileSystem.find(path)
@open = true
end
def close
@open = false
end
def read
raise IOError.new('closed stream') unless @open
@file.content
end
@ -208,6 +214,8 @@ module FakeFS
end
def write(content)
raise IOError.new('closed stream') unless @open
if !File.exists?(@path)
@file = FileSystem.add(path, MockFile.new)
end
@ -215,6 +223,7 @@ module FakeFS
@file.content += content
end
alias_method :print, :write
alias_method :<<, :write
def flush; self; end
end

View File

@ -94,6 +94,14 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal "Yatta!", File.read(path)
end
def test_can_write_to_files
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 'Yada Yada', File.read(path)
end
def test_can_read_with_File_readlines
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
@ -104,6 +112,16 @@ class FakeFSTest < Test::Unit::TestCase
assert_equal ["Yatta!", "woot"], File.readlines(path)
end
def test_File_close_disallows_further_access
path = '/path/to/file.txt'
file = File.open(path, 'w')
file.write 'Yada'
file.close
assert_raise IOError do
file.read
end
end
def test_can_read_from_file_objects
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
@ -445,7 +463,7 @@ class FakeFSTest < Test::Unit::TestCase
FileUtils.ln_s 'subdir', 'new'
assert_equal 'works', File.open('new/nother'){|f| f.read }
end
def test_files_can_be_touched
FileUtils.touch('touched_file')
assert File.exists?('touched_file')