2012-02-23 08:27:29 -05:00
|
|
|
require 'test/unit'
|
2013-04-05 00:23:02 -04:00
|
|
|
require_relative 'ruby/envutil'
|
2012-02-23 08:27:29 -05:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'curses'
|
2013-04-05 00:23:02 -04:00
|
|
|
require 'pty'
|
2012-02-23 08:27:29 -05:00
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestCurses < Test::Unit::TestCase
|
|
|
|
def test_version
|
|
|
|
assert_instance_of(String, Curses::VERSION)
|
|
|
|
end
|
|
|
|
end if defined? Curses
|
2013-04-05 00:23:02 -04:00
|
|
|
|
|
|
|
class TestCurses
|
|
|
|
def run_curses(src, input = nil, timeout: 1)
|
2013-06-19 03:47:19 -04:00
|
|
|
r, w, pid = PTY.spawn({"TERM"=>ENV["TERM"]||"dumb"}, EnvUtil.rubybin, "-e", <<-"src")
|
2013-04-05 00:23:02 -04:00
|
|
|
require 'timeout'
|
|
|
|
require 'curses'
|
|
|
|
include Curses
|
|
|
|
init_screen
|
|
|
|
begin
|
|
|
|
result = Timeout.timeout(#{timeout}) do
|
2013-04-13 17:39:23 -04:00
|
|
|
print "!"
|
2013-04-05 00:23:02 -04:00
|
|
|
#{src}
|
|
|
|
end
|
|
|
|
rescue Exception => e
|
|
|
|
ensure
|
|
|
|
close_screen
|
|
|
|
puts "", [Marshal.dump([result, e])].pack('m').delete("\n")
|
2013-04-08 01:12:08 -04:00
|
|
|
print "\\0"
|
2013-04-10 17:59:13 -04:00
|
|
|
$stdio.flush
|
2013-04-05 00:23:02 -04:00
|
|
|
end
|
|
|
|
src
|
2013-06-19 03:47:19 -04:00
|
|
|
begin
|
2013-04-13 17:39:23 -04:00
|
|
|
wait = r.readpartial(1)
|
|
|
|
if wait != "!"
|
|
|
|
wait << r.readpartial(1000)
|
|
|
|
raise wait
|
|
|
|
end
|
2013-04-05 00:23:02 -04:00
|
|
|
if input
|
|
|
|
w.print(input)
|
|
|
|
w.flush
|
|
|
|
end
|
2013-04-08 01:12:08 -04:00
|
|
|
res = r.gets("\0")
|
2013-04-05 00:23:02 -04:00
|
|
|
return unless res
|
2013-04-08 01:12:08 -04:00
|
|
|
res.chomp!("\0")
|
2013-04-05 00:23:02 -04:00
|
|
|
res, error = Marshal.load(res[/(.*)\Z/, 1].unpack('m')[0])
|
|
|
|
raise error if error
|
|
|
|
return res
|
2013-06-19 03:47:19 -04:00
|
|
|
ensure
|
|
|
|
r.close unless r.closed?
|
|
|
|
w.close unless w.closed?
|
|
|
|
Process.wait(pid)
|
|
|
|
end
|
2013-04-05 00:23:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_getch
|
|
|
|
assert_equal("a", run_curses("getch", "a"))
|
|
|
|
end
|
|
|
|
def test_getch_cbreak
|
|
|
|
assert_equal("a", run_curses("cbreak; getch", "a"))
|
|
|
|
end
|
|
|
|
def test_getch_nocbreak
|
|
|
|
assert_raise(Timeout::Error) {run_curses("nocbreak; getch", "a")}
|
|
|
|
end
|
2013-04-05 00:23:05 -04:00
|
|
|
def test_getch_crmode
|
|
|
|
assert_equal("a", run_curses("crmode; getch", "a"))
|
|
|
|
end
|
|
|
|
def test_getch_nocrmode
|
|
|
|
assert_raise(Timeout::Error) {run_curses("nocrmode; getch", "a")}
|
|
|
|
end
|
2013-04-05 00:23:02 -04:00
|
|
|
end if defined? TestCurses and defined? PTY
|