PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/_minimal_curses/test/test_curses.py

https://bitbucket.org/pypy/pypy/
Python | 108 lines | 98 code | 6 blank | 4 comment | 3 complexity | eb4d5223204ca4f7b9547be8d5865c9c MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  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. def setup_module(mod):
  7. try:
  8. import curses
  9. curses.setupterm()
  10. except:
  11. py.test.skip("Cannot test this here")
  12. class TestCurses(object):
  13. """ We need to fork here, to prevent
  14. the setup to be done
  15. """
  16. def _spawn(self, *args, **kwds):
  17. import pexpect
  18. kwds.setdefault('timeout', 600)
  19. print 'SPAWN:', args, kwds
  20. child = pexpect.spawn(*args, **kwds)
  21. child.logfile = sys.stdout
  22. return child
  23. def spawn(self, argv):
  24. py_py = py.path.local(pypydir).join('bin', 'pyinteractive.py')
  25. return self._spawn(sys.executable, [str(py_py)] + argv)
  26. def setup_class(self):
  27. try:
  28. import pexpect
  29. except ImportError:
  30. py.test.skip('pexpect not found')
  31. def test_setupterm(self):
  32. source = py.code.Source("""
  33. import _minimal_curses
  34. try:
  35. _minimal_curses.tigetstr('cup')
  36. except _minimal_curses.error:
  37. print 'ok!'
  38. """)
  39. f = udir.join("test_setupterm.py")
  40. f.write(source)
  41. child = self.spawn(['--withmod-_minimal_curses', str(f)])
  42. child.expect('ok!')
  43. def test_tigetstr(self):
  44. source = py.code.Source("""
  45. import _minimal_curses
  46. _minimal_curses.setupterm()
  47. assert _minimal_curses.tigetstr('cup') == '\x1b[%i%p1%d;%p2%dH'
  48. print 'ok!'
  49. """)
  50. f = udir.join("test_tigetstr.py")
  51. f.write(source)
  52. child = self.spawn(['--withmod-_minimal_curses', str(f)])
  53. child.expect('ok!')
  54. def test_tparm(self):
  55. source = py.code.Source("""
  56. import _minimal_curses
  57. _minimal_curses.setupterm()
  58. assert _minimal_curses.tparm(_minimal_curses.tigetstr('cup'), 5, 3) == '\033[6;4H'
  59. print 'ok!'
  60. """)
  61. f = udir.join("test_tparm.py")
  62. f.write(source)
  63. child = self.spawn(['--withmod-_minimal_curses', str(f)])
  64. child.expect('ok!')
  65. class TestCCurses(object):
  66. """ Test compiled version
  67. """
  68. def test_csetupterm(self):
  69. from rpython.translator.c.test.test_genc import compile
  70. from pypy.module._minimal_curses import interp_curses
  71. def runs_setupterm():
  72. interp_curses._curses_setupterm_null(1)
  73. fn = compile(runs_setupterm, [])
  74. fn()
  75. def test_ctgetstr(self):
  76. from rpython.translator.c.test.test_genc import compile
  77. from pypy.module._minimal_curses import interp_curses
  78. def runs_ctgetstr():
  79. interp_curses._curses_setupterm("xterm", 1)
  80. return interp_curses._curses_tigetstr('cup')
  81. fn = compile(runs_ctgetstr, [])
  82. res = fn()
  83. assert res == '\x1b[%i%p1%d;%p2%dH'
  84. def test_ctparm(self):
  85. from rpython.translator.c.test.test_genc import compile
  86. from pypy.module._minimal_curses import interp_curses
  87. def runs_tparm():
  88. interp_curses._curses_setupterm("xterm", 1)
  89. cup = interp_curses._curses_tigetstr('cup')
  90. return interp_curses._curses_tparm(cup, [5, 3])
  91. fn = compile(runs_tparm, [])
  92. res = fn()
  93. assert res == '\033[6;4H'