<commit_before># -*- coding: utf-8 -*-
'''
    Observers tests
    ~~~~~~~~~~~~~~~

    :copyright: (c) 2011 by <NAME>
    :license: MIT, see LICENSE for more details.
'''

import unittest

from mock import Mock

from guimvc import Observer


def _get_mock(obs, attr):
    return obs._Observer__callbacks[attr][0][0]


class TestObserver(Observer):

    __callbacks__ = [('attr1', Mock()),
                     ('attr2', Mock(), ('arg1', 'arg2')),
                     ('attr3', Mock(), ('arg1',), dict(kwarg1=1))
                    ]


class ObserverTest(unittest.TestCase):

    def test_callbacks(self):
        obs = TestObserver(Mock())

        mock = _get_mock(obs, 'attr1')
        args = ('attr1', 0, 1)
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock = _get_mock(obs, 'attr2')
        args = ['attr2', 0, 1]
        obs.notify(*args)
        args = args + ['arg1', 'arg2']
        mock.assert_called_with(*args)

        mock = _get_mock(obs, 'attr3')
        args = ['attr3', 0, 1]
        obs.notify(*args)
        args = args + ['arg1']
        kwargs = dict(kwarg1=1)
        mock.assert_called_with(*args, **kwargs)

        mock = Mock()
        obs.register_callback('attr4', mock, 'arg1', kwarg1=1)
        args = ['attr4', 0, 1]
        obs.notify(*args)
        args = args + ['arg1']
        mock.assert_called_with(*args, **kwargs)

    def test_wildcards(self):
        obs = TestObserver(Mock())
        mock = Mock()
        obs.register_callback('*', mock)

        args = ['attr1', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        args = ['test_attr', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock = Mock()
        obs.register_callback('attr?', mock)

        args = ['attr1', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock.reset_mock()
        args = ['test_attr', 0, 1]
        obs.notify(*args)
        self.assertFalse(mock.called)<commit_msg>tests for new decorator style<commit_after># -*- coding: utf-8 -*-
'''
    Observers tests
    ~~~~~~~~~~~~~~~

    :copyright: (c) 2011 by <NAME>
    :license: MIT, see LICENSE for more details.
'''

import unittest

from mock import Mock

from guimvc import Observer, observemethod


class BaseObserver(Observer):
    pass


class TestObserver(BaseObserver):
    pass

# This tests the inspection of the base classes
BaseObserver.attr1 = observemethod('attr1')(Mock())
TestObserver.attr2 = observemethod('attr2', 'arg1', 'arg2')(Mock())
TestObserver.attr3 = observemethod('attr3', 'arg1', kwarg1=1)(Mock())


class ObserverTest(unittest.TestCase):

    def test_callbacks(self):
        obs = TestObserver(Mock())

        args = ('attr1', 0, 1)
        obs.notify(*args)
        obs.attr1.assert_called_with(*args)

        args = ['attr2', 0, 1]
        obs.notify(*args)
        args = args + ['arg1', 'arg2']
        obs.attr2.assert_called_with(*args)

        args = ['attr3', 0, 1]
        obs.notify(*args)
        args = args + ['arg1']
        kwargs = dict(kwarg1=1)
        obs.attr3.assert_called_with(*args, **kwargs)

        mock = Mock()
        obs.register_callback('attr4', mock, 'arg1', kwarg1=1)
        args = ['attr4', 0, 1]
        obs.notify(*args)
        args = args + ['arg1']
        mock.assert_called_with(*args, **kwargs)

    def test_wildcards(self):
        obs = Observer(Mock())
        mock = Mock()
        obs.register_callback('*', mock)

        args = ['attr1', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        args = ['test_attr', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock = Mock()
        obs.register_callback('attr?', mock)

        args = ['attr1', 0, 1]
        obs.notify(*args)
        mock.assert_called_with(*args)

        mock.reset_mock()
        args = ['test_attr', 0, 1]
        obs.notify(*args)
        self.assertFalse(mock.called)
{'fraction_non_alphanumeric': 0.15809284818067754, 'fraction_numerical': 0.02534504391468005, 'mean_word_length': 2.346767422334173, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6778239', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1266, 'n_words': 283}