# -*- coding: utf-8 -*-

# MIT License
#
# Copyright 2018-2020 New York University Abu Dhabi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
Tests for camel_tools.transliterate.
"""

from __future__ import absolute_import

import pytest

from camel_tools.utils.charmap import CharMapper
from camel_tools.utils.transliterate import Transliterator


# A mapper that translates lower-case English characters to a lower-case x and
# upper-case English characters to an upper-case X. This makes it easy to
# predict what the transliteration should be.
TEST_MAP = {
    u'A-Z': u'X',
    u'a-z': u'x',
}
TEST_MAPPER = CharMapper(TEST_MAP, None)


class TestTransliteratorInit(object):
    """Test class for Transliterator.__init__.
    """

    def test_init_none_mapper(self):
        """Test that init raises a TypeError when given a mapper that is None.
        """

        with pytest.raises(TypeError):
            Transliterator(None)

    def test_init_invalid_type_mapper(self):
        """Test that init raises a TypeError when given a mapper that is not a
        CharMapper instance.
        """

        with pytest.raises(TypeError):
            Transliterator({})

    def test_init_valid_mapper(self):
        """Test that init doesn't raise an error when given a valid mapper.
        """

        assert Transliterator(TEST_MAPPER)

    def test_init_none_marker(self):
        """Test that init raises a TypeError when given a marker that is None.
        """

        with pytest.raises(TypeError):
            Transliterator(TEST_MAPPER, None)

    def test_init_invalid_type_marker(self):
        """Test that init raises a TypeError when given a marker that is not a
        string.
        """

        with pytest.raises(TypeError):
            Transliterator(TEST_MAPPER, [])

    def test_init_empty_marker(self):
        """Test that init raises a ValueError when given a marker that is an
        empty string.
        """

        with pytest.raises(ValueError):
            Transliterator(TEST_MAPPER, '')

    def test_init_invalid_marker1(self):
        """Test that init raises a ValueError when given an invalid marker (
        wgitespace in the middle).
        """

        with pytest.raises(ValueError):
            Transliterator(TEST_MAPPER, '@@LAT @@')

    def test_init_invalid_marker2(self):
        """Test that init raises a ValueError when given an invalid marker (
        whitespace at the end).
        """

        with pytest.raises(ValueError):
            Transliterator(TEST_MAPPER, '@@LAT@@ ')

    def test_init_invalid_marker3(self):
        """Test that init raises a ValueError when given an invalid marker (
        whitespace at the beginning).
        """

        with pytest.raises(ValueError):
            Transliterator(TEST_MAPPER, ' @@LAT@@')

    def test_init_valid_marker1(self):
        """Test that init doesn't raise an error when given a valid marker.
        """

        assert Transliterator(TEST_MAPPER, '@@LAT@@')

    def test_init_valid_marker2(self):
        """Test that init doesn't raise an error when given a valid marker.
        """

        assert Transliterator(TEST_MAPPER, u'@@LAT@@')


class TestTransliteratorTranslate(object):
    """Test class for Transliterator.translate.
    """

    def test_trans_empty(self):
        """Test that transliterating an empty string returns an empty string.
        """

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(u'') == u''

    def test_trans_single_no_markers(self):
        """Test that a single word with no markers gets transliterated.
        """

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(u'Hello') == u'Xxxxx'

    def test_trans_single_with_markers(self):
        """Test that a single word with markers does not get transliterated.
        """

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(u'@@Hello') == u'@@Hello'

    def test_trans_single_strip(self):
        """Test that a single word with markers does not get transliterated
        but markers do get stripped when strip_markers is set to True.
        """

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(u'@@Hello', True) == u'Hello'

    def test_trans_single_ignore(self):
        """Test that a single word with markers gets transliterated when ignore
        markers is set to True.
        """

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(u'@@Hello', False, True) == u'@@Xxxxx'

    def test_trans_single_ignore_strip(self):
        """Test that a single word with markers gets transliterated with
        markers stripped when both strip_markers and ignore_markers are set to
        True.
        """

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(u'@@Hello', True, True) == u'Xxxxx'

    def test_trans_sent_no_markers(self):
        """Test that a sentence with no markers gets transliterated.
        """

        sent_orig = u'Hello World, this is a sentence!'
        sent_out = u'Xxxxx Xxxxx, xxxx xx x xxxxxxxx!'

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(sent_orig) == sent_out

    def test_trans_sent_with_markers(self):
        """Test that tokens with markers in a sentence do not get
        transliterated.
        """

        sent_orig = u'Hello @@World, this is a @@sentence!'
        sent_out = u'Xxxxx @@World, xxxx xx x @@sentence!'

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(sent_orig) == sent_out

    def test_trans_sent_strip(self):
        """Test that tokens with markers in a sentence do not get
        transliterated but markers do get stripped when strip_markers is set
        to True.
        """

        sent_orig = u'Hello @@World, this is a @@sentence!'
        sent_out = u'Xxxxx World, xxxx xx x sentence!'

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(sent_orig, True) == sent_out

    def test_trans_sent_ignore(self):
        """Test that tokens with markers in a sentence get transliterated
        when ignore markers is set to True.
        """

        sent_orig = u'Hello @@World, this is a @@sentence!'
        sent_out = u'Xxxxx @@Xxxxx, xxxx xx x @@xxxxxxxx!'

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(sent_orig, False, True) == sent_out

    def test_trans_sent_ignore_strip(self):
        """Test that tokens with markers in a sentence get transliterated with
        markers stripped when both strip_markers and ignore_markers are set to
        True.
        """

        sent_orig = u'Hello @@World, this is a @@sentence!'
        sent_out = u'Xxxxx Xxxxx, xxxx xx x xxxxxxxx!'

        trans = Transliterator(TEST_MAPPER, '@@')
        assert trans.transliterate(sent_orig, True, True) == sent_out

[((1626, 1652), 'camel_tools.utils.charmap.CharMapper', 'CharMapper', (['TEST_MAP', 'None'], {}), '(TEST_MAP, None)\n', (1636, 1652), False, 'from camel_tools.utils.charmap import CharMapper\n'), ((2330, 2357), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER'], {}), '(TEST_MAPPER)\n', (2344, 2357), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((3966, 4004), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@LAT@@"""'], {}), "(TEST_MAPPER, '@@LAT@@')\n", (3980, 4004), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((4149, 4188), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', 'u"""@@LAT@@"""'], {}), "(TEST_MAPPER, u'@@LAT@@')\n", (4163, 4188), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((4430, 4463), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (4444, 4463), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((4657, 4690), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (4671, 4690), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((4901, 4934), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (4915, 4934), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((5212, 5245), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (5226, 5245), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((5493, 5526), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (5507, 5526), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((5843, 5876), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (5857, 5876), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((6201, 6234), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (6215, 6234), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((6577, 6610), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (6591, 6610), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((7012, 7045), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (7026, 7045), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((7416, 7449), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (7430, 7449), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((7883, 7916), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@"""'], {}), "(TEST_MAPPER, '@@')\n", (7897, 7916), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((1891, 1915), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (1904, 1915), False, 'import pytest\n'), ((1929, 1949), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['None'], {}), '(None)\n', (1943, 1949), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((2130, 2154), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (2143, 2154), False, 'import pytest\n'), ((2168, 2186), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['{}'], {}), '({})\n', (2182, 2186), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((2501, 2525), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (2514, 2525), False, 'import pytest\n'), ((2539, 2572), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', 'None'], {}), '(TEST_MAPPER, None)\n', (2553, 2572), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((2740, 2764), 'pytest.raises', 'pytest.raises', (['TypeError'], {}), '(TypeError)\n', (2753, 2764), False, 'import pytest\n'), ((2778, 2809), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '[]'], {}), '(TEST_MAPPER, [])\n', (2792, 2809), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((2974, 2999), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (2987, 2999), False, 'import pytest\n'), ((3013, 3044), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '""""""'], {}), "(TEST_MAPPER, '')\n", (3027, 3044), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((3225, 3250), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (3238, 3250), False, 'import pytest\n'), ((3264, 3303), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@LAT @@"""'], {}), "(TEST_MAPPER, '@@LAT @@')\n", (3278, 3303), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((3481, 3506), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (3494, 3506), False, 'import pytest\n'), ((3520, 3559), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '"""@@LAT@@ """'], {}), "(TEST_MAPPER, '@@LAT@@ ')\n", (3534, 3559), False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((3743, 3768), 'pytest.raises', 'pytest.raises', (['ValueError'], {}), '(ValueError)\n', (3756, 3768), False, 'import pytest\n'), ((3782, 3821), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', (['TEST_MAPPER', '""" @@LAT@@"""'], {}), "(TEST_MAPPER, ' @@LAT@@')\n", (3796, 3821), False, 'from camel_tools.utils.transliterate import Transliterator\n')]