<commit_before>from __future__ import unicode_literals
from ben10.foundation import handle_exception
from ben10.foundation.string import Dedent
import pytest
import sys



#===================================================================================================
# exception_handler
#===================================================================================================
@pytest.fixture
def exception_handler(request):
    '''
    Captures the exceptions using handle_exception module.

    This code was in coilib50's base TestCase.
    '''

    class Handler():

        def __init__(self):
            self.exceptions = []
            handle_exception.on_exception_handled.Register(self._OnHandledException)

        def Finalizer(self):
            self.exceptions = []
            handle_exception.on_exception_handled.Unregister(self._OnHandledException)

        def _OnHandledException(self):
            info = sys.exc_info()
            self.exceptions.append(info)

    result = Handler()
    request.addfinalizer(result.Finalizer)
    return result



#===================================================================================================
# Test
#===================================================================================================
class Test(object):

    def testHandleException(self, exception_handler, capfd):
        try:
            raise RuntimeError()
        except:
            handle_exception.HandleException('Test')
        assert len(exception_handler.exceptions) == 1

        obtained = capfd.readouterr()
        assert tuple(obtained) == (
            '',
            Dedent(
                '''
                Traceback (most recent call last):
                  File "%s", line 47, in testHandleException
                    raise RuntimeError()
                RuntimeError

                ''' % __file__,
            )
        )


    def testIgnoreHandleException(self, exception_handler, capfd):
        handle_exception.StartIgnoreHandleException()
        try:
            try:
                raise RuntimeError()
            except:
                handle_exception.HandleException('Test')
            assert len(exception_handler.exceptions) == 1
        finally:
            handle_exception.EndIgnoreHandleException()

        obtained = capfd.readouterr()
        assert tuple(obtained) == (u'', u'')<commit_msg>Fixing test so that it works with the fixture which reports handled exceptions as errors.<commit_after>from __future__ import unicode_literals
from ben10.foundation import handle_exception
from ben10.foundation.string import Dedent
import pytest
import sys



#===================================================================================================
# exception_handler
#===================================================================================================
@pytest.fixture
def exception_handler(request):
    '''
    Captures the exceptions using handle_exception module.

    This code was in coilib50's base TestCase.
    '''

    class Handler():

        def __init__(self):
            self.exceptions = []
            handle_exception.on_exception_handled.Register(self._OnHandledException)

        def Finalizer(self):
            self.exceptions = []
            handle_exception.on_exception_handled.Unregister(self._OnHandledException)

        def _OnHandledException(self):
            info = sys.exc_info()
            self.exceptions.append(info)

    result = Handler()
    request.addfinalizer(result.Finalizer)
    return result



#===================================================================================================
# Test
#===================================================================================================
class Test(object):

    def testHandleException(self, exception_handler, capfd, handled_exceptions):
        try:
            raise RuntimeError()
        except:
            handle_exception.HandleException('Test')
        assert len(exception_handler.exceptions) == 1

        obtained = capfd.readouterr()
        assert tuple(obtained) == (
            '',
            Dedent(
                '''
                Traceback (most recent call last):
                  File "%s", line 47, in testHandleException
                    raise RuntimeError()
                RuntimeError

                ''' % __file__,
            )
        )
        handled_exceptions.ClearHandledExceptions()


    def testIgnoreHandleException(self, exception_handler, capfd, handled_exceptions):
        handle_exception.StartIgnoreHandleException()
        try:
            try:
                raise RuntimeError()
            except:
                handle_exception.HandleException('Test')
            assert len(exception_handler.exceptions) == 1
        finally:
            handle_exception.EndIgnoreHandleException()

        obtained = capfd.readouterr()
        assert tuple(obtained) == (u'', u'')
        handled_exceptions.ClearHandledExceptions()
{'fraction_non_alphanumeric': 0.2255952380952381, 'fraction_numerical': 0.003968253968253968, 'mean_word_length': 2.7845345345345347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3955764', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1093, 'n_words': 237}