Ejemplo n.º 1
0
def test_py3_str_slicing(params, use_str):
    # Note: much simpler in python because __repr__ is required to return str
    # (which is actually unicode).
    safe_repr = SafeRepr()
    safe_repr.locale_preferred_encoding = 'ascii'
    safe_repr.sys_stdout_encoding = params.get('sys_stdout_encoding', 'ascii')

    safe_repr.maxother_outer = params['maxother_outer']

    if not use_str:

        class MyObj(object):

            def __repr__(self):
                return params['input']

        safe_repr_input = MyObj()
    else:
        safe_repr_input = params['input']
    expected_output = params['output']
    computed = safe_repr(safe_repr_input)
    expected = repr(expected_output)
    if use_str:
        expected = repr(expected)
    assert repr(computed) == expected

    # Check that we can json-encode the return.
    assert json.dumps(computed)
Ejemplo n.º 2
0
def test_py2_bytes_slicing(params, use_str):
    safe_repr = SafeRepr()
    safe_repr.locale_preferred_encoding = 'ascii'
    safe_repr.sys_stdout_encoding = params.get('sys_stdout_encoding', 'ascii')

    safe_repr.maxother_outer = params['maxother_outer']

    # This is the encoding that we expect back (because json needs to be able to encode it
    # later on, so, the return from SafeRepr must always be utf-8 regardless of the input).
    encoding = 'utf-8'

    if not use_str:

        class MyObj(object):

            def __repr__(self):
                ret = params['input']
                if isinstance(ret, unicode):
                    ret = ret.encode(encoding)
                return ret

        safe_repr_input = MyObj()
    else:
        safe_repr_input = params['input']

    computed = safe_repr(safe_repr_input)

    if use_str:
        expected_output = params['output_str']
    else:
        expected_output = params['output']

    expect_unicode = False
    if isinstance(expected_output, unicode):
        expect_unicode = True
    if isinstance(expected_output, tuple) and isinstance(expected_output[0], unicode):
        expect_unicode = True

    if expect_unicode:
        computed = computed.decode(encoding)
        if isinstance(expected_output, tuple):
            assert computed in expected_output
        else:
            assert computed == expected_output
    else:
        if isinstance(expected_output, tuple):
            assert computed in expected_output
        else:
            assert computed == expected_output

    # Check that we can json-encode the return.
    assert json.dumps(computed)
Ejemplo n.º 3
0
def test_py3_str_slicing(params):
    # Note: much simpler in python because __repr__ is required to return str
    # (which is actually unicode).
    safe_repr = SafeRepr()
    safe_repr.locale_preferred_encoding = 'ascii'
    safe_repr.sys_stdout_encoding = params.get('sys_stdout_encoding', 'ascii')

    safe_repr.maxother_outer = params['maxother_outer']

    class MyObj(object):

        def __repr__(self):
            return params['input']

    expected_output = params['output']
    computed = safe_repr(MyObj())
    assert repr(computed) == repr(expected_output)

    # Check that we can json-encode the return.
    assert json.dumps(computed)
Ejemplo n.º 4
0
def test_py2_bytes_slicing(params):
    safe_repr = SafeRepr()
    safe_repr.locale_preferred_encoding = 'ascii'
    safe_repr.sys_stdout_encoding = params.get('sys_stdout_encoding', 'ascii')

    safe_repr.maxother_outer = params['maxother_outer']

    # This is the encoding that we expect back (because json needs to be able to encode it
    # later on, so, the return from SafeRepr must always be utf-8 regardless of the input).
    encoding = 'utf-8'

    class MyObj(object):

        def __repr__(self):
            ret = params['input']
            if isinstance(ret, unicode):
                ret = ret.encode(encoding)
            return ret

    expected_output = params['output']
    computed = safe_repr(MyObj())

    expect_unicode = False
    if isinstance(expected_output, unicode):
        expect_unicode = True
    if isinstance(expected_output, tuple) and isinstance(expected_output[0], unicode):
        expect_unicode = True

    if expect_unicode:
        computed = computed.decode(encoding)
        if isinstance(expected_output, tuple):
            assert computed in expected_output
        else:
            assert computed == expected_output
    else:
        assert repr(computed) == repr(expected_output)

    # Check that we can json-encode the return.
    assert json.dumps(computed)