def test_include_local_file_with_subdirectory(self):
     other_header = "somedirectory/other.h"
     f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
     handler = FakeHandler({other_header: ["1\n"]})
     ret = simplecpreprocessor.preprocess(f_obj,
                                          header_handler=handler)
     self.assertEqual(list(ret), ["1\n"])
Ejemplo n.º 2
0
def test_ifdef_file_guard():
    other_header = "somedirectory/other.h"
    f_obj = FakeFile("header.h",
                     ['#include "%s"\n' % other_header])
    handler = FakeHandler({other_header: ["1\n"]})
    ret = preprocess(f_obj, header_handler=handler)
    assert "".join(ret) == "1\n"
Ejemplo n.º 3
0
def test_else_left_open_causes_error():
    f_obj = FakeFile("header.h", ["#ifdef FOO\n", "#else\n"])
    with pytest.raises(ParseError) as excinfo:
        "".join(preprocess(f_obj))
    s = str(excinfo.value)
    assert "else" in s
    assert "left open" in s
 def test_platform_constants(self):
     f_obj = FakeFile("header.h", ['#ifdef ODDPLATFORM\n',
                                   'ODDPLATFORM\n', '#endif'])
     const = {"ODDPLATFORM": "ODDPLATFORM"}
     ret = simplecpreprocessor.preprocess(f_obj,
                                          platform_constants=const)
     self.assertEqual(list(ret), ["ODDPLATFORM\n"])
Ejemplo n.º 5
0
def test_platform_undefine():
    with mock.patch(extract_platform_spec_path) as mock_spec:
        mock_spec.return_value = "Linux", "32bit"
        f_obj = FakeFile("header.h", [
            '#undef __i386__\n'
            '__i386__\n', ])
        ret = preprocess(f_obj)
        assert "".join(ret) == "__i386__\n"
Ejemplo n.º 6
0
def test_include_preresolved():
    f_obj = FakeFile("header.h", ['#include <other.h>\n'])
    header = "other.h"
    path = posixpath.join("subdirectory", header)
    handler = FakeHandler({path: ["1\n"]})
    handler.resolved[header] = path
    ret = preprocess(f_obj, header_handler=handler)
    assert "".join(ret) == "1\n"
Ejemplo n.º 7
0
def main(args=None):
    args = parser.parse_args(args)
    with open(args.input_file) as i:
        with open(args.output_file, "w") as o:
            for line in preprocess(i,
                                   include_paths=args.include_paths,
                                   ignore_headers=args.ignore_headers):
                o.write(line)
Ejemplo n.º 8
0
def test_include_local_fallback():
    other_header = "other.h"
    path = "bogus"
    f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
    handler = FakeHandler({"%s/%s" % (path, other_header): ["2\n"]},
                          include_paths=[path])
    ret = preprocess(f_obj, header_handler=handler)
    assert "".join(ret) == "2\n"
 def test_include_missing_local_file(self):
     other_header = os.path.join("somedirectory", "other.h")
     f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
     handler = FakeHandler({})
     with self.assertRaises(simplecpreprocessor.ParseError):
         ret = simplecpreprocessor.preprocess(f_obj,
                                              header_handler=handler)
         list(ret)
Ejemplo n.º 10
0
def test_platform_constants():
    f_obj = FakeFile("header.h", ['#ifdef ODDPLATFORM\n',
                                  'ODDPLATFORM\n', '#endif\n'])
    const = {
        "ODDPLATFORM": [Token.from_string(None, "ODDPLATFORM")]
    }
    ret = preprocess(f_obj, platform_constants=const)
    assert "".join(ret) == "ODDPLATFORM\n"
Ejemplo n.º 11
0
def test_include_with_path_list():
    f_obj = FakeFile("header.h", ['#include <other.h>\n'])
    directory = "subdirectory"
    handler = FakeHandler({posixpath.join(directory,
                                          "other.h"): ["1\n"]})
    include_paths = [directory]
    ret = preprocess(f_obj, include_paths=include_paths,
                     header_handler=handler)
    assert "".join(ret) == "1\n"
 def test_include_with_path_list(self):
     f_obj = FakeFile("header.h", ['#include <other.h>\n'])
     handler = FakeHandler({os.path.join("subdirectory",
                                         "other.h"): ["1\n"]})
     include_paths = ["subdirectory"]
     ret = simplecpreprocessor.preprocess(f_obj,
                                          include_paths=include_paths,
                                          header_handler=handler)
     self.assertEqual(list(ret), ["1\n"])
Ejemplo n.º 13
0
def test_include_with_path_list_with_subdirectory():
    header_file = posixpath.join("nested", "other.h")
    include_path = "somedir"
    f_obj = FakeFile("header.h", ['#include <%s>\n' % header_file])
    handler = FakeHandler({posixpath.join(include_path,
                                          header_file): ["1\n"]})
    include_paths = [include_path]
    ret = preprocess(f_obj, include_paths=include_paths,
                     header_handler=handler)
    assert "".join(ret) == "1\n"
