Ejemplo n.º 1
0
    def test_mixed(self):
        lines = textwrap.dedent('''
           int spam;
           static const char const *eggs;

           PyObject * start(void) {
               static int initialized = 0;
               if (initialized) {
                   initialized = 1;
                   init();
               }
               return _start();
           }

           char* ham;

           static int stop(char *reason) {
               ham = reason;
               return _stop();
           }
           ''').splitlines()
        expected = [
            (
                textwrap.dedent('''
                PyObject * start(void) {
                static int initialized = 0;
                if (initialized) {
                initialized = 1;
                init();
                }
                return _start();
                }
                ''').strip(),
                textwrap.dedent('''
                static int initialized = 0;
                if (initialized) {
                initialized = 1;
                init();
                }
                return _start();
                ''').strip(),
            ),
            (
                textwrap.dedent('''
                static int stop(char *reason) {
                ham = reason;
                return _stop();
                }
                ''').strip(),
                textwrap.dedent('''
                ham = reason;
                return _stop();
                ''').strip(),
            ),
        ]

        stmts = list(iter_global_declarations(lines))

        self.assertEqual(stmts, expected)
Ejemplo n.º 2
0
    def test_bogus(self):
        tests = [
            (
                textwrap.dedent('''
                    int spam;
                    static const char const *eggs;

                    PyObject * start(void) {
                        static int initialized = 0;
                        if (initialized) {
                            initialized = 1;
                            init();
                        }
                        return _start();
                    }

                    char* ham;

                    static int _stop(void) {
                    // missing closing bracket

                    static int stop(char *reason) {
                        ham = reason;
                        return _stop();
                    }
                    '''),
                [
                    (
                        textwrap.dedent('''
                    PyObject * start(void) {
                    static int initialized = 0;
                    if (initialized) {
                    initialized = 1;
                    init();
                    }
                    return _start();
                    }
                    ''').strip(),
                        textwrap.dedent('''
                    static int initialized = 0;
                    if (initialized) {
                    initialized = 1;
                    init();
                    }
                    return _start();
                    ''').strip(),
                    ),
                    # Neither "stop()" nor "_stop()" are here.
                ],
            ),
        ]
        for lines, expected in tests:
            with self.subTest(lines):
                lines = lines.splitlines()

                stmts = list(iter_global_declarations(lines))

                self.assertEqual(stmts, expected)
Ejemplo n.º 3
0
    def test_declaration_multiple_vars(self):
        lines = ['static const int const *spam, *ham=NULL, eggs = 3;']

        stmts = list(iter_global_declarations(lines))

        self.assertEqual(stmts, [
            ('static const int const *spam;', None),
            ('static const int *ham=NULL;', None),
            ('static const int eggs = 3;', None),
        ])
Ejemplo n.º 4
0
    def test_ignore_comments(self):
        tests = [
            ('// msg', None),
            ('// int stmt;', None),
            ('    // ...    ', None),
            ('// /*', None),
            ('/* int stmt; */', None),
            ("""
             /**
              * ...
              * int stmt;
              */
             """, None),
        ]
        for lines, expected in tests:
            with self.subTest(lines):
                lines = lines.splitlines()

                stmts = list(iter_global_declarations(lines))

                self.assertEqual(stmts, [expected] if expected else [])
Ejemplo n.º 5
0
    def test_declarations(self):
        tests = [
            'int spam;',
            'long long spam;',
            'static const int const *spam;',
            'int spam;',
            'typedef int myint;',
            'typedef PyObject * (*unaryfunc)(PyObject *);',
            # typedef struct
            # inline struct
            # enum
            # inline enum
        ]
        for text in tests:
            expected = (text, ' '.join(l.strip() for l in text.splitlines()))
            with self.subTest(lines):
                lines = lines.splitlines()

                stmts = list(iter_global_declarations(lines))

                self.assertEqual(stmts, [expected])
Ejemplo n.º 6
0
    def test_functions(self):
        tests = [
            (
                textwrap.dedent('''
                void func1() {
                    return;
                }
                '''),
                textwrap.dedent('''
                void func1() {
                return;
                }
                ''').strip(),
            ),
            (
                textwrap.dedent('''
                static unsigned int * _func1(
                    const char *arg1,
                    int *arg2
                    long long arg3
                    )
                {
                    return _do_something(arg1, arg2, arg3);
                }
                '''),
                textwrap.dedent('''
                static unsigned int * _func1( const char *arg1, int *arg2 long long arg3 ) {
                return _do_something(arg1, arg2, arg3);
                }
                ''').strip(),
            ),
            (
                textwrap.dedent('''
                static PyObject *
                _func1(const char *arg1, PyObject *arg2)
                {
                    static int initialized = 0;
                    if (!initialized) {
                        initialized = 1;
                        _init(arg1);
                    }

                    PyObject *result = _do_something(arg1, arg2);
                    Py_INCREF(result);
                    return result;
                }
                '''),
                textwrap.dedent('''
                static PyObject * _func1(const char *arg1, PyObject *arg2) {
                static int initialized = 0;
                if (!initialized) {
                initialized = 1;
                _init(arg1);
                }
                PyObject *result = _do_something(arg1, arg2);
                Py_INCREF(result);
                return result;
                }
                ''').strip(),
            ),
        ]
        for lines, expected in tests:
            body = textwrap.dedent(
                expected.partition('{')[2].rpartition('}')[0]).strip()
            expected = (expected, body)
            with self.subTest(lines):
                lines = lines.splitlines()

                stmts = list(iter_global_declarations(lines))

                self.assertEqual(stmts, [expected])
Ejemplo n.º 7
0
    def test_no_statements(self):
        lines = []

        stmts = list(iter_global_declarations(lines))

        self.assertEqual(stmts, [])