test_docs.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import pytest
  2. import numpy as np
  3. from numpy.testing import assert_array_equal, assert_equal
  4. from . import util
  5. from pathlib import Path
  6. def get_docdir():
  7. parents = Path(__file__).resolve().parents
  8. try:
  9. # Assumes that spin is used to run tests
  10. nproot = parents[8]
  11. except IndexError:
  12. docdir = None
  13. else:
  14. docdir = nproot / "doc" / "source" / "f2py" / "code"
  15. if docdir and docdir.is_dir():
  16. return docdir
  17. # Assumes that an editable install is used to run tests
  18. return parents[3] / "doc" / "source" / "f2py" / "code"
  19. pytestmark = pytest.mark.skipif(
  20. not get_docdir().is_dir(),
  21. reason=f"Could not find f2py documentation sources"
  22. f"({get_docdir()} does not exist)",
  23. )
  24. def _path(*args):
  25. return get_docdir().joinpath(*args)
  26. @pytest.mark.slow
  27. class TestDocAdvanced(util.F2PyTest):
  28. # options = ['--debug-capi', '--build-dir', '/tmp/build-f2py']
  29. sources = [_path('asterisk1.f90'), _path('asterisk2.f90'),
  30. _path('ftype.f')]
  31. def test_asterisk1(self):
  32. foo = self.module.foo1
  33. assert_equal(foo(), b'123456789A12')
  34. def test_asterisk2(self):
  35. foo = self.module.foo2
  36. assert_equal(foo(2), b'12')
  37. assert_equal(foo(12), b'123456789A12')
  38. assert_equal(foo(20), b'123456789A123456789B')
  39. def test_ftype(self):
  40. ftype = self.module
  41. ftype.foo()
  42. assert_equal(ftype.data.a, 0)
  43. ftype.data.a = 3
  44. ftype.data.x = [1, 2, 3]
  45. assert_equal(ftype.data.a, 3)
  46. assert_array_equal(ftype.data.x,
  47. np.array([1, 2, 3], dtype=np.float32))
  48. ftype.data.x[1] = 45
  49. assert_array_equal(ftype.data.x,
  50. np.array([1, 45, 3], dtype=np.float32))
  51. # TODO: implement test methods for other example Fortran codes