diff options
Diffstat (limited to 'spec/ruby/core/io')
| -rw-r--r-- | spec/ruby/core/io/buffer/map_spec.rb | 46 | ||||
| -rw-r--r-- | spec/ruby/core/io/each_char_spec.rb | 71 | ||||
| -rw-r--r-- | spec/ruby/core/io/each_codepoint_spec.rb | 51 | ||||
| -rw-r--r-- | spec/ruby/core/io/each_line_spec.rb | 246 | ||||
| -rw-r--r-- | spec/ruby/core/io/each_spec.rb | 10 | ||||
| -rw-r--r-- | spec/ruby/core/io/eof_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/io/foreach_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/io/isatty_spec.rb | 5 | ||||
| -rw-r--r-- | spec/ruby/core/io/pos_spec.rb | 32 | ||||
| -rw-r--r-- | spec/ruby/core/io/read_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/io/readlines_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/chars.rb | 73 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/codepoints.rb | 54 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/each.rb | 251 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/pos.rb | 34 | ||||
| -rw-r--r-- | spec/ruby/core/io/shared/tty.rb | 24 | ||||
| -rw-r--r-- | spec/ruby/core/io/tell_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/io/to_i_spec.rb | 9 | ||||
| -rw-r--r-- | spec/ruby/core/io/to_path_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/core/io/tty_spec.rb | 23 |
20 files changed, 463 insertions, 491 deletions
diff --git a/spec/ruby/core/io/buffer/map_spec.rb b/spec/ruby/core/io/buffer/map_spec.rb index 4b28539ad8..97764c2dd7 100644 --- a/spec/ruby/core/io/buffer/map_spec.rb +++ b/spec/ruby/core/io/buffer/map_spec.rb @@ -73,30 +73,34 @@ describe "IO::Buffer.map" do @buffer.should.valid? end - guard -> { Process.respond_to?(:fork) } do - it "is shareable across processes" do - file_name = tmp("shared_buffer") - @file = File.open(file_name, "w+") - @file << "I'm private" - @file.rewind - @buffer = IO::Buffer.map(@file) - - IO.popen("-") do |child_pipe| - if child_pipe - # Synchronize on child's output. - child_pipe.readlines.first.chomp.should == @buffer.to_s - @buffer.get_string.should == "I'm shared!" - - @file.read.should == "I'm shared!" - else - @buffer.set_string("I'm shared!") - puts @buffer + # IO::Buffer.map seems not shareable across processes on OpenBSD. + # See https://rubyci.s3.amazonaws.com/openbsd-current/ruby-master/log/20260129T163005Z.fail.html.gz + platform_is_not :openbsd do + guard -> { Process.respond_to?(:fork) } do + it "is shareable across processes" do + file_name = tmp("shared_buffer") + @file = File.open(file_name, "w+") + @file << "I'm private" + @file.rewind + @buffer = IO::Buffer.map(@file) + + IO.popen("-") do |child_pipe| + if child_pipe + # Synchronize on child's output. + child_pipe.readlines.first.chomp.should == @buffer.to_s + @buffer.get_string.should == "I'm shared!" + + @file.read.should == "I'm shared!" + else + @buffer.set_string("I'm shared!") + puts @buffer + end + ensure + child_pipe&.close end ensure - child_pipe&.close + File.unlink(file_name) end - ensure - File.unlink(file_name) end end diff --git a/spec/ruby/core/io/each_char_spec.rb b/spec/ruby/core/io/each_char_spec.rb index 5d460a1e7c..7c1ca4f069 100644 --- a/spec/ruby/core/io/each_char_spec.rb +++ b/spec/ruby/core/io/each_char_spec.rb @@ -1,12 +1,75 @@ -# -*- encoding: utf-8 -*- require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/chars' describe "IO#each_char" do - it_behaves_like :io_chars, :each_char + before :each do + @io = IOSpecs.io_fixture "lines.txt" + ScratchPad.record [] + end + + after :each do + @io.close unless @io.closed? + end + + it "yields each character" do + @io.readline.should == "Voici la ligne une.\n" + + count = 0 + @io.each_char do |c| + ScratchPad << c + break if 4 < count += 1 + end + + ScratchPad.recorded.should == ["Q", "u", "i", " ", "è"] + end + + describe "when no block is given" do + it "returns an Enumerator" do + enum = @io.each_char + enum.should.instance_of?(Enumerator) + enum.first(5).should == ["V", "o", "i", "c", "i"] + end + + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + @io.each_char.size.should == nil + end + end + end + end + + it "returns itself" do + @io.each_char { |c| }.should.equal?(@io) + end + + it "returns an enumerator for a closed stream" do + IOSpecs.closed_io.each_char.should.instance_of?(Enumerator) + end + + it "raises an IOError when an enumerator created on a closed stream is accessed" do + -> { IOSpecs.closed_io.each_char.first }.should.raise(IOError) + end + + it "raises IOError on closed stream" do + -> { IOSpecs.closed_io.each_char {} }.should.raise(IOError) + end end describe "IO#each_char" do - it_behaves_like :io_chars_empty, :each_char + before :each do + @name = tmp("io_each_char") + @io = new_io @name, "w+:utf-8" + ScratchPad.record [] + end + + after :each do + @io.close unless @io.closed? + rm_r @name + end + + it "does not yield any characters on an empty stream" do + @io.each_char { |c| ScratchPad << c } + ScratchPad.recorded.should == [] + end end diff --git a/spec/ruby/core/io/each_codepoint_spec.rb b/spec/ruby/core/io/each_codepoint_spec.rb index 26cc87fc0e..758a524986 100644 --- a/spec/ruby/core/io/each_codepoint_spec.rb +++ b/spec/ruby/core/io/each_codepoint_spec.rb @@ -1,10 +1,57 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/codepoints' # See redmine #1667 describe "IO#each_codepoint" do - it_behaves_like :io_codepoints, :each_codepoint + before :each do + @io = IOSpecs.io_fixture "lines.txt" + @enum = @io.each_codepoint + end + + after :each do + @io.close + end + + describe "when no block is given" do + it "returns an Enumerator" do + @enum.should.instance_of?(Enumerator) + end + + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + @enum.size.should == nil + end + end + end + end + + it "yields each codepoint" do + @enum.first(25).should == [ + 86, 111, 105, 99, 105, 32, 108, 97, 32, 108, 105, 103, 110, + 101, 32, 117, 110, 101, 46, 10, 81, 117, 105, 32, 232 + ] + end + + it "yields each codepoint starting from the current position" do + @io.pos = 130 + @enum.to_a.should == [101, 32, 115, 105, 120, 46, 10] + end + + it "raises an error if reading invalid sequence" do + @io.pos = 60 # inside of a multibyte sequence + -> { @enum.first }.should.raise(ArgumentError) + end + + it "does not change $_" do + $_ = "test" + @enum.to_a + $_.should == "test" + end + + it "raises an IOError when self is not readable" do + -> { IOSpecs.closed_io.each_codepoint.to_a }.should.raise(IOError) + end end describe "IO#each_codepoint" do diff --git a/spec/ruby/core/io/each_line_spec.rb b/spec/ruby/core/io/each_line_spec.rb index 58d26b325d..bcda4040b8 100644 --- a/spec/ruby/core/io/each_line_spec.rb +++ b/spec/ruby/core/io/each_line_spec.rb @@ -1,11 +1,251 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'shared/each' describe "IO#each_line" do - it_behaves_like :io_each, :each_line + before :each do + @io = IOSpecs.io_fixture "lines.txt" + ScratchPad.record [] + end + + after :each do + @io.close if @io + end + + describe "with no separator" do + it "yields each line to the passed block" do + @io.each_line { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines + end + + it "yields each line starting from the current position" do + @io.pos = 41 + @io.each_line { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines[2..-1] + end + + it "returns self" do + @io.each_line { |l| l }.should.equal?(@io) + end + + it "does not change $_" do + $_ = "test" + @io.each_line { |s| s } + $_.should == "test" + end + + it "raises an IOError when self is not readable" do + -> { IOSpecs.closed_io.each_line {} }.should.raise(IOError) + end + + it "makes line count accessible via lineno" do + @io.each_line { ScratchPad << @io.lineno } + ScratchPad.recorded.should == [ 1,2,3,4,5,6,7,8,9 ] + end + + it "makes line count accessible via $." do + @io.each_line { ScratchPad << $. } + ScratchPad.recorded.should == [ 1,2,3,4,5,6,7,8,9 ] + end + + describe "when no block is given" do + it "returns an Enumerator" do + enum = @io.each_line + enum.should.instance_of?(Enumerator) + + enum.each { |l| ScratchPad << l } + ScratchPad.recorded.should == IOSpecs.lines + end + + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + @io.each_line.size.should == nil + end + end + end + end + end + + describe "with limit" do + describe "when limit is 0" do + it "raises an ArgumentError" do + # must pass block so Enumerator is evaluated and raises + -> { @io.each_line(0){} }.should.raise(ArgumentError) + end + end + + it "does not accept Integers that don't fit in a C off_t" do + -> { @io.each_line(2**128){} }.should.raise(RangeError) + end + end + + describe "when passed a String containing one space as a separator" do + it "uses the passed argument as the line separator" do + @io.each_line(" ") { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines_space_separator + end + + it "does not change $_" do + $_ = "test" + @io.each_line(" ") { |s| } + $_.should == "test" + end + + it "tries to convert the passed separator to a String using #to_str" do + obj = mock("to_str") + obj.stub!(:to_str).and_return(" ") + + @io.each_line(obj) { |l| ScratchPad << l } + ScratchPad.recorded.should == IOSpecs.lines_space_separator + end + end + + describe "when passed nil as a separator" do + it "yields self's content starting from the current position when the passed separator is nil" do + @io.pos = 100 + @io.each_line(nil) { |s| ScratchPad << s } + ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] + end + end + + describe "when passed an empty String as a separator" do + it "yields each paragraph" do + @io.each_line("") { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs + end + + it "discards leading newlines" do + @io.readline + @io.readline + @io.each_line("") { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] + end + end + + describe "with both separator and limit" do + describe "when no block is given" do + it "returns an Enumerator" do + enum = @io.each_line(nil, 1024) + enum.should.instance_of?(Enumerator) + + enum.each { |l| ScratchPad << l } + ScratchPad.recorded.should == [IOSpecs.lines.join] + end + + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + @io.each_line(nil, 1024).size.should == nil + end + end + end + end + + describe "when a block is given" do + it "accepts an empty block" do + @io.each_line(nil, 1024) {}.should.equal?(@io) + end + + describe "when passed nil as a separator" do + it "yields self's content starting from the current position when the passed separator is nil" do + @io.pos = 100 + @io.each_line(nil, 1024) { |s| ScratchPad << s } + ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] + end + end + + describe "when passed an empty String as a separator" do + it "yields each paragraph" do + @io.each_line("", 1024) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs + end + + it "discards leading newlines" do + @io.readline + @io.readline + @io.each_line("", 1024) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] + end + end + end + end + + describe "when passed chomp" do + it "yields each line without trailing newline characters to the passed block" do + @io.each_line(chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines_without_newline_characters + end + + it "raises exception when options passed as Hash" do + -> { + @io.each_line({ chomp: true }) { |s| } + }.should.raise(TypeError) + + -> { + @io.each_line("\n", 1, { chomp: true }) { |s| } + }.should.raise(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") + end + end + + describe "when passed chomp and a separator" do + it "yields each line without separator to the passed block" do + @io.each_line(" ", chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines_space_separator_without_trailing_spaces + end + end + + describe "when passed chomp and empty line as a separator" do + it "yields each paragraph without trailing new line characters" do + @io.each_line("", 1024, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs_without_trailing_new_line_characters + end + end + + describe "when passed chomp and nil as a separator" do + it "yields self's content" do + @io.pos = 100 + @io.each_line(nil, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] + end + end + + describe "when passed chomp, nil as a separator, and a limit" do + it "yields each line of limit size without truncating trailing new line character" do + # 43 - is a size of the 1st paragraph in the file + @io.each_line(nil, 43, chomp: true) { |s| ScratchPad << s } + + ScratchPad.recorded.should == [ + "Voici la ligne une.\nQui è la linea due.\n\n\n", + "Aquí está la línea tres.\n" + "Hier ist Zeile ", + "vier.\n\nEstá aqui a linha cinco.\nHere is li", + "ne six.\n" + ] + end + end + + describe "when passed too many arguments" do + it "raises ArgumentError" do + -> { + @io.each_line("", 1, "excess argument", chomp: true) {} + }.should.raise(ArgumentError) + end + end end describe "IO#each_line" do - it_behaves_like :io_each_default_separator, :each_line + before :each do + @io = IOSpecs.io_fixture "lines.txt" + ScratchPad.record [] + suppress_warning {@sep, $/ = $/, " "} + end + + after :each do + @io.close if @io + suppress_warning {$/ = @sep} + end + + it "uses $/ as the default line separator" do + @io.each_line { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines_space_separator + end end diff --git a/spec/ruby/core/io/each_spec.rb b/spec/ruby/core/io/each_spec.rb index 91ecbd19c8..594052256e 100644 --- a/spec/ruby/core/io/each_spec.rb +++ b/spec/ruby/core/io/each_spec.rb @@ -1,11 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/each' describe "IO#each" do - it_behaves_like :io_each, :each -end - -describe "IO#each" do - it_behaves_like :io_each_default_separator, :each + it "is an alias of IO#each_line" do + IO.instance_method(:each).should == IO.instance_method(:each_line) + end end diff --git a/spec/ruby/core/io/eof_spec.rb b/spec/ruby/core/io/eof_spec.rb index c8955abde0..561daa4ec3 100644 --- a/spec/ruby/core/io/eof_spec.rb +++ b/spec/ruby/core/io/eof_spec.rb @@ -105,3 +105,9 @@ describe "IO#eof?" do @r.should.eof? end end + +describe "IO#eof" do + it "is an alias of IO#eof?" do + IO.instance_method(:eof).should == IO.instance_method(:eof?) + end +end diff --git a/spec/ruby/core/io/foreach_spec.rb b/spec/ruby/core/io/foreach_spec.rb index 015988f9fb..ccd2f25517 100644 --- a/spec/ruby/core/io/foreach_spec.rb +++ b/spec/ruby/core/io/foreach_spec.rb @@ -28,7 +28,7 @@ describe "IO.foreach" do ScratchPad.recorded.should == ["hello\n", "line2\n"] end - platform_is_not :windows do + guard -> { Process.respond_to?(:fork) } do it "gets data from a fork when passed -" do parent_pid = $$ diff --git a/spec/ruby/core/io/isatty_spec.rb b/spec/ruby/core/io/isatty_spec.rb index 3b6c69b190..60b97d21be 100644 --- a/spec/ruby/core/io/isatty_spec.rb +++ b/spec/ruby/core/io/isatty_spec.rb @@ -1,6 +1,7 @@ require_relative '../../spec_helper' -require_relative 'shared/tty' describe "IO#isatty" do - it_behaves_like :io_tty, :isatty + it "is an alias of IO#tty?" do + IO.instance_method(:isatty).should == IO.instance_method(:tty?) + end end diff --git a/spec/ruby/core/io/pos_spec.rb b/spec/ruby/core/io/pos_spec.rb index e6cda2643d..bbe25ce97b 100644 --- a/spec/ruby/core/io/pos_spec.rb +++ b/spec/ruby/core/io/pos_spec.rb @@ -3,7 +3,37 @@ require_relative 'fixtures/classes' require_relative 'shared/pos' describe "IO#pos" do - it_behaves_like :io_pos, :pos + before :each do + @fname = tmp('test.txt') + File.open(@fname, 'w') { |f| f.write "123" } + end + + after :each do + rm_r @fname + end + + it "gets the offset" do + File.open @fname do |f| + f.pos.should == 0 + f.read 1 + f.pos.should == 1 + f.read 2 + f.pos.should == 3 + end + end + + it "raises IOError on closed stream" do + -> { IOSpecs.closed_io.pos }.should.raise(IOError) + end + + it "resets #eof?" do + open @fname do |io| + io.read 1 + io.read 1 + io.pos + io.should_not.eof? + end + end end describe "IO#pos=" do diff --git a/spec/ruby/core/io/read_spec.rb b/spec/ruby/core/io/read_spec.rb index dd787c9b60..5be969e280 100644 --- a/spec/ruby/core/io/read_spec.rb +++ b/spec/ruby/core/io/read_spec.rb @@ -163,7 +163,7 @@ ruby_version_is ""..."4.0" do end end - platform_is_not :windows do + guard -> { Process.respond_to?(:fork) } do it "opens a pipe to a fork if the rest is -" do str = nil suppress_warning do # https://bugs.ruby-lang.org/issues/19630 diff --git a/spec/ruby/core/io/readlines_spec.rb b/spec/ruby/core/io/readlines_spec.rb index 640e253200..d41d24d7d1 100644 --- a/spec/ruby/core/io/readlines_spec.rb +++ b/spec/ruby/core/io/readlines_spec.rb @@ -189,7 +189,7 @@ describe "IO.readlines" do lines.should == ["hello\n", "line2\n"] end - platform_is_not :windows do + guard -> { Process.respond_to?(:fork) } do it "gets data from a fork when passed -" do lines = nil suppress_warning do # https://bugs.ruby-lang.org/issues/19630 diff --git a/spec/ruby/core/io/shared/chars.rb b/spec/ruby/core/io/shared/chars.rb deleted file mode 100644 index efd4d5ee10..0000000000 --- a/spec/ruby/core/io/shared/chars.rb +++ /dev/null @@ -1,73 +0,0 @@ -# -*- encoding: utf-8 -*- -describe :io_chars, shared: true do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - ScratchPad.record [] - end - - after :each do - @io.close unless @io.closed? - end - - it "yields each character" do - @io.readline.should == "Voici la ligne une.\n" - - count = 0 - @io.send(@method) do |c| - ScratchPad << c - break if 4 < count += 1 - end - - ScratchPad.recorded.should == ["Q", "u", "i", " ", "è"] - end - - describe "when no block is given" do - it "returns an Enumerator" do - enum = @io.send(@method) - enum.should.instance_of?(Enumerator) - enum.first(5).should == ["V", "o", "i", "c", "i"] - end - - describe "returned Enumerator" do - describe "size" do - it "should return nil" do - @io.send(@method).size.should == nil - end - end - end - end - - it "returns itself" do - @io.send(@method) { |c| }.should.equal?(@io) - end - - it "returns an enumerator for a closed stream" do - IOSpecs.closed_io.send(@method).should.instance_of?(Enumerator) - end - - it "raises an IOError when an enumerator created on a closed stream is accessed" do - -> { IOSpecs.closed_io.send(@method).first }.should.raise(IOError) - end - - it "raises IOError on closed stream" do - -> { IOSpecs.closed_io.send(@method) {} }.should.raise(IOError) - end -end - -describe :io_chars_empty, shared: true do - before :each do - @name = tmp("io_each_char") - @io = new_io @name, "w+:utf-8" - ScratchPad.record [] - end - - after :each do - @io.close unless @io.closed? - rm_r @name - end - - it "does not yield any characters on an empty stream" do - @io.send(@method) { |c| ScratchPad << c } - ScratchPad.recorded.should == [] - end -end diff --git a/spec/ruby/core/io/shared/codepoints.rb b/spec/ruby/core/io/shared/codepoints.rb deleted file mode 100644 index 21c756986f..0000000000 --- a/spec/ruby/core/io/shared/codepoints.rb +++ /dev/null @@ -1,54 +0,0 @@ -# -*- encoding: utf-8 -*- -require_relative '../fixtures/classes' - -describe :io_codepoints, shared: true do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - @enum = @io.send(@method) - end - - after :each do - @io.close - end - - describe "when no block is given" do - it "returns an Enumerator" do - @enum.should.instance_of?(Enumerator) - end - - describe "returned Enumerator" do - describe "size" do - it "should return nil" do - @enum.size.should == nil - end - end - end - end - - it "yields each codepoint" do - @enum.first(25).should == [ - 86, 111, 105, 99, 105, 32, 108, 97, 32, 108, 105, 103, 110, - 101, 32, 117, 110, 101, 46, 10, 81, 117, 105, 32, 232 - ] - end - - it "yields each codepoint starting from the current position" do - @io.pos = 130 - @enum.to_a.should == [101, 32, 115, 105, 120, 46, 10] - end - - it "raises an error if reading invalid sequence" do - @io.pos = 60 # inside of a multibyte sequence - -> { @enum.first }.should.raise(ArgumentError) - end - - it "does not change $_" do - $_ = "test" - @enum.to_a - $_.should == "test" - end - - it "raises an IOError when self is not readable" do - -> { IOSpecs.closed_io.send(@method).to_a }.should.raise(IOError) - end -end diff --git a/spec/ruby/core/io/shared/each.rb b/spec/ruby/core/io/shared/each.rb deleted file mode 100644 index ae60c3506a..0000000000 --- a/spec/ruby/core/io/shared/each.rb +++ /dev/null @@ -1,251 +0,0 @@ -# -*- encoding: utf-8 -*- -require_relative '../fixtures/classes' - -describe :io_each, shared: true do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - ScratchPad.record [] - end - - after :each do - @io.close if @io - end - - describe "with no separator" do - it "yields each line to the passed block" do - @io.send(@method) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines - end - - it "yields each line starting from the current position" do - @io.pos = 41 - @io.send(@method) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines[2..-1] - end - - it "returns self" do - @io.send(@method) { |l| l }.should.equal?(@io) - end - - it "does not change $_" do - $_ = "test" - @io.send(@method) { |s| s } - $_.should == "test" - end - - it "raises an IOError when self is not readable" do - -> { IOSpecs.closed_io.send(@method) {} }.should.raise(IOError) - end - - it "makes line count accessible via lineno" do - @io.send(@method) { ScratchPad << @io.lineno } - ScratchPad.recorded.should == [ 1,2,3,4,5,6,7,8,9 ] - end - - it "makes line count accessible via $." do - @io.send(@method) { ScratchPad << $. } - ScratchPad.recorded.should == [ 1,2,3,4,5,6,7,8,9 ] - end - - describe "when no block is given" do - it "returns an Enumerator" do - enum = @io.send(@method) - enum.should.instance_of?(Enumerator) - - enum.each { |l| ScratchPad << l } - ScratchPad.recorded.should == IOSpecs.lines - end - - describe "returned Enumerator" do - describe "size" do - it "should return nil" do - @io.send(@method).size.should == nil - end - end - end - end - end - - describe "with limit" do - describe "when limit is 0" do - it "raises an ArgumentError" do - # must pass block so Enumerator is evaluated and raises - -> { @io.send(@method, 0){} }.should.raise(ArgumentError) - end - end - - it "does not accept Integers that don't fit in a C off_t" do - -> { @io.send(@method, 2**128){} }.should.raise(RangeError) - end - end - - describe "when passed a String containing one space as a separator" do - it "uses the passed argument as the line separator" do - @io.send(@method, " ") { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines_space_separator - end - - it "does not change $_" do - $_ = "test" - @io.send(@method, " ") { |s| } - $_.should == "test" - end - - it "tries to convert the passed separator to a String using #to_str" do - obj = mock("to_str") - obj.stub!(:to_str).and_return(" ") - - @io.send(@method, obj) { |l| ScratchPad << l } - ScratchPad.recorded.should == IOSpecs.lines_space_separator - end - end - - describe "when passed nil as a separator" do - it "yields self's content starting from the current position when the passed separator is nil" do - @io.pos = 100 - @io.send(@method, nil) { |s| ScratchPad << s } - ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] - end - end - - describe "when passed an empty String as a separator" do - it "yields each paragraph" do - @io.send(@method, "") { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.paragraphs - end - - it "discards leading newlines" do - @io.readline - @io.readline - @io.send(@method, "") { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] - end - end - - describe "with both separator and limit" do - describe "when no block is given" do - it "returns an Enumerator" do - enum = @io.send(@method, nil, 1024) - enum.should.instance_of?(Enumerator) - - enum.each { |l| ScratchPad << l } - ScratchPad.recorded.should == [IOSpecs.lines.join] - end - - describe "returned Enumerator" do - describe "size" do - it "should return nil" do - @io.send(@method, nil, 1024).size.should == nil - end - end - end - end - - describe "when a block is given" do - it "accepts an empty block" do - @io.send(@method, nil, 1024) {}.should.equal?(@io) - end - - describe "when passed nil as a separator" do - it "yields self's content starting from the current position when the passed separator is nil" do - @io.pos = 100 - @io.send(@method, nil, 1024) { |s| ScratchPad << s } - ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] - end - end - - describe "when passed an empty String as a separator" do - it "yields each paragraph" do - @io.send(@method, "", 1024) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.paragraphs - end - - it "discards leading newlines" do - @io.readline - @io.readline - @io.send(@method, "", 1024) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] - end - end - end - end - - describe "when passed chomp" do - it "yields each line without trailing newline characters to the passed block" do - @io.send(@method, chomp: true) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines_without_newline_characters - end - - it "raises exception when options passed as Hash" do - -> { - @io.send(@method, { chomp: true }) { |s| } - }.should.raise(TypeError) - - -> { - @io.send(@method, "\n", 1, { chomp: true }) { |s| } - }.should.raise(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") - end - end - - describe "when passed chomp and a separator" do - it "yields each line without separator to the passed block" do - @io.send(@method, " ", chomp: true) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines_space_separator_without_trailing_spaces - end - end - - describe "when passed chomp and empty line as a separator" do - it "yields each paragraph without trailing new line characters" do - @io.send(@method, "", 1024, chomp: true) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.paragraphs_without_trailing_new_line_characters - end - end - - describe "when passed chomp and nil as a separator" do - it "yields self's content" do - @io.pos = 100 - @io.send(@method, nil, chomp: true) { |s| ScratchPad << s } - ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] - end - end - - describe "when passed chomp, nil as a separator, and a limit" do - it "yields each line of limit size without truncating trailing new line character" do - # 43 - is a size of the 1st paragraph in the file - @io.send(@method, nil, 43, chomp: true) { |s| ScratchPad << s } - - ScratchPad.recorded.should == [ - "Voici la ligne une.\nQui è la linea due.\n\n\n", - "Aquí está la línea tres.\n" + "Hier ist Zeile ", - "vier.\n\nEstá aqui a linha cinco.\nHere is li", - "ne six.\n" - ] - end - end - - describe "when passed too many arguments" do - it "raises ArgumentError" do - -> { - @io.send(@method, "", 1, "excess argument", chomp: true) {} - }.should.raise(ArgumentError) - end - end -end - -describe :io_each_default_separator, shared: true do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - ScratchPad.record [] - suppress_warning {@sep, $/ = $/, " "} - end - - after :each do - @io.close if @io - suppress_warning {$/ = @sep} - end - - it "uses $/ as the default line separator" do - @io.send(@method) { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines_space_separator - end -end diff --git a/spec/ruby/core/io/shared/pos.rb b/spec/ruby/core/io/shared/pos.rb index f4d0405863..450058e159 100644 --- a/spec/ruby/core/io/shared/pos.rb +++ b/spec/ruby/core/io/shared/pos.rb @@ -1,37 +1,3 @@ -describe :io_pos, shared: true do - before :each do - @fname = tmp('test.txt') - File.open(@fname, 'w') { |f| f.write "123" } - end - - after :each do - rm_r @fname - end - - it "gets the offset" do - File.open @fname do |f| - f.send(@method).should == 0 - f.read 1 - f.send(@method).should == 1 - f.read 2 - f.send(@method).should == 3 - end - end - - it "raises IOError on closed stream" do - -> { IOSpecs.closed_io.send(@method) }.should.raise(IOError) - end - - it "resets #eof?" do - open @fname do |io| - io.read 1 - io.read 1 - io.send(@method) - io.should_not.eof? - end - end -end - describe :io_set_pos, shared: true do before :each do @fname = tmp('test.txt') diff --git a/spec/ruby/core/io/shared/tty.rb b/spec/ruby/core/io/shared/tty.rb deleted file mode 100644 index 1dc0e95739..0000000000 --- a/spec/ruby/core/io/shared/tty.rb +++ /dev/null @@ -1,24 +0,0 @@ -require_relative '../fixtures/classes' - -describe :io_tty, shared: true do - platform_is_not :windows do - it "returns true if this stream is a terminal device (TTY)" do - begin - # check to enabled tty - File.open('/dev/tty') {} - rescue Errno::ENXIO - skip "workaround for not configured environment like OS X" - else - File.open('/dev/tty') { |f| f.send(@method) }.should == true - end - end - end - - it "returns false if this stream is not a terminal device (TTY)" do - File.open(__FILE__) { |f| f.send(@method) }.should == false - end - - it "raises IOError on closed stream" do - -> { IOSpecs.closed_io.send @method }.should.raise(IOError) - end -end diff --git a/spec/ruby/core/io/tell_spec.rb b/spec/ruby/core/io/tell_spec.rb index 0d6c6b02d3..a6b51adc17 100644 --- a/spec/ruby/core/io/tell_spec.rb +++ b/spec/ruby/core/io/tell_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/pos' describe "IO#tell" do - it_behaves_like :io_pos, :tell + it "is an alias of IO#pos" do + IO.instance_method(:tell).should == IO.instance_method(:pos) + end end diff --git a/spec/ruby/core/io/to_i_spec.rb b/spec/ruby/core/io/to_i_spec.rb index 1d0cf2a1f6..b271112a81 100644 --- a/spec/ruby/core/io/to_i_spec.rb +++ b/spec/ruby/core/io/to_i_spec.rb @@ -1,12 +1,7 @@ require_relative '../../spec_helper' -require_relative 'fixtures/classes' describe "IO#to_i" do - it "returns the numeric file descriptor of the given IO object" do - $stdout.to_i.should == 1 - end - - it "raises IOError on closed stream" do - -> { IOSpecs.closed_io.to_i }.should.raise(IOError) + it "is an alias of IO#fileno" do + IO.instance_method(:to_i).should == IO.instance_method(:fileno) end end diff --git a/spec/ruby/core/io/to_path_spec.rb b/spec/ruby/core/io/to_path_spec.rb new file mode 100644 index 0000000000..ec6dffc115 --- /dev/null +++ b/spec/ruby/core/io/to_path_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' + +describe "IO#to_path" do + it "is an alias of IO#path" do + IO.instance_method(:to_path).should == IO.instance_method(:path) + end +end diff --git a/spec/ruby/core/io/tty_spec.rb b/spec/ruby/core/io/tty_spec.rb index 3b76c6d2b8..e1848a1760 100644 --- a/spec/ruby/core/io/tty_spec.rb +++ b/spec/ruby/core/io/tty_spec.rb @@ -1,6 +1,25 @@ require_relative '../../spec_helper' -require_relative 'shared/tty' +require_relative 'fixtures/classes' describe "IO#tty?" do - it_behaves_like :io_tty, :tty? + platform_is_not :windows do + it "returns true if this stream is a terminal device (TTY)" do + begin + # check to enabled tty + File.open('/dev/tty') {} + rescue Errno::ENXIO + skip "workaround for not configured environment like OS X" + else + File.open('/dev/tty') { |f| f.tty? }.should == true + end + end + end + + it "returns false if this stream is not a terminal device (TTY)" do + File.open(__FILE__) { |f| f.tty? }.should == false + end + + it "raises IOError on closed stream" do + -> { IOSpecs.closed_io.tty? }.should.raise(IOError) + end end |
