Ejemplo n.º 1
0
class Tests(unittest.TestCase):
    def setUp(self):
        '''
        Every test starts off with 3 unfinished tasks in the queue.
        '''
        self.session = Session(TEST_STORAGE_FILE)
        for i in range(3):
            self.session.add_task('Task {}'.format(i + 1))

    def tearDown(self):
        '''
        Cleans up the default storage file after every test.
        '''
        if os.path.exists(TEST_STORAGE_FILE):
            os.remove(TEST_STORAGE_FILE)

    def test_add_task(self):
        self.assertEqual(self.session.count_tasks(), 3)

    def test_clear_tasks(self):
        self.assertEqual(self.session.count_tasks(), 3)
        self.session.clear_tasks()
        self.assertEqual(self.session.count_tasks(), 0)

    def test_complete_task(self):
        self.assertFalse(self.session.list_tasks()[1].is_completed)
        self.session.complete_task(1)
        self.assertTrue(self.session.list_tasks()[1].is_completed)

    def test_remove_task(self):
        self.session.remove_task(1)
        self.assertEqual(self.session.count_tasks(), 2)

    def test_storage(self):
        self.session.save_tasks()
        self.session.clear_tasks()
        self.session.load_tasks()
        self.assertEqual(self.session.count_tasks(), 3)
Ejemplo n.º 2
0
class CLI:
    '''
    Command-line interface
    '''
    def __init__(self):
        self.init_session()
        while True:
            input_str = input('Enter command: ')
            self.dispatch(input_str)

    def init_session(self):
        '''
        Initializes the session with a storage file based on user input.
        '''
        DEFAULT_STORAGE_FILE = 'tasks.csv'
        self.session = Session(
            input('Enter storage filename to load [{}]: '.format(
                DEFAULT_STORAGE_FILE)) or DEFAULT_STORAGE_FILE)

    def dispatch(self, input_str):
        '''
        Parses an input line and runs the relevant underlying function.
        '''
        args = input_str.split(' ')
        (cmd, arg) = (args[0], ' '.join(args[1:]))
        try:
            cmd_func = getattr(self, '{}_cmd'.format(cmd))
            cmd_func(arg)
        except AttributeError:
            print('Invalid command. Type help for a list.')

    def help_cmd(self, _):
        '''
        help - lists all available commands
        '''
        commands = [getattr(self, f) for f in dir(self) if f.endswith('_cmd')]
        for cmd in commands:
            print('\t{}'.format(cmd.__doc__.strip()))

    def list_cmd(self, _):
        '''
        list - lists all tasks
        '''
        queue = self.session.list_tasks()
        print('Tasks [{}]:'.format(self.session.count_tasks()))
        for (idx, task) in enumerate(queue):
            print('\t- {} [{}] {}'.format(idx,
                                          'x' if task.is_completed else ' ',
                                          task.name))

    def add_cmd(self, task_name):
        '''
        add [task_name] - adds a new task with the given name
        '''
        self.session.add_task(task_name)

    def complete_cmd(self, task_id):
        '''
        complete [task_id] - marks a task with a given id as completed
        '''
        self.session.complete_task(int(task_id))

    def remove_cmd(self, task_id):
        '''
        remove [task_id] - remove a task with a given id
        '''
        self.session.remove_task(int(task_id))

    def clear_cmd(self, _):
        '''
        clear - clears the task list
        '''
        self.session.clear_tasks()

    def save_cmd(self, _):
        '''
        save - saves tasks into storage
        '''
        self.session.save_tasks()

    def load_cmd(self, _):
        '''
        load - loads tasks from storage
        '''
        self.session.load_tasks()

    def exit_cmd(self, _):
        '''
        exit - close the program
        '''
        exit()