summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/common/text/converters/test_container_to_text.py
blob: 39038f5133347675c00b80f0cb77751d0e56ab06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
# Copyright 2019, Andrew Klychkov @Andersson007 <aaklychkov@mail.ru>
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

import pytest

from ansible.module_utils.common.text.converters import container_to_text


DEFAULT_ENCODING = 'utf-8'
DEFAULT_ERR_HANDLER = 'surrogate_or_strict'


@pytest.mark.parametrize(
    'test_input,expected',
    [
        ({1: 1}, {1: 1}),
        ([1, 2], [1, 2]),
        ((1, 2), (1, 2)),
        (1, 1),
        (1.1, 1.1),
        (b'str', u'str'),
        (u'str', u'str'),
        ([b'str'], [u'str']),
        ((b'str'), (u'str')),
        ({b'str': b'str'}, {u'str': u'str'}),
    ]
)
@pytest.mark.parametrize('encoding', ['utf-8', 'latin1', 'shift-jis', 'big5', 'koi8_r', ])
@pytest.mark.parametrize('errors', ['strict', 'surrogate_or_strict', 'surrogate_then_replace', ])
def test_container_to_text_different_types(test_input, expected, encoding, errors):
    """Test for passing objects to container_to_text()."""
    assert container_to_text(test_input, encoding=encoding, errors=errors) == expected


@pytest.mark.parametrize(
    'test_input,expected',
    [
        ({1: 1}, {1: 1}),
        ([1, 2], [1, 2]),
        ((1, 2), (1, 2)),
        (1, 1),
        (1.1, 1.1),
        (True, True),
        (None, None),
        (u'str', u'str'),
        (u'くらとみ'.encode(DEFAULT_ENCODING), u'くらとみ'),
        (u'café'.encode(DEFAULT_ENCODING), u'café'),
        (u'str'.encode(DEFAULT_ENCODING), u'str'),
        ([u'str'.encode(DEFAULT_ENCODING)], [u'str']),
        ((u'str'.encode(DEFAULT_ENCODING)), (u'str')),
        ({b'str': b'str'}, {u'str': u'str'}),
    ]
)
def test_container_to_text_default_encoding_and_err(test_input, expected):
    """
    Test for passing objects to container_to_text(). Default encoding and errors
    """
    assert container_to_text(test_input, encoding=DEFAULT_ENCODING,
                             errors=DEFAULT_ERR_HANDLER) == expected


@pytest.mark.parametrize(
    'test_input,encoding,expected',
    [
        (u'й'.encode('utf-8'), 'latin1', u'й'),
        (u'café'.encode('utf-8'), 'shift_jis', u'cafテゥ'),
    ]
)
@pytest.mark.parametrize('errors', ['strict', 'surrogate_or_strict', 'surrogate_then_replace', ])
def test_container_to_text_incomp_encod_chars(test_input, encoding, errors, expected):
    """
    Test for passing incompatible characters and encodings container_to_text().
    """
    assert container_to_text(test_input, encoding=encoding, errors=errors) == expected