Debian Codegraph @

lib/cmock_generator_plugin_callback.rb

raw symbols SCIP
1 # ==========================================
2 # CMock Project - Automatic Mock Generation for C
3 # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
4 # [Released under MIT License. Please refer to license.txt for details]
5 # ==========================================
6
7 class CMockGeneratorPluginCallback
8 attr_accessor :include_count
9 attr_reader :priority
10 attr_reader :config, :utils
11
12 def initialize(config, utils)
13 @config = config
14 @utils = utils
15 @priority = 6
16
17 @include_count = @config.callback_include_count
18 end
19
20 def instance_structure(function)
21 func_name = function[:name]
22 " char #{func_name}_CallbackBool;\n" \
23 " CMOCK_#{func_name}_CALLBACK #{func_name}_CallbackFunctionPointer;\n" \
24 " int #{func_name}_CallbackCalls;\n"
25 end
26
27 def mock_function_declarations(function)
28 func_name = function[:name]
29 return_type = function[:return][:type]
30 action = @config.callback_after_arg_check ? 'AddCallback' : 'Stub'
31 style = (@include_count ? 1 : 0) | (function[:args].empty? ? 0 : 2)
32 styles = ['void', 'int cmock_num_calls', function[:args_string], "#{function[:args_string]}, int cmock_num_calls"]
33 "typedef #{return_type} (* CMOCK_#{func_name}_CALLBACK)(#{styles[style]});\n" \
34 "void #{func_name}_AddCallback(CMOCK_#{func_name}_CALLBACK Callback);\n" \
35 "void #{func_name}_Stub(CMOCK_#{func_name}_CALLBACK Callback);\n" \
36 "#define #{func_name}_StubWithCallback #{func_name}_#{action}\n"
37 end
38
39 def generate_call(function)
40 args = function[:args].map { |m| m[:name] }
41 args << "Mock.#{function[:name]}_CallbackCalls++" if @include_count
42 "Mock.#{function[:name]}_CallbackFunctionPointer(#{args.join(', ')})"
43 end
44
45 def mock_implementation(function)
46 " if (Mock.#{function[:name]}_CallbackFunctionPointer != NULL)\n {\n" +
47 if function[:return][:void?]
48 " #{generate_call(function)};\n }\n"
49 else
50 " cmock_call_instance->ReturnVal = #{generate_call(function)};\n }\n"
51 end
52 end
53
54 def mock_implementation_precheck(function)
55 " if (!Mock.#{function[:name]}_CallbackBool &&\n" \
56 " Mock.#{function[:name]}_CallbackFunctionPointer != NULL)\n {\n" +
57 if function[:return][:void?]
58 " #{generate_call(function)};\n" \
59 " UNITY_CLR_DETAILS();\n" \
60 " return;\n }\n"
61 else
62 " #{function[:return][:type]} cmock_cb_ret = #{generate_call(function)};\n" \
63 " UNITY_CLR_DETAILS();\n" \
64 " return cmock_cb_ret;\n }\n"
65 end
66 end
67
68 def mock_interfaces(function)
69 func_name = function[:name]
70 has_ignore = @config.plugins.include? :ignore
71 lines = ''
72 lines << "void #{func_name}_AddCallback(CMOCK_#{func_name}_CALLBACK Callback)\n{\n"
73 lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if has_ignore
74 lines << " Mock.#{func_name}_CallbackBool = (char)1;\n"
75 lines << " Mock.#{func_name}_CallbackFunctionPointer = Callback;\n}\n\n"
76 lines << "void #{func_name}_Stub(CMOCK_#{func_name}_CALLBACK Callback)\n{\n"
77 lines << " Mock.#{func_name}_IgnoreBool = (char)0;\n" if has_ignore
78 lines << " Mock.#{func_name}_CallbackBool = (char)0;\n"
79 lines << " Mock.#{func_name}_CallbackFunctionPointer = Callback;\n}\n\n"
80 end
81
82 def mock_verify(function)
83 func_name = function[:name]
84 " if (Mock.#{func_name}_CallbackFunctionPointer != NULL)\n {\n" \
85 " call_instance = CMOCK_GUTS_NONE;\n" \
86 " (void)call_instance;\n }\n"
87 end
88 end