<commit_before>"""
Tests for the .strings module.
"""
from contextlib import contextmanager
from functools import reduce
import re

from taipan._compat import IS_PY3
from taipan.collections import is_mapping
from taipan.testing import skipIf, skipUnless, TestCase

import taipan.strings as __unit__


class IsString(TestCase):
    DEFAULT_STRING = 'foo'
    UNICODE_STRING = u'foo'
    BYTE_STRING = b'foo'

    def test_none(self):
        self.assertFalse(__unit__.is_string(None))

    def test_some_object(self):
        self.assertFalse(__unit__.is_string(object()))

    def test_default_string(self):
        self.assertTrue(__unit__.is_string(self.DEFAULT_STRING))

    def test_unicode_string(self):
        self.assertTrue(__unit__.is_string(self.UNICODE_STRING))

    @skipIf(IS_PY3, "requires Python 2.x")
    def test_bytestring__py2(self):
        self.assertTrue(__unit__.is_string(self.BYTE_STRING))

    @skipUnless(IS_PY3, "requires Python 3.x")
    def test_bytestring__py3(self):
        self.assertFalse(__unit__.is_string(self.BYTE_STRING))


class IsRegex(TestCase):
    DEFAULT_STRING_RE = re.compile('foo')
    UNICODE_STRING_RE = re.compile(u'foo')
    BYTE_STRING_RE = re.compile(b'foo')

    def test_none(self):
        self.assertFalse(__unit__.is_regex(None))

    def test_some_object(self):
        self.assertFalse(__unit__.is_regex(object()))

    def test_default_string_re(self):
        self.assertTrue(__unit__.is_regex(self.DEFAULT_STRING_RE))

    def test_unicode_string_re(self):
        self.assertTrue(__unit__.is_regex(self.UNICODE_STRING_RE))

    def test_bytestirng_re(self):
        self.assertTrue(__unit__.is_regex(self.BYTE_STRING_RE))


class EnsureString(TestCase):
    DEFAULT_STRING = 'foo'
    UNICODE_STRING = u'foo'
    BYTE_STRING = b'foo'

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

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

    def test_default_string(self):
        __unit__.ensure_string(self.DEFAULT_STRING)

    def test_unicode_string(self):
        __unit__.ensure_string(self.UNICODE_STRING)

    @skipIf(IS_PY3, "requires Python 2.x")
    def test_bytestring__py2(self):
        __unit__.ensure_string(self.BYTE_STRING)

    @skipUnless(IS_PY3, "requires Python 3.x")
    def test_bytestring__py3(self):
        with self.assertRaises(TypeError):
            __unit__.ensure_string(self.BYTE_STRING)


class EnsureRegex(TestCase):
    DEFAULT_STRING_RE = re.compile('foo')
    UNICODE_STRING_RE = re.compile(u'foo')
    BYTE_STRING_RE = re.compile(b'foo')

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

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

    def test_default_string_re(self):
        __unit__.ensure_regex(self.DEFAULT_STRING_RE)

    def test_unicode_string_re(self):
        __unit__.ensure_regex(self.UNICODE_STRING_RE)

    def test_bytestirng_re(self):
        __unit__.ensure_regex(self.BYTE_STRING_RE)


class Split(TestCase):
    TEXT = 'Alice has a cat'
    WORDS = ['Alice', 'has', 'a', 'cat']

    STRING = 'fooXbarXbaz'
    REGEX = 'ba.'
    SPLIT_BY_X = ['foo', 'bar', 'baz']
    SPLIT_BY_A_OR_X = ['foo', 'b', 'r', 'b', 'z']
    SPLIT_BY_REGEX = ['fooX', 'X', '']

    MAXSPLIT = 1

    def test_string__none(self):
        with self.assertRaises(TypeError):
            __unit__.split(None)

    def test_string__some_object(self):
        with self.assertRaises(TypeError):
            __unit__.split(object())

    def test_string__empty__no_separator(self):
        # result contingent on TODO inside the ``split`` function
        self.assertEquals([], __unit__.split(''))

    def test_string__some_string__no_separator(self):<commit_msg>Tests for .strings.trim<commit_after>"""
Tests for the .strings module.
"""
from contextlib import contextmanager
from functools import reduce
import re

from taipan._compat import IS_PY3
from taipan.collections import is_mapping
from taipan.testing import skipIf, skipUnless, TestCase

import taipan.strings as __unit__


class IsString(TestCase):
    DEFAULT_STRING = 'foo'
    UNICODE_STRING = u'foo'
    BYTE_STRING = b'foo'

    def test_none(self):
        self.assertFalse(__unit__.is_string(None))

    def test_some_object(self):
        self.assertFalse(__unit__.is_string(object()))

    def test_default_string(self):
        self.assertTrue(__unit__.is_string(self.DEFAULT_STRING))

    def test_unicode_string(self):
        self.assertTrue(__unit__.is_string(self.UNICODE_STRING))

    @skipIf(IS_PY3, "requires Python 2.x")
    def test_bytestring__py2(self):
        self.assertTrue(__unit__.is_string(self.BYTE_STRING))

    @skipUnless(IS_PY3, "requires Python 3.x")
    def test_bytestring__py3(self):
        self.assertFalse(__unit__.is_string(self.BYTE_STRING))


