From 828f2a12008bb912032ab11697bb48515440b9b1 Mon Sep 17 00:00:00 2001 From: Mahak Gupta <92160780+Mahak314@users.noreply.github.com> Date: Mon, 15 Jul 2024 15:24:41 +0530 Subject: [PATCH] Add files via upload --- test.py | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 000000000..17677fad4 --- /dev/null +++ b/test.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +"""tests for wc.py""" + +import os +import random +import re +import string +from subprocess import getstatusoutput + +prg = './wc.py' +empty = './inputs/empty.txt' +one_line = './inputs/one.txt' +two_lines = './inputs/two.txt' +fox = '../inputs/fox.txt' +sonnet = '../inputs/sonnet-29.txt' + + +# -------------------------------------------------- +def test_exists(): + """exists""" + + assert os.path.isfile(prg) + + +# -------------------------------------------------- +def test_usage(): + """usage""" + + for flag in ['-h', '--help']: + rv, out = getstatusoutput(f'{prg} {flag}') + assert rv == 0 + assert re.match("usage", out, re.IGNORECASE) + + +# -------------------------------------------------- +def random_string(): + """generate a random string""" + + k = random.randint(5, 10) + return ''.join(random.choices(string.ascii_letters + string.digits, k=k)) + + +# -------------------------------------------------- +def test_bad_file(): + """bad_file""" + + bad = random_string() + rv, out = getstatusoutput(f'{prg} {bad}') + assert rv != 0 + assert re.search(f"No such file or directory: '{bad}'", out) + + +# -------------------------------------------------- +def test_empty(): + """Test on empty""" + + rv, out = getstatusoutput(f'{prg} {empty}') + assert rv == 0 + assert out.rstrip() == ' 0 0 0 ./inputs/empty.txt' + + +# -------------------------------------------------- +def test_one(): + """Test on one""" + + rv, out = getstatusoutput(f'{prg} {one_line}') + assert rv == 0 + assert out.rstrip() == ' 1 1 2 ./inputs/one.txt' + + +# -------------------------------------------------- +def test_two(): + """Test on two""" + + rv, out = getstatusoutput(f'{prg} {two_lines}') + assert rv == 0 + assert out.rstrip() == ' 2 2 4 ./inputs/two.txt' + + +# -------------------------------------------------- +def test_fox(): + """Test on fox""" + + rv, out = getstatusoutput(f'{prg} {fox}') + assert rv == 0 + assert out.rstrip() == ' 1 9 45 ../inputs/fox.txt' + + +# -------------------------------------------------- +def test_more(): + """Test on more than one file""" + rv, out = getstatusoutput(f'{prg} {fox} {sonnet}') + expected = (' 1 9 45 ../inputs/fox.txt\n' + ' 17 118 669 ../inputs/sonnet-29.txt\n' + ' 18 127 714 total') + assert rv == 0 + assert out.rstrip() == expected + + +# -------------------------------------------------- +def test_stdin(): + """Test on stdin""" + + rv, out = getstatusoutput(f'{prg} < {fox}') + assert rv == 0 + assert out.rstrip() == ' 1 9 45 '