Exemplo n.º 1
0
 def test_read_onPredefinedMacroDict_doesNotModifyPredefinedMacroDict(
         self, tmpdir):
     c_file = tmpdir.join('predef_macro.c')
     c_file.write_binary(b'#define B')
     predef_macros = dict(A=1)
     parser = CParser(predef_macros)
     parser.read(Path(c_file))
     assert predef_macros == dict(A=1)
Exemplo n.º 2
0
 def test_read_onAdditionalIncludeDirs_searchIncludeDirs(self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include "test.h"',
             'sub dir': {
                 'test.h': b'int func(void);'
             }
         })
     parser = CParser(include_dirs=[basedir / 'sub dir'])
     parser.read(basedir / 'test.c')
     assert 'func' in parser.funcs
Exemplo n.º 3
0
 def test_read_onIncludes_addsIncludeFileNamesToSourceFiles(self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'source.c': b'#include "include_1.h"',
             'include_1.h': b'#include "include_2.h"',
             'include_2.h': b''
         })
     parser = CParser()
     parser.read(basedir / 'source.c')
     assert parser.source_files \
            == { basedir / 'source.c',
                 basedir / 'include_1.h',
                 basedir / 'include_2.h'}
Exemplo n.º 4
0
 def test_read_onSystemIncludeDirs_searchPassedSysIncludeDirsAndIgnoreFuncsAndVarsAndSourceFiles(
         self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include <test.h>',
             'sys-incl': {
                 'test.h': b'int func(void);\n'
                 b'extern int var;'
             }
         })
     parser = CParser(sys_include_dirs=[basedir / 'sys-incl'])
     parser.read(basedir / 'test.c')
     assert 'func' not in parser.funcs
     assert 'var' not in parser.vars
     assert parser.source_files == {basedir / 'test.c'}
Exemplo n.º 5
0
 def test_read_onWhitelistedSystemHeaderEntries_doNotIgnore(self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include <test.h>\n',
             'sys-incl': {
                 'test.h':
                 b'struct strct { };\n'
                 b'#define MACRO\n'
                 b'void func(void);\n'
             }
         })
     parser = CParser(sys_include_dirs=[basedir / 'sys-incl'],
                      sys_whitelist=['strct', 'MACRO', 'func'])
     parser.read(basedir / 'test.c')
     assert 'strct' in parser.structs
     assert 'MACRO' in parser.macros
     assert 'func' in parser.funcs
Exemplo n.º 6
0
 def parse(cls,
           content,
           patches=None,
           sys_include_dirs=None,
           target_compiler=None,
           **predef_macros):
     parser = CParser(predef_macros, [],
                      sys_include_dirs,
                      target_compiler=target_compiler)
     fileobj = NamedTemporaryFile(suffix='.c', delete=False, mode='w+t')
     try:
         fileobj.write(content)
         fileobj.close()
         parser.read(fileobj.name, patches)
     finally:
         os.remove(fileobj.name)
     return parser
Exemplo n.º 7
0
 def test_read_onSystemHeaderFile_ignoresAllEntriesExceptRequiredTypes(
         self, tmpdir):
     basedir = build_tree(
         tmpdir, {
             'test.c': b'#include <test.h>\n'
             b'void func(req_type * param);\n',
             'sys-incl': {
                 'test.h':
                 b'typedef struct req_struct { } req_type;\n'
                 b'typedef struct not_req_struct { } not_req_type;\n'
                 b'void sys_func(not_req_type * param);\n'
             }
         })
     parser = CParser(sys_include_dirs=[basedir / 'sys-incl'])
     parser.read(basedir / 'test.c')
     assert 'req_type' in parser.typedefs, \
         'a type required by module under test was not parsed'
     assert 'not_req_type' not in parser.typedefs, \
         'a not required by module under test was parsed'
     assert 'sys_func' not in parser.funcs, \
         'a system function was parsed'
Exemplo n.º 8
0
 def test_read_onNotExistingFile_raisesFileNotFoundError(self):
     parser = CParser()
     with pytest.raises(FileNotFoundError):
         parser.read('invalid-file-name')
Exemplo n.º 9
0
 def test_read_onNotExistinFile_raisesFileNotFoundError(self):
     with pytest.raises(FileNotFoundError):
         parser = CParser()
         parser.read('not_existing_file.c')