Skip to content

Commit 04d2eca

Browse files
committed
tests for howler
1 parent e646e19 commit 04d2eca

10 files changed

Lines changed: 237 additions & 39 deletions

File tree

howler/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: pdf test
2+
3+
pdf:
4+
pandoc README.md -o README.pdf
5+
6+
test:
7+
pytest -v test.py

howler/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
# Howler
22

33
Write a Python program `howler.py` that will uppercase all the text from the command line or from a file.
4+
5+
````
6+
$ ./howler.py
7+
usage: howler.py [-h] [-o str] STR
8+
howler.py: error: the following arguments are required: STR
9+
$ ./howler.py -h
10+
usage: howler.py [-h] [-o str] STR
11+
12+
Howler (upper-case input)
13+
14+
positional arguments:
15+
STR Input string or file
16+
17+
optional arguments:
18+
-h, --help show this help message and exit
19+
-o str, --outfile str
20+
Output filename (default: )
21+
````
22+
23+
# Skills
24+
25+
* Reading text from command line or a file
26+
* Transforming text
27+
* Write to a file or STDOUT

howler/howler.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

howler/mk-test-out.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -u
4+
5+
PRG="./howler.py"
6+
IN_DIR="../inputs"
7+
OUT_DIR="test-outs"
8+
9+
[[ ! -d "$OUT_DIR" ]] && mkdir -p "$OUT_DIR"
10+
11+
for FILE in sonnet-29.txt the-bustle.txt preamble.txt; do
12+
IN_FILE="$IN_DIR/$FILE"
13+
OUT_FILE="$OUT_DIR/$FILE"
14+
15+
if [[ -f "$IN_FILE" ]]; then
16+
$PRG "$IN_FILE" -o "$OUT_FILE"
17+
fi
18+
done
19+
20+
echo "Done."

