test_extension.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """Tests for distutils.extension."""
  2. import unittest
  3. import os
  4. import warnings
  5. from test.support import run_unittest
  6. from test.support.warnings_helper import check_warnings
  7. from distutils.extension import read_setup_file, Extension
  8. class ExtensionTestCase(unittest.TestCase):
  9. def test_read_setup_file(self):
  10. # trying to read a Setup file
  11. # (sample extracted from the PyGame project)
  12. setup = os.path.join(os.path.dirname(__file__), 'Setup.sample')
  13. exts = read_setup_file(setup)
  14. names = [ext.name for ext in exts]
  15. names.sort()
  16. # here are the extensions read_setup_file should have created
  17. # out of the file
  18. wanted = ['_arraysurfarray', '_camera', '_numericsndarray',
  19. '_numericsurfarray', 'base', 'bufferproxy', 'cdrom',
  20. 'color', 'constants', 'display', 'draw', 'event',
  21. 'fastevent', 'font', 'gfxdraw', 'image', 'imageext',
  22. 'joystick', 'key', 'mask', 'mixer', 'mixer_music',
  23. 'mouse', 'movie', 'overlay', 'pixelarray', 'pypm',
  24. 'rect', 'rwobject', 'scrap', 'surface', 'surflock',
  25. 'time', 'transform']
  26. self.assertEqual(names, wanted)
  27. def test_extension_init(self):
  28. # the first argument, which is the name, must be a string
  29. self.assertRaises(AssertionError, Extension, 1, [])
  30. ext = Extension('name', [])
  31. self.assertEqual(ext.name, 'name')
  32. # the second argument, which is the list of files, must
  33. # be a list of strings
  34. self.assertRaises(AssertionError, Extension, 'name', 'file')
  35. self.assertRaises(AssertionError, Extension, 'name', ['file', 1])
  36. ext = Extension('name', ['file1', 'file2'])
  37. self.assertEqual(ext.sources, ['file1', 'file2'])
  38. # others arguments have defaults
  39. for attr in ('include_dirs', 'define_macros', 'undef_macros',
  40. 'library_dirs', 'libraries', 'runtime_library_dirs',
  41. 'extra_objects', 'extra_compile_args', 'extra_link_args',
  42. 'export_symbols', 'swig_opts', 'depends'):
  43. self.assertEqual(getattr(ext, attr), [])
  44. self.assertEqual(ext.language, None)
  45. self.assertEqual(ext.optional, None)
  46. # if there are unknown keyword options, warn about them
  47. with check_warnings() as w:
  48. warnings.simplefilter('always')
  49. ext = Extension('name', ['file1', 'file2'], chic=True)
  50. self.assertEqual(len(w.warnings), 1)
  51. self.assertEqual(str(w.warnings[0].message),
  52. "Unknown Extension options: 'chic'")
  53. def test_suite():
  54. return unittest.TestLoader().loadTestsFromTestCase(ExtensionTestCase)
  55. if __name__ == "__main__":
  56. run_unittest(test_suite())