def test_single_char_arguments():
    """Tests failures for passing invalid inputs to char-accepting functions"""
    from pybind11_tests import ord_char, ord_char16, ord_char32, ord_wchar, wchar_size

    def toobig_message(r):
        return "Character code point not in range({0:#x})".format(r)

    toolong_message = "Expected a character, but multi-character string found"

    assert ord_char(u'a') == 0x61  # simple ASCII
    assert ord_char(
        u'é'
    ) == 0xE9  # requires 2 bytes in utf-8, but can be stuffed in a char
    with pytest.raises(ValueError) as excinfo:
        assert ord_char(
            u'Ā') == 0x100  # requires 2 bytes, doesn't fit in a char
    assert str(excinfo.value) == toobig_message(0x100)
    with pytest.raises(ValueError) as excinfo:
        assert ord_char(u'ab')
    assert str(excinfo.value) == toolong_message

    assert ord_char16(u'a') == 0x61
    assert ord_char16(u'é') == 0xE9
    assert ord_char16(u'Ā') == 0x100
    assert ord_char16(u'‽') == 0x203d
    assert ord_char16(u'♥') == 0x2665
    with pytest.raises(ValueError) as excinfo:
        assert ord_char16(u'🎂') == 0x1F382  # requires surrogate pair
    assert str(excinfo.value) == toobig_message(0x10000)
    with pytest.raises(ValueError) as excinfo:
        assert ord_char16(u'aa')
    assert str(excinfo.value) == toolong_message

    assert ord_char32(u'a') == 0x61
    assert ord_char32(u'é') == 0xE9
    assert ord_char32(u'Ā') == 0x100
    assert ord_char32(u'‽') == 0x203d
    assert ord_char32(u'♥') == 0x2665
    assert ord_char32(u'🎂') == 0x1F382
    with pytest.raises(ValueError) as excinfo:
        assert ord_char32(u'aa')
    assert str(excinfo.value) == toolong_message

    assert ord_wchar(u'a') == 0x61
    assert ord_wchar(u'é') == 0xE9
    assert ord_wchar(u'Ā') == 0x100
    assert ord_wchar(u'‽') == 0x203d
    assert ord_wchar(u'♥') == 0x2665
    if wchar_size == 2:
        with pytest.raises(ValueError) as excinfo:
            assert ord_wchar(u'🎂') == 0x1F382  # requires surrogate pair
        assert str(excinfo.value) == toobig_message(0x10000)
    else:
        assert ord_wchar(u'🎂') == 0x1F382
    with pytest.raises(ValueError) as excinfo:
        assert ord_wchar(u'aa')
    assert str(excinfo.value) == toolong_message
예제 #2
0
def test_single_char_arguments():
    """Tests failures for passing invalid inputs to char-accepting functions"""
    from pybind11_tests import ord_char, ord_char16, ord_char32, ord_wchar, wchar_size

    def toobig_message(r):
        return "Character code point not in range({0:#x})".format(r)
    toolong_message = "Expected a character, but multi-character string found"

    assert ord_char(u'a') == 0x61  # simple ASCII
    assert ord_char(u'é') == 0xE9  # requires 2 bytes in utf-8, but can be stuffed in a char
    with pytest.raises(ValueError) as excinfo:
        assert ord_char(u'Ā') == 0x100  # requires 2 bytes, doesn't fit in a char
    assert str(excinfo.value) == toobig_message(0x100)
    with pytest.raises(ValueError) as excinfo:
        assert ord_char(u'ab')
    assert str(excinfo.value) == toolong_message

    assert ord_char16(u'a') == 0x61
    assert ord_char16(u'é') == 0xE9
    assert ord_char16(u'Ā') == 0x100
    assert ord_char16(u'‽') == 0x203d
    assert ord_char16(u'♥') == 0x2665
    with pytest.raises(ValueError) as excinfo:
        assert ord_char16(u'🎂') == 0x1F382  # requires surrogate pair
    assert str(excinfo.value) == toobig_message(0x10000)
    with pytest.raises(ValueError) as excinfo:
        assert ord_char16(u'aa')
    assert str(excinfo.value) == toolong_message

    assert ord_char32(u'a') == 0x61
    assert ord_char32(u'é') == 0xE9
    assert ord_char32(u'Ā') == 0x100
    assert ord_char32(u'‽') == 0x203d
    assert ord_char32(u'♥') == 0x2665
    assert ord_char32(u'🎂') == 0x1F382
    with pytest.raises(ValueError) as excinfo:
        assert ord_char32(u'aa')
    assert str(excinfo.value) == toolong_message

    assert ord_wchar(u'a') == 0x61
    assert ord_wchar(u'é') == 0xE9
    assert ord_wchar(u'Ā') == 0x100
    assert ord_wchar(u'‽') == 0x203d
    assert ord_wchar(u'♥') == 0x2665
    if wchar_size == 2:
        with pytest.raises(ValueError) as excinfo:
            assert ord_wchar(u'🎂') == 0x1F382  # requires surrogate pair
        assert str(excinfo.value) == toobig_message(0x10000)
    else:
        assert ord_wchar(u'🎂') == 0x1F382
    with pytest.raises(ValueError) as excinfo:
        assert ord_wchar(u'aa')
    assert str(excinfo.value) == toolong_message
