Skip to content

Commit

Permalink
Write unit test for TaskView.get_title()
Browse files Browse the repository at this point in the history
  • Loading branch information
SqAtx committed Apr 22, 2024
1 parent 42205d0 commit dd7654a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
50 changes: 3 additions & 47 deletions GTG/gtk/editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,17 @@ def __init__(self, app, task):
provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

# self.textview.browse_tag_cb = app.select_tag
# self.textview.new_subtask_cb = self.new_subtask
# self.textview.get_subtasks_cb = task.get_children
# self.textview.delete_subtask_cb = self.remove_subtask
# self.textview.rename_subtask_cb = self.rename_subtask
# self.textview.open_subtask_cb = self.open_subtask
# self.textview.save_cb = self.light_save
# self.textview.add_tasktag_cb = self.tag_added
# self.textview.remove_tasktag_cb = self.tag_removed
# self.textview.refresh_cb = self.refresh_editor
# self.textview.get_tagslist_cb = task.get_tags_name
# self.textview.tid = task.id

self.textview.browse_tag_cb = app.select_tag
self.textview.new_subtask_cb = self.new_subtask
# self.textview.get_subtasks_cb = task.get_children
self.textview.delete_subtask_cb = self.remove_subtask
self.textview.rename_subtask_cb = self.rename_subtask
self.textview.open_subtask_cb = self.open_subtask
self.textview.save_cb = self.light_save
self.textview.add_tasktag_cb = self.tag_added
self.textview.remove_tasktag_cb = self.tag_removed
self.textview.refresh_cb = self.refresh_editor
# self.textview.get_tagslist_cb = task.get_tags_name
self.textview.tid = task.id

# Voila! it's done
Expand All @@ -179,40 +168,7 @@ def __init__(self, app, task):
textview_focus_controller.connect("leave", self.on_textview_focus_out)
self.textview.add_controller(textview_focus_controller)

tags = task.tags
text = self.task.content
title = self.task.title

# Insert text and tags as a non_undoable action, otherwise
# the user can CTRL+Z even this inserts.
self.textview.buffer.begin_irreversible_action()
self.textview.buffer.set_text(f"{title}\n")

if text:
self.textview.insert(text)

# Insert any remaining tags
if tags:
tag_names = [t.name for t in tags]
self.textview.insert_tags(tag_names)
else:
# If not text, we insert tags
if tags:
tag_names = [t.name for t in tags]
self.textview.insert_tags(tag_names)
start = self.textview.buffer.get_end_iter()
self.textview.buffer.insert(start, '\n')

# Insert subtasks if they weren't inserted in the text
subtasks = task.children
for sub in subtasks:
if sub.id not in self.textview.subtasks['tags']:
self.textview.insert_existing_subtask(sub)

if self.is_new():
self.textview.select_title()

self.textview.buffer.end_irreversible_action()
self.textview.set_text_from_task()

# Connect search field to tags popup
self.tags_tree.set_search_entry(self.tags_entry)
Expand Down
32 changes: 32 additions & 0 deletions GTG/gtk/editor/taskview.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,38 @@ def __init__(self, ds: Datastore, task, clipboard, dark) -> None:
press_gesture.connect('begin', self.on_single_begin)
self.add_controller(press_gesture)

def set_text_from_task(self) -> None:
"""Sets the text of the view, from the text of the task"""
# Insert text and tags as a non_undoable action, otherwise
# the user can CTRL+Z even this inserts.
self.buffer.begin_irreversible_action()
self.buffer.set_text(f"{self.task.title}\n")

if self.task.content:
self.insert(self.task.content)

# Insert any remaining tags
if self.task.tags:
tag_names = [t.name for t in self.task.tags]
self.insert_tags(tag_names)
else:
# If not text, we insert tags
if self.task.tags:
tag_names = [t.name for t in self.task.tags]
self.insert_tags(tag_names)
start = self.buffer.get_end_iter()
self.buffer.insert(start, '\n')

# Insert subtasks if they weren't inserted in the text
subtasks = self.task.children
for sub in subtasks:
if sub.id not in self.subtasks['tags']:
self.insert_existing_subtask(sub)

# if self.is_new(): # TODO move down to Task
# self.select_title()

self.buffer.end_irreversible_action()

def on_modified(self, buffer: Gtk.TextBuffer) -> None:
"""Called every time the text buffer changes."""
Expand Down
17 changes: 16 additions & 1 deletion tests/core/test_taskview.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
# -----------------------------------------------------------------------------

import re
from uuid import uuid4
from unittest import TestCase
from GTG.gtk.editor.taskview import TAG_REGEX
from GTG.core.datastore import Datastore
from GTG.core.tasks import Task
from GTG.gtk.editor.taskview import TaskView, TAG_REGEX


class TestTaskView(TestCase):
Expand All @@ -41,3 +44,15 @@ def test_no_detect_tags(self):
matches = re.findall(TAG_REGEX, content)

self.assertEqual([], matches)

def test_get_title(self):
task_title = 'Very important task'
task = Task(id = uuid4(), title=task_title)
view = TaskView(Datastore(), task, None, False)
view.refresh_cb = lambda x: x # Refresh CB that does nothing
view.set_text_from_task()
view.detect_title()

view_title = view.get_title()

self.assertEqual(view_title, task_title)

0 comments on commit dd7654a

Please sign in to comment.