Debian Codegraph @

pypy/module/_minimal_curses/test/test_curses.py

raw symbols SCIP
1 from pypy import pypydir
2 from rpython.tool.udir import udir
3 import py
4 import sys
5 # tests here are run as snippets through a pexpected python subprocess
6
7
8 def setup_module(mod):
9 try:
10 import curses
11 curses.setupterm()
12 except:
13 py.test.skip("Cannot test this here")
14
15 class TestCurses(object):
16 """ We need to fork here, to prevent
17 the setup to be done
18 """
19 def _spawn(self, *args, **kwds):
20 import pexpect
21 kwds.setdefault('timeout', 600)
22 print 'SPAWN:', args, kwds
23 child = pexpect.spawn(*args, **kwds)
24 child.logfile = sys.stdout
25 return child
26
27 def spawn(self, argv):
28 py_py = py.path.local(pypydir).join('bin', 'pyinteractive.py')
29 return self._spawn(sys.executable, [str(py_py), '-S'] + argv)
30
31 def setup_class(self):
32 try:
33 import pexpect
34 except ImportError:
35 py.test.skip('pexpect not found')
36
37 def test_setupterm(self):
38 source = py.code.Source("""
39 import _minimal_curses
40 try:
41 _minimal_curses.tigetstr('cup')
42 except _minimal_curses.error:
43 print('ok!')
44 """)
45 f = udir.join("test_setupterm.py")
46 f.write(source)
47 child = self.spawn(['--withmod-_minimal_curses', str(f)])
48 child.expect('ok!')
49
50 def test_tigetstr(self):
51 source = py.code.Source("""
52 import _minimal_curses
53 _minimal_curses.setupterm()
54 assert _minimal_curses.tigetstr('cup') == b'\x1b[%i%p1%d;%p2%dH'
55 print('ok!')
56 """)
57 f = udir.join("test_tigetstr.py")
58 f.write(source)
59 child = self.spawn(['--withmod-_minimal_curses', str(f)])
60 child.expect('ok!')
61
62 def test_tparm(self):
63 source = py.code.Source("""
64 import _minimal_curses
65 _minimal_curses.setupterm()
66 assert _minimal_curses.tparm(_minimal_curses.tigetstr('cup'), 5, 3) == b'\033[6;4H'
67 print('ok!')
68 """)
69 f = udir.join("test_tparm.py")
70 f.write(source)
71 child = self.spawn(['--withmod-_minimal_curses', str(f)])
72 child.expect('ok!')
73
74 class TestCCurses(object):
75 """ Test compiled version
76 """
77 def test_csetupterm(self):
78 from rpython.translator.c.test.test_genc import compile
79 from rpython.rtyper.lltypesystem import lltype, rffi
80 from pypy.module._minimal_curses import fficurses
81
82 def runs_setupterm():
83 null = lltype.nullptr(rffi.CCHARP.TO)
84 p_errret = lltype.malloc(rffi.INT_realP.TO, 1, flavor='raw')
85 errval = fficurses.setupterm(null, 1, p_errret)
86
87 fn = compile(runs_setupterm, [])
88 fn()
89
90 def test_ctgetstr(self):
91 from rpython.translator.c.test.test_genc import compile
92 from rpython.rtyper.lltypesystem import lltype, rffi
93 from pypy.module._minimal_curses import fficurses
94
95 def runs_ctgetstr():
96 p_errret = lltype.malloc(rffi.INT_realP.TO, 1, flavor='raw')
97 with rffi.scoped_str2charp("xterm") as ll_term:
98 errval = fficurses.setupterm(ll_term, 1, p_errret)
99 with rffi.scoped_str2charp("cup") as ll_capname:
100 ll = fficurses.rpy_curses_tigetstr(ll_capname)
101 return rffi.charp2str(ll)
102
103 fn = compile(runs_ctgetstr, [])
104 res = fn()
105 assert res == '\x1b[%i%p1%d;%p2%dH'
106
107 def test_ctparm(self):
108 from rpython.translator.c.test.test_genc import compile
109 from rpython.rtyper.lltypesystem import lltype, rffi
110 from pypy.module._minimal_curses import fficurses
111
112 def runs_tparm():
113 p_errret = lltype.malloc(rffi.INT_realP.TO, 1, flavor='raw')
114 with rffi.scoped_str2charp("xterm") as ll_term:
115 errval = fficurses.setupterm(ll_term, 1, p_errret)
116 with rffi.scoped_str2charp("cup") as ll_capname:
117 cup = fficurses.rpy_curses_tigetstr(ll_capname)
118 res = fficurses.rpy_curses_tparm(cup, 5, 3, 0, 0, 0, 0, 0, 0, 0)
119 return rffi.charp2str(res)
120
121 fn = compile(runs_tparm, [])
122 res = fn()
123 assert res == '\033[6;4H'
124