Skip to content

Commit

Permalink
pytests's capsys instead of sys
Browse files Browse the repository at this point in the history
  • Loading branch information
VaasuDevanS committed Oct 11, 2023
1 parent 75c3023 commit 433a213
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 37 deletions.
5 changes: 4 additions & 1 deletion cowsay/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations
from typing import Callable

from .main import (

__version__,
Expand All @@ -16,7 +19,7 @@

# This is where we create functions for each character dynamically

char_funcs = {}
char_funcs: dict[str, Callable] = {}

for ch_name, ch_lines in CHARS.items():

Expand Down
7 changes: 4 additions & 3 deletions cowsay/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations
import re

from .characters import CHARS

__version__ = '6.1'

CHARS = dict(sorted(CHARS.items()))
char_names = list(CHARS.keys())
CHARS: dict[str, str] = dict(sorted(CHARS.items()))
char_names: list[str] = list(CHARS.keys())


class CowsayError(LookupError):
Expand Down Expand Up @@ -51,7 +52,7 @@ def generate_char(char_lines: str, text_width: int) -> list:
return output


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

if len(re.sub(r'\s', '', text)) == 0:
raise CowsayError('Pass something meaningful to cowsay')
Expand Down
2 changes: 0 additions & 2 deletions cowsay/tests/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@
\
\
\
("`-' '-/") .___..--' ' "`-._
` *_ * ) `-. ( ) .`-.__. `)
(_Y_.) ' ._ ) `._` ; `` -. .-'
Expand Down Expand Up @@ -495,7 +494,6 @@
/" "| /-".:%%%%%%%\
;,-"'`)%%)
/" "|
''',


Expand Down
5 changes: 0 additions & 5 deletions cowsay/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,16 @@ def test_char_names():


def test_draw_error():

with pytest.raises(cowsay.CowsayError) as e:
cowsay.draw('', '')

assert e.value.args[0] == 'Pass something meaningful to cowsay'


def test_get_output_string():

assert isinstance(cowsay.get_output_string(char='cow', text='Hello'), str)


def test_get_output_string_error():

with pytest.raises(cowsay.CowsayError) as e:
cowsay.get_output_string('random', 'random text')

assert e.value.args[0] == f'Available Characters: {cowsay.char_names}'
30 changes: 4 additions & 26 deletions cowsay/tests/test_output.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
import io
import sys
from typing import Callable

import pytest

from cowsay import char_funcs, char_names
from . import solutions


def capture_output(function: Callable, arguments: str) -> str:
captured_output = io.StringIO()
sys.stdout = captured_output
function(arguments)
sys.stdout = sys.__stdout__
captured_output.seek(0)
return captured_output.read()


def delete_empty_lines(data):
new_data = []
for line in data.splitlines():
if len(line.strip()) > 0:
new_data.append(line.rstrip())
return new_data


@pytest.mark.parametrize('char', char_names, ids=char_names)
def test_char_solution(char):
def test_char_solution(char, capsys):

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

output = capture_output(char_funcs[char], lorem)
output = delete_empty_lines(output)
solution = delete_empty_lines(solutions.CHARS_SOLUTIONS[char])
assert output == solution
char_funcs[char](lorem)
out, _ = capsys.readouterr()
assert out == solutions.CHARS_SOLUTIONS[char].lstrip('\n')

0 comments on commit 433a213

Please sign in to comment.