Skip to content

Commit

Permalink
Move test subclass for loadUi to tests directory
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Jun 14, 2016
1 parent 48eecd3 commit c9dc1cb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
7 changes: 0 additions & 7 deletions qtpy/uic.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,3 @@ def loadUi(uifile, baseinstance=None, customWidgets=None,
widget = loader.load(uifile)
QMetaObject.connectSlotsByName(widget)
return widget


class _QComboBoxSubclass(QComboBox):
"""
This is a private class that is used only for testing purposes.
"""
pass
2 changes: 1 addition & 1 deletion tests/test_custom.ui
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<customwidget>
<class>_QComboBoxSubclass</class>
<extends>QComboBox</extends>
<header>qtpy.uic</header>
<header>qcombobox_subclass</header>
</customwidget>
</customwidgets>
<resources/>
Expand Down
2 changes: 2 additions & 0 deletions tests/test_patch_qcombobox.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import absolute_import

from qtpy import QtGui, QtWidgets

def get_qapp(icon_path=None):
Expand Down
55 changes: 45 additions & 10 deletions tests/test_uic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import os
import sys
import contextlib

import pytest

from qtpy import QtWidgets, PYSIDE
from qtpy.QtWidgets import QComboBox
from qtpy.uic import loadUi, _QComboBoxSubclass
from qtpy.uic import loadUi


QCOMBOBOX_SUBCLASS = """
from qtpy.QtWidgets import QComboBox
class _QComboBoxSubclass(QComboBox):
pass
"""

@contextlib.contextmanager
def enabled_qcombobox_subclass(tmpdir):
"""
Context manager that sets up a temporary module with a QComboBox subclass
and then removes it once we are done.
"""

with open(tmpdir.join('qcombobox_subclass.py').strpath, 'w') as f:
f.write(QCOMBOBOX_SUBCLASS)

sys.path.insert(0, tmpdir.strpath)

yield

sys.path.pop(0)


def get_qapp(icon_path=None):
Expand All @@ -28,31 +53,41 @@ def test_load_ui():
assert isinstance(ui.comboBox, QComboBox)


def test_load_ui_custom():
def test_load_ui_custom(tmpdir):
"""
Test that we can load a .ui file with custom widgets
"""

app = get_qapp()
if PYSIDE:
customWidgets = {'_QComboBoxSubclass': _QComboBoxSubclass}
ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'),
customWidgets=customWidgets)
else:
ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'))

with enabled_qcombobox_subclass(tmpdir):
from qcombobox_subclass import _QComboBoxSubclass
if PYSIDE:
customWidgets = {'_QComboBoxSubclass': _QComboBoxSubclass}
ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'),
customWidgets=customWidgets)
else:
ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'))

assert isinstance(ui.pushButton, QtWidgets.QPushButton)
assert isinstance(ui.comboBox, _QComboBoxSubclass)


@pytest.mark.xfail(PYSIDE, reason='The PySide loadUi wrapper does not yet '
'support determining custom widgets '
'automatically')
def test_load_ui_custom_auto():
def test_load_ui_custom_auto(tmpdir):
"""
Test that we can load a .ui file with custom widgets without having to
explicitly specify a dictionary of custom widgets, even in the case of
PySide.
"""

app = get_qapp()
ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'))

with enabled_qcombobox_subclass(tmpdir):
from qcombobox_subclass import _QComboBoxSubclass
ui = loadUi(os.path.join(os.path.dirname(__file__), 'test_custom.ui'))

assert isinstance(ui.pushButton, QtWidgets.QPushButton)
assert isinstance(ui.comboBox, _QComboBoxSubclass)

0 comments on commit c9dc1cb

Please sign in to comment.