summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/ungetc_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/io/ungetc_spec.rb')
-rw-r--r--spec/ruby/core/io/ungetc_spec.rb43
1 files changed, 31 insertions, 12 deletions
diff --git a/spec/ruby/core/io/ungetc_spec.rb b/spec/ruby/core/io/ungetc_spec.rb
index ce4cc9d346..4a9e67f126 100644
--- a/spec/ruby/core/io/ungetc_spec.rb
+++ b/spec/ruby/core/io/ungetc_spec.rb
@@ -1,6 +1,6 @@
# -*- encoding: utf-8 -*-
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
describe "IO#ungetc" do
before :each do
@@ -31,6 +31,22 @@ describe "IO#ungetc" do
@io.getc.should == ?c
end
+ it "interprets the codepoint in the external encoding" do
+ @io.set_encoding(Encoding::UTF_8)
+ @io.ungetc(233)
+ c = @io.getc
+ c.encoding.should == Encoding::UTF_8
+ c.should == "é"
+ c.bytes.should == [195, 169]
+
+ @io.set_encoding(Encoding::IBM437)
+ @io.ungetc(130)
+ c = @io.getc
+ c.encoding.should == Encoding::IBM437
+ c.bytes.should == [130]
+ c.encode(Encoding::UTF_8).should == "é"
+ end
+
it "pushes back one character when invoked at the end of the stream" do
# read entire content
@io.read
@@ -58,10 +74,10 @@ describe "IO#ungetc" do
touch(@empty)
File.open(@empty) { |empty|
- empty.eof?.should == true
+ empty.should.eof?
empty.getc.should == nil
empty.ungetc(100)
- empty.eof?.should == false
+ empty.should_not.eof?
}
end
@@ -84,18 +100,17 @@ describe "IO#ungetc" do
it "makes subsequent unbuffered operations to raise IOError" do
@io.getc
@io.ungetc(100)
- lambda { @io.sysread(1) }.should raise_error(IOError)
+ -> { @io.sysread(1) }.should.raise(IOError)
end
- it "does not affect the stream and returns nil when passed nil" do
+ it "raises TypeError if passed nil" do
@io.getc.should == ?V
- @io.ungetc(nil)
- @io.getc.should == ?o
+ proc{@io.ungetc(nil)}.should.raise(TypeError)
end
it "puts one or more characters back in the stream" do
@io.gets
- @io.ungetc("Aquí ").should be_nil
+ @io.ungetc("Aquí ").should == nil
@io.gets.chomp.should == "Aquí Qui è la linea due."
end
@@ -103,17 +118,21 @@ describe "IO#ungetc" do
chars = mock("io ungetc")
chars.should_receive(:to_str).and_return("Aquí ")
- @io.ungetc(chars).should be_nil
+ @io.ungetc(chars).should == nil
@io.gets.chomp.should == "Aquí Voici la ligne une."
end
it "returns nil when invoked on stream that was not yet read" do
- @io.ungetc(100).should be_nil
+ @io.ungetc(100).should == nil
+ end
+
+ it "raises IOError on stream not opened for reading" do
+ -> { STDOUT.ungetc(100) }.should.raise(IOError, "not opened for reading")
end
it "raises IOError on closed stream" do
@io.getc
@io.close
- lambda { @io.ungetc(100) }.should raise_error(IOError)
+ -> { @io.ungetc(100) }.should.raise(IOError)
end
end