<commit_before>import unittest

from satella.coding import for_argument, precondition, short_none, has_keys
from satella.exceptions import PreconditionError


class TestTypecheck(unittest.TestCase):

    def test_has_keys(self):
        p = has_keys(['a', 'b'])
        self.assertRaises(PreconditionError, lambda: p({'a': 3}))
        p({'a': 3, 'b': 4})

    def test_short_none(self):
        a = short_none('x == 2')
        self.assertTrue(a(2))
        self.assertIsNone(a(None))
        self.assertFalse(a(3))

    def test_precondition_none(self):
        @precondition(a=None, b=lambda: True)
        def do_any(a, b):
            return a
        self.assertEqual(do_any(a=5, b=True), 5)

    def test_precondition_kwargs(self):
        @precondition(value='x == 2')
        def kwarg(**kwargs):
            return kwargs['value']

        self.assertRaises(PreconditionError, lambda: kwarg(value=3))
        kwarg(value=2)

    def test_precondition(self):
        @precondition('len(x) == 1', lambda x: x == 1, None)
        def return_double(x, y, z):
            pass

        self.assertRaises(PreconditionError, lambda: return_double([], 1, 5))
        self.assertRaises(PreconditionError, lambda: return_double([1], 2, 5))
        return_double([1], 1, 'dupa')

    def test_precondition_fail(self):
        @precondition('len(x) == 1', lambda x: x == 1, None, None, None)
        def return_double(x, y, z):
            pass

        self.assertRaises(AssertionError, lambda: return_double([1], 1, 5))

    def test_forarg_kwords(self):
        @for_argument(int, typed=bool)
        def check(v1, typed='True'):
            if typed:<commit_msg>add test for precondition<commit_after>import unittest

from satella.coding import for_argument, precondition, short_none, has_keys
from satella.exceptions import PreconditionError


class TestTypecheck(unittest.TestCase):

    def test_has_keys(self):
        p = has_keys(['a', 'b'])
        self.assertRaises(PreconditionError, lambda: p({'a': 3}))
        p({'a': 3, 'b': 4})

    def test_short_none(self):
        a = short_none('x == 2')
        self.assertTrue(a(2))
        self.assertIsNone(a(None))
        self.assertFalse(a(3))

    def test_precondition_none(self):
        @precondition(a=None)
        def do_any(a):
            return a
        self.assertEqual(do_any(a=5), 5)

    def test_precondition_kwargs(self):
        @precondition(value='x == 2')
        def kwarg(**kwargs):
            return kwargs['value']

        self.assertRaises(PreconditionError, lambda: kwarg(value=3))
        kwarg(value=2)

    def test_precondition(self):
        @precondition('len(x) == 1', lambda x: x == 1, None)
        def return_double(x, y, z):
            pass

        self.assertRaises(PreconditionError, lambda: return_double([], 1, 5))
        self.assertRaises(PreconditionError, lambda: return_double([1], 2, 5))
        return_double([1], 1, 'dupa')

    def test_precondition_fail(self):
        @precondition('len(x) == 1', lambda x: x == 1, None, None, None)
        def return_double(x, y, z):
            pass

        self.assertRaises(AssertionError, lambda: return_double([1], 1, 5))

    def test_forarg_kwords(self):
        @for_argument(int, typed=bool)
        def check(v1, typed='True'):
            if typed:
{'fraction_non_alphanumeric': 0.13703591910654994, 'fraction_numerical': 0.01569574403863568, 'mean_word_length': 2.853488372093023, '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': '6415785', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1066, 'n_words': 234}