def test_single_char_arguments():
    """Tests failures for passing invalid inputs to char-accepting functions"""
    from pybind11_tests import ord_char, ord_char16, ord_char32, ord_wchar, wchar_size

    def toobig_message(r):
        return "Character code point not in range({0:#x})".format(r)

    toolong_message = "Expected a character, but multi-character string found"

    if ord_char(u'a') != 0x61:
        raise AssertionError
    if ord_char(u'é') != 0xE9:
        raise AssertionError
    with pytest.raises(ValueError) as excinfo:
        if ord_char(u'Ā') != 0x100:
            raise AssertionError
    if str(excinfo.value) != toobig_message(0x100):
        raise AssertionError
    with pytest.raises(ValueError) as excinfo:
        if not ord_char(u'ab'):
            raise AssertionError
    if str(excinfo.value) != toolong_message:
        raise AssertionError

    if ord_char16(u'a') != 0x61:
        raise AssertionError
    if ord_char16(u'é') != 0xE9:
        raise AssertionError
    if ord_char16(u'Ā') != 0x100:
        raise AssertionError
    if ord_char16(u'‽') != 0x203d:
        raise AssertionError
    if ord_char16(u'♥') != 0x2665:
        raise AssertionError
    with pytest.raises(ValueError) as excinfo:
        if ord_char16(u'🎂') != 0x1F382:
            raise AssertionError
    if str(excinfo.value) != toobig_message(0x10000):
        raise AssertionError
    with pytest.raises(ValueError) as excinfo:
        if not ord_char16(u'aa'):
            raise AssertionError
    if str(excinfo.value) != toolong_message:
        raise AssertionError

    if ord_char32(u'a') != 0x61:
        raise AssertionError
    if ord_char32(u'é') != 0xE9:
        raise AssertionError
    if ord_char32(u'Ā') != 0x100:
        raise AssertionError
    if ord_char32(u'‽') != 0x203d:
        raise AssertionError
    if ord_char32(u'♥') != 0x2665:
        raise AssertionError
    if ord_char32(u'🎂') != 0x1F382:
        raise AssertionError
    with pytest.raises(ValueError) as excinfo:
        if not ord_char32(u'aa'):
            raise AssertionError
    if str(excinfo.value) != toolong_message:
        raise AssertionError

    if ord_wchar(u'a') != 0x61:
        raise AssertionError
    if ord_wchar(u'é') != 0xE9:
        raise AssertionError
    if ord_wchar(u'Ā') != 0x100:
        raise AssertionError
    if ord_wchar(u'‽') != 0x203d:
        raise AssertionError
    if ord_wchar(u'♥') != 0x2665:
        raise AssertionError
    if wchar_size == 2:
        with pytest.raises(ValueError) as excinfo:
            if ord_wchar(u'🎂') != 0x1F382:
                raise AssertionError
        if str(excinfo.value) != toobig_message(0x10000):
            raise AssertionError
    else:
        if ord_wchar(u'🎂') != 0x1F382:
            raise AssertionError
    with pytest.raises(ValueError) as excinfo:
        if not ord_wchar(u'aa'):
            raise AssertionError
    if str(excinfo.value) != toolong_message:
        raise AssertionError