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)
|
|
|
|
PTY.spawn(EnvUtil.rubybin, "-e", <<-"src") {|r, w, pid|
|
|
|
|
require 'timeout'
|
|
|
|
require 'curses'
|
|
|
|
include Curses
|
|
|
|
init_screen
|
|
|
|
begin
|
|
|
|
result = Timeout.timeout(#{timeout}) do
|
|
|
|
#{src}
|
|
|
|
end
|
|
|
|
rescue Exception => e
|
|
|
|
ensure
|
|
|
|
close_screen
|
|
|
|
puts "", [Marshal.dump([result, e])].pack('m').delete("\n")
|
|
|
|
end
|
|
|
|
src
|
|
|
|
if input
|
|
|
|
w.print(input)
|
|
|
|
w.flush
|
|
|
|
end
|
|
|
|
res = r.read
|
|
|
|
return unless res
|
|
|
|
res, error = Marshal.load(res[/(.*)\Z/, 1].unpack('m')[0])
|
|
|
|
raise error if error
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
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
|
|
|
|
end if defined? TestCurses and defined? PTY
|