Skip to content

Commit

Permalink
#46 Fixed curses tests
Browse files Browse the repository at this point in the history
  • Loading branch information
campadrenalin committed Dec 15, 2013
1 parent b10bf89 commit b1c8b50
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 88 deletions.
17 changes: 9 additions & 8 deletions deje/dexter/prompt.py
Expand Up @@ -42,15 +42,16 @@ def __init__(self, interface):
self.history = []

def draw(self):
self.terminal.prompt_win.erase()
width = self.terminal.width
height = self.terminal.height
outstr = self.pstring + self.current
self.terminal.prompt_win.insstr(0,0, outstr)
self.terminal.prompt_win.move(
0,
min(len(outstr), self.terminal.width-1)
)
self.terminal.prompt_win.cursyncup()
self.terminal.prompt_win.refresh()

y = height - 1
x = min(len(outstr), width-1)

self.terminal.stdscr.insstr(y,0, outstr.ljust(width))
self.terminal.stdscr.move(y, x)
self.terminal.stdscr.cursyncup()

def _wait(self):
while 1:
Expand Down
12 changes: 6 additions & 6 deletions deje/dexter/terminal.py
Expand Up @@ -24,10 +24,10 @@ def __init__(self):
self.setup()

def setup(self):
self.stdscr = curses.initscr()
height, width = self.stdscr.getmaxyx()
self.view_win = self.stdscr.subwin(0, 0)
self.prompt_win = self.stdscr.subwin(height-1, 0)
self.stdscr = curses.initscr()

def stop(self):
curses.endwin()

def __enter__(self):
curses.noecho()
Expand All @@ -37,13 +37,13 @@ def __enter__(self):
return self

def __exit__(self, *args):
curses.endwin()
self.stop()

def getch(self):
return self.stdscr.getch()

def on_resize(self):
curses.endwin()
self.stop()
self.setup()

@property
Expand Down
14 changes: 8 additions & 6 deletions deje/dexter/view.py
Expand Up @@ -22,15 +22,17 @@ def __init__(self, interface, desc = None):
self.contents = []

def draw(self):
self.terminal.view_win.erase()
y_pos = self.terminal.height - 2
x_max = self.terminal.width
for line in reversed(self.contents):
self.terminal.view_win.addstr(y_pos, 0, line[:x_max])
lines = list(self.contents)
while y_pos >= 0:
if len(lines):
line = lines.pop()
else:
line = ''
outstr = line[:x_max].ljust(x_max)
self.terminal.stdscr.addstr(y_pos, 0, outstr)
y_pos -= 1
if y_pos < 0:
break
self.terminal.view_win.refresh()

def append(self, text):
self.contents.extend(text.split('\n'))
Expand Down
24 changes: 1 addition & 23 deletions deje/tests/dexter_commands.py
Expand Up @@ -21,7 +21,6 @@
from ejtp.tests.test_scripts import IOMock

from deje.dexter.interface import DexterInterface
#from deje.tests.dummy_curses import DummyCursesTerminal

class DexterDemoGroup(object):
def __init__(self):
Expand All @@ -42,28 +41,7 @@ def setUp(self):
self.commands.groups.add(self.demo_group)

def tearDown(self):
with self.terminal:
pass

def get_line(self, y):
width = self.terminal.width
return ''.join(
chr(self.terminal.stdscr.inch(y, x))
for x in range(width)
)

def get_lines(self):
height = self.terminal.height
return [
self.get_line(y) for y in range(height)
]

def blank_lines(self, n):
return [self.blank_line] * n

@property
def blank_line(self):
return ' ' * self.terminal.width
self.terminal.stop()

@property
def demo_log(self):
Expand Down
2 changes: 0 additions & 2 deletions deje/tests/test_dexter_commands_deje.py
Expand Up @@ -93,7 +93,6 @@ def test_dinit_success(self):
self.interface.owner.identities.find_by_location(location)
)

