spec/unit/stream_spec.rb in bulldog-0.0.5 vs spec/unit/stream_spec.rb in bulldog-0.0.6
- old
+ new
@@ -80,10 +80,44 @@
stream = stream('content')
stream.file_name.should be_nil
end
end
end
+
+ describe "#reload" do
+ if options[:reloadable]
+ it "should make #size return the new size of the file" do
+ stream = stream('content')
+ update_target(stream, 'new content')
+ stream.reload
+ stream.size.should == 'new content'.size
+ end
+
+ it "should make #content_type return the new content type of the file" do
+ jpg_data = File.read(test_path('test.jpg'))
+ png_data = File.read(test_path('test.png'))
+ stream = stream(jpg_data)
+ stream.content_type.should =~ %r'\Aimage/jpeg'
+ update_target(stream, png_data)
+ stream.reload
+ stream.content_type.should =~ %r'\Aimage/png'
+ end
+ else
+ it "should not change the result of #size" do
+ stream = stream('content')
+ stream.reload
+ stream.size.should == 'content'.size
+ end
+
+ it "should not change the result of #content_type" do
+ jpg_data = File.read(test_path('test.jpg'))
+ stream = stream(jpg_data)
+ stream.reload
+ stream.content_type.should =~ %r'\Aimage/jpeg'
+ end
+ end
+ end
end
describe 'for a small uploaded file' do
it_should_behave_like_all_streams :file_name => :original_path
@@ -98,42 +132,42 @@
describe 'for a large uploaded file' do
it_should_behave_like_all_streams :file_name => :original_path
def object(content)
- stringio = StringIO.new(content)
- class << stringio
+ tempfile = Tempfile.new('bulldog-spec')
+ tempfile.print(content)
+ class << tempfile
attr_accessor :original_path
end
- stringio
+ tempfile
end
end
- describe 'for a StringIO' do
+ describe Stream::ForStringIO do
it_should_behave_like_all_streams
def object(content)
- tempfile = Tempfile.new('bulldog-spec')
- tempfile.print(content)
- class << tempfile
+ stringio = StringIO.new(content)
+ class << stringio
attr_accessor :original_path
end
- tempfile
+ stringio
end
end
- describe 'for a Tempfile' do
+ describe Stream::ForTempfile do
it_should_behave_like_all_streams
def object(content)
file = Tempfile.new('bulldog-spec')
file.print(content)
file
end
end
- describe 'for a File opened for reading' do
+ describe "Stream::ForFile (opened for writing)" do
it_should_behave_like_all_streams :file_name => :basename
def object(content)
path = "#{temporary_directory}/file"
File.open(path, 'w'){|f| f.print content}
@@ -150,31 +184,35 @@
File.read(path).should == 'content'
end
end
end
- describe 'for a File opened for writing' do
+ describe "Stream::ForFile (opened for writing)" do
it_should_behave_like_all_streams :file_name => :basename
def object(content)
file = File.open("#{temporary_directory}/file", 'w')
file.print content
autoclose_stream(file)
end
end
- describe 'for an SavedFile' do
- it_should_behave_like_all_streams :file_name => :file_name
+ describe Stream::ForSavedFile do
+ it_should_behave_like_all_streams :file_name => :file_name, :reloadable => true
def object(content)
path = "#{temporary_directory}/file"
open(path, 'w'){|f| f.print content}
SavedFile.new(path)
end
+
+ def update_target(stream, content)
+ open(stream.target.path, 'w'){|f| f.print content}
+ end
end
- describe 'for a MissingFile' do
+ describe Stream::ForMissingFile do
it_should_behave_like_all_streams :file_name => :file_name
def object(content)
path = "#{temporary_directory}/missing-file"
open(path, 'w'){|f| f.print content}
@@ -208,15 +246,17 @@
File.read(path).should == ''
end
end
end
- describe 'for an IO' do
+ describe Stream::ForIO do
it_should_behave_like_all_streams
def object(content)
- io = IO.popen("echo -n #{content}")
- autoclose_stream(io)
+ readable, writable = IO.pipe
+ writable.print content
+ writable.close
+ autoclose_stream(readable)
end
describe "#path" do
it "should preserve the file extension if an #original_path is available" do
io = object('content')