test_extension.py 2.7 KB

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