class IsRegex(TestCase):
    DEFAULT_STRING_RE = re.compile('foo')
    UNICODE_STRING_RE = re.compile(u'foo')
    BYTE_STRING_RE = re.compile(b'foo')

    def test_none(self):
        self.assertFalse(__unit__.is_regex(None))

    def test_some_object(self):
        self.assertFalse(__unit__.is_regex(object()))

    def test_default_string_re(self):
        self.assertTrue(__unit__.is_regex(self.DEFAULT_STRING_RE))

    def test_unicode_string_re(self):
        self.assertTrue(__unit__.is_regex(self.UNICODE_STRING_RE))

    def test_bytestirng_re(self):
        self.assertTrue(__unit__.is_regex(self.BYTE_STRING_RE))


class EnsureString(TestCase):
    DEFAULT_STRING = 'foo'
    UNICODE_STRING = u'foo'
    BYTE_STRING = b'foo'

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

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

    def test_default_string(self):
        __unit__.ensure_string(self.DEFAULT_STRING)

    def test_unicode_string(self):
        __unit__.ensure_string(self.UNICODE_STRING)

    @skipIf(IS_PY3, "requires Python 2.x")
    def test_bytestring__py2(self):
        __unit__.ensure_string(self.BYTE_STRING)

    @skipUnless(IS_PY3, "requires Python 3.x")
    def test_bytestring__py3(self):
        with self.assertRaises(TypeError):
            __unit__.ensure_string(self.BYTE_STRING)


class EnsureRegex(TestCase):
    DEFAULT_STRING_RE = re.compile('foo')
    UNICODE_STRING_RE = re.compile(u'foo')
    BYTE_STRING_RE = re.compile(b'foo')

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

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

    def test_default_string_re(self):
        __unit__.ensure_regex(self.DEFAULT_STRING_RE)

    def test_unicode_string_re(self):
        __unit__.ensure_regex(self.UNICODE_STRING_RE)

    def test_bytestirng_re(self):
        __unit__.ensure_regex(self.BYTE_STRING_RE)


class Trim(TestCase):
    STRING = 'some arbitrary string'

    def test_string__none(self):
        with self.assertRaises(TypeError):
            __unit__.trim(None)

    def test_string__some_object(self):
        with self.assertRaises(TypeError):
            __unit__.trim(object())

    def test_no_infixes(self):
        with self.assertRaises(ValueError) as r:
            __unit__.trim(self.STRING)
        self.assertIn("exactly one", str(r.exception))

    def test_prefix__empty(self):
        self.assertEquals(self.STRING, __unit__.trim(self.STRING, prefix=""))

    def test_prefix__present(self):
        self.assertEquals('bar', __unit__.trim('foobar', prefix='foo'))

    def test_prefix__absent__non_strict(self):
        self.assertEquals(
            'foobar', __unit__.trim('foobar', prefix='thud', strict=False))

    def test_prefix__absent__strict(self):
        with self.assertRaises(ValueError) as r:
            __unit__.trim('foobar', prefix='thud', strict=True)
        self.assertIn("expected", str(r.exception))
        self.assertIn("prefix", str(r.exception))

    def test_suffix__empty(self):
        self.assertEquals(self.STRING, __unit__.trim(self.STRING, suffix=""))

    def test_suffix__present(self):
        self.assertEquals('foo', __unit__.trim('foobar', suffix='bar'))

    def test_suffix__absent__non_strict(self):
        self.assertEquals(
            'foobar', __unit__.trim('foobar', suffix='thud', strict=False))

    def test_suffix__absent__strict(self):
        with self.assertRaises(ValueError) as r:
            __unit__.trim('foobar', suffix='thud', strict=True)
        self.assertIn("expected", str(r.exception))
        self.assertIn("suffix", str(r.exception))


class Split(TestCase):
    TEXT = 'Alice has a cat'
    WORDS = ['Alice', 'has', 'a', 'cat']

    STRING = 'fooXbarXbaz'
    REGEX = 'ba.'
    SPLIT_BY_X = ['foo', 'bar', 'baz']
    SPLIT_BY_A_OR_X = ['foo', 'b', 'r', 'b', 'z']
    SPLIT_BY_REGEX = ['fooX', 'X', '']

    MAXSPLIT = 1

    def test_string__none(self):
        with self.assertRaises(TypeError):
            __unit__.split(None)

    def test_string__some_object(self):
        with self.assertRaises(TypeError):
            __unit__.split(object())

    def test_string__empty__no_separator(self):
        # result contingent on TODO inside the ``split`` function
        self.assertEquals([], __unit__.split(''))

    def test_string__some_string__no_separator(self):
{'fraction_non_alphanumeric': 0.11351069379238393, 'fraction_numerical': 0.002921231090245175, 'mean_word_length': 3.5953978906999042, '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': '1319680', 'n_tokens_mistral': 3247, 'n_tokens_neox': 3098, 'n_words': 513}