Implemented File#<< and File#close
This commit is contained in:
parent
d711edc590
commit
2d31ba9ebb
@ -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
|
||||
|
@ -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|
|
||||
|
Loading…
Reference in New Issue
Block a user