test_modules.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from pybind11_tests import modules as m
  2. from pybind11_tests.modules import subsubmodule as ms
  3. from pybind11_tests import ConstructorStats
  4. def test_nested_modules():
  5. import pybind11_tests
  6. assert pybind11_tests.__name__ == "pybind11_tests"
  7. assert pybind11_tests.modules.__name__ == "pybind11_tests.modules"
  8. assert pybind11_tests.modules.subsubmodule.__name__ == "pybind11_tests.modules.subsubmodule"
  9. assert m.__name__ == "pybind11_tests.modules"
  10. assert ms.__name__ == "pybind11_tests.modules.subsubmodule"
  11. assert ms.submodule_func() == "submodule_func()"
  12. def test_reference_internal():
  13. b = ms.B()
  14. assert str(b.get_a1()) == "A[1]"
  15. assert str(b.a1) == "A[1]"
  16. assert str(b.get_a2()) == "A[2]"
  17. assert str(b.a2) == "A[2]"
  18. b.a1 = ms.A(42)
  19. b.a2 = ms.A(43)
  20. assert str(b.get_a1()) == "A[42]"
  21. assert str(b.a1) == "A[42]"
  22. assert str(b.get_a2()) == "A[43]"
  23. assert str(b.a2) == "A[43]"
  24. astats, bstats = ConstructorStats.get(ms.A), ConstructorStats.get(ms.B)
  25. assert astats.alive() == 2
  26. assert bstats.alive() == 1
  27. del b
  28. assert astats.alive() == 0
  29. assert bstats.alive() == 0
  30. assert astats.values() == ['1', '2', '42', '43']
  31. assert bstats.values() == []
  32. assert astats.default_constructions == 0
  33. assert bstats.default_constructions == 1
  34. assert astats.copy_constructions == 0
  35. assert bstats.copy_constructions == 0
  36. # assert astats.move_constructions >= 0 # Don't invoke any
  37. # assert bstats.move_constructions >= 0 # Don't invoke any
  38. assert astats.copy_assignments == 2
  39. assert bstats.copy_assignments == 0
  40. assert astats.move_assignments == 0
  41. assert bstats.move_assignments == 0
  42. def test_importing():
  43. from pybind11_tests.modules import OD
  44. from collections import OrderedDict
  45. assert OD is OrderedDict
  46. assert str(OD([(1, 'a'), (2, 'b')])) == "OrderedDict([(1, 'a'), (2, 'b')])"
  47. def test_pydoc():
  48. """Pydoc needs to be able to provide help() for everything inside a pybind11 module"""
  49. import pybind11_tests
  50. import pydoc
  51. assert pybind11_tests.__name__ == "pybind11_tests"
  52. assert pybind11_tests.__doc__ == "pybind11 test module"
  53. assert pydoc.text.docmodule(pybind11_tests)
  54. def test_duplicate_registration():
  55. """Registering two things with the same name"""
  56. assert m.duplicate_registration() == []