From de3d413d41dfa107f5ab8e0efe19f80fcc3f3595 Mon Sep 17 00:00:00 2001 From: Hugh Kramer Date: Wed, 28 May 2025 14:55:38 -0400 Subject: [PATCH] add testing for tasks practice --- tasks/.cs50.yml | 4 ++++ tasks/__init__.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++ tasks/monday.txt | 2 ++ tasks/testing.py | 16 +++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 tasks/.cs50.yml create mode 100644 tasks/__init__.py create mode 100644 tasks/monday.txt create mode 100644 tasks/testing.py diff --git a/tasks/.cs50.yml b/tasks/.cs50.yml new file mode 100644 index 0000000..c2de080 --- /dev/null +++ b/tasks/.cs50.yml @@ -0,0 +1,4 @@ +check50: + files: &check50_files + - !exclude "*" + - !require tasks.py diff --git a/tasks/__init__.py b/tasks/__init__.py new file mode 100644 index 0000000..895b9a3 --- /dev/null +++ b/tasks/__init__.py @@ -0,0 +1,61 @@ +import check50 +from re import escape + +@check50.check() +def exists(): + """tasks.py exists""" + check50.exists("tasks.py") + check50.include("testing.py", "monday.txt") + +@check50.check(exists) +def test_correct_input(): + """correctly accepts two command-line inputs""" + check50.run("python3 tasks.py").exit(code=1) + +@check50.check(exists) +def test_print_add(): + """correctly prints added task""" + output = "1. Attend Lecture\n2. Rip a Phonebook\n3. Call David" + check50.run("python3 tasks.py monday.txt").stdin("add Call David", prompt=True).stdout(regex(output), output).kill() + +@check50.check(exists) +def test_case_add(): + """disregards the case of add""" + output = "1. Attend Lecture\n2. Rip a Phonebook\n3. Call David" + check50.run("python3 tasks.py monday.txt").stdin("aDd Call David", prompt=True).stdout(regex(output), output).kill() + +@check50.check(exists) +def test_add_txt(): + """test""" + check50.run("python3 testing.py get_level") + +@check50.check(exists) +def test_print_remove(): + """correctly prints list after removal""" + output = "1. Rip a Phonebook" + check50.run("python3 tasks.py monday.txt").stdin("remove Attend Lecture", prompt=True).stdout(regex(output), output).kill() + +@check50.check(exists) +def test_case_remove(): + """disregards the case of remove""" + output = "1. Rip a Phonebook" + check50.run("python3 tasks.py monday.txt").stdin("rEMoVe Attend Lecture", prompt=True).stdout(regex(output), output).kill() + +@check50.check(exists) +def test_invalid_remove(): + """correctly exits when no match to remove""" + check50.run("python3 tasks.py monday.txt").stdin("remove Feed the Cat", prompt=True).exit(code=1) + +@check50.check(exists) +def test_just_command(): + """correctly exits when no description""" + check50.run("python3 tasks.py monday.txt").stdin("add", prompt=True).exit(code=1) + +@check50.check(exists) +def test_invalid_command(): + """correctly exits when invalid command""" + check50.run("python3 tasks.py monday.txt").stdin("edit Rip a Phonebook", prompt=True).exit(code=1) + +def regex(text): + """match case-sensitively with any characters on either side""" + return rf"^.*{escape(text)}.*$" \ No newline at end of file diff --git a/tasks/monday.txt b/tasks/monday.txt new file mode 100644 index 0000000..30fe521 --- /dev/null +++ b/tasks/monday.txt @@ -0,0 +1,2 @@ +Attend Lecture +Rip a Phonebook \ No newline at end of file diff --git a/tasks/testing.py b/tasks/testing.py new file mode 100644 index 0000000..b7eebbd --- /dev/null +++ b/tasks/testing.py @@ -0,0 +1,16 @@ +from tasks import add_task, remove_task, txt_length, update_txt +import sys + +def main(): + + argument = sys.argv[1] + tasks = ['Attend Lecture', 'Rip a Phonebook'] + description = sys.argv[3] + filepath = sys.argv[1] + + if argument == "add": + tasks.add_task(tasks, description, filepath) + print(tasks.txt_length(filepath)) + +if __name__ == "__main__": + main()