def test_shouldWriteMultipleFunctions(self):
        # Given
        mockgen = MagicMock()
        mockgen.mocks = [
            MockInfo(mockname = "mock_func1",
                     funcname = "func1",
                     prototype = "void func1(void)",
                     return_text = "void",
                     return_hint = ReturnHint.VOID,
                     args_info = []),
            MockInfo(mockname = "mock_func2",
                     funcname = "func2",
                     prototype = "void func2(void)",
                     return_text = "void",
                     return_hint = ReturnHint.VOID,
                     args_info = []),
        ]
        mock_c_writer = MockImplementationWriter(self.mockpaths, mockgen)

        # When
        mock_c_writer.write_implementation(self.cBuffer)

        # Then
        self.assertEqual(
            self.cBuffer.getvalue(),
            '#include "mock-mockable.h"\n'
            + boilerplate_includes
            + """pfstest_mock_define(mock_func1, "func1", 0);
void func1(void)
{
    pfstest_value_t *__pfstest_return_value =
        pfstest_mock_invoke(mock_func1,
                            NULL);

    (void)__pfstest_return_value;
}

pfstest_mock_define(mock_func2, "func2", 0);
void func2(void)
{
    pfstest_value_t *__pfstest_return_value =
        pfstest_mock_invoke(mock_func2,
                            NULL);

    (void)__pfstest_return_value;
}

"""
        )
    def test_shouldWritePointerParameter(self):
        # Given
        mockgen = MagicMock()
        mockgen.mocks = [
            MockInfo(mockname = "mock_func1",
                     funcname = "func1",
                     prototype = "void func1(char *__pfstest_arg_0)",
                     return_text = "void",
                     return_hint = ReturnHint.VOID,
                     args_info = [ArgInfo('__pfstest_arg_0',
                                          ArgHint.POINTER)]
            )]
        mock_c_writer = MockImplementationWriter(self.mockpaths, mockgen)

        # When
        mock_c_writer.write_implementation(self.cBuffer)

        # Then
        self.assertEqual(
            self.cBuffer.getvalue(),
            '#include "mock-mockable.h"\n'
            + boilerplate_includes
            + """pfstest_mock_define(mock_func1, "func1", 1);
void func1(char *__pfstest_arg_0)
{
    pfstest_value_t *__pfstest_return_value =
        pfstest_mock_invoke(mock_func1,
                            NULL,
                            pfstest_the_pointer(__pfstest_arg_0));

    (void)__pfstest_return_value;
}

"""
        )
    def test_shouldHandlePointerReturnTypes(self):
        # Given
        mockgen = MagicMock()
        mockgen.mocks = [
            MockInfo(mockname = "mock_func1",
                     funcname = "func1",
                     prototype = "char *func1(void)",
                     return_text = "char *",
                     return_hint = ReturnHint.POINTER,
                     args_info = [])
        ]
        mock_c_writer = MockImplementationWriter(self.mockpaths, mockgen)

        # When
        mock_c_writer.write_implementation(self.cBuffer)

        # Then
        self.assertEqual(
            self.cBuffer.getvalue(),
            '#include "mock-mockable.h"\n'
            + boilerplate_includes
            + """pfstest_mock_define(mock_func1, "func1", 0);
char *func1(void)
{
    char * __pfstest_default_return = NULL;

    pfstest_value_t *__pfstest_return_value =
        pfstest_mock_invoke(mock_func1,
                            pfstest_the_pointer(__pfstest_default_return));

    return (char *)pfstest_value_data(__pfstest_return_value);
}

"""
        )
Ejemplo n.º 4
0
    def test_shouldWriteHeader(self):
        # Given
        mockpaths = MagicMock()
        mockpaths.headerrelpath = "mockable.h"
        mockpaths.guardmacro = "MOCK_MOCKABLE_H"
        mockgen = MagicMock()
        mockgen.mocks = [
            MockInfo(mockname="mock_func1",
                     funcname="",
                     prototype="",
                     return_text="",
                     return_hint="",
                     args_info=[]),
            MockInfo(mockname="mock_func2",
                     funcname="",
                     prototype="",
                     return_text="",
                     return_hint="",
                     args_info=[])
        ]
        mock_h_writer = MockHeaderWriter(mockpaths, mockgen)

        # When
        mock_h_writer.write_header(self.hBuffer)

        # Then
        self.assertEqual(
            self.hBuffer.getvalue(), """#ifndef MOCK_MOCKABLE_H
#define MOCK_MOCKABLE_H

#include "mockable.h"

#include "pfstest-mock.h"

pfstest_mock_declare(mock_func1);
pfstest_mock_declare(mock_func2);

#endif /* !defined(MOCK_MOCKABLE_H) */
""")
Ejemplo n.º 5
0
 def test_shouldGenerateMultipleMocks(self):
     # Given
     mgen = MockGenerator(
         self.mpaths, cgen,
         cparser.parse("void func1(void);" + "void func2(void);",
                       defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="void func1(void)",
                  return_text="void",
                  return_hint=ReturnHint.VOID,
                  args_info=[]),
         MockInfo(mockname="mock_func2",
                  funcname="func2",
                  prototype="void func2(void)",
                  return_text="void",
                  return_hint=ReturnHint.VOID,
                  args_info=[])
     ])