howler/solution.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : kyclark
4+
Date : 2019-05-17
5+
Purpose: Howler
6+
"""
7+
8+
import argparse
9+
import os
10+
import sys
11+
12+
13+
# --------------------------------------------------
14+
def get_args():
15+
"""get command-line arguments"""
16+
parser = argparse.ArgumentParser(
17+
description='Howler (upper-case input)',
18+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
19+
20+
parser.add_argument('text', metavar='STR', help='Input string or file')
21+
22+
parser.add_argument(
23+
'-o',
24+
'--outfile',
25+
help='Output filename',
26+
metavar='str',
27+
type=str,
28+
default='')
29+
30+
return parser.parse_args()
31+
32+
33+
# --------------------------------------------------
34+
def warn(msg):
35+
"""Print a message to STDERR"""
36+
print(msg, file=sys.stderr)
37+
38+
39+
# --------------------------------------------------
40+
def die(msg='Something bad happened'):
41+
"""warn() and exit with error"""
42+
warn(msg)
43+
sys.exit(1)
44+
45+
46+
# --------------------------------------------------
47+
def main():
48+
"""Make a jazz noise here"""
49+
args = get_args()
50+
text = args.text
51+
out_file = args.outfile
52+
53+
if os.path.isfile(text):
54+
text = open(text).read()
55+
56+
out_fh = open(out_file, 'wt') if out_file else sys.stdout
57+
out_fh.write(text.upper() + '\n')
58+
59+
60+
# --------------------------------------------------
61+
if __name__ == '__main__':
62+
main()

howler/test-outs/preamble.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
WHEN, IN THE COURSE OF HUMAN EVENTS, IT BECOMES NECESSARY FOR ONE PEOPLE TO
2+
DISSOLVE THE POLITICAL BANDS WHICH HAVE CONNECTED THEM WITH ANOTHER, AND TO
3+
ASSUME AMONG THE POWERS OF THE EARTH, THE SEPARATE AND EQUAL STATION TO
4+
WHICH THE LAWS OF NATURE AND OF NATURE'S GOD ENTITLE THEM, A DECENT RESPECT
5+
TO THE OPINIONS OF MANKIND REQUIRES THAT THEY SHOULD DECLARE THE CAUSES
6+
WHICH IMPEL THEM TO THE SEPARATION.
7+

howler/test-outs/sonnet-29.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
SONNET 29
2+
WILLIAM SHAKESPEARE
3+
4+
WHEN, IN DISGRACE WITH FORTUNE AND MEN’S EYES,
5+
I ALL ALONE BEWEEP MY OUTCAST STATE,
6+
AND TROUBLE DEAF HEAVEN WITH MY BOOTLESS CRIES,
7+
AND LOOK UPON MYSELF AND CURSE MY FATE,
8+
WISHING ME LIKE TO ONE MORE RICH IN HOPE,
9+
FEATURED LIKE HIM, LIKE HIM WITH FRIENDS POSSESSED,
10+
DESIRING THIS MAN’S ART AND THAT MAN’S SCOPE,
11+
WITH WHAT I MOST ENJOY CONTENTED LEAST;
12+
YET IN THESE THOUGHTS MYSELF ALMOST DESPISING,
13+
HAPLY I THINK ON THEE, AND THEN MY STATE,
14+
(LIKE TO THE LARK AT BREAK OF DAY ARISING
15+
FROM SULLEN EARTH) SINGS HYMNS AT HEAVEN’S GATE;
16+
FOR THY SWEET LOVE REMEMBERED SUCH WEALTH BRINGS
17+
THAT THEN I SCORN TO CHANGE MY STATE WITH KINGS.
18+

howler/test-outs/the-bustle.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
THE BUSTLE IN A HOUSE
2+
THE MORNING AFTER DEATH
3+
IS SOLEMNEST OF INDUSTRIES
4+
ENACTED UPON EARTH,—
5+
6+
THE SWEEPING UP THE HEART,
7+
AND PUTTING LOVE AWAY
8+
WE SHALL NOT WANT TO USE AGAIN
9+
UNTIL ETERNITY.
10+

howler/test.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python3
2+
"""tests for howler.py"""
3+
4+
import os
5+
import re
6+
import random
7+
import string
8+
from subprocess import getstatusoutput, getoutput
9+
10+
prg = './howler.py'
11+
12+
13+
# --------------------------------------------------
14+
def random_string():
15+
"""generate a random string"""
16+
17+
k = random.randint(5, 10)
18+
return ''.join(random.choices(string.ascii_letters + string.digits, k=k))
19+
20+
21+
# --------------------------------------------------
22+
def out_flag():
23+
"""Either -o or --outfile"""
24+
25+
return '-o' if random.randint(0, 1) else '--outfile'
26+
27+
28+
# --------------------------------------------------
29+
def test_usage():
30+
"""usage"""
31+
32+
for flag in ['-h', '--help']:
33+
rv, out = getstatusoutput('{} {}'.format(prg, flag))
34+
assert rv == 0
35+
assert re.match("usage", out, re.IGNORECASE)
36+
37+
38+
# --------------------------------------------------
39+
def test_text_stdout():
40+
"""Test STDIN/STDOUT"""
41+
42+
out = getoutput('{} "foo bar baz"'.format(prg))
43+
expected = 'FOO BAR BAZ'
44+
assert out.strip() == expected
45+
46+
47+
# --------------------------------------------------
48+
def test_text_outfile():
49+
"""Test STDIN/outfile"""
50+
51+
out_file = random_string()
52+
53+
if os.path.isfile(out_file):
54+
os.remove(out_file)
55+
56+
try:
57+
out = getoutput('{} {} {} "foo bar baz"'.format(
58+
prg, out_flag(), out_file))
59+
assert out.strip() == ''
60+
assert os.path.isfile(out_file)
61+
text = open(out_file).read().rstrip()
62+
assert text == 'FOO BAR BAZ'
63+
finally:
64+
if os.path.isfile(out_file):
65+
os.remove(out_file)
66+
67+
68+
# --------------------------------------------------
69+
def test_file():
70+
"""Test file in/out"""
71+
72+
for expected_file in os.listdir('test-outs'):
73+
try:
74+
out_file = random_string()
75+
if os.path.isfile(out_file):
76+
os.remove(out_file)
77+
78+
basename = os.path.basename(expected_file)
79+
in_file = os.path.join('../inputs', basename)
80+
out = getoutput('{} {} {} {}'.format(prg, out_flag(), out_file,
81+
in_file))
82+
assert out.strip() == ''
83+
produced = open(out_file).read().rstrip()
84+
expected = open(os.path.join('test-outs',
85+
expected_file)).read().strip()
86+
assert expected == produced
87+
finally:
88+
if os.path.isfile(out_file):
89+
os.remove(out_file)

howler/the-bustle.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)