From 7cd3fcfbaa8efabe756f52ef36c91469be525d5d Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 5 Apr 2013 04:23:02 +0000 Subject: [PATCH] * test/test_curses.rb: tests for getch. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/test_curses.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/test_curses.rb b/test/test_curses.rb index 03462d6f7e..d59f7dd354 100644 --- a/test/test_curses.rb +++ b/test/test_curses.rb @@ -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