* test/test_curses.rb: tests for getch.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-04-05 04:23:02 +00:00
Родитель b0446f37a6
Коммит 7cd3fcfbaa
1 изменённых файлов: 42 добавлений и 0 удалений

Просмотреть файл

@ -1,7 +1,9 @@
require 'test/unit'
require_relative 'ruby/envutil'
begin
require 'curses'
require 'pty'
rescue LoadError
end
@ -10,3 +12,43 @@ class TestCurses < Test::Unit::TestCase
assert_instance_of(String, Curses::VERSION)
end
end if defined? Curses
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