summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-05 04:23:02 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-05 04:23:02 +0000
commit7cd3fcfbaa8efabe756f52ef36c91469be525d5d (patch)
treee1123229f5748ecd0c56c312fb76b633c7c75e77 /test
parentb0446f37a61c5e5164b73248a7aaf1f92d30b8de (diff)
* test/test_curses.rb: tests for getch.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40117 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/test_curses.rb42
1 files changed, 42 insertions, 0 deletions
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