Commit 51bf164f authored by Bram Schoenmakers's avatar Bram Schoenmakers

Merge pull request #29 from mruwek/fix-edit-regressions_5562ed7d

Fix EditCommand inheritance problems
parents 61a35f8f 6ab221a5
......@@ -17,11 +17,13 @@
import unittest
import mock
from six import u
import os
from topydo.commands.EditCommand import EditCommand
from test.CommandTest import CommandTest, utf8
from topydo.lib.TodoList import TodoList
from topydo.lib.Todo import Todo
from topydo.lib.Config import config
class EditCommandTest(CommandTest):
def setUp(self):
......@@ -114,7 +116,6 @@ class EditCommandTest(CommandTest):
self.assertEqual(self.errors, "")
self.assertEqual(str(self.todolist), utf8(u("Foo id:1\nBar p:1 @test\nBaz @test\nLazy Cat")))
@mock.patch('topydo.commands.EditCommand.EditCommand._todos_from_temp')
@mock.patch('topydo.commands.EditCommand.EditCommand._open_in_editor')
def test_edit_expr(self, mock_open_in_editor, mock_todos_from_temp):
......@@ -125,9 +126,27 @@ class EditCommandTest(CommandTest):
command = EditCommand(["-e", "@test"], self.todolist, self.out, self.error, None)
command.execute()
expected = utf8(u("| 3| Lazy Cat\n| 4| Lazy Dog\n"))
self.assertTrue(self.todolist.is_dirty())
self.assertEqual(self.errors, "")
self.assertEqual(self.output, expected)
self.assertEqual(str(self.todolist), utf8(u("Foo id:1\nFo\u00f3B\u0105\u017a\nLazy Cat\nLazy Dog")))
@mock.patch('topydo.commands.EditCommand.call')
def test_edit_archive(self, mock_call):
""" Edit archive file. """
mock_call.return_value = 0
editor = 'vi'
os.environ['EDITOR'] = editor
archive = config().archive()
command = EditCommand([u("-d")], self.todolist, self.out, self.error, None)
command.execute()
self.assertEqual(self.errors, "")
mock_call.assert_called_once_with([editor, archive])
if __name__ == '__main__':
unittest.main()
......@@ -20,7 +20,7 @@ import tempfile
from six import text_type, u
from topydo.commands.ListCommand import ListCommand
from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.MultiCommand import MultiCommand
from topydo.lib.Config import config
from topydo.lib.Todo import Todo
......@@ -32,7 +32,7 @@ from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
# cannot use super() inside the class itself
BASE_TODOLIST = lambda tl: super(TodoList, tl)
class EditCommand(MultiCommand, ListCommand):
class EditCommand(MultiCommand, ExpressionCommand):
def __init__(self, p_args, p_todolist, p_output, p_error, p_input):
super(EditCommand, self).__init__(p_args, p_todolist, p_output,
p_error, p_input)
......
......@@ -16,7 +16,7 @@
import re
from topydo.lib.Command import Command
from topydo.lib.ExpressionCommand import ExpressionCommand
from topydo.lib.Config import config
from topydo.lib import Filter
from topydo.lib.PrettyPrinterFilter import (
......@@ -26,7 +26,7 @@ from topydo.lib.PrettyPrinterFilter import (
from topydo.lib.Sorter import Sorter
from topydo.lib.View import View
class ListCommand(Command):
class ListCommand(ExpressionCommand):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
......@@ -48,42 +48,6 @@ class ListCommand(Command):
self.args = args
def _filters(self):
filters = []
def arg_filters():
result = []
for arg in self.args:
if re.match(Filter.ORDINAL_TAG_MATCH, arg):
argfilter = Filter.OrdinalTagFilter(arg)
elif len(arg) > 1 and arg[0] == '-':
# when a word starts with -, exclude it
argfilter = Filter.GrepFilter(arg[1:])
argfilter = Filter.NegationFilter(argfilter)
else:
argfilter = Filter.GrepFilter(arg)
result.append(argfilter)
return result
if not self.show_all:
filters.append(Filter.DependencyFilter(self.todolist))
filters.append(Filter.RelevanceFilter())
filters += arg_filters()
if not self.show_all:
filters.append(Filter.LimitFilter(config().list_limit()))
return filters
def _view(self):
sorter = Sorter(self.sort_expression)
filters = self._filters()
return View(sorter, filters, self.todolist, self.printer)
def _print(self):
""" Prints the todos. """
indent = config().list_indent()
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 - 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
from topydo.lib.Command import Command
from topydo.lib.Config import config
from topydo.lib import Filter
from topydo.lib.Sorter import Sorter
from topydo.lib.View import View
class ExpressionCommand(Command):
"""
A common class for commands operating on todos selected by expressions.
"""
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(ExpressionCommand, self).__init__(
p_args, p_todolist, p_out, p_err, p_prompt)
self.sort_expression = config().sort_string()
self.show_all = False
def _filters(self):
filters = []
def arg_filters():
result = []
for arg in self.args:
if re.match(Filter.ORDINAL_TAG_MATCH, arg):
argfilter = Filter.OrdinalTagFilter(arg)
elif len(arg) > 1 and arg[0] == '-':
# when a word starts with -, exclude it
argfilter = Filter.GrepFilter(arg[1:])
argfilter = Filter.NegationFilter(argfilter)
else:
argfilter = Filter.GrepFilter(arg)
result.append(argfilter)
return result
if not self.show_all:
filters.append(Filter.DependencyFilter(self.todolist))
filters.append(Filter.RelevanceFilter())
filters += arg_filters()
if not self.show_all:
filters.append(Filter.LimitFilter(config().list_limit()))
return filters
def _view(self):
sorter = Sorter(self.sort_expression)
filters = self._filters()
return View(sorter, filters, self.todolist, self.printer)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment