<commit_before>"""
Tests for the .collections.dicts module.
"""
from taipan.collections import is_mapping, is_sequence
from taipan.testing import TestCase

import taipan.collections.dicts as __unit__


ALPHABET = 'abcdefghijklmnopqrstuvwxyz'


class AbsentDict(TestCase):
    EXISTING_KEY = 'foo'
    EXISTING_VALUE = 0
    NONEXISTING_KEY = 'baz'
    DICT_WITH_ALL_PRESENT = {EXISTING_KEY: EXISTING_VALUE, 'bar': 2}

    ABSENT_KEY = 'bar'
    DICT_WITH_ONE_ABSENT = {EXISTING_KEY: EXISTING_VALUE,
                            ABSENT_KEY: __unit__.ABSENT}
    DICT_WITH_ALL_ABSENT = {'foo': __unit__.ABSENT, 'bar': __unit__.ABSENT}

    def test_ctor__no_args(self):
        dict_ = __unit__.AbsentDict()
        self._assertIsMapping(dict_)
        self.assertEmpty(dict_)

    def test_ctor__none(self):
        with self.assertRaises(TypeError):
            __unit__.AbsentDict(None)

    def test_ctor__some_object(self):
        with self.assertRaises(TypeError):
            __unit__.AbsentDict(object())

    def test_ctor__dict__empty(self):
        self.assertEquals({}, __unit__.AbsentDict({}))

    def test_ctor__dict__all_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        self.assertEquals(self.DICT_WITH_ALL_PRESENT, dict_)

    def test_ctor__dict__one_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT)
        self.assertEquals(len(self.DICT_WITH_ONE_ABSENT) - 1, len(dict_))
        self.assertNotIn(self.ABSENT_KEY, dict_)

    def test_ctor__dict__all_absent(self):
        self.assertEquals({}, __unit__.AbsentDict(self.DICT_WITH_ALL_ABSENT))

    def test_ctor__pairlist__empty(self):
        self.assertEquals({}, __unit__.AbsentDict([]))

    def test_ctor__pairlist__all_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT.items())
        self.assertEquals(self.DICT_WITH_ALL_PRESENT, dict_)

    def test_ctor__pairlist__one_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT.items())
        self.assertEquals(len(self.DICT_WITH_ONE_ABSENT) - 1, len(dict_))
        self.assertNotIn(self.ABSENT_KEY, dict_)

    def test_ctor__pairlist__all_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_ABSENT.items())
        self.assertEquals({}, dict_)

    def test_setitem__existing_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        dict_[self.EXISTING_KEY] = self.EXISTING_VALUE  # should be no-op
        self.assertEquals(self.DICT_WITH_ALL_PRESENT, dict_)

    def test_setitem__nonexisting_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        dict_[self.NONEXISTING_KEY] = 42  # should add the key normally
        self.assertIn(self.NONEXISTING_KEY, dict_)

    def test_setitem__present_to_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        dict_[self.EXISTING_KEY] = __unit__.ABSENT  # should delete the key
        self.assertNotIn(self.EXISTING_KEY, dict_)

    def test_setitem__absent_to_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT)
        dict_[self.ABSENT_KEY] = 42  # should add the key
        self.assertIn(self.ABSENT_KEY, dict_)

    def test_setitem__still_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT)
        dict_[self.ABSENT_KEY] = __unit__.ABSENT  # should be no-op
        self.assertNotIn(self.ABSENT_KEY, dict_)

    # Assertions

    def _assertIsMapping(self, obj, msg=None):
        if not is_mapping(obj):
            self.fail(msg or "%r is not a mapping" % (obj,))


# Compatibility shims

class _Shim(TestCase):
    KEYS = ['foo', 'bar']
    VALUES = list(range(len(KEYS)))
    ITEMS = list(zip(KEYS, VALUES))
    DICT = dict(ITEMS)

    def _assertSequence(self, obj):
        self.assertTrue(is_sequence(obj), msg="%r is not a sequence" % (obj,))

    def _assertNotSequence(self, obj):
        self.assertFalse(is_sequence(obj), msg="%r is a sequence" % (obj,))

    def _assertEmptySequence(self, obj):
        self._assertSequence(obj)
        self.assertEmpty(obj)

    def _assertEmptyNonSequence(self, obj):
        self._assertNotSequence(obj)
        self.assertEmpty(obj)

    def _assertSequenceItems(self, items, obj):
        self._assertSequence(obj)
        self.assertItemsEqual(items, obj)

    def _assertNonSequenceItems(self, items, obj):
        self._assertNotSequence(obj)
        self.assertItemsEqual(items, obj)


class IterItems(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.iteritems(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.iteritems(object())

    def test_dict__empty(self):
        self._assertEmptyNonSequence(__unit__.iteritems({}))

    def test_dict__normal(self):
        iteritems = __unit__.iteritems(self.DICT)
        self._assertNonSequenceItems(self.ITEMS, iteritems)


class IterKeys(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.iterkeys(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.iterkeys(object())

    def test_dict__empty(self):
        self._assertEmptyNonSequence(__unit__.iterkeys({}))

    def test_dict__normal(self):
        iterkeys = __unit__.iterkeys(self.DICT)
        self._assertNonSequenceItems(self.KEYS, iterkeys)


class IterValues(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.itervalues(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.itervalues(object())

    def test_dict__empty(self):
        self._assertEmptyNonSequence(__unit__.itervalues({}))

    def test_dict__normal(self):
        itervalues = __unit__.itervalues(self.DICT)
        self._assertNonSequenceItems(self.VALUES, itervalues)
<commit_msg>Tests for .collections.dicts.X() compatibility functions<commit_after>"""
Tests for the .collections.dicts module.
"""
from taipan.collections import is_mapping, is_sequence
from taipan.testing import TestCase

import taipan.collections.dicts as __unit__


ALPHABET = 'abcdefghijklmnopqrstuvwxyz'


class AbsentDict(TestCase):
    EXISTING_KEY = 'foo'
    EXISTING_VALUE = 0
    NONEXISTING_KEY = 'baz'
    DICT_WITH_ALL_PRESENT = {EXISTING_KEY: EXISTING_VALUE, 'bar': 2}

    ABSENT_KEY = 'bar'
    DICT_WITH_ONE_ABSENT = {EXISTING_KEY: EXISTING_VALUE,
                            ABSENT_KEY: __unit__.ABSENT}
    DICT_WITH_ALL_ABSENT = {'foo': __unit__.ABSENT, 'bar': __unit__.ABSENT}

    def test_ctor__no_args(self):
        dict_ = __unit__.AbsentDict()
        self._assertIsMapping(dict_)
        self.assertEmpty(dict_)

    def test_ctor__none(self):
        with self.assertRaises(TypeError):
            __unit__.AbsentDict(None)

    def test_ctor__some_object(self):
        with self.assertRaises(TypeError):
            __unit__.AbsentDict(object())

    def test_ctor__dict__empty(self):
        self.assertEquals({}, __unit__.AbsentDict({}))

    def test_ctor__dict__all_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        self.assertEquals(self.DICT_WITH_ALL_PRESENT, dict_)

    def test_ctor__dict__one_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT)
        self.assertEquals(len(self.DICT_WITH_ONE_ABSENT) - 1, len(dict_))
        self.assertNotIn(self.ABSENT_KEY, dict_)

    def test_ctor__dict__all_absent(self):
        self.assertEquals({}, __unit__.AbsentDict(self.DICT_WITH_ALL_ABSENT))

    def test_ctor__pairlist__empty(self):
        self.assertEquals({}, __unit__.AbsentDict([]))

    def test_ctor__pairlist__all_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT.items())
        self.assertEquals(self.DICT_WITH_ALL_PRESENT, dict_)

    def test_ctor__pairlist__one_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT.items())
        self.assertEquals(len(self.DICT_WITH_ONE_ABSENT) - 1, len(dict_))
        self.assertNotIn(self.ABSENT_KEY, dict_)

    def test_ctor__pairlist__all_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_ABSENT.items())
        self.assertEquals({}, dict_)

    def test_setitem__existing_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        dict_[self.EXISTING_KEY] = self.EXISTING_VALUE  # should be no-op
        self.assertEquals(self.DICT_WITH_ALL_PRESENT, dict_)

    def test_setitem__nonexisting_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        dict_[self.NONEXISTING_KEY] = 42  # should add the key normally
        self.assertIn(self.NONEXISTING_KEY, dict_)

    def test_setitem__present_to_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ALL_PRESENT)
        dict_[self.EXISTING_KEY] = __unit__.ABSENT  # should delete the key
        self.assertNotIn(self.EXISTING_KEY, dict_)

    def test_setitem__absent_to_present(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT)
        dict_[self.ABSENT_KEY] = 42  # should add the key
        self.assertIn(self.ABSENT_KEY, dict_)

    def test_setitem__still_absent(self):
        dict_ = __unit__.AbsentDict(self.DICT_WITH_ONE_ABSENT)
        dict_[self.ABSENT_KEY] = __unit__.ABSENT  # should be no-op
        self.assertNotIn(self.ABSENT_KEY, dict_)

    # Assertions

    def _assertIsMapping(self, obj, msg=None):
        if not is_mapping(obj):
            self.fail(msg or "%r is not a mapping" % (obj,))


# Compatibility shims

class _Shim(TestCase):
    KEYS = ['foo', 'bar']
    VALUES = list(range(len(KEYS)))
    ITEMS = list(zip(KEYS, VALUES))
    DICT = dict(ITEMS)

    def _assertSequence(self, obj):
        self.assertTrue(is_sequence(obj), msg="%r is not a sequence" % (obj,))

    def _assertNotSequence(self, obj):
        self.assertFalse(is_sequence(obj), msg="%r is a sequence" % (obj,))

    def _assertEmptySequence(self, obj):
        self._assertSequence(obj)
        self.assertEmpty(obj)

    def _assertEmptyNonSequence(self, obj):
        self._assertNotSequence(obj)
        self.assertEmpty(obj)

    def _assertSequenceItems(self, items, obj):
        self._assertSequence(obj)
        self.assertItemsEqual(items, obj)

    def _assertNonSequenceItems(self, items, obj):
        self._assertNotSequence(obj)
        self.assertItemsEqual(items, obj)


class IterItems(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.iteritems(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.iteritems(object())

    def test_dict__empty(self):
        self._assertEmptyNonSequence(__unit__.iteritems({}))

    def test_dict__normal(self):
        iteritems = __unit__.iteritems(self.DICT)
        self._assertNonSequenceItems(self.ITEMS, iteritems)


class IterKeys(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.iterkeys(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.iterkeys(object())

    def test_dict__empty(self):
        self._assertEmptyNonSequence(__unit__.iterkeys({}))

    def test_dict__normal(self):
        iterkeys = __unit__.iterkeys(self.DICT)
        self._assertNonSequenceItems(self.KEYS, iterkeys)


class IterValues(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.itervalues(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.itervalues(object())

    def test_dict__empty(self):
        self._assertEmptyNonSequence(__unit__.itervalues({}))

    def test_dict__normal(self):
        itervalues = __unit__.itervalues(self.DICT)
        self._assertNonSequenceItems(self.VALUES, itervalues)


class Items(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.items(None)

    def test_some_object(self):
        with self.assertRaises(TypeError):
            __unit__.items(object())

    def test_dict__empty(self):
        self._assertEmptySequence(__unit__.items({}))

    def test_dict__normal(self):
        items = __unit__.items(self.DICT)
        self._assertSequenceItems(self.ITEMS, items)


class Keys(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.keys(None)

    def test_some_objects(self):
        with self.assertRaises(TypeError):
            __unit__.keys(object())

    def test_dict__empty(self):
        self._assertEmptySequence(__unit__.keys({}))

    def test_dict__normal(self):
        keys = __unit__.keys(self.DICT)
        self._assertSequenceItems(self.KEYS, keys)


class Values(_Shim):

    def test_none(self):
        with self.assertRaises(TypeError):
            __unit__.values(None)

    def test_some_objects(self):
        with self.assertRaises(TypeError):
            __unit__.values(object())

    def test_dict__empty(self):
        self._assertEmptySequence(__unit__.values({}))

    def test_dict__normal(self):
        values = __unit__.values(self.DICT)
        self._assertSequenceItems(self.VALUES, values)

{'fraction_non_alphanumeric': 0.10143841774048547, 'fraction_numerical': 0.001198681450404555, 'mean_word_length': 3.388231426692965, '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': '2475532', 'n_tokens_mistral': 4547, 'n_tokens_neox': 4396, 'n_words': 677}