# -*- 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

[((44, 14, 44, 40), 'camel_tools.utils.charmap.CharMapper', 'CharMapper', ({(44, 25, 44, 33): 'TEST_MAP', (44, 35, 44, 39): 'None'}, {}), '(TEST_MAP, None)', False, 'from camel_tools.utils.charmap import CharMapper\n'), ((70, 15, 70, 42), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(70, 30, 70, 41): 'TEST_MAPPER'}, {}), '(TEST_MAPPER)', False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((123, 15, 123, 53), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(123, 30, 123, 41): 'TEST_MAPPER', (123, 43, 123, 52): '"""@@LAT@@"""'}, {}), "(TEST_MAPPER, '@@LAT@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((129, 15, 129, 54), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(129, 30, 129, 41): 'TEST_MAPPER', (129, 43, 129, 53): 'u"""@@LAT@@"""'}, {}), "(TEST_MAPPER, u'@@LAT@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((140, 16, 140, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(140, 31, 140, 42): 'TEST_MAPPER', (140, 44, 140, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((147, 16, 147, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(147, 31, 147, 42): 'TEST_MAPPER', (147, 44, 147, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((154, 16, 154, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(154, 31, 154, 42): 'TEST_MAPPER', (154, 44, 154, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((162, 16, 162, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(162, 31, 162, 42): 'TEST_MAPPER', (162, 44, 162, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((170, 16, 170, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(170, 31, 170, 42): 'TEST_MAPPER', (170, 44, 170, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((179, 16, 179, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(179, 31, 179, 42): 'TEST_MAPPER', (179, 44, 179, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((189, 16, 189, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(189, 31, 189, 42): 'TEST_MAPPER', (189, 44, 189, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((200, 16, 200, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(200, 31, 200, 42): 'TEST_MAPPER', (200, 44, 200, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((212, 16, 212, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(212, 31, 212, 42): 'TEST_MAPPER', (212, 44, 212, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((223, 16, 223, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(223, 31, 223, 42): 'TEST_MAPPER', (223, 44, 223, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((235, 16, 235, 49), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(235, 31, 235, 42): 'TEST_MAPPER', (235, 44, 235, 48): '"""@@"""'}, {}), "(TEST_MAPPER, '@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((55, 13, 55, 37), 'pytest.raises', 'pytest.raises', ({(55, 27, 55, 36): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((56, 12, 56, 32), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(56, 27, 56, 31): 'None'}, {}), '(None)', False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((63, 13, 63, 37), 'pytest.raises', 'pytest.raises', ({(63, 27, 63, 36): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((64, 12, 64, 30), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(64, 27, 64, 29): '{}'}, {}), '({})', False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((76, 13, 76, 37), 'pytest.raises', 'pytest.raises', ({(76, 27, 76, 36): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((77, 12, 77, 45), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(77, 27, 77, 38): 'TEST_MAPPER', (77, 40, 77, 44): 'None'}, {}), '(TEST_MAPPER, None)', False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((84, 13, 84, 37), 'pytest.raises', 'pytest.raises', ({(84, 27, 84, 36): 'TypeError'}, {}), '(TypeError)', False, 'import pytest\n'), ((85, 12, 85, 43), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(85, 27, 85, 38): 'TEST_MAPPER', (85, 40, 85, 42): '[]'}, {}), '(TEST_MAPPER, [])', False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((92, 13, 92, 38), 'pytest.raises', 'pytest.raises', ({(92, 27, 92, 37): 'ValueError'}, {}), '(ValueError)', False, 'import pytest\n'), ((93, 12, 93, 43), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(93, 27, 93, 38): 'TEST_MAPPER', (93, 40, 93, 42): '""""""'}, {}), "(TEST_MAPPER, '')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((100, 13, 100, 38), 'pytest.raises', 'pytest.raises', ({(100, 27, 100, 37): 'ValueError'}, {}), '(ValueError)', False, 'import pytest\n'), ((101, 12, 101, 51), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(101, 27, 101, 38): 'TEST_MAPPER', (101, 40, 101, 50): '"""@@LAT @@"""'}, {}), "(TEST_MAPPER, '@@LAT @@')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((108, 13, 108, 38), 'pytest.raises', 'pytest.raises', ({(108, 27, 108, 37): 'ValueError'}, {}), '(ValueError)', False, 'import pytest\n'), ((109, 12, 109, 51), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(109, 27, 109, 38): 'TEST_MAPPER', (109, 40, 109, 50): '"""@@LAT@@ """'}, {}), "(TEST_MAPPER, '@@LAT@@ ')", False, 'from camel_tools.utils.transliterate import Transliterator\n'), ((116, 13, 116, 38), 'pytest.raises', 'pytest.raises', ({(116, 27, 116, 37): 'ValueError'}, {}), '(ValueError)', False, 'import pytest\n'), ((117, 12, 117, 51), 'camel_tools.utils.transliterate.Transliterator', 'Transliterator', ({(117, 27, 117, 38): 'TEST_MAPPER', (117, 40, 117, 50): '""" @@LAT@@"""'}, {}), "(TEST_MAPPER, ' @@LAT@@')", False, 'from camel_tools.utils.transliterate import Transliterator\n')]