Ejemplo n.º 14
0
def test_ignore_include_path():
    f_obj = FakeFile("header.h", ['#include <other.h>\n'])
    handler = FakeHandler({posixpath.join("subdirectory",
                                          "other.h"): ["1\n"]})
    paths = ["subdirectory"]
    ignored = ["other.h"]
    ret = preprocess(f_obj, include_paths=paths,
                     header_handler=handler,
                     ignore_headers=ignored)
    assert "".join(ret) == ""
 def test_include_with_path_list_with_subdirectory(self):
     header_file = os.path.join("nested", "other.h")
     include_path = "somedir"
     f_obj = FakeFile("header.h", ['#include <%s>\n' % header_file])
     handler = FakeHandler({os.path.join(include_path,
                                         header_file): ["1\n"]})
     include_paths = [include_path]
     ret = simplecpreprocessor.preprocess(f_obj,
                                          include_paths=include_paths,
                                          header_handler=handler)
     self.assertEqual(list(ret), ["1\n"])
 def test_ignore_include_path(self):
     f_obj = FakeFile("header.h", ['#include <other.h>\n'])
     handler = FakeHandler({os.path.join("subdirectory",
                                         "other.h"): ["1\n"]})
     paths = ["subdirectory"]
     ignored = ["other.h"]
     ret = simplecpreprocessor.preprocess(f_obj,
                                          include_paths=paths,
                                          header_handler=handler,
                                          ignore_headers=ignored)
     self.assertEqual(list(ret), [])
Ejemplo n.º 17
0
def test_unexpected_macro_gives_parse_error():
    f_obj = FakeFile("header.h", ["#something_unsupported foo bar\n"])
    with pytest.raises(ParseError):
        "".join(preprocess(f_obj))
Ejemplo n.º 18
0
def test_lines_normalize_custom():
    f_obj = FakeFile("header.h", ["foo\n", "bar\n"])
    expected = "foo\r\nbar\r\n"
    ret = preprocess(f_obj, line_ending="\r\n")
    assert "".join(ret) == expected
 def test_lines_normalize_custom(self):
     f_obj = FakeFile("header.h", ["foo\n", "bar\n"])
     expected_list = ["foo\r\n", "bar\r\n"]
     ret = simplecpreprocessor.preprocess(f_obj,
                                          line_ending="\r\n")
     self.assertEqual(list(ret), expected_list)
Ejemplo n.º 20
0
def test_invalid_include():
    f_obj = FakeFile("header.h", ["#include bogus\n"])
    with pytest.raises(ParseError) as excinfo:
        "".join(preprocess(f_obj))
    assert "Invalid include" in str(excinfo.value)
 def test_ifndef_left_open_causes_error(self):
     f_obj = FakeFile("header.h", ["#ifndef FOO\n"])
     with self.assertRaises(simplecpreprocessor.ParseError):
         list(simplecpreprocessor.preprocess(f_obj))
 def test_unexpected_macro_gives_parse_error(self):
     f_obj = FakeFile("header.h", ["#something_unsupported foo bar\n"])
     with self.assertRaises(simplecpreprocessor.ParseError):
         list(simplecpreprocessor.preprocess(f_obj))
 def test_extra_endif_causes_error(self):
     input_list = ["#endif\n"]
     with self.assertRaises(simplecpreprocessor.ParseError):
         list(simplecpreprocessor.preprocess(input_list))
Ejemplo n.º 24
0
def run_case(input_list, expected):
    ret = preprocess(input_list)
    output = "".join(ret)
    assert output == expected
Ejemplo n.º 25
0
def test_include_missing_local_file():
    other_header = posixpath.join("somedirectory", "other.h")
    f_obj = FakeFile("header.h", ['#include "%s"\n' % other_header])
    handler = FakeHandler({})
    with pytest.raises(ParseError):
        "".join(preprocess(f_obj, header_handler=handler))
Ejemplo n.º 26
0
def test_repeated_macro():
    f_obj = FakeFile("header.h", [
        '#define A value\n'
        'A A\n', ])
    ret = preprocess(f_obj)
    assert "".join(ret) == "value value\n"
Ejemplo n.º 27
0
def test_unsupported_pragma():
    f_obj = FakeFile("header.h", ["#pragma bogus\n"])
    with pytest.raises(ParseError) as excinfo:
        "".join(preprocess(f_obj))
    assert "Unsupported pragma" in str(excinfo.value)
 def run_case(self, input_list, expected_list):
     output_list = list(simplecpreprocessor.preprocess(input_list))
     self.assertEqual(output_list, expected_list)
Ejemplo n.º 29
0
def test_extra_else_causes_error():
    input_list = ["#else\n"]
    with pytest.raises(ParseError) as excinfo:
        "".join(preprocess(input_list))
    assert "Unexpected #else" in str(excinfo.value)