Exemplo n.º 1
0
def test_basic_func(function_cursors):
    func = SUT.Function(function_cursors[0])
    assert 'basic_function' == func.name
    assert 'int' == func.result_type
    assert ['int a', 'int b'] == [str(arg) for arg in func.arguments]
    assert 'int basic_function( int a, int b )' == str(func)

    func1 = SUT.Function(function_cursors[1])
    assert 'simple_user_defined_type_function' == func1.name
    assert 'my_type_t' == func1.result_type
    assert ['const char *arg1',
            'const my_type_t *arg2'] == [str(arg) for arg in func1.arguments]
    assert 'my_type_t simple_user_defined_type_function( const char *arg1, const my_type_t *arg2 )' == str(
        func1)

    func2 = SUT.Function(function_cursors[2])
    assert 'simple_user_defined_type_function2' == func2.name
    assert 'const my_type_t *' == func2.result_type
    assert [] == [str(arg) for arg in func2.arguments]
    assert 'const my_type_t *simple_user_defined_type_function2()' == str(
        func2)

    func3 = SUT.Function(function_cursors[3])
    assert 'void variadic_function( int arg1, ... )' == str(func3)

    func4 = SUT.Function(function_cursors[4])
    assert 'my_type my_function( const char *arg1, const my_type2 *arg2 )' == str(
        func4)
Exemplo n.º 2
0
def test_stub_file(function_cursors, gmock4c_config):
    header_file_name = 'test_header.hpp'
    stubs_file = Stub.StubsFile(gmock4c_config, header_file_name, header_file_name)
    for f in function_cursors:
        func = AST.Function(f)
        stub = Stub.StubFunction(func, gmock4c_config)
        stubs_file.append_stub_function(stub)

    stubs_file.write()
Exemplo n.º 3
0
def test_mock_files(function_cursors, gmock4c_config):
    mock = Mock.Mock(gmock4c_config)
    mock.add_header("header.hpp")
    for f in function_cursors:
        func = AST.Function(f)
        mock_method = Mock.MockMethod(func)
        mock.append_mock_method(str(mock_method))

    mock.write_header()
    mock.write_src()
Exemplo n.º 4
0
def test_mock_methods(function_cursors):
    func = AST.Function(function_cursors[0])
    mock_method = Mock.MockMethod(func)
    assert str(
        mock_method) == 'MOCK_METHOD2( basic_function, int( int a, int b ) );'

    func1 = AST.Function(function_cursors[1])
    mock_method1 = Mock.MockMethod(func1)
    assert str(
        mock_method1
    ) == 'MOCK_METHOD2( simple_user_defined_type_function, my_type_t( const char *arg1, const my_type_t *arg2 ) );'

    func2 = AST.Function(function_cursors[2])
    mock_method2 = Mock.MockMethod(func2)
    assert str(
        mock_method2
    ) == 'MOCK_METHOD0( simple_user_defined_type_function2, const my_type_t*() );'

    # This is a variadic function --> we do not mock this.
    func3 = AST.Function(function_cursors[3])
    mock_method3 = Mock.MockMethod(func3)
    assert str(mock_method3) == ''
Exemplo n.º 5
0
        def parse_node(node, stubs):
            # NOTE: the parsed header was precompiled (i.e., it includes the content of files of '#include', so we need
            # to filter and process only functions declared inside the current header)
            if node.kind != Clang.CursorKind.TRANSLATION_UNIT \
                    and (node.location.file is None or not node.location.file.name.endswith(f)):
                return

            if node.kind == Clang.CursorKind.FUNCTION_DECL:
                func = GMockAST.Function(node)
                stub_function = GMockStub.StubFunction(func, self._config)
                mock_method = GMock.MockMethod(func)
                self._mock.append_mock_method(str(mock_method))
                stubs.append_stub_function(stub_function)
                return

            for child in [c for c in node.get_children()]:
                parse_node(child, stubs)
Exemplo n.º 6
0
def test_stub_methods(function_cursors, gmock4c_config):
    func = AST.Function(function_cursors[0])
    stub = Stub.StubFunction(func, gmock4c_config)
    expected_result = """
int basic_function( int a, int b ) {
    return MyClassMock::getInstance()->basic_function( a, b );
}
"""
    assert str(stub) == expected_result

    func = AST.Function(function_cursors[1])
    stub = Stub.StubFunction(func, gmock4c_config)
    expected_result = """
my_type_t simple_user_defined_type_function( const char *arg1, const my_type_t *arg2 ) {
    return MyClassMock::getInstance()->simple_user_defined_type_function( arg1, arg2 );
}
"""
    assert str(stub) == expected_result

    func = AST.Function(function_cursors[2])
    stub = Stub.StubFunction(func, gmock4c_config)
    expected_result = """
const my_type_t *simple_user_defined_type_function2() {
    return MyClassMock::getInstance()->simple_user_defined_type_function2();
}
"""
    assert str(stub) == expected_result

    func = AST.Function(function_cursors[3])
    stub = Stub.StubFunction(func, gmock4c_config)
    assert str(stub) == "\nvoid variadic_function( int arg1, ... ) {\n}\n"

    func = AST.Function(function_cursors[4])
    stub = Stub.StubFunction(func, gmock4c_config)
    expected_result = """
my_type my_function( const char *arg1, const my_type2 *arg2 ) {
    return MyClassMock::getInstance()->my_function( arg1, arg2 );
}
"""
    assert str(stub) == expected_result

    func = AST.Function(function_cursors[5])
    stub = Stub.StubFunction(func, gmock4c_config)
    expected_result = """
void my_void_function( int a ) {
     MyClassMock::getInstance()->my_void_function( a );
}
"""
    assert str(stub) == expected_result
Exemplo n.º 7
0
def test_wrong_cursor_type(translation_unit_cursor):
    with pytest.raises(TypeError) as pytest_wrapped_e:
        SUT.Function(translation_unit_cursor)
    assert TypeError == pytest_wrapped_e.type