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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
begin
require 'io/console'
require 'test/unit'
require 'pty'
rescue LoadError
end
require_relative '../../ruby/envutil'
class TestIO_Console < Test::Unit::TestCase
Bug6116 = '[ruby-dev:45309]'
def test_raw
helper {|m, s|
s.print "abc\n"
assert_equal("abc\r\n", m.gets)
assert_send([s, :echo?])
s.raw {
assert_not_send([s, :echo?], Bug6116)
s.print "def\n"
assert_equal("def\n", m.gets)
}
assert_send([s, :echo?])
s.print "ghi\n"
assert_equal("ghi\r\n", m.gets)
}
end
def test_cooked
helper {|m, s|
assert_send([s, :echo?])
s.raw {
s.print "abc\n"
assert_equal("abc\n", m.gets)
assert_not_send([s, :echo?], Bug6116)
s.cooked {
assert_send([s, :echo?])
s.print "def\n"
assert_equal("def\r\n", m.gets)
}
assert_not_send([s, :echo?], Bug6116)
}
assert_send([s, :echo?])
s.print "ghi\n"
assert_equal("ghi\r\n", m.gets)
}
end
def test_echo
helper {|m, s|
assert_send([s, :echo?])
m.print "a"
assert_equal("a", m.readpartial(10))
}
end
def test_noecho
helper {|m, s|
s.noecho {
assert_not_send([s, :echo?])
m.print "a"
sleep 0.1
}
m.print "b"
assert_equal("b", m.readpartial(10))
}
end
def test_noecho2
helper {|m, s|
assert_send([s, :echo?])
m.print "a\n"
sleep 0.1
s.print "b\n"
sleep 0.1
assert_equal("a\r\nb\r\n", m.readpartial(10))
assert_equal("a\n", s.readpartial(10))
s.noecho {
assert_not_send([s, :echo?])
m.print "a\n"
s.print "b\n"
assert_equal("b\r\n", m.readpartial(10))
assert_equal("a\n", s.readpartial(10))
}
assert_send([s, :echo?])
m.print "a\n"
sleep 0.1
s.print "b\n"
sleep 0.1
assert_equal("a\r\nb\r\n", m.readpartial(10))
assert_equal("a\n", s.readpartial(10))
}
end
def test_setecho
helper {|m, s|
assert_send([s, :echo?])
s.echo = false
m.print "a"
sleep 0.1
s.echo = true
m.print "b"
assert_equal("b", m.readpartial(10))
}
end
def test_setecho2
helper {|m, s|
assert_send([s, :echo?])
m.print "a\n"
sleep 0.1
s.print "b\n"
sleep 0.1
assert_equal("a\r\nb\r\n", m.readpartial(10))
assert_equal("a\n", s.readpartial(10))
s.echo = false
assert_not_send([s, :echo?])
m.print "a\n"
s.print "b\n"
assert_equal("b\r\n", m.readpartial(10))
assert_equal("a\n", s.readpartial(10))
s.echo = true
assert_send([s, :echo?])
m.print "a\n"
sleep 0.1
s.print "b\n"
sleep 0.1
assert_equal("a\r\nb\r\n", m.readpartial(10))
assert_equal("a\n", s.readpartial(10))
}
end
def test_iflush
helper {|m, s|
m.print "a"
s.iflush
m.print "b\n"
assert_equal("b\n", s.readpartial(10))
}
end
def test_oflush
helper {|m, s|
s.print "a"
s.oflush # oflush may be issued after "a" is already sent.
s.print "b"
assert_include(["b", "ab"], m.readpartial(10))
}
end
def test_ioflush
helper {|m, s|
m.print "a"
s.ioflush
m.print "b\n"
assert_equal("b\n", s.readpartial(10))
}
end
def test_ioflush2
helper {|m, s|
s.print "a"
s.ioflush # ioflush may be issued after "a" is already sent.
s.print "b"
assert_include(["b", "ab"], m.readpartial(10))
}
end
def test_winsize
helper {|m, s|
begin
assert_equal([0, 0], s.winsize)
rescue Errno::EINVAL # OpenSolaris 2009.06 TIOCGWINSZ causes Errno::EINVAL before TIOCSWINSZ.
end
}
end
if IO.console
def test_sync
assert(IO.console.sync, "console should be unbuffered")
end
else
def test_sync
r, w, pid = PTY.spawn(EnvUtil.rubybin, "-rio/console", "-e", "p IO.console.class")
rescue RuntimeError
skip $!
else
con = r.gets.chomp
Process.wait(pid)
assert_match("File", con)
end
end
private
def helper
m, s = PTY.open
rescue RuntimeError
skip $!
else
yield m, s
ensure
m.close if m
s.close if s
end
end if defined?(PTY) and defined?(IO::console)
class TestIO_Console < Test::Unit::TestCase
case
when Process.respond_to?(:daemon)
noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"]
when !(rubyw = RbConfig::CONFIG["RUBYW_INSTALL_NAME"]).empty?
dir, base = File.split(EnvUtil.rubybin)
noctty = [File.join(dir, base.sub(/ruby/, rubyw))]
end
if noctty
require 'tempfile'
NOCTTY = noctty
def test_noctty
t = Tempfile.new("console")
t.close
t2 = Tempfile.new("console")
t2.close
cmd = NOCTTY + [
'--disable=gems',
'-rio/console',
'-e', 'open(ARGV[0], "w") {|f| f.puts IO.console.inspect}',
'-e', 'File.unlink(ARGV[1])',
'--', t.path, t2.path]
system(*cmd)
sleep 0.1 while File.exist?(t2.path)
t.open
assert_equal("nil", t.gets.chomp)
ensure
t.close! if t and !t.closed?
t2.close!
end
end
end if defined?(IO.console)
class TestIO_Console < Test::Unit::TestCase
def test_stringio_getch
assert_ruby_status(%w"--disable=gems -rstringio -rio/console", "exit(StringIO.method_defined?(:getch))")
assert_ruby_status(%w"--disable=gems -rio/console -rstringio", "exit(StringIO.method_defined?(:getch))")
assert_ruby_status(%w"--disable=gems -rstringio", "exit(!StringIO.method_defined?(:getch))")
end
end
|