commit 4bbf74c07c0e8b681b782159f884ebd1384f887b
parent b81a9d8562dcda0480b4cc5524f7fb3245749245
Author: Isak Lindhé <isak.e.lindhe@gmail.com>
Date: Wed, 10 Apr 2019 23:57:33 +0200
one test per command
Diffstat:
2 files changed, 38 insertions(+), 20 deletions(-)
diff --git a/txtv/test.py b/txtv/test.py
@@ -1,22 +1,28 @@
from pytest import fixture
+@fixture
+def default_config():
+ from txtv.config import CONFIG_DEFAULT_VALUES
+ from configparser import ConfigParser
+ return ConfigParser().read_dict(CONFIG_DEFAULT_VALUES)
-def test_help(capsys):
+
+def test_help():
from txtv.txtv import cmd_help, commands
- print(cmd_help(), end='')
- cap = capsys.readouterr()
- assert len(cap.out.splitlines()) == 1 + len([c for c in commands if 'help' in c]) # header + commands
- assert len(cap.err) == 0
+ out = cmd_help()
+ assert len(out.splitlines()) == \
+ 1 + len([
+ c for c in commands
+ if 'help' in c
+ ]) # header + commands
-def test_list(capsys):
+def test_list():
import re
from txtv.txtv import cmd_list, Page
from colorama import Fore
- print(cmd_list(), end='')
- cap = capsys.readouterr()
- assert len(cap.err) == 0
- lines = cap.out.splitlines()
+ out = cmd_list()
+ lines = out.splitlines()
for line in lines:
if len(line) == 0:
continue
@@ -25,13 +31,25 @@ def test_list(capsys):
Page(num)
-# def test_next(capsys):
-# pass
+def test_next():
+ from txtv.txtv import Page, cmd_next
+ state = dict(page=Page(100))
+ out = cmd_next(state=state)
+ assert state['page'].num == 101
+ assert out.split()[0] == '101'
+
+def test_prev():
+ from txtv.txtv import Page, cmd_prev
+ state = dict(page=Page(101))
+ out = cmd_prev(state=state)
+ assert state['page'].num == 100
+ assert out.split()[0] == '100'
-# def test_prev(capsys):
-# pass
+def test_page(default_config):
+ from txtv.txtv import cmd_page
+ out = cmd_page(arg='100', cfg=default_config)
+ lines = out.splitlines()
+ assert len(lines) == 21
-# def test_goto(capsys):
-# pass
diff --git a/txtv/txtv.py b/txtv/txtv.py
@@ -85,7 +85,7 @@ def interactive(start_page: Page, cfg: configparser.ConfigParser):
continue
cmd, m = match_command(raw, interactive=True)
if cmd:
- print(cmd['func'](state=state, match=m), end='')
+ print(cmd['func'](state=state, arg=raw), end='')
else:
err("That's not a command, kompis. 'help' gives you a list of commands.", fatal=False)
except (EOFError, KeyboardInterrupt):
@@ -136,9 +136,9 @@ def cmd_list(**kwargs) -> str:
return out
-def cmd_page(match, state: dict=None, cfg: configparser.ConfigParser=None, **kwargs) -> str:
+def cmd_page(arg: str, state: dict=None, **kwargs) -> str:
try:
- num = validate_page_nbr(match.group(0))
+ num = validate_page_nbr(arg)
except ValueError as e:
err(str(e), fatal=(state is None))
return ''
@@ -200,7 +200,7 @@ def run():
real_arg = apply_aliases(raw_arg, cfg)
cmd, m = match_command(real_arg)
if cmd:
- print(cmd['func'](match=m, cfg=cfg), end='')
+ print(cmd['func'](arg=real_arg, cfg=cfg), end='')
sys.exit(0)
else:
err("That's not a command, kompis. 'txtv help' gives you a list of commands.")