# Path: qcrash/api.py
# def install_backend(*args):
# def get_backends():
# def install_except_hook(except_hook=_hooks.except_hook):
# def set_qsettings(qsettings):
# def show_report_dialog(window_title='Report an issue...',
#                        window_icon=None, traceback=None, issue_title='',
#                        issue_description='', parent=None,
#                        modal=None, include_log=True, include_sys_info=True):
# def _return_empty_string():
# 
# Path: qcrash/_hooks.py
# class QtExceptHook(QtCore.QObject):
#     _report_exception_requested = Signal(object, object)
# 
#     def __init__(self, except_hook, *args, **kwargs):
#         super(QtExceptHook, self).__init__(*args, **kwargs)
#         sys.excepthook = self._except_hook
#         self._report_exception_requested.connect(except_hook)
# 
#     def _except_hook(self, exc_type, exc_val, tb):
#         tb = '\n'.join([''.join(traceback.format_tb(tb)),
#                         '{0}: {1}'.format(exc_type.__name__, exc_val)])
#         _logger().critical('unhandled exception:\n%s', tb)
#         # exception might come from another thread, use a signal
#         # so that we can be sure we will show the bug report dialog from
#         # the main gui thread.
#         self._report_exception_requested.emit(exc_val, tb)


import sys
import pytest
from qcrash import api
from qcrash.qt import QtCore
from qcrash._hooks import QtExceptHook
    assert api._qsettings.organizationName() == 'QCrash'
    api.set_qsettings(QtCore.QSettings('TestQCrash'))
    assert api._qsettings.organizationName() == 'TestQCrash'


def test_value_errors():
    api._backends[:] = []
    with pytest.raises(ValueError):
        api.install_except_hook()
    with pytest.raises(ValueError):
        api.show_report_dialog()


def test_install_backend():
    api._backends[:] = []
    assert len(api.get_backends()) == 0
    b1 = api.backends.GithubBackend('ColinDuquesnoy', 'TestQCrash')
    b2 = api.backends.EmailBackend('colin.duquesnoy@gmail.com', 'TestQCrash')
    api.install_backend(b1, b2)
    assert len(api.get_backends()) == 2
    b1 = api.backends.GithubBackend('ColinDuquesnoy', 'TestQCrash')
    b2 = api.backends.EmailBackend('colin.duquesnoy@gmail.com', 'TestQCrash')
    api.install_backend(b1)
    assert len(api.get_backends()) == 3
    api.install_backend(b2)
    assert len(api.get_backends()) == 4


def test_install_except_hook():
    api.install_except_hook()

# Path: qcrash/api.py
# def install_backend(*args):
# def get_backends():
# def install_except_hook(except_hook=_hooks.except_hook):
# def set_qsettings(qsettings):
# def show_report_dialog(window_title='Report an issue...',
#                        window_icon=None, traceback=None, issue_title='',
#                        issue_description='', parent=None,
#                        modal=None, include_log=True, include_sys_info=True):
# def _return_empty_string():
# 
# Path: qcrash/_hooks.py
# class QtExceptHook(QtCore.QObject):
#     _report_exception_requested = Signal(object, object)
# 
#     def __init__(self, except_hook, *args, **kwargs):
#         super(QtExceptHook, self).__init__(*args, **kwargs)
#         sys.excepthook = self._except_hook
#         self._report_exception_requested.connect(except_hook)
# 
#     def _except_hook(self, exc_type, exc_val, tb):
#         tb = '\n'.join([''.join(traceback.format_tb(tb)),
#                         '{0}: {1}'.format(exc_type.__name__, exc_val)])
#         _logger().critical('unhandled exception:\n%s', tb)
#         # exception might come from another thread, use a signal
#         # so that we can be sure we will show the bug report dialog from
#         # the main gui thread.
#         self._report_exception_requested.emit(exc_val, tb)


# Path: tests/test_api.py
import sys
import pytest
from qcrash import api
from qcrash.qt import QtCore
from qcrash._hooks import QtExceptHook

    assert api._qsettings.organizationName() == 'QCrash'
    api.set_qsettings(QtCore.QSettings('TestQCrash'))
    assert api._qsettings.organizationName() == 'TestQCrash'


def test_value_errors():
    api._backends[:] = []
    with pytest.raises(ValueError):
        api.install_except_hook()
    with pytest.raises(ValueError):
        api.show_report_dialog()


def test_install_backend():
    api._backends[:] = []
    assert len(api.get_backends()) == 0
    b1 = api.backends.GithubBackend('ColinDuquesnoy', 'TestQCrash')
    b2 = api.backends.EmailBackend('colin.duquesnoy@gmail.com', 'TestQCrash')
    api.install_backend(b1, b2)
    assert len(api.get_backends()) == 2
    b1 = api.backends.GithubBackend('ColinDuquesnoy', 'TestQCrash')
    b2 = api.backends.EmailBackend('colin.duquesnoy@gmail.com', 'TestQCrash')
    api.install_backend(b1)
    assert len(api.get_backends()) == 3
    api.install_backend(b2)
    assert len(api.get_backends()) == 4


def test_install_except_hook():
    api.install_except_hook()

    assert isinstance(api._except_hook, QtExceptHook)