|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""tests for wc.py""" |
| 3 | + |
| 4 | +import os |
| 5 | +import random |
| 6 | +import re |
| 7 | +import string |
| 8 | +from subprocess import getstatusoutput |
| 9 | + |
| 10 | +prg = './wc.py' |
| 11 | +fox = '../inputs/fox.txt' |
| 12 | +sonnet = '../inputs/sonnet-29.txt' |
| 13 | + |
| 14 | + |
| 15 | +# -------------------------------------------------- |
| 16 | +def test_exists(): |
| 17 | + """exists""" |
| 18 | + |
| 19 | + assert os.path.isfile(prg) |
| 20 | + |
| 21 | + |
| 22 | +# -------------------------------------------------- |
| 23 | +def test_usage(): |
| 24 | + """usage""" |
| 25 | + |
| 26 | + for flag in ['-h', '--help']: |
| 27 | + rv, out = getstatusoutput('{} {}'.format(prg, flag)) |
| 28 | + assert rv == 0 |
| 29 | + assert re.match("usage", out, re.IGNORECASE) |
| 30 | + |
| 31 | + |
| 32 | +# -------------------------------------------------- |
| 33 | +def random_string(): |
| 34 | + """generate a random string""" |
| 35 | + |
| 36 | + k = random.randint(5, 10) |
| 37 | + return ''.join(random.choices(string.ascii_letters + string.digits, k=k)) |
| 38 | + |
| 39 | + |
| 40 | +# -------------------------------------------------- |
| 41 | +def test_bad_file(): |
| 42 | + """bad_file""" |
| 43 | + |
| 44 | + bad = random_string() |
| 45 | + rv, out = getstatusoutput('{} {}'.format(prg, bad)) |
| 46 | + assert rv != 0 |
| 47 | + assert re.search(f"No such file or directory: '{bad}'", out) |
| 48 | + |
| 49 | + |
| 50 | +# -------------------------------------------------- |
| 51 | +def test_fox(): |
| 52 | + """Test on fox""" |
| 53 | + |
| 54 | + rv, out = getstatusoutput('{} {}'.format(prg, fox)) |
| 55 | + |
| 56 | + assert rv == 0 |
| 57 | + assert out.rstrip() == ' 1 9 45 ../inputs/fox.txt' |
| 58 | + |
| 59 | + |
| 60 | +# -------------------------------------------------- |
| 61 | +def test_more(): |
| 62 | + """Test on more than one file""" |
| 63 | + |
| 64 | + rv, out = getstatusoutput('{} {} {}'.format(prg, fox, sonnet)) |
| 65 | + |
| 66 | + expected = (' 1 9 45 ../inputs/fox.txt\n' |
| 67 | + ' 17 118 661 ../inputs/sonnet-29.txt') |
| 68 | + assert rv == 0 |
| 69 | + assert out.rstrip() == expected |
0 commit comments