Ejemplo n.º 1
0
def i18n_repr(i18n_text):
    """
    Return a representation suitable for direct inclusion in a __repr__
    value, for example f'<Item title={i18n_repr(self.title)}>'.
    """
    if type(i18n_text) == dict and 'en' in i18n_text:
        # Add a prefix to indicate the English was picked out.
        return '[en]{}'.format(truncate(i18n_text['en']))

    return truncate(i18n_text)
Ejemplo n.º 2
0
    def test_truncate(self):
        # Create a string that exceeds the truncation length.
        long_string = 100 * 'a'

        cases = [
            ('abc', "'abc'"),
            # Test a string that exceeds the truncation length.
            (long_string, "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'..."),
            # Test a non-string.
            ({'a': 1}, '"{\'a\': 1}"'),
            # Test a non-string that exceeds the truncation length.
            ({long_string: 1}, '"{\'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"...'),
        ]
        for obj, expected in cases:
            with self.subTest(obj=obj):
                actual = utils.truncate(obj)
                self.assertEqual(actual, expected)