summaryrefslogtreecommitdiff
path: root/test/test_curses.rb
blob: d59f7dd35404f466e8c4c64a7f69a578903999a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
require 'test/unit'
require_relative 'ruby/envutil'

begin
  require 'curses'
  require 'pty'
rescue LoadError
end

class TestCurses < Test::Unit::TestCase
  def test_version
    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