summaryrefslogtreecommitdiff
path: root/test/test_curses.rb
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-15 14:19:55 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-15 14:19:55 +0000
commitd9aa81ea3eadd09de5d856ff26f57e3e7cfc0e3f (patch)
tree91766da1a477e88e6caeba94eef4d4847235bc6f /test/test_curses.rb
parent7514d35dc777b003d714d36be703b0c632ceb18f (diff)
merge revision(s) 40117,40118: [Backport #8222]
* test/test_curses.rb: tests for getch. * ext/curses/curses.c (Init_curses): fix implementation function, crmode should be same as cbreak. [ruby-core:54013] [Bug #8222] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@40311 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/test_curses.rb')
-rw-r--r--test/test_curses.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/test_curses.rb b/test/test_curses.rb
index 03462d6f7e..9432c024bb 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,49 @@ 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
+ 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
+end if defined? TestCurses and defined? PTY