Fixed GridIO#gets to handle the end of a file

Neither a multi-character nor single-character separator search returned nil
after hitting the EOF.
This commit is contained in:
Ryan McGeary 2011-05-03 11:36:33 -04:00
parent eef9abfbdf
commit c672168236
2 changed files with 20 additions and 2 deletions

View File

@ -235,7 +235,7 @@ module Mongo
end end
end end
end end
result result.length > 0 ? result : nil
else else
result = '' result = ''
len = 0 len = 0
@ -244,7 +244,7 @@ module Mongo
len += 1 len += 1
break if char == separator || (length ? len >= length : false) break if char == separator || (length ? len >= length : false)
end end
result result.length > 0 ? result : nil
end end
end end

View File

@ -69,6 +69,24 @@ class GridIOTest < Test::Unit::TestCase
assert_equal 10, string.length assert_equal 10, string.length
end end
should "read to the end of the file one line at a time" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
bytes = 0
while string = file.gets
bytes += string.length
end
assert_equal 1_000_000, bytes
end
should "read to the end of the file one multi-character separator at a time" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
bytes = 0
while string = file.gets("45")
bytes += string.length
end
assert_equal 1_000_000, bytes
end
should "read to a given separator" do should "read to a given separator" do
file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id}) file = GridIO.new(@files, @chunks, nil, "r", :query => {:_id => @file.files_id})
string = file.gets("5") string = file.gets("5")