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