Add ability to generate skeleton from header. woo!

This commit is contained in:
mvandervoord
2020-03-12 12:20:33 -04:00
parent 2eb209b2a8
commit f5abf20f4b
10 changed files with 233 additions and 13 deletions
+23 -7
View File
@@ -282,6 +282,22 @@ You may specify the options explicitly:
cmock = Cmock.new(:plugins => [:cexception, :ignore], :mock_path => 'my/mocks/')
Creating Skeletons:
-------------------
Not only is CMock able to generate mock files from a header file, but it is also able
to generate (and update) skeleton C files from headers. It does this by creating a
(mostly) empty implementation for every function that is declared in the header. If you later
add to that header list, just run this feature again and it will add prototypes for the missing
functions!
Like the normal usecase for CMock, this feature can be used from the command line
or from within its ruby API. For example, from the command line, add `--skeleton` to
generate a skeleton instead:
```
ruby cmock.rb --skeleton ../create/c/for/this.h
```
Config Options:
---------------
@@ -394,14 +410,14 @@ from the defaults. We've tried to specify what the defaults are below.
* default: mocks
* `:mock_prefix`:
The prefix to prepend to your mock files. For example, if it's Mock, a file
The prefix to prepend to your mock files. For example, if it's `Mock`, a file
“USART.h” will get a mock called “MockUSART.c”. This CAN be used with a suffix
at the same time.
* default: Mock
* `:mock_suffix`:
The suffix to append to your mock files. For example, it it's "_Mock", a file
The suffix to append to your mock files. For example, it it's `_Mock`, a file
"USART.h" will get a mock called "USART_Mock.h". This CAN be used with a prefix
at the same time.
@@ -637,8 +653,8 @@ from the defaults. We've tried to specify what the defaults are below.
Parameters must match *both* `:array_size_type` and `:array_size_name` (and
must come right after a pointer parameter) to be treated as an array size.
Once you've told it how to recognize your arrays, CMock will give you _Expect
calls that work more like _ExpectWithArray, and compare an array of objects
Once you've told it how to recognize your arrays, CMock will give you `_Expect`
calls that work more like `_ExpectWithArray`, and compare an array of objects
rather than just a single object.
For example, if you write the following, CMock will check that GoBananas is
@@ -653,14 +669,14 @@ from the defaults. We've tried to specify what the defaults are below.
GoBananas_ExpectWithArray(b, 2, 2);
* `:fail_on_unexpected_calls`:
By default, CMock will fail a test if a mock is called without _Expect and _Ignore
By default, CMock will fail a test if a mock is called without `_Expect` and `_Ignore`
called first. While this forces test writers to be more explicit in their expectations,
it can clutter tests with _Expect or _Ignore calls for functions which are not the focus
it can clutter tests with `_Expect` or `_Ignore` calls for functions which are not the focus
of the test. While this is a good indicator that this module should be refactored, some
users are not fans of the additional noise.
Therefore, :fail_on_unexpected_calls can be set to false to force all mocks to start with
the assumption that they are operating as _Ignore unless otherwise specified.
the assumption that they are operating as `_Ignore` unless otherwise specified.
* default: true
* **note:**
+19 -1
View File
@@ -32,6 +32,12 @@ class CMock
end
end
def setup_skeletons(files)
[files].flatten.each do |src|
generate_skeleton src
end
end
private ###############################
def generate_mock(src)
@@ -39,6 +45,12 @@ class CMock
puts "Creating mock for #{name}..." unless @silent
@cm_generator.create_mock(name, @cm_parser.parse(name, File.read(src)))
end
def generate_skeleton(src)
name = File.basename(src, '.h')
puts "Creating skeleton for #{name}..." unless @silent
@cm_generator.create_skeleton(name, @cm_parser.parse(name, File.read(src)))
end
end
def option_maker(options, key, val)
@@ -75,6 +87,8 @@ if ($0 == __FILE__)
ARGV.each do |arg|
if (arg =~ /^-o\"?([a-zA-Z0-9._\\\/:\s]+)\"?/)
options.merge! CMockConfig.load_config_file_from_yaml( arg.gsub(/^-o/,'') )
elsif (arg == "--skeleton")
options[:skeleton] = true
elsif (arg =~ /^--([a-zA-Z0-9._\\\/:\s]+)=\"?([a-zA-Z0-9._\-\\\/:\s\;]+)\"?/)
options = option_maker(options, $1, $2)
else
@@ -82,5 +96,9 @@ if ($0 == __FILE__)
end
end
CMock.new(options).setup_mocks(filelist)
if (options[:skeleton])
CMock.new(options).setup_skeletons(filelist)
else
CMock.new(options).setup_mocks(filelist)
end
end
+1
View File
@@ -40,6 +40,7 @@ class CMockConfig
:orig_header_include_fmt => "#include \"%s\"",
:array_size_type => [],
:array_size_name => 'size|len',
:skeleton => false,
# Format to look for inline functions.
# This is a combination of "static" and "inline" keywords ("static inline", "inline static", "inline", "static")
+8
View File
@@ -33,6 +33,14 @@ class CMockFileWriter
update_file(full_file_name_done, full_file_name_temp)
end
def append_file(filename, subdir)
raise "Where's the block of data to create?" unless block_given?
full_file_name = "#{@config.mock_path}/#{subdir+'/' if subdir}#{filename}"
File.open(full_file_name, 'a') do |file|
yield(file, filename)
end
end
private ###################################
def update_file(dest, src)
+37 -1
View File
@@ -55,6 +55,11 @@ class CMockGenerator
create_mock_source_file(parsed_stuff)
end
def create_skeleton(module_name, parsed_stuff)
@module_name = module_name
create_skeleton_source_file(parsed_stuff)
end
private if $ThisIsOnlyATest.nil? ##############################
def create_mock_subdir()
@@ -94,6 +99,17 @@ class CMockGenerator
end
end
def create_skeleton_source_file(parsed_stuff)
filename = "#{@config.mock_path}/#{@subdir+'/' if @subdir}#{module_name}.c"
existing = File.exists?(filename) ? File.read(filename) : ""
@file_writer.append_file(@module_name + ".c", @subdir) do |file, filename|
create_source_header_section(file, filename, []) if existing.empty?
parsed_stuff[:functions].each do |function|
create_function_skeleton(file, function, existing)
end
end
end
def create_mock_header_header(file, filename)
define_name = @clean_mock_name.upcase
orig_filename = (@subdir ? @subdir + "/" : "") + @module_name + ".h"
@@ -146,7 +162,7 @@ class CMockGenerator
def create_source_header_section(file, filename, functions)
header_file = (@subdir ? @subdir + '/' : '') + filename.gsub(".c",".h")
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n"
file << "/* AUTOGENERATED FILE. DO NOT EDIT. */\n" unless functions.empty?
file << "#include <string.h>\n"
file << "#include <stdlib.h>\n"
file << "#include <setjmp.h>\n"
@@ -272,4 +288,24 @@ class CMockGenerator
file << @utils.code_add_argument_loader(function)
file << @plugins.run(:mock_interfaces, function)
end
def create_function_skeleton(file, function, existing)
# prepare return value and arguments
function_mod_and_rettype = (function[:modifier].empty? ? '' : "#{function[:modifier]} ") +
(function[:return][:type]) +
(function[:c_calling_convention] ? " #{function[:c_calling_convention]}" : '')
args_string = function[:args_string]
args_string += (", " + function[:var_arg]) unless (function[:var_arg].nil?)
decl = "#{function_mod_and_rettype} #{function[:name]}(#{args_string})"
unless (existing.include?(decl))
file << "#{decl}\n"
file << "{\n"
file << " //TODO: Implement Me!\n"
function[:args].each {|arg| file << " (void)#{arg[:name]};\n"}
file << " return (#{(function[:return][:type])})0;\n" unless (function[:return][:void?])
file << "}\n\n"
end
end
end
+1 -1
View File
@@ -92,7 +92,7 @@ namespace :test do
#individual system tests
FileList['system/test_interactions/*.yml'].each do |test|
basename = File.basename(test,'.*')
desc "Run system test #{basename}"
#desc "Run system test #{basename}"
task basename do
run_system_test_interactions([test])
end
+14 -2
View File
@@ -66,6 +66,7 @@ class SystemTestGenerator
generate_test_file(yaml_hash, namix, name)
generate_source_file(yaml_hash, namix, name)
generate_skeleton_file(yaml_hash, namix, name)
end
def generate_types_file(yaml_hash, namix)
@@ -136,11 +137,22 @@ class SystemTestGenerator
includes << (namix + MOCKABLE_H) if not yaml_hash[:systest][:mockable].nil?
includes << header_file
write_source_file(GENERATED_PATH + name + C_EXTENSION, includes.flatten) do |out|
out.puts(source[:code])
unless (source[:code].nil?)
write_source_file(GENERATED_PATH + name + C_EXTENSION, includes.flatten) do |out|
out.puts(source[:code])
end
end
end
def generate_skeleton_file(yaml_hash, namix, name)
source = yaml_hash[:systest][:skeleton]
return if source.nil?
require 'cmock.rb'
cmock = CMock.new(GENERATED_PATH + namix + 'cmock' + YAML_EXTENSION)
cmock.setup_skeletons("#{$cfg['compiler']['source_path']}#{name}.h")
end
def write_header_file(filename, upcase_name, include_list=[])
File.open(filename, 'w') do |out|
out.puts("#ifndef _#{upcase_name}")
@@ -0,0 +1,54 @@
---
:cmock:
:plugins:
- # none
:systest:
:types: |
#define UINT32 unsigned int
:mockable: |
UINT32 something(int a);
:skeleton: skeleton.h
:source:
:header: |
void function_a(void);
int function_b(int a, int b);
const char* function_c(void);
# we are purposefully not including a :code section because it will be generated with skeleton
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
:units:
- :pass: TRUE
:should: 'generate an empty shell for functions with no return values'
:code: |
test()
{
function_a();
}
- :pass: TRUE
:should: 'return numerical zero for numerical return values'
:code: |
test()
{
TEST_ASSERT_EQUAL_INT(0, function_b(1, 2));
}
- :pass: TRUE
:should: 'return null for pointer return values'
:code: |
test()
{
TEST_ASSERT_NULL(function_c());
}
...
@@ -0,0 +1,75 @@
---
:cmock:
:plugins:
- # none
:systest:
:types: |
#define UINT32 unsigned int
:mockable: |
UINT32 something(int a);
:skeleton: skeleton_update.h
:source:
:header: |
void function_a(void);
int function_b(int a, int b);
const char* function_c(void);
# note that this code section exists and will be updated by the skeleton generator
:code: |
const char* donuts = "donuts";
const char* function_c(void)
{
return donuts;
}
int function_d(void)
{
return 77;
}
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
extern int function_d();
:units:
- :pass: TRUE
:should: 'generate an empty shell for functions with no return values'
:code: |
test()
{
function_a();
}
- :pass: TRUE
:should: 'return numerical zero for numerical return values'
:code: |
test()
{
TEST_ASSERT_EQUAL_INT(0, function_b(1, 2));
}
- :pass: TRUE
:should: 'not overwrite functions that already exist'
:code: |
test()
{
TEST_ASSERT_EQUAL_STRING("donuts", function_c());
}
- :pass: TRUE
:should: 'leave functions it has never heard of'
:code: |
test()
{
TEST_ASSERT_EQUAL_INT(77, function_d());
}
...
+1 -1