2016-08-12 11:50:00 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def test_constants():
|
|
|
|
from pybind11_tests import some_constant
|
|
|
|
|
|
|
|
assert some_constant == 14
|
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_function_overloading():
|
2016-08-12 11:50:00 +00:00
|
|
|
from pybind11_tests import EMyEnumeration, test_function
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
assert test_function() == "test_function()"
|
|
|
|
assert test_function(7) == "test_function(7)"
|
|
|
|
assert test_function(EMyEnumeration.EFirstEntry) == "test_function(enum=1)"
|
|
|
|
assert test_function(EMyEnumeration.ESecondEntry) == "test_function(enum=2)"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_unscoped_enum():
|
|
|
|
from pybind11_tests import EMyEnumeration, EFirstEntry
|
|
|
|
|
|
|
|
assert str(EMyEnumeration.EFirstEntry) == "EMyEnumeration.EFirstEntry"
|
|
|
|
assert str(EMyEnumeration.ESecondEntry) == "EMyEnumeration.ESecondEntry"
|
|
|
|
assert str(EFirstEntry) == "EMyEnumeration.EFirstEntry"
|
|
|
|
|
|
|
|
# no TypeError exception for unscoped enum ==/!= int comparisons
|
|
|
|
y = EMyEnumeration.ESecondEntry
|
|
|
|
assert y == 2
|
|
|
|
assert y != 3
|
|
|
|
|
|
|
|
assert int(EMyEnumeration.ESecondEntry) == 2
|
|
|
|
assert str(EMyEnumeration(2)) == "EMyEnumeration.ESecondEntry"
|
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_scoped_enum():
|
2016-08-12 11:50:00 +00:00
|
|
|
from pybind11_tests import ECMyEnum, test_ecenum
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
assert test_ecenum(ECMyEnum.Three) == "test_ecenum(ECMyEnum::Three)"
|
2016-08-12 11:50:00 +00:00
|
|
|
z = ECMyEnum.Two
|
2016-08-12 20:28:31 +00:00
|
|
|
assert test_ecenum(z) == "test_ecenum(ECMyEnum::Two)"
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
# expected TypeError exceptions for scoped enum ==/!= int comparisons
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert z == 2
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert z != 3
|
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_implicit_conversion():
|
2016-08-12 11:50:00 +00:00
|
|
|
from pybind11_tests import ExampleWithEnum
|
|
|
|
|
|
|
|
assert str(ExampleWithEnum.EMode.EFirstMode) == "EMode.EFirstMode"
|
|
|
|
assert str(ExampleWithEnum.EFirstMode) == "EMode.EFirstMode"
|
|
|
|
|
|
|
|
f = ExampleWithEnum.test_function
|
|
|
|
first = ExampleWithEnum.EFirstMode
|
|
|
|
second = ExampleWithEnum.ESecondMode
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
assert f(first) == 1
|
|
|
|
|
|
|
|
assert f(first) == f(first)
|
|
|
|
assert not f(first) != f(first)
|
|
|
|
|
|
|
|
assert f(first) != f(second)
|
|
|
|
assert not f(first) == f(second)
|
|
|
|
|
|
|
|
assert f(first) == int(f(first))
|
|
|
|
assert not f(first) != int(f(first))
|
|
|
|
|
|
|
|
assert f(first) != int(f(second))
|
|
|
|
assert not f(first) == int(f(second))
|
|
|
|
|
|
|
|
# noinspection PyDictCreation
|
|
|
|
x = {f(first): 1, f(second): 2}
|
|
|
|
x[f(first)] = 3
|
|
|
|
x[f(second)] = 4
|
2016-08-12 11:50:00 +00:00
|
|
|
# Hashing test
|
|
|
|
assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
|
|
|
|
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
def test_bytes():
|
2016-08-12 11:50:00 +00:00
|
|
|
from pybind11_tests import return_bytes, print_bytes
|
|
|
|
|
2016-08-12 20:28:31 +00:00
|
|
|
assert print_bytes(return_bytes()) == "bytes[1 0 2 0]"
|