'''
class TestDexterDEJEGroupInitialized(DexterCommandTester):

def setUp(self):
Expand Down Expand Up @@ -217,4 +216,3 @@ def test_msg_sent_valid(self):
'DEJE initialized',
'["local",null,"jackson"] (ME) : example',
])
'''
67 changes: 25 additions & 42 deletions deje/tests/test_dexter_view.py
Expand Up @@ -30,19 +30,21 @@ def setUp(self):
self.io = IOMock()
with self.io:
self.interface = DexterInterface()
self.view = DexterView(self.interface)
self.view = self.interface.view
self.terminal = self.interface.terminal

def tearDown(self):
with self.terminal:
pass

def get_line(self, y):
return String(self.terminal.stdscr.instr(y,0)).export()
return ''.join(
chr(self.terminal.stdscr.inch(y,x))
for x in range(self.terminal.width)
)

def get_lines(self):
height = self.terminal.height
self.terminal.stdscr.putwin(open("hello", "wb"))
return [
self.get_line(y) for y in range(height)
]
Expand All @@ -67,63 +69,44 @@ def test_multiple_append(self):
self.view.append("Line 2\nLine 3")
self.assertEqual(self.view.contents, ["Line 1","Line 2","Line 3"])

'''
def assertLines(self, lines):
width = self.terminal.width
expected = self.blank_lines(self.terminal.height-len(lines))
expected += [ line.ljust(width) for line in lines ]
self.assertEqual(self.get_lines(), expected)

def test_draw_empty(self):
with self.io:
self.view.draw()
self.assertEqual(self.get_lines(), [
'CONTEXT ENTER: location(0,0)',
'clear',
'CONTEXT EXIT: location(0,0)',
'CONTEXT ENTER: location(0,60)',
self.interface.redraw()
self.assertLines([
'msglog>',
'CONTEXT EXIT: location(0,60)',
])

def test_draw_one_line(self):
with self.io:
self.view.append("Hello")
self.view.draw()
self.assertEqual(self.get_lines(), [
'CONTEXT ENTER: location(0,0)',
'clear',
'CONTEXT EXIT: location(0,0)',
'CONTEXT ENTER: location(0,60)',
'msglog>',
'CONTEXT EXIT: location(0,60)',
'CONTEXT ENTER: location(0,59)',
self.interface.redraw()
self.assertLines([
'Hello',
'CONTEXT EXIT: location(0,59)',
'msglog>',
])
'''

def test_draw_two_lines(self):
with self.io:
self.view.append("Hello\nworld")
self.interface.redraw()
width = self.terminal.width
self.maxDiff = None
self.assertEqual(self.get_lines(),
self.blank_lines(self.terminal.height-2) + [
'Hello'.ljust(width),
'world'.ljust(width),
'msglog>'.ljust(width),
self.assertLines([
'Hello',
'world',
'msglog>',
])

'''
def test_draw_long_line(self):
with self.io:
self.view.append("q" * 85)
self.view.draw()
self.assertEqual(self.get_lines(), [
'CONTEXT ENTER: location(0,0)',
'clear',
'CONTEXT EXIT: location(0,0)',
'CONTEXT ENTER: location(0,60)',
width = self.terminal.width
self.view.append("q" * (width + 5))
self.interface.redraw()
self.assertLines([
'q' * width, # Trimmed to terminal width
'msglog>',
'CONTEXT EXIT: location(0,60)',
'CONTEXT ENTER: location(0,59)',
'q' * 80, # Trimmed to terminal width
'CONTEXT EXIT: location(0,59)',
])
'''
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -10,7 +10,7 @@ deps=
git+git://github.com/campadrenalin/lupa.git@fetchy
commands=
{envpython} setup.py --quiet build install
{envpython} -m deje.tests.runner test_dexter_view
{envpython} -m deje.tests.runner
sitepackages=False

[testenv:py26]
Expand Down

0 comments on commit b1c8b50

Please sign in to comment.