mirror of
https://github.com/ThrowTheSwitch/CMock.git
synced 2026-07-28 06:47:48 +00:00
Add unit tests for normalize_source
- Refactor normalize_source to do the minimum amount that is necessary. For now, this is just removing inline functions.
This commit is contained in:
@@ -56,14 +56,6 @@ class CMockHeaderParser
|
||||
# let's clean up the encoding in case they've done anything weird with the characters we might find
|
||||
source = source.force_encoding("ISO-8859-1").encode("utf-8", :replace => nil)
|
||||
|
||||
# # smush multiline macros into single line (checking for continuation character at end of line '\')
|
||||
source.gsub!(/\s*\\\s*/m, ' ')
|
||||
|
||||
# #remove comments (block and line, in three steps to ensure correct precedence)
|
||||
source.gsub!(/\/\/(?:.+\/\*|\*(?:$|[^\/])).*$/, '') # remove line comments that comment out the start of blocks
|
||||
source.gsub!(/\/\*.*?\*\//m, '') # remove block comments
|
||||
source.gsub!(/\/\/.*$/, '') # remove line comments (all that remain)
|
||||
|
||||
source.gsub!(/(static|inline)+.*\{.*\w*\}/m) do |m|
|
||||
m.gsub!(/(static|inline)/, '') # remove static and inline
|
||||
# remove nested pairs of braces because no function declarations will be inside of them (leave outer pair for function definition detection)
|
||||
@@ -81,18 +73,19 @@ class CMockHeaderParser
|
||||
m.gsub!(/\s*\{\s\}/, ";")
|
||||
|
||||
# Cleanup the function declarations
|
||||
# Not strictly necessary compile-wise, but it can help debugging
|
||||
# Not strictly necessary, it will compile just fine, but it can help during debugging
|
||||
m_lines = m.split(/\s*;\s*/).uniq
|
||||
m_lines.each {
|
||||
|m_line|
|
||||
m_line.gsub!(/^\s+/, '') # remove extra white space from beginning of line
|
||||
m_line.gsub!(/\s+/, ' ') # remove remaining extra white space
|
||||
m_line.gsub!(/^\s+/, '') # remove extra white space from beginning of line
|
||||
m_line.gsub(/\s+/, ' ') # remove remaining extra white space
|
||||
m_line.gsub(/\n/, '') # remove newlines
|
||||
}
|
||||
|
||||
m_lines.join(";\n") + ";" # Join the lines and add the last semicolon manually
|
||||
end
|
||||
|
||||
source.gsub!(/\s+$/, '') # remove extra white space from end of line
|
||||
return source
|
||||
end
|
||||
|
||||
def import_source(source)
|
||||
|
||||
@@ -1780,4 +1780,106 @@ describe CMockHeaderParser, "Verify CMockHeaderParser Module" do
|
||||
end
|
||||
end
|
||||
|
||||
it "Normalize source doesn't change a header with no inlines" do
|
||||
source =
|
||||
"#ifndef _NOINCLUDES\n" +
|
||||
"#define _NOINCLUDES\n" +
|
||||
"#include \"unity.h\"\n" +
|
||||
"#include \"cmock.h\"\n" +
|
||||
"#include \"YetAnotherHeader.h\"\n" +
|
||||
"\n" +
|
||||
"/* Ignore the following warnings since we are copying code */\n" +
|
||||
"#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
|
||||
"#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
|
||||
"#pragma GCC diagnostic push\n" +
|
||||
"#endif\n" +
|
||||
"#if !defined(__clang__)\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
|
||||
"#endif\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
|
||||
"#endif\n" +
|
||||
"\n" +
|
||||
"struct my_struct {\n" +
|
||||
"int a;\n" +
|
||||
"int b;\n" +
|
||||
"int b;\n" +
|
||||
"char c;\n" +
|
||||
"};\n" +
|
||||
"int my_function(int a);\n" +
|
||||
"int my_better_function(struct my_struct *s);\n" +
|
||||
"\n"
|
||||
"#endif _NOINCLUDES\n"
|
||||
|
||||
assert_equal(source, @parser.normalize_source(source))
|
||||
end
|
||||
|
||||
it "Normalize source changes inline functions to function declarations" do
|
||||
source =
|
||||
"#ifndef _NOINCLUDES\n" +
|
||||
"#define _NOINCLUDES\n" +
|
||||
"#include \"unity.h\"\n" +
|
||||
"#include \"cmock.h\"\n" +
|
||||
"#include \"YetAnotherHeader.h\"\n" +
|
||||
"\n" +
|
||||
"/* Ignore the following warnings since we are copying code */\n" +
|
||||
"#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
|
||||
"#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
|
||||
"#pragma GCC diagnostic push\n" +
|
||||
"#endif\n" +
|
||||
"#if !defined(__clang__)\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
|
||||
"#endif\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
|
||||
"#endif\n" +
|
||||
"\n" +
|
||||
"struct my_struct {\n" +
|
||||
"int a;\n" +
|
||||
"int b;\n" +
|
||||
"int b;\n" +
|
||||
"char c;\n" +
|
||||
"};\n" +
|
||||
"int my_function(int a);\n" +
|
||||
"int my_better_function(struct my_struct *s);\n" +
|
||||
"static inline int get_member_a(struct my_struct *s)\n" +
|
||||
"{\n" +
|
||||
" return s->a;\n" +
|
||||
"}\n"
|
||||
"#endif _NOINCLUDES\n"
|
||||
|
||||
expected =
|
||||
"#ifndef _NOINCLUDES\n" +
|
||||
"#define _NOINCLUDES\n" +
|
||||
"#include \"unity.h\"\n" +
|
||||
"#include \"cmock.h\"\n" +
|
||||
"#include \"YetAnotherHeader.h\"\n" +
|
||||
"\n" +
|
||||
"/* Ignore the following warnings since we are copying code */\n" +
|
||||
"#if defined(__GNUC__) && !defined(__ICC) && !defined(__TMS470__)\n" +
|
||||
"#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))\n" +
|
||||
"#pragma GCC diagnostic push\n" +
|
||||
"#endif\n" +
|
||||
"#if !defined(__clang__)\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wpragmas\"\n" +
|
||||
"#endif\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wunknown-pragmas\"\n" +
|
||||
"#pragma GCC diagnostic ignored \"-Wduplicate-decl-specifier\"\n" +
|
||||
"#endif\n" +
|
||||
"\n" +
|
||||
"struct my_struct {\n" +
|
||||
"int a;\n" +
|
||||
"int b;\n" +
|
||||
"int b;\n" +
|
||||
"char c;\n" +
|
||||
"};\n" +
|
||||
"int my_function(int a);\n" +
|
||||
"int my_better_function(struct my_struct *s);\n" +
|
||||
"int get_member_a(struct my_struct *s);\n"
|
||||
"\n"
|
||||
"#endif _NOINCLUDES\n"
|
||||
|
||||
assert_equal(expected, @parser.normalize_source(source))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user