Skip to content

Commit 433a213

Browse files
committed
pytests's capsys instead of sys
1 parent 75c3023 commit 433a213

File tree

5 files changed

+12
-37
lines changed

5 files changed

+12
-37
lines changed

cowsay/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
from typing import Callable
3+
14
from .main import (
25

36
__version__,
@@ -16,7 +19,7 @@
1619

1720
# This is where we create functions for each character dynamically
1821

19-
char_funcs = {}
22+
char_funcs: dict[str, Callable] = {}
2023

2124
for ch_name, ch_lines in CHARS.items():
2225

cowsay/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from __future__ import annotations
12
import re
23

34
from .characters import CHARS
45

56
__version__ = '6.1'
67

7-
CHARS = dict(sorted(CHARS.items()))
8-
char_names = list(CHARS.keys())
8+
CHARS: dict[str, str] = dict(sorted(CHARS.items()))
9+
char_names: list[str] = list(CHARS.keys())
910

1011

1112
class CowsayError(LookupError):
@@ -51,7 +52,7 @@ def generate_char(char_lines: str, text_width: int) -> list:
5152
return output
5253

5354

54-
def draw(text: str, char_lines: str, to_console: bool = True) -> str:
55+
def draw(text: str, char_lines: str, to_console: bool = True) -> None | str:
5556

5657
if len(re.sub(r'\s', '', text)) == 0:
5758
raise CowsayError('Pass something meaningful to cowsay')

cowsay/tests/solutions.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@
202202
\
203203
\
204204
\
205-
206205
("`-' '-/") .___..--' ' "`-._
207206
` *_ * ) `-. ( ) .`-.__. `)
208207
(_Y_.) ' ._ ) `._` ; `` -. .-'
@@ -495,7 +494,6 @@
495494
/" "| /-".:%%%%%%%\
496495
;,-"'`)%%)
497496
/" "|
498-
499497
''',
500498

501499

cowsay/tests/test_api.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,16 @@ def test_char_names():
1515

1616

1717
def test_draw_error():
18-
1918
with pytest.raises(cowsay.CowsayError) as e:
2019
cowsay.draw('', '')
21-
2220
assert e.value.args[0] == 'Pass something meaningful to cowsay'
2321

2422

2523
def test_get_output_string():
26-
2724
assert isinstance(cowsay.get_output_string(char='cow', text='Hello'), str)
2825

2926

3027
def test_get_output_string_error():
31-
3228
with pytest.raises(cowsay.CowsayError) as e:
3329
cowsay.get_output_string('random', 'random text')
34-
3530
assert e.value.args[0] == f'Available Characters: {cowsay.char_names}'

cowsay/tests/test_output.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
1-
import io
2-
import sys
3-
from typing import Callable
4-
51
import pytest
62

73
from cowsay import char_funcs, char_names
84
from . import solutions
95

106

11-
def capture_output(function: Callable, arguments: str) -> str:
12-
captured_output = io.StringIO()
13-
sys.stdout = captured_output
14-
function(arguments)
15-
sys.stdout = sys.__stdout__
16-
captured_output.seek(0)
17-
return captured_output.read()
18-
19-
20-
def delete_empty_lines(data):
21-
new_data = []
22-
for line in data.splitlines():
23-
if len(line.strip()) > 0:
24-
new_data.append(line.rstrip())
25-
return new_data
26-
27-
287
@pytest.mark.parametrize('char', char_names, ids=char_names)
29-
def test_char_solution(char):
8+
def test_char_solution(char, capsys):
309

3110
lorem: str = (
3211
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam'
3312
'nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,'
3413
'sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.'
3514
)
3615

37-
output = capture_output(char_funcs[char], lorem)
38-
output = delete_empty_lines(output)
39-
solution = delete_empty_lines(solutions.CHARS_SOLUTIONS[char])
40-
assert output == solution
16+
char_funcs[char](lorem)
17+
out, _ = capsys.readouterr()
18+
assert out == solutions.CHARS_SOLUTIONS[char].lstrip('\n')

0 commit comments

Comments
 (0)