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