io-like 0.3.1 → 0.4.0.pre1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +1 -1
- data/NEWS.md +14 -1
- data/README.md +75 -94
- data/lib/io/like.rb +1916 -1314
- data/lib/io/like_helpers/abstract_io.rb +512 -0
- data/lib/io/like_helpers/blocking_io.rb +86 -0
- data/lib/io/like_helpers/buffered_io.rb +555 -0
- data/lib/io/like_helpers/character_io/basic_reader.rb +122 -0
- data/lib/io/like_helpers/character_io/converter_reader.rb +252 -0
- data/lib/io/like_helpers/character_io.rb +529 -0
- data/lib/io/like_helpers/delegated_io.rb +250 -0
- data/lib/io/like_helpers/duplexed_io.rb +259 -0
- data/lib/io/like_helpers/io.rb +21 -0
- data/lib/io/like_helpers/io_wrapper.rb +290 -0
- data/lib/io/like_helpers/pipeline.rb +77 -0
- data/lib/io/like_helpers/ruby_facts.rb +33 -0
- data/lib/io/like_helpers.rb +14 -0
- metadata +107 -224
- data/.yardopts +0 -1
- data/Rakefile +0 -228
- data/ruby.1.8.mspec +0 -7
- data/spec/binmode_spec.rb +0 -29
- data/spec/close_read_spec.rb +0 -64
- data/spec/close_spec.rb +0 -36
- data/spec/close_write_spec.rb +0 -61
- data/spec/closed_spec.rb +0 -16
- data/spec/each_byte_spec.rb +0 -38
- data/spec/each_line_spec.rb +0 -11
- data/spec/each_spec.rb +0 -11
- data/spec/eof_spec.rb +0 -11
- data/spec/fixtures/classes.rb +0 -96
- data/spec/fixtures/gets.txt +0 -9
- data/spec/fixtures/numbered_lines.txt +0 -5
- data/spec/fixtures/one_byte.txt +0 -1
- data/spec/fixtures/paragraphs.txt +0 -7
- data/spec/fixtures/readlines.txt +0 -6
- data/spec/flush_spec.rb +0 -8
- data/spec/getc_spec.rb +0 -44
- data/spec/gets_spec.rb +0 -212
- data/spec/isatty_spec.rb +0 -6
- data/spec/lineno_spec.rb +0 -84
- data/spec/output_spec.rb +0 -47
- data/spec/pos_spec.rb +0 -53
- data/spec/print_spec.rb +0 -97
- data/spec/printf_spec.rb +0 -24
- data/spec/putc_spec.rb +0 -57
- data/spec/puts_spec.rb +0 -99
- data/spec/read_spec.rb +0 -162
- data/spec/readchar_spec.rb +0 -49
- data/spec/readline_spec.rb +0 -60
- data/spec/readlines_spec.rb +0 -140
- data/spec/readpartial_spec.rb +0 -92
- data/spec/rewind_spec.rb +0 -56
- data/spec/seek_spec.rb +0 -72
- data/spec/shared/each.rb +0 -204
- data/spec/shared/eof.rb +0 -116
- data/spec/shared/pos.rb +0 -39
- data/spec/shared/tty.rb +0 -12
- data/spec/shared/write.rb +0 -53
- data/spec/sync_spec.rb +0 -56
- data/spec/sysread_spec.rb +0 -87
- data/spec/sysseek_spec.rb +0 -68
- data/spec/syswrite_spec.rb +0 -60
- data/spec/tell_spec.rb +0 -7
- data/spec/to_io_spec.rb +0 -19
- data/spec/tty_spec.rb +0 -6
- data/spec/ungetc_spec.rb +0 -118
- data/spec/write_spec.rb +0 -61
- data/spec_helper.rb +0 -49
- /data/{LICENSE-rubyspec → rubyspec/LICENSE} +0 -0
data/spec/close_read_spec.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
require 'fileutils'
|
4
|
-
|
5
|
-
describe "IO::Like#close_read" do
|
6
|
-
before :each do
|
7
|
-
@io = IO.popen('cat', "r+")
|
8
|
-
@iowrapper = DuplexedIOWrapper.open(@io)
|
9
|
-
@filename = tmp('IO_Like__close_read_test')
|
10
|
-
end
|
11
|
-
|
12
|
-
after :each do
|
13
|
-
@iowrapper.close unless @iowrapper.closed?
|
14
|
-
@io.close unless @io.closed?
|
15
|
-
File.delete(@filename) if File.exist?(@filename)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "closes the read end of a duplex I/O stream" do
|
19
|
-
@iowrapper.close_read
|
20
|
-
|
21
|
-
lambda { @iowrapper.read }.should raise_error(IOError)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "raises an IOError on subsequent invocations" do
|
25
|
-
@iowrapper.close_read
|
26
|
-
|
27
|
-
lambda { @iowrapper.close_read }.should raise_error(IOError)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "allows subsequent invocation of close" do
|
31
|
-
@iowrapper.close_read
|
32
|
-
|
33
|
-
lambda { @iowrapper.close }.should_not raise_error
|
34
|
-
end
|
35
|
-
|
36
|
-
it "raises an IOError if the stream is writable and not duplexed" do
|
37
|
-
io = File.open(@filename, 'w')
|
38
|
-
iowrapper = WritableIOWrapper.open(io)
|
39
|
-
|
40
|
-
begin
|
41
|
-
lambda { iowrapper.close_read }.should raise_error(IOError)
|
42
|
-
ensure
|
43
|
-
iowrapper.close unless iowrapper.closed?
|
44
|
-
io.close unless io.closed?
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
it "closes the stream if it is neither writable nor duplexed" do
|
49
|
-
FileUtils.touch(@filename)
|
50
|
-
io = File.open(@filename)
|
51
|
-
iowrapper = ReadableIOWrapper.open(io)
|
52
|
-
|
53
|
-
iowrapper.close_read
|
54
|
-
iowrapper.closed?.should == true
|
55
|
-
|
56
|
-
io.close
|
57
|
-
end
|
58
|
-
|
59
|
-
it "raises IOError on closed stream" do
|
60
|
-
@io.close
|
61
|
-
|
62
|
-
lambda { @io.close_read }.should raise_error(IOError)
|
63
|
-
end
|
64
|
-
end
|
data/spec/close_spec.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
|
4
|
-
describe "IO::Like#close" do
|
5
|
-
before :each do
|
6
|
-
@filename = tmp('IO_Like__close_test')
|
7
|
-
@io = File.open(@filename, 'w+')
|
8
|
-
@iowrapper = IOWrapper.open(@io)
|
9
|
-
end
|
10
|
-
|
11
|
-
after :each do
|
12
|
-
@iowrapper.close unless @iowrapper.closed?
|
13
|
-
@io.close unless @io.closed?
|
14
|
-
File.unlink(@filename) if File.exist?(@filename)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "closes the stream" do
|
18
|
-
lambda { @iowrapper.close }.should_not raise_error
|
19
|
-
@iowrapper.closed?.should == true
|
20
|
-
end
|
21
|
-
|
22
|
-
it "returns nil" do
|
23
|
-
@iowrapper.close.should == nil
|
24
|
-
end
|
25
|
-
|
26
|
-
it "makes the stream unavailable for any further data operations" do
|
27
|
-
@iowrapper.close
|
28
|
-
lambda { @iowrapper.write("attempt to write") }.should raise_error(IOError)
|
29
|
-
lambda { @iowrapper.read }.should raise_error(IOError)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "raises an IOError on subsequent invocations" do
|
33
|
-
@iowrapper.close
|
34
|
-
lambda { @iowrapper.close }.should raise_error(IOError)
|
35
|
-
end
|
36
|
-
end
|
data/spec/close_write_spec.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
|
4
|
-
describe "IO::Like#close_write" do
|
5
|
-
before :each do
|
6
|
-
@io = IO.popen('cat', 'r+')
|
7
|
-
@iowrapper = DuplexedIOWrapper.open(@io)
|
8
|
-
@filename = tmp('IO_Like__close_write_test')
|
9
|
-
end
|
10
|
-
|
11
|
-
after :each do
|
12
|
-
@iowrapper.close unless @iowrapper.closed?
|
13
|
-
@io.close unless @io.closed?
|
14
|
-
File.delete(@filename) if File.exist?(@filename)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "closes the write end of a duplex I/O stream" do
|
18
|
-
@iowrapper.close_write
|
19
|
-
lambda { @iowrapper.write("attempt to write") }.should raise_error(IOError)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "raises an IOError on subsequent invocations" do
|
23
|
-
@iowrapper.close_write
|
24
|
-
lambda { @iowrapper.close_write }.should raise_error(IOError)
|
25
|
-
end
|
26
|
-
|
27
|
-
it "allows subsequent invocation of close" do
|
28
|
-
@iowrapper.close_write
|
29
|
-
lambda { @iowrapper.close }.should_not raise_error
|
30
|
-
end
|
31
|
-
|
32
|
-
it "raises an IOError if the stream is readable and not duplexed" do
|
33
|
-
io = File.open(@filename, 'w+')
|
34
|
-
iowrapper = IOWrapper.open(io)
|
35
|
-
|
36
|
-
begin
|
37
|
-
lambda { iowrapper.close_write }.should raise_error(IOError)
|
38
|
-
ensure
|
39
|
-
iowrapper.close unless iowrapper.closed?
|
40
|
-
io.close unless io.closed?
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "closes the stream if it is neither readable nor duplexed" do
|
45
|
-
IOSpecs.writable_iowrapper do |iowrapper|
|
46
|
-
iowrapper.close_write
|
47
|
-
iowrapper.closed?.should == true
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it "flushes and closes the write stream" do
|
52
|
-
@iowrapper.puts('12345')
|
53
|
-
@iowrapper.close_write
|
54
|
-
@io.close_write
|
55
|
-
@iowrapper.read.should == "12345\n"
|
56
|
-
end
|
57
|
-
|
58
|
-
it "raises IOError on closed stream" do
|
59
|
-
lambda { IOSpecs.closed_file.close_write }.should raise_error(IOError)
|
60
|
-
end
|
61
|
-
end
|
data/spec/closed_spec.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
|
4
|
-
describe "IO::Like#closed?" do
|
5
|
-
it "returns true on closed stream" do
|
6
|
-
IOSpecs.closed_file.closed?.should == true
|
7
|
-
end
|
8
|
-
|
9
|
-
it "returns false on open stream" do
|
10
|
-
File.open(File.dirname(__FILE__) + '/fixtures/gets.txt', 'r') do |io|
|
11
|
-
ReadableIOWrapper.open(io) do |iowrapper|
|
12
|
-
iowrapper.closed?.should == false
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
data/spec/each_byte_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
|
4
|
-
describe "IO::Like#each_byte" do
|
5
|
-
it "raises IOError on closed stream" do
|
6
|
-
# each_byte must have a block in order to raise the Error.
|
7
|
-
# MRI 1.8.7 returns enumerator if block is not provided.
|
8
|
-
# See [ruby-core:16557].
|
9
|
-
lambda { IOSpecs.closed_file.each_byte {} }.should raise_error(IOError)
|
10
|
-
end
|
11
|
-
|
12
|
-
it "yields each byte" do
|
13
|
-
File.open(IOSpecs.gets_fixtures) do |io|
|
14
|
-
ReadableIOWrapper.open(io) do |iowrapper|
|
15
|
-
bytes = []
|
16
|
-
|
17
|
-
iowrapper.each_byte do |byte|
|
18
|
-
bytes << byte
|
19
|
-
break if bytes.length >= 5
|
20
|
-
end
|
21
|
-
|
22
|
-
bytes.should == [86, 111, 105, 99, 105]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
it "works on empty streams" do
|
28
|
-
@filename = tmp("IO_Like__each_byte_test")
|
29
|
-
File.open(@filename, "w+") do |io|
|
30
|
-
IOWrapper.open(io) do |iowrapper|
|
31
|
-
lambda do
|
32
|
-
iowrapper.each_byte { |b| raise IOError }
|
33
|
-
end.should_not raise_error
|
34
|
-
end
|
35
|
-
end
|
36
|
-
File.unlink(@filename) if File.exist?(@filename)
|
37
|
-
end
|
38
|
-
end
|
data/spec/each_line_spec.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
require File.dirname(__FILE__) + '/shared/each'
|
4
|
-
|
5
|
-
describe "IO::Like#each_line" do
|
6
|
-
it_behaves_like :io_like__each, :each_line
|
7
|
-
end
|
8
|
-
|
9
|
-
describe "IO::Like#each_line when passed a separator" do
|
10
|
-
it_behaves_like :io_like__each_separator, :each_line
|
11
|
-
end
|
data/spec/each_spec.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
require File.dirname(__FILE__) + '/shared/each'
|
4
|
-
|
5
|
-
describe "IO::Like#each" do
|
6
|
-
it_behaves_like :io_like__each, :each
|
7
|
-
end
|
8
|
-
|
9
|
-
describe "IO::Like#each when passed a separator" do
|
10
|
-
it_behaves_like :io_like__each_separator, :each
|
11
|
-
end
|
data/spec/eof_spec.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
require File.dirname(__FILE__) + '/shared/eof'
|
4
|
-
|
5
|
-
describe "IO::Like#eof" do
|
6
|
-
it_behaves_like :io_like__eof, :eof
|
7
|
-
end
|
8
|
-
|
9
|
-
describe "IO::Like#eof?" do
|
10
|
-
it_behaves_like :io_like__eof, :eof?
|
11
|
-
end
|
data/spec/fixtures/classes.rb
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../../lib/io/like'
|
2
|
-
|
3
|
-
class IOWrapper
|
4
|
-
include IO::Like
|
5
|
-
|
6
|
-
def self.open(io)
|
7
|
-
iow = new(io)
|
8
|
-
return iow unless block_given?
|
9
|
-
|
10
|
-
begin
|
11
|
-
yield(iow)
|
12
|
-
ensure
|
13
|
-
iow.close unless iow.closed?
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def initialize(io)
|
18
|
-
@io = io
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def unbuffered_read(length)
|
24
|
-
@io.sysread(length)
|
25
|
-
end
|
26
|
-
|
27
|
-
def unbuffered_seek(offset, whence = IO::SEEK_SET)
|
28
|
-
@io.sysseek(offset, whence)
|
29
|
-
end
|
30
|
-
|
31
|
-
def unbuffered_write(string)
|
32
|
-
@io.syswrite(string)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class DuplexedIOWrapper < IOWrapper
|
37
|
-
def duplexed?
|
38
|
-
true
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class ReadableIOWrapper < IOWrapper
|
43
|
-
def writable?
|
44
|
-
false
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class WritableIOWrapper < IOWrapper
|
49
|
-
def readable?
|
50
|
-
false
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
module IOSpecs
|
55
|
-
def self.lines
|
56
|
-
[
|
57
|
-
"Voici la ligne une.\n",
|
58
|
-
"Qui \303\250 la linea due.\n",
|
59
|
-
"\n",
|
60
|
-
"\n",
|
61
|
-
"Aqu\303\255 est\303\241 la l\303\255nea tres.\n",
|
62
|
-
"Ist hier Linie vier.\n",
|
63
|
-
"\n",
|
64
|
-
"Est\303\241 aqui a linha cinco.\n",
|
65
|
-
"Here is line six.\n"
|
66
|
-
]
|
67
|
-
end
|
68
|
-
|
69
|
-
def self.gets_fixtures
|
70
|
-
File.dirname(__FILE__) + '/gets.txt'
|
71
|
-
end
|
72
|
-
|
73
|
-
def self.gets_output
|
74
|
-
File.dirname(__FILE__) + '/gets_output.txt'
|
75
|
-
end
|
76
|
-
|
77
|
-
def self.closed_file
|
78
|
-
File.open(gets_fixtures, 'r') do |f|
|
79
|
-
ReadableIOWrapper.open(f) { |iowrapper| iowrapper }
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def self.readable_iowrapper(&b)
|
84
|
-
File.open(gets_fixtures, 'r') do |f|
|
85
|
-
ReadableIOWrapper.open(f, &b)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
def self.writable_iowrapper(&b)
|
90
|
-
File.open(gets_output, 'w') do |f|
|
91
|
-
WritableIOWrapper.open(f, &b)
|
92
|
-
end
|
93
|
-
ensure
|
94
|
-
File.delete(gets_output)
|
95
|
-
end
|
96
|
-
end
|
data/spec/fixtures/gets.txt
DELETED
data/spec/fixtures/one_byte.txt
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1
|
data/spec/fixtures/readlines.txt
DELETED
data/spec/flush_spec.rb
DELETED
data/spec/getc_spec.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
|
4
|
-
describe "IO::Like#getc" do
|
5
|
-
before :each do
|
6
|
-
@file_name = File.dirname(__FILE__) + '/fixtures/readlines.txt'
|
7
|
-
@file = File.open(@file_name, 'r')
|
8
|
-
@iowrapper = ReadableIOWrapper.open(@file)
|
9
|
-
end
|
10
|
-
|
11
|
-
after :each do
|
12
|
-
@iowrapper.close unless @iowrapper.closed?
|
13
|
-
@file.close unless @file.closed?
|
14
|
-
end
|
15
|
-
|
16
|
-
it "returns the next byte from the stream" do
|
17
|
-
@iowrapper.getc.should == 86
|
18
|
-
@iowrapper.getc.should == 111
|
19
|
-
@iowrapper.getc.should == 105
|
20
|
-
# read the rest of line
|
21
|
-
@iowrapper.readline.should == "ci la ligne une.\n"
|
22
|
-
@iowrapper.getc.should == 81
|
23
|
-
end
|
24
|
-
|
25
|
-
it "returns nil when invoked at the end of the stream" do
|
26
|
-
# read entire content
|
27
|
-
@iowrapper.read
|
28
|
-
@iowrapper.getc.should == nil
|
29
|
-
end
|
30
|
-
|
31
|
-
it "returns nil on empty stream" do
|
32
|
-
path = tmp('empty.txt')
|
33
|
-
File.open(path, "w+") do |empty|
|
34
|
-
IOWrapper.open(empty) do |iowrapper|
|
35
|
-
iowrapper.getc.should == nil
|
36
|
-
end
|
37
|
-
end
|
38
|
-
File.unlink(path)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "raises IOError on closed stream" do
|
42
|
-
lambda { IOSpecs.closed_file.getc }.should raise_error(IOError)
|
43
|
-
end
|
44
|
-
end
|
data/spec/gets_spec.rb
DELETED
@@ -1,212 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
require File.dirname(__FILE__) + '/fixtures/classes'
|
3
|
-
|
4
|
-
describe "IO::Like#gets" do
|
5
|
-
it "returns the next line of string that were separated by $/" do
|
6
|
-
IOSpecs.readable_iowrapper do |f|
|
7
|
-
IOSpecs.lines.each { |line| line.should == f.gets }
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
it "returns tainted strings" do
|
12
|
-
IOSpecs.readable_iowrapper do |f|
|
13
|
-
while (line = f.gets(nil))
|
14
|
-
line.tainted?.should == true
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
IOSpecs.readable_iowrapper do |f|
|
19
|
-
while (line = f.gets(""))
|
20
|
-
line.tainted?.should == true
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
IOSpecs.readable_iowrapper do |f|
|
25
|
-
while (line = f.gets)
|
26
|
-
line.tainted?.should == true
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
IOSpecs.readable_iowrapper do |f|
|
31
|
-
while (line = f.gets("la"))
|
32
|
-
line.tainted?.should == true
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
it "updates lineno with each invocation" do
|
38
|
-
count = 1
|
39
|
-
IOSpecs.readable_iowrapper do |f|
|
40
|
-
while (line = f.gets(nil))
|
41
|
-
f.lineno.should == count
|
42
|
-
count += 1
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
count = 1
|
47
|
-
IOSpecs.readable_iowrapper do |f|
|
48
|
-
while (line = f.gets(""))
|
49
|
-
f.lineno.should == count
|
50
|
-
count += 1
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
count = 1
|
55
|
-
IOSpecs.readable_iowrapper do |f|
|
56
|
-
while (line = f.gets)
|
57
|
-
f.lineno.should == count
|
58
|
-
count += 1
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
count = 1
|
63
|
-
IOSpecs.readable_iowrapper do |f|
|
64
|
-
while (line = f.gets("la"))
|
65
|
-
f.lineno.should == count
|
66
|
-
count += 1
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
it "updates $. with each invocation" do
|
72
|
-
count = 1
|
73
|
-
IOSpecs.readable_iowrapper do |f|
|
74
|
-
while (line = f.gets(nil))
|
75
|
-
$..should == count
|
76
|
-
count += 1
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
count = 1
|
81
|
-
IOSpecs.readable_iowrapper do |f|
|
82
|
-
while (line = f.gets(""))
|
83
|
-
$..should == count
|
84
|
-
count += 1
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
count = 1
|
89
|
-
IOSpecs.readable_iowrapper do |f|
|
90
|
-
while (line = f.gets)
|
91
|
-
$..should == count
|
92
|
-
count += 1
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
count = 1
|
97
|
-
IOSpecs.readable_iowrapper do |f|
|
98
|
-
while (line = f.gets("la"))
|
99
|
-
$..should == count
|
100
|
-
count += 1
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
# MRI LIMITATION:
|
106
|
-
# $_ will not cross out of the implementation of gets since its value is
|
107
|
-
# limited to the local scope in managed code.
|
108
|
-
#
|
109
|
-
#it "assigns the returned line to $_" do
|
110
|
-
# IOSpecs.readable_iowrapper do |f|
|
111
|
-
# IOSpecs.lines.each do |line|
|
112
|
-
# f.gets
|
113
|
-
# $line.should == line
|
114
|
-
# end
|
115
|
-
# end
|
116
|
-
#end
|
117
|
-
|
118
|
-
it "returns nil if called at the end of the stream" do
|
119
|
-
IOSpecs.readable_iowrapper do |f|
|
120
|
-
IOSpecs.lines.length.times { f.gets }
|
121
|
-
f.gets.should == nil
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
it "returns the entire content if the separator is nil" do
|
126
|
-
IOSpecs.readable_iowrapper do |f|
|
127
|
-
f.gets(nil).should == IOSpecs.lines.join('')
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
it "reads and returns all data available before a SystemCallError is raised when the separator is nil" do
|
132
|
-
file = File.open(IOSpecs.gets_fixtures)
|
133
|
-
# Overrride file.sysread to raise SystemCallError every other time it's
|
134
|
-
# called.
|
135
|
-
class << file
|
136
|
-
alias :sysread_orig :sysread
|
137
|
-
def sysread(length)
|
138
|
-
if @error_raised then
|
139
|
-
@error_raised = false
|
140
|
-
sysread_orig(length)
|
141
|
-
else
|
142
|
-
@error_raised = true
|
143
|
-
raise SystemCallError, 'Test Error'
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
ReadableIOWrapper.open(file) do |iowrapper|
|
149
|
-
lambda { iowrapper.gets(nil) }.should raise_error(SystemCallError)
|
150
|
-
iowrapper.gets(nil).should == IOSpecs.lines.join('')
|
151
|
-
end
|
152
|
-
file.close
|
153
|
-
end
|
154
|
-
|
155
|
-
# Two successive newlines in the input separate paragraphs.
|
156
|
-
# When there are more than two successive newlines, only two are kept.
|
157
|
-
it "returns the next paragraph if the separator's length is 0" do
|
158
|
-
a = "Voici la ligne une.\nQui \303\250 la linea due.\n\n"
|
159
|
-
b = "Aqu\303\255 est\303\241 la l\303\255nea tres.\nIst hier Linie vier.\n\n"
|
160
|
-
c = "Est\303\241 aqui a linha cinco.\nHere is line six.\n"
|
161
|
-
|
162
|
-
IOSpecs.readable_iowrapper do |f|
|
163
|
-
f.gets("").should == a
|
164
|
-
f.gets("").should == b
|
165
|
-
f.gets("").should == c
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
# This could probably be added to the previous test using pos, but right now
|
170
|
-
# this doesn't pass and the previous test does.
|
171
|
-
it "reads until the beginning of the next paragraph when the separator's length is 0" do
|
172
|
-
# Leverage the fact that there are three newlines between the first and
|
173
|
-
# second paragraph.
|
174
|
-
IOSpecs.readable_iowrapper do |f|
|
175
|
-
f.gets('')
|
176
|
-
|
177
|
-
# This should return 'A', the first character of the next paragraph, not
|
178
|
-
# $/.
|
179
|
-
f.read(1).should == 'A'
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
it "raises an IOError if the stream is not opened for reading" do
|
184
|
-
path = tmp("gets_spec")
|
185
|
-
lambda do
|
186
|
-
File.open(path, 'w') do |f|
|
187
|
-
WritableIOWrapper.open(f) { |iowrapper| iowrapper.gets }
|
188
|
-
end
|
189
|
-
end.should raise_error(IOError)
|
190
|
-
File.unlink(path) if File.exist?(path)
|
191
|
-
end
|
192
|
-
|
193
|
-
it "raises IOError on closed stream" do
|
194
|
-
lambda { IOSpecs.closed_file.gets }.should raise_error(IOError)
|
195
|
-
end
|
196
|
-
|
197
|
-
it "accepts a separator" do
|
198
|
-
path = tmp("gets_specs")
|
199
|
-
f = File.open(path, "w")
|
200
|
-
f.print("A\n\n\nB\n")
|
201
|
-
f.close
|
202
|
-
f = File.open(path, "r")
|
203
|
-
iowrapper = ReadableIOWrapper.open(f)
|
204
|
-
iowrapper.gets("\n\n")
|
205
|
-
b = iowrapper.gets("\n\n")
|
206
|
-
iowrapper.gets("\n\n")
|
207
|
-
iowrapper.close
|
208
|
-
f.close
|
209
|
-
b.should == "\nB\n"
|
210
|
-
File.unlink(path)
|
211
|
-
end
|
212
|
-
end
|