blob: a05d80ee9c25d478f5ba105db6d06f0d362bf25a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe "IO#ungetc" do
before :each do
@io = IOSpecs.io_fixture "lines.txt"
@empty = tmp('empty.txt')
end
after :each do
@io.close unless @io.closed?
rm_r @empty
end
it "pushes back one character onto stream" do
@io.getc.should == ?V
@io.ungetc(86)
@io.getc.should == ?V
@io.ungetc(10)
@io.getc.should == ?\n
@io.getc.should == ?o
@io.getc.should == ?i
# read the rest of line
@io.readline.should == "ci la ligne une.\n"
@io.getc.should == ?Q
@io.ungetc(99)
@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
@io.ungetc(100)
@io.getc.should == ?d
end
it "pushes back one character when invoked at the start of the stream" do
@io.read(0)
@io.ungetc(100)
@io.getc.should == ?d
end
it "pushes back one character when invoked on empty stream" do
touch(@empty)
File.open(@empty) { |empty|
empty.getc().should == nil
empty.ungetc(10)
empty.getc.should == ?\n
}
end
it "affects EOF state" do
touch(@empty)
File.open(@empty) { |empty|
empty.should.eof?
empty.getc.should == nil
empty.ungetc(100)
empty.should_not.eof?
}
end
it "adjusts the stream position" do
@io.pos.should == 0
# read one char
c = @io.getc
@io.pos.should == 1
@io.ungetc(c)
@io.pos.should == 0
# read all
@io.read
pos = @io.pos
@io.ungetc(98)
@io.pos.should == pos - 1
end
it "makes subsequent unbuffered operations to raise IOError" do
@io.getc
@io.ungetc(100)
-> { @io.sysread(1) }.should raise_error(IOError)
end
ruby_version_is "0"..."3.0" do
it "does not affect the stream and returns nil when passed nil" do
@io.getc.should == ?V
@io.ungetc(nil)
@io.getc.should == ?o
end
end
ruby_version_is "3.0" do
it "raises TypeError if passed nil" do
@io.getc.should == ?V
proc{@io.ungetc(nil)}.should raise_error(TypeError)
end
end
it "puts one or more characters back in the stream" do
@io.gets
@io.ungetc("Aquí ").should be_nil
@io.gets.chomp.should == "Aquí Qui è la linea due."
end
it "calls #to_str to convert the argument if it is not an Integer" do
chars = mock("io ungetc")
chars.should_receive(:to_str).and_return("Aquí ")
@io.ungetc(chars).should be_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
end
it "raises IOError on stream not opened for reading" do
-> { STDOUT.ungetc(100) }.should raise_error(IOError, "not opened for reading")
end
it "raises IOError on closed stream" do
@io.getc
@io.close
-> { @io.ungetc(100) }.should raise_error(IOError)
end
end
|