fixed return type processing to handle all variations of '*' placement for pointer types

git-svn-id: http://cmock.svn.sourceforge.net/svnroot/cmock/trunk@75 bf332499-1b4d-0410-844d-d2d48d5cc64c
This commit is contained in:
mkarlesky
2009-04-14 22:08:23 +00:00
parent 156879d750
commit 2513d1f54f
3 changed files with 118 additions and 11 deletions
+5 -8
View File
@@ -2,12 +2,12 @@ class CMockHeaderParser
attr_accessor :match_type, :attribute_match, :src_lines, :funcs, :c_attributes, :declaration_parse_matcher, :included
def initialize(source, match_type=/\w+\**/, attributes=['static', '__monitor', '__ramfunc', '__irq', '__fiq'])
def initialize(source, match_type=/[^\s]+\s*\**?/, attributes=['static', '__monitor', '__ramfunc', '__irq', '__fiq'])
import_source(source)
@funcs = nil
@match_type = match_type
@c_attributes = attributes
@declaration_parse_matcher = /(\w*\s+)*([^\s]+)\s+(\w+)\s*\(([^\)]*)\)/
@declaration_parse_matcher = /(\w*\s+)*??(#{match_type})\s*(\w+)\s*\(([^\)]*)\)/
@included = nil
end
@@ -82,7 +82,7 @@ class CMockHeaderParser
@funcs = []
depth = 0
@src_lines.each do |line|
if depth.zero? && line =~ /#{@attribute_match}\s*#{@match_type}\s+\w+\s*\(.*\)/m
if depth.zero? && line =~ /#{@attribute_match}\s*#{@match_type}\s*\w+\s*\(.*\)/m
@funcs << line.strip.gsub(/\s+/, ' ')
end
if line =~ /\{/
@@ -129,15 +129,12 @@ class CMockHeaderParser
modifier = $1
modifier = '' if modifier.nil?
decl[:modifier] = modifier.strip
decl[:rettype] = $2
decl[:rettype] = $2.strip
decl[:name] = $3
args = $4
#put the asterisk with the type (where it belongs)
if (decl[:name][0] == '*')
decl[:rettype] << '*'
decl[:name].slice!(0)
end
decl[:rettype].gsub!(/\s+\*/,'*')
#remove default parameter statements from mock definitions
args.gsub!(/=\s*[a-zA-Z0-9_\.]+\s*\,/, ',')
+82
View File
@@ -0,0 +1,82 @@
---
:cmock:
:plugins:
- # no plugins
:systest:
:types: |
typedef unsigned short U16;
:mockable: |
U16 *ptr_return1(int a);
U16* ptr_return2(int a);
U16 * ptr_return3(int a);
void var_args1(int a, ...);
void var_args2(int a, int b, ...);
void arg_list(int * a, int *b, int* c);
:source:
:header: |
U16* function1(int a);
void function2(int a, int b);
void function3(void);
:code: |
int A, B, C;
U16* function1(int a)
{
ptr_return1(a);
ptr_return2(a);
return ptr_return3(a);
}
void function2(int a, int b)
{
var_args1(a, 3);
var_args2(a, b, 'c');
}
void function3(void)
{
arg_list(&A, &B, &C);
}
:tests:
:common: |
void setUp(void) {}
void tearDown(void) {}
extern int A, B, C;
:units:
- :pass: TRUE
:should: 'just do cursory return value check - compilation was the important part'
:code: |
test()
{
U16 retval;
ptr_return1_ExpectAndReturn(2, NULL);
ptr_return2_ExpectAndReturn(2, NULL);
ptr_return3_ExpectAndReturn(2, &retval);
TEST_ASSERT_EQUAL(&retval, function1(2));
}
- :pass: TRUE
:should: 'just do cursory var arg check - compilation was the important part'
:code: |
test()
{
var_args1_Expect(2);
var_args2_Expect(2, 3);
function2(2, 3);
}
- :pass: TRUE
:should: 'just do cursory argument list check - compilation was the important part'
:code: |
test()
{
arg_list_Expect(&A, &B, &C);
function3();
}
...
+31 -3
View File
@@ -13,9 +13,9 @@ class CMockHeaderParserTest < Test::Unit::TestCase
should "create and initialize variables to defaults appropriately" do
@parser = CMockHeaderParser.new("//some contents")
assert_nil(@parser.funcs)
assert_equal(/\w+\**/, @parser.match_type)
assert_equal(/[^\s]+\s*\**?/, @parser.match_type)
assert_equal(['static', '__monitor', '__ramfunc', '__irq', '__fiq'], @parser.c_attributes)
assert_equal(/(\w*\s+)*([^\s]+)\s+(\w+)\s*\(([^\)]*)\)/, @parser.declaration_parse_matcher)
assert_equal(/(\w*\s+)*??(#{@parser.match_type})\s*(\w+)\s*\(([^\)]*)\)/, @parser.declaration_parse_matcher)
assert_nil(@parser.included)
end
@@ -320,7 +320,9 @@ class CMockHeaderParserTest < Test::Unit::TestCase
source =
"MY_STRUCT* HooWah(char * format);\n" +
"bool* HotShot(HIS_STRUCT *p, unsigned int* pint);\n"
"bool* HotShot(HIS_STRUCT *p, unsigned int* pint);\n" +
"bool * HotDog(BOW_WOW *p, unsigned int* pint);\n" +
"static bool *HotToTrot(unsigned int * struttin);\n"
@parser = CMockHeaderParser.new(source)
parsed_stuff = @parser.parse
@@ -347,7 +349,33 @@ class CMockHeaderParserTest < Test::Unit::TestCase
{:type => "unsigned int*", :name => "pint"}
],
:name => "HotShot"
},
{
:modifier => "",
:args_string => "BOW_WOW *p, unsigned int* pint",
:rettype => "bool*",
:var_arg => nil,
:args =>
[
{:type => "BOW_WOW*", :name => "p"},
{:type => "unsigned int*", :name => "pint"}
],
:name => "HotDog"
},
{
:modifier => "static",
:args_string => "unsigned int * struttin",
:rettype => "bool*",
:var_arg => nil,
:args =>
[
{:type => "unsigned int*", :name => "struttin"}
],
:name => "HotToTrot"
}
]
assert_equal(expected, parsed_stuff[:functions])