Ejemplo n.º 6
0
 def test_shouldHandleSimplePrimitiveReturnType(self):
     # Given
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse("int func1(void);", defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="int func1(void)",
                  return_text="int",
                  return_hint=ReturnHint.PRIMITIVE,
                  args_info=[])
     ])
Ejemplo n.º 7
0
 def test_shouldHandlePointerReturnType(self):
     # Given
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse("char *func1(void);", defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="char *func1(void)",
                  return_text="char *",
                  return_hint=ReturnHint.POINTER,
                  args_info=[])
     ])
Ejemplo n.º 8
0
 def test_shouldHandleCompoundTypeSpecifierInReturnType(self):
     # Given
     mgen = MockGenerator(
         self.mpaths, cgen,
         cparser.parse("unsigned long int func1(void);", defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks,
                      [MockInfo(mockname = "mock_func1",
                                funcname = "func1",
                                prototype = \
                                "unsigned long int func1(void)",
                                return_text = "unsigned long int",
                                return_hint = ReturnHint.PRIMITIVE,
                                args_info = [])
                      ])
Ejemplo n.º 9
0
 def test_shouldHandlePrimitiveParam(self):
     # Given
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse("void func1(int);", defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks,
                      [MockInfo(mockname = "mock_func1",
                                funcname = "func1",
                                prototype = \
                                "void func1(int __pfstest_arg_0)",
                                return_text = "void",
                                return_hint = ReturnHint.VOID,
                                args_info = [ArgInfo("__pfstest_arg_0",
                                                     ArgHint.BLOB)])
                      ])
Ejemplo n.º 10
0
 def test_shouldSkipDuplicateFunctionDeclarations(self):
     source = """
         void func1(void);
         void func1(void);
     """
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse(source, defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="void func1(void)",
                  return_text="void",
                  return_hint=ReturnHint.VOID,
                  args_info=[])
     ])
Ejemplo n.º 11
0
 def test_shouldUseTypedefsForReturnHints(self):
     # Given
     source = """
         typedef char *charp;
         charp func1(void);
     """
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse(source, defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="charp func1(void)",
                  return_text="charp",
                  return_hint=ReturnHint.POINTER,
                  args_info=[])
     ])
Ejemplo n.º 12
0
 def test_shouldIgnoreFuncDeclsFromIncludedFiles(self):
     # Given
     source = """
         # 1 "another-header.h"
         int func2(int);
         # 1 "../mockable.h"
         void func1(void);
     """
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse(source, defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="void func1(void)",
                  return_text="void",
                  return_hint=ReturnHint.VOID,
                  args_info=[])
     ])
Ejemplo n.º 13
0
 def test_shouldUseTypedefsForArgHints(self):
     # Given
     source = """
         typedef char *charp;
         void func1(charp cp);
     """
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse(source, defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks,
                      [MockInfo(mockname = "mock_func1",
                                funcname = "func1",
                                prototype = \
                                "void func1(charp __pfstest_arg_0)",
                                return_text = "void",
                                return_hint = ReturnHint.VOID,
                                args_info = [ArgInfo("__pfstest_arg_0",
                                                     ArgHint.POINTER)])
                      ])
    def test_shouldHandleStructReturnTypes(self):
        # Given
        mockgen = MagicMock()
        mockgen.mocks = [
            MockInfo(mockname = "mock_func1",
                     funcname = "func1",
                     prototype = "struct foo func1(void)",
                     return_text = "struct foo",
                     return_hint = ReturnHint.BLOB,
                     args_info = [])
        ]
        mock_c_writer = MockImplementationWriter(self.mockpaths, mockgen)

        # When
        mock_c_writer.write_implementation(self.cBuffer)

        # Then
        self.assertEqual(
            self.cBuffer.getvalue(),
            '#include "mock-mockable.h"\n'
            + boilerplate_includes
            + """pfstest_mock_define(mock_func1, "func1", 0);
struct foo func1(void)
{
    struct foo __pfstest_default_return;
    memset(&__pfstest_default_return, 0, sizeof(__pfstest_default_return));

    pfstest_value_t *__pfstest_return_value =
        pfstest_mock_invoke(mock_func1,
                            pfstest_the_memory(
                                &__pfstest_default_return,
                                sizeof(__pfstest_default_return)));

    return *(struct foo *)pfstest_value_data(__pfstest_return_value);
}

"""
        )
Ejemplo n.º 15
0
 def test_shouldIgnoreNonFunctionDeclarations(self):
     # Given
     source = """
         extern int i;
         struct foo {int a; int b;};
         enum bar {A, B};
         union baz {int A; char B;};
         inline int ifunc(void) { return 0; }
         void func1(void);
     """
     mgen = MockGenerator(self.mpaths, cgen,
                          cparser.parse(source, defaulthname))
     # When
     mocks = mgen.mocks
     # Then
     self.assertEqual(mocks, [
         MockInfo(mockname="mock_func1",
                  funcname="func1",
                  prototype="void func1(void)",
                  return_text="void",
                  return_hint=ReturnHint.VOID,
                  args_info=[])
     ])