Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: correct attribute name #1860

Merged
merged 4 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions traitsui/qt4/file_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def show_file_dialog(self):
dlg.open()

if dlg.return_code == OK:
if self.factory.truncate:
self.value = splitext(dlg.path)
if self.factory.truncate_ext:
self.value = splitext(dlg.path)[0]
else:
self.value = dlg.path
self.update_editor()
Expand Down
48 changes: 48 additions & 0 deletions traitsui/tests/editors/test_file_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
# Thanks for using Enthought open source!

import unittest
from unittest import mock

from pyface.api import OK
from traits.api import Event, File, HasTraits

from traitsui.api import FileEditor, Item, View
from traitsui.tests._tools import (
BaseTestMixin,
Expand All @@ -29,6 +32,15 @@ class FileModel(HasTraits):
existing_filepath = File(exists=True)


def trait_set_side_effect(**traits):

def side_effect(self, *args, **kwargs):
self.trait_set(**traits)
return mock.DEFAULT

return side_effect


@requires_toolkit([ToolkitName.qt, ToolkitName.wx])
class TestSimpleFileEditor(BaseTestMixin, unittest.TestCase):
"""Test FileEditor (simple style)."""
Expand Down Expand Up @@ -100,6 +112,42 @@ def test_simple_editor_reset_text_if_validation_error(self):
# the widget is synchronized to the trait value.
self.assertEqual(filepath_field.inspect(DisplayedText()), "")

@mock.patch(
"pyface.api.FileDialog.open",
autospec=True,
side_effect=trait_set_side_effect(
return_code=OK,
path="some_file.txt",
),
)
def test_show_file_dialog(self, mock_open):
view = View(Item("filepath", editor=FileEditor()))
obj = FileModel()
tester = UITester()
with tester.create_ui(obj, dict(view=view)) as ui:
editor = ui.get_editors("filepath")[0]
editor.show_file_dialog()

self.assertEqual(editor.value, "some_file.txt")

@mock.patch(
"pyface.api.FileDialog.open",
autospec=True,
side_effect=trait_set_side_effect(
return_code=OK,
path="some_file.txt",
),
)
def test_show_file_dialog_truncate_ext(self, mock_open):
view = View(Item("filepath", editor=FileEditor(truncate_ext=True)))
obj = FileModel()
tester = UITester()
with tester.create_ui(obj, dict(view=view)) as ui:
editor = ui.get_editors("filepath")[0]
editor.show_file_dialog()

self.assertEqual(editor.value, "some_file")


# Run this against wx too when enthought/traitsui#752 is also fixed.
@requires_toolkit([ToolkitName.qt])
Expand Down
4 changes: 2 additions & 2 deletions traitsui/wx/file_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def show_file_dialog(self, event):
dlg.open()

if dlg.return_code == OK:
if self.factory.truncate:
self.value = splitext(dlg.path)
if self.factory.truncate_ext:
self.value = splitext(dlg.path)[0]
else:
self.value = dlg.path
self.update_editor()
Expand Down