Exemplo n.º 1
0
def make_empty_test_suite(filename):
    cpp = CppFile()
    file = open(filename)
    parser = CPPParser(file)
    parser.detect_methods()
    file.close()

    # Generate Tests
    for i in range(len(parser.methods)):
        cpp.add_component(
            make_empty_test(parser.detected_class_name,
                            parser.methods[i].name + str(i)))

    cpp.add_include("iostream")
    cpp.add_include("gtest/gtest.h")
    cpp.add_include("\"" + filename + "\"")

    # Generates and adds the main for running tests, could make this a separate function
    run_tests = Function("int", "main", "int argc", "char **argv")
    run_tests.add_statement("testing::InitGoogleTest(&argc, argv)")
    run_tests.add_return("RUN_ALL_TESTS()")

    cpp.add_component(run_tests)

    return cpp
def find_class_file(class_name):
    if not is_cpp_keyword(class_name):
        if class_file_exists(class_name + ".h"):
            return class_name + ".h"
        elif class_file_exists(class_name + ".cpp"):
            return class_name + ".cpp"
        else:
            for root, dirs, files in os.walk("."):
                for file in files:
                    if file.endswith(".cpp") or file.endswith(".h"):
                        par = CPPParser(open(file))
                        par.ensure_class_detected()
                        if par.detected_class is not None:
                            if par.detected_class_name == class_name:
                                return file
    return None
Exemplo n.º 3
0
def make_full_file(filename, transfer_class_to_new_file=False):
    cpp = CppFile()
    file = open(filename)
    parser = CPPParser(file)
    parser.detect_methods()
    file.close()

    # Generate mock class
    mc = create_mock_class(parser)

    # Generate Tests
    for i in range(len(parser.methods)):
        cpp.add_component(
            make_empty_test(parser.detected_class_name,
                            parser.methods[i].name + str(i)))

    cpp.add_include("iostream")
    cpp.add_include("gtest/gtest.h")
    cpp.add_include("\"gmock/gmock.h\"")

    # Adds class to file or includes class it came from
    # leaving this functionality out for now, can cause complications that we cannot fix yet
    # if transfer_class_to_new_file:
    #    cpp.add_component(parser)
    #    includes = find_every_include(filename, delete_keyword=True)
    #    for i in includes:
    #        if ("iostream" not in i) and ("gtest/gtest.h" not in i):
    #           cpp.add_include(i)
    # else:
    cpp.add_include("\"" + filename + "\"")

    cpp.add_component(mc.get_class())

    # Generates and adds the main for running tests, could make this a separate function
    run_tests = Function("int", "main", "int argc", "char **argv")
    run_tests.add_statement("testing::InitGoogleTest(&argc, argv)")
    run_tests.add_return("RUN_ALL_TESTS()")

    cpp.add_component(run_tests)

    return cpp
def create_mock_class_from_file(file_obj, write_to_disk=True):
    # parse file
    parser = CPPParser(file_obj)
    parser.detect_methods()

    # create mock class
    mock_class = MockClass(parser.detected_class_name,
                           inherits=parser.has_virtual_method())
    for m in parser.methods:
        params = [] if not m.params else m.params
        p = []
        for param in params:
            # have to index the first one because param is
            # of the form [type name]
            mock_user_defined_type(param[0])
            if ',' in param[0]:
                temp = ["("]
                for i in param:
                    temp.append(i)
                temp.append(")")
                p.append(temp)
            else:
                p.append(param)
        mock_user_defined_type(m.return_type, write_to_disk=write_to_disk)
        if ',' in m.return_type:
            mock_class.add_mock_method("(" + m.return_type + ")", m.name, p,
                                       m.is_virtual, m.is_constant)
        else:
            mock_class.add_mock_method(m.return_type, m.name, p, m.is_virtual,
                                       m.is_constant)

    if write_to_disk:
        mock_file = CppFile()
        mock_file.add_component(mock_class.get_class())
        mock_file.add_include("gmock/gmock.h")
        mock_file.add_include("\"" + file_obj.name + "\"")
        mock_file.write_to_file(mock_class.name)

    return mock_class
Exemplo n.º 5
0
from cpp_parser import CPPParser

file_path = r'geeks.cpp'
file = open(file_path, 'r')
parser = CPPParser(file)
parser.detect_methods()

print('Detected {} methods in the class\n'.format(len(parser.methods)))

for m in parser.methods:
    print('Info for method {}:'.format(m.name))
    print('Return type: {}'.format(m.return_type))
    print('Is virtual: {}'.format(m.is_virtual))
    print('Is constant: {}'.format(m.is_constant))
    print('Parameters: {}'.format(m.params))
    print('')

# Output:

# Detected 2 methods in the class
#
# Info for method printName:
# Return type: void
# Is virtual: False
# Is constant: True
# Parameters: None
#
# Info for method doNothing:
# Return type: int
# Is virtual: False
# Is constant: False
def parse_cpp_file(file_obj):
    return CPPParser(file_obj)
Exemplo n.º 7
0
from cpp_parser import CPPParser

fileobj = open('geeks.cpp')
parser = CPPParser(fileobj)
parser.detect_public_methods()
parser.print_detected_method_info(parser.public_methods)