3

I am studying OOP and made a command line todo list for practicing. I tested all my methods from the classes used in Menu but couldn't figure out a way to test my class Menu.
Inside the Menu class, the methods ask for the user to type inputs and I don't know how to test it.

I'm not posting all of my code here because I'm using it only as an example to my question, because I want a more generic answer.

import sys

from todo.task import TaskContainer

class Menu:

    def __init__(self):
        self.tasks = TaskContainer()
        self.choices = {
                1 : self.create_new_task,
                2 : self.edit_task,
                3 : self.edit_task_description,
                4 : self.edit_task_status,
                5 : self.search_task_by_id,
                6 : self.search_task_by_word,
                7 : self.delete_task,
                8 : self.show_all_tasks,
                9 : self.show_task_description,
                10 : self.show_task_date,
                11 : self.quit}

    def display_menu(self):
        print("""
                1 : Create new task
                2 : Edit task
                3 : Edit task description
                4 : Edit task status
                5 : Search task by id
                6 : Search task by matching word
                7 : Delete task by id
                8 : Show all tasks
                9 : Show task description
                10 : Show task dates
                11 : Quit
                """)

    def run(self):
        while True:
            self.display_menu()
            choice = int(input("Enter an option: "))
            action = self.choices.get(choice)
            if action:
                action()
            else:
                print("{} is not a valid choice.".format(choice))

    def create_new_task(self):
        task = input("Task: ")
        due_date = input("What is the due date for this task? ")
        description = input("If you wish, add a description to this task:")
        self.tasks.new_task(task)

    def edit_task(self):
        id = int(input("Type the ID of the task you wish to edit: "))
        changed_task = input("New task: ")
        self.tasks.edit_task(id, changed_task)

    def edit_task_description(self):
        id = int(input("Type the id of the task you want to edit the description? "))
        description = input("Type description: ")
        self.tasks.edit_task_description(id, description)

    def edit_task_status(self):
        id = int(input("Type the ID of the task you wish to change status: "))
        status = input("Status: ")
        self.tasks.edit_task_status(id, status)

    def search_task_by_id(self):
        id = int(input("Type task ID: "))
        print(self.tasks.search(id))

    def search_task_by_word(self):
        word = input("Type the word to be matched: ")
        matches = self.tasks.search_word(word)
        [print(task) for task in matches]

    def delete_task(self):
        id = int(input("Type the ID of the task you wish to delete: "))
        self.tasks.delete_task(id)

    def show_all_tasks(self):
        self.tasks.show_all_tasks()

    def show_task_description(self):
        id = int(input("Type the ID you wish to see the description: "))
        print(self.tasks.show_description(id))

    def show_task_date(self):
        id = int(input("Type the ID you wish to see the due date: "))
        print(self.tasks.show_due_date(id))

    def quit(self):
        sys.exit(0)

if __name__ == "__main__":
    Menu().run()

jofrev
  • 324
  • 3
  • 11
Andressa Cabistani
  • 463
  • 1
  • 5
  • 14

1 Answers1

1

At the bottom, put the name of the function (display__menu()) and it will run with the rest of the program

Lanius
  • 11
  • 1
  • My display_menu() method is being called inside the run() method. I call Menu().run() to run it already. This was not an answer to my question, what I did works fine. – Andressa Cabistani Jul 08 '19 at 13:48