Skip to content

Commit

Permalink
Merge from 3.x: PRs #5952, #5950
Browse files Browse the repository at this point in the history
Fixes #5885
  • Loading branch information
ccordoba12 committed Dec 10, 2017
2 parents 0b9f787 + 2c145f6 commit a921f4e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ def test_runconfig_workdir(main_window, qtbot, tmpdir):


@flaky(max_runs=3)
@pytest.mark.skipif(os.name == 'nt' and PY2, reason="It's failing there")
def test_dedicated_consoles(main_window, qtbot):
"""Test running code in dedicated consoles."""
# ---- Load test file ----
Expand Down
18 changes: 15 additions & 3 deletions spyder/widgets/variableexplorer/collectionseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,12 @@ def copy_item(self, erase_original=False):
else:
title = _('Duplicate')
field_text = _('Variable name:')
new_key, valid = QInputDialog.getText(self, title, field_text,
QLineEdit.Normal, orig_key)
data = self.model.get_data()
if isinstance(data, (list, set)):
new_key, valid = len(data), True
else:
new_key, valid = QInputDialog.getText(self, title, field_text,
QLineEdit.Normal, orig_key)
if valid and to_text_string(new_key):
new_key = try_to_eval(to_text_string(new_key))
if new_key == orig_key:
Expand Down Expand Up @@ -1172,7 +1176,12 @@ def remove_values(self, keys):
def copy_value(self, orig_key, new_key):
"""Copy value"""
data = self.model.get_data()
data[new_key] = data[orig_key]
if isinstance(data, list):
data.append(data[orig_key])
if isinstance(data, set):
data.add(data[orig_key])
else:
data[new_key] = data[orig_key]
self.set_data(data)

def new_value(self, key, value):
Expand Down Expand Up @@ -1258,6 +1267,9 @@ def refresh_menu(self):
self.edit_action.setEnabled( condition )
self.remove_action.setEnabled( condition )
self.insert_action.setEnabled( not self.readonly )
self.duplicate_action.setEnabled(condition)
condition_rename = not isinstance(data, (tuple, list, set))
self.rename_action.setEnabled(condition_rename)
self.refresh_plot_entries(index)

def set_filter(self, dictfilter=None):
Expand Down
19 changes: 19 additions & 0 deletions spyder/widgets/variableexplorer/tests/test_collectioneditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

# Standard library imports
import copy
try:
from unittest.mock import Mock
except ImportError:
Expand Down Expand Up @@ -144,5 +145,23 @@ def test_sort_collectionsmodel_with_many_rows():
cm.fetchMore()
assert cm.rowCount() == len(coll)


def test_rename_and_duplicate_item_in_collection_editor():
collections = {'list': ([1, 2, 3], False, True),
'tuple': ((1, 2, 3), False, False),
'dict': ({'a': 1, 'b': 2}, True, True)}
for coll, rename_enabled, duplicate_enabled in collections.values():
coll_copy = copy.copy(coll)
editor = CollectionsEditorTableView(None, coll)
assert editor.rename_action.isEnabled()
assert editor.duplicate_action.isEnabled()
editor.setCurrentIndex(editor.model.createIndex(0, 0))
editor.refresh_menu()
assert editor.rename_action.isEnabled() == rename_enabled
assert editor.duplicate_action.isEnabled() == duplicate_enabled
if isinstance(coll, list):
editor.duplicate_item()
assert editor.model.get_data() == coll_copy + [coll_copy[0]]

if __name__ == "__main__":
pytest.main()

0 comments on commit a921f4e

Please sign in to comment.