Files
CMock/lib/cmock_generator_plugin_cexception.rb
2026-07-07 17:55:56 -04:00

60 lines
2.4 KiB
Ruby

# =========================================================================
# CMock - Automatic Mock Generation for C
# ThrowTheSwitch.org
# Copyright (c) 2007-26 Mike Karlesky, Mark VanderVoord, & Greg Williams
# SPDX-License-Identifier: MIT
# =========================================================================
class CMockGeneratorPluginCexception
attr_reader :priority, :config, :utils
def initialize(config, utils)
@config = config
@utils = utils
@priority = 7
raise 'Error: cexception is not supported without setjmp support' if @config.exclude_setjmp_h
end
def include_files
"#include \"CException.h\"\n"
end
def instance_typedefs(_function)
" CEXCEPTION_T ExceptionToThrow;\n"
end
def mock_function_declarations(function)
if function[:args_string] == 'void'
"#define #{function[:name]}_ExpectAndThrow(cmock_to_throw) #{function[:name]}_CMockExpectAndThrow(__LINE__, cmock_to_throw)\n" \
"void #{function[:name]}_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, CEXCEPTION_T cmock_to_throw);\n"
else
"#define #{function[:name]}_ExpectAndThrow(#{function[:args_call]}, cmock_to_throw) #{function[:name]}_CMockExpectAndThrow(__LINE__, #{function[:args_call]}, cmock_to_throw)\n" \
"void #{function[:name]}_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, #{helper_args_string(function)}, CEXCEPTION_T cmock_to_throw);\n"
end
end
def mock_implementation(_function)
" if (cmock_call_instance->ExceptionToThrow != CEXCEPTION_NONE)\n {\n" \
" UNITY_CLR_DETAILS();\n" \
" Throw(cmock_call_instance->ExceptionToThrow);\n }\n"
end
def mock_interfaces(function)
arg_insert = function[:args_string] == 'void' ? '' : "#{helper_args_string(function)}, "
["void #{function[:name]}_CMockExpectAndThrow(UNITY_LINE_TYPE cmock_line, #{arg_insert}CEXCEPTION_T cmock_to_throw)\n{\n",
@utils.code_add_base_expectation(function[:name]),
@utils.code_call_argument_loader(function),
" cmock_call_instance->ExceptionToThrow = cmock_to_throw;\n",
"}\n\n"].join
end
private
def helper_args_string(function)
return function[:args_string] if function[:args_string] == 'void'
return function[:args_string] unless function[:args]&.any? { |m| m.is_a?(Hash) && m[:array_dims] }
function[:args].map { |m| CMockGeneratorUtils.arg_declaration(m) }.join(', ')
end
end