def create_mock_class_new(class_name, methods, write_to_disk=True):
    mock_class = MockClass(class_name)
    for m in 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.write_to_file(mock_class.name)
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
Example #3
0
from cpp_gen import CppFile, CppClass

cpp = CppFile()

# create class
my_class = CppClass('MyClass')
my_class_public = my_class.add_public_specifier()
my_class_public.add_comment('You could put constructors here')
my_class_public.add_comment('You could put public methods here')
my_class_private = my_class.add_private_specifier()
my_class_private.add_comment('You could put private methods here')

# add methods
cpp.add_component(my_class)
print(cpp.generate())

# output (note this does not compile):

# class MyClass {
# public:
# 	// You could put constructors here
# 	// You could put public methods here
# private:
# 	// You could put private methods here
# };

    num_methods = int(input("Enter the amount of methods the class has: "))

    for i in range(num_methods):
        mname = input("Enter the name of method " + str(i) + ": ")
        rt = input("Enter the return type of method " + mname + ": ")

        v = input("Is the method virtual? (Y/N): ")
        if v.lower() == "y":
            virt = True
        else:
            virt = False

        c = input("Is the method const? (Y/N): ")
        if c.lower() == "y":
            con = True
        else:
            con = False

        num_params = int(
            input("Enter the number of parameters for the method: "))
        params = []
        for j in range(num_params):
            params.append(input("Enter parameter " + str(j) + ": "))
        mc.add_mock_method(rt, mname, params, virtual=virt, const=con)

    return mc


cpp = CppFile()
cpp.add_component(new_mock_class_manual().get_class())
print(cpp.generate())
Example #5
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
Example #6
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 start_step_through_format(mock_class_name, methods):
    test_suite_name = input('Enter test suite name: ')

    # start creation of file
    cpp = CppFile()

    # add includes
    cpp.add_include('iostream')
    cpp.add_include('gtest/gtest.h')
    cpp.add_include("{}.cpp".format(mock_class_name))

    # create a test for each method
    for m in methods:
        if input('Do you want to test the method {}? (y/n)'.format(m.name)) == "y":
            test_name = input("Enter name of test: ")

            test = MacroFunction('TEST', test_suite_name, test_name)
            statements = {
                'ASSERT_TRUE': test.add_assert_true,
                'ASSERT_FALSE': test.add_assert_false,
                'ASSERT_EQ': test.add_assert_eq,
                'ASSERT_NE': test.add_assert_ne,
                'ASSERT_LE': test.add_assert_le,
                'ASSERT_GT': test.add_assert_gt,
                'ASSERT_GE': test.add_assert_ge,
                'EXPECT_TRUE': test.add_expect_true,
                'EXPECT_FALSE': test.add_expect_false,
                'EXPECT_EQ': test.add_expect_eq,
                'EXPECT_NE': test.add_expect_ne,
                'EXPECT_LE': test.add_expect_le,
                'EXPECT_GT': test.add_expect_gt,
                'EXPECT_GE': test.add_expect_ge
            }

            entering_statements = True
            while entering_statements:
                print('Would you like to include any of the following statements?')
                print('Enter -1 for none of these')
                for index, k in enumerate(statements):
                    print('{}. {}'.format(index, k))
                response = int(input())
                if response == -1:
                    entering_statements = False
                else:
                    statements[list(statements.keys())[response]]()

            cpp.add_component(test)

    # end with creating main function and write to file
    main_function = Function('int', 'main', 'int argc', 'char **argv')
    main_function.add_function_call('InitGoogleTest', '&argc', 'argv',
                                    namespace='testing')
    main_function.add_run_all_tests_and_return()
    cpp.add_component(main_function)
    cpp.write_to_file(test_suite_name)
Example #8
0
from mockclass_gen import MockClass
from cpp_gen import CppFile

cpp = CppFile()

mc = MockClass("TestClass")

mc.add_mock_method("bool",
                   "CheckOne", ["bool", "int", "String"],
                   virtual=False,
                   const=False)
mc.add_mock_method("int", "CheckTwo", ["float"], virtual=True, const=False)
mc.add_mock_method("void", "DoMath", ["int", "int"], virtual=False, const=True)

cpp.add_component(mc.get_class())
cpp.write_to_file('mock_class_example.cpp')

# generates:
# class MOCK_TestClass {
# public:
#   MOCK_METHOD(bool, CheckOne, (bool, int, String));
#   MOCK_METHOD(int, CheckTwo, (float), (override));
#   MOCK_METHOD(void, DoMath, (int, int)(const));
#   MOCK_METHOD(PacketStream*, Connect, (), (override, const));
# };
from cpp_gen import CppFile, Function, MacroFunction

cpp = CppFile()

# add includes
cpp.add_include('iostream')
cpp.add_include('gtest/gtest.h')
cpp.add_namespace('std')

# test definition
test_one = MacroFunction('TEST', 'MyTestSuite', 'TestOne')
test_one.add_assert_eq(1, 1)

# main function definition
main_function = Function('int', 'main', 'int argc', 'char **argv')
main_function.add_function_call('InitGoogleTest',
                                '&argc',
                                'argv',
                                namespace='testing')
main_function.add_run_all_tests_and_return()

# add methods
cpp.add_component(test_one)
cpp.add_component(main_function)
cpp.write_to_file('simple_test_suite')
Example #10
0
from cpp_gen import CppFile, Function

func_1 = Function('int', 'main')
func_1.add_comment('this is a comment')
func_1.add_cout('Hello world!')

cpp = CppFile()
cpp.add_include('iostream')
cpp.add_namespace('std')
cpp.add_component(func_1)
cpp.write_to_file('hello_world')