Skip to content

Commit

Permalink
Refactor more code into testable subroutines, place them into Functio…
Browse files Browse the repository at this point in the history
…ns.pm, test them in 01_functions.t.
  • Loading branch information
jkeenan committed Dec 2, 2010
1 parent 5195fb1 commit dd3c6c9
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 11 deletions.
30 changes: 30 additions & 0 deletions lib/Parrot/Headerizer/Functions.pm
Expand Up @@ -15,6 +15,8 @@ our @EXPORT_OK = qw(
shim_test
handle_modified_args
add_asserts_to_declarations
add_newline_if_multiline
func_modifies
add_headerizer_markers
);

Expand Down Expand Up @@ -289,6 +291,13 @@ sub handle_modified_args {
return ($decl, $multiline);
}

# $decl .= $multiline ? ";\n" : ";";
sub add_newline_if_multiline {
my ($decl, $multiline) = @_;
$decl .= $multiline ? ";\n" : ";";
return $decl;
}

sub add_asserts_to_declarations {
my ($funcs_ref, $decls_ref) = @_;
foreach my $func (@{ $funcs_ref }) {
Expand All @@ -312,6 +321,27 @@ sub add_asserts_to_declarations {
return @{ $decls_ref };
}

=pod
@mods = func_modifies($arg, \@mods);
=cut

sub func_modifies {
my ($arg, $modsref) = @_;
my @mods = @{$modsref};
if ( $arg =~ m{ARG(?:MOD|OUT)(?:_NULLOK)?\((.+?)\)} ) {
my $modified = $1;
if ( $modified =~ s/.*\*/*/ ) {
# We're OK
}
else {
$modified =~ s/.* (\w+)$/$1/ or die qq{Unable to figure out the modified parm out of "$modified"};
}
push( @mods, "FUNC_MODIFIES($modified)" );
}
return @mods;
}
=pod
return add_headerizer_markers( {
Expand Down
15 changes: 4 additions & 11 deletions lib/Parrot/Headerizer/Object.pm
Expand Up @@ -37,7 +37,9 @@ use Parrot::Headerizer::Functions qw(
asserts_from_args
shim_test
handle_modified_args
add_newline_if_multiline
add_asserts_to_declarations
func_modifies
add_headerizer_markers
);

Expand Down Expand Up @@ -581,7 +583,7 @@ sub make_function_decls {
my @macros = @{ $func->{macros} };
$multiline = 1 if @macros;

$decl .= $multiline ? ";\n" : ";";
$decl = add_newline_if_multiline($decl, $multiline);
$decl = join( "\n", @macros, $decl );
$decl =~ s/\t/ /g;
push( @decls, $decl );
Expand All @@ -605,16 +607,7 @@ sub attrs_from_args {
my $n = 0;
for my $arg (@args) {
++$n;
if ( $arg =~ m{ARG(?:MOD|OUT)(?:_NULLOK)?\((.+?)\)} ) {
my $modified = $1;
if ( $modified =~ s/.*\*/*/ ) {
# We're OK
}
else {
$modified =~ s/.* (\w+)$/$1/ or die qq{Unable to figure out the modified parm out of "$modified"};
}
push( @mods, "FUNC_MODIFIES($modified)" );
}
@mods = func_modifies($arg, \@mods);
if ( $arg =~ m{(ARGIN|ARGOUT|ARGMOD|ARGFREE_NOTNULL|NOTNULL)\(} || $arg eq 'PARROT_INTERP' ) {
push( @attrs, "__attribute__nonnull__($n)" );
}
Expand Down
60 changes: 60 additions & 0 deletions t/tools/dev/headerizer/01_functions.t
Expand Up @@ -22,7 +22,9 @@ use Parrot::Headerizer::Functions qw(
asserts_from_args
shim_test
handle_modified_args
add_newline_if_multiline
add_asserts_to_declarations
func_modifies
add_headerizer_markers
);

Expand Down Expand Up @@ -417,6 +419,20 @@ is( $decl_out, $expected,
"Got expected portion of declaration (long SHIM one arg)" );
ok( $multiline, "Long portion of declaration means multiline" );

# add_newline_if_multiline()
$decl_in = 'alpha';
$multiline = 1;
$decl_out = add_newline_if_multiline($decl_in, $multiline);
is( $decl_out, "alpha;\n",
"Got expected value from add_newline_if_multiline()" );

$decl_in = 'alpha';
$multiline = 0;
$decl_out = add_newline_if_multiline($decl_in, $multiline);
is( $decl_out, "alpha;",
"Got expected value from add_newline_if_multiline()" );


# add_asserts_to_declarations()
$funcs_ref = [
{
Expand Down Expand Up @@ -448,6 +464,50 @@ $expected .= ' , PARROT_ASSERT_ARG(item))';
is( $decls[0], $expected,
"Got expected declaration from add_asserts_to_declarations()" );

# func_modifies()
my ($arg, @mods, @mods_out);
$arg = 'ARGMOD(List_Item_Header *item)';
@mods = ( 'FUNC_MODIFIES(*list)' );
$expected = [
'FUNC_MODIFIES(*list)',
'FUNC_MODIFIES(*item)',
];
@mods_out = func_modifies($arg, \@mods);
is_deeply( \@mods_out, $expected,
"Got expected output of func_modifies()" );

$arg = 'foobar';
@mods = ( 'FUNC_MODIFIES(*list)' );
$expected = [
'FUNC_MODIFIES(*list)',
];
@mods_out = func_modifies($arg, \@mods);
is_deeply( \@mods_out, $expected,
"Got expected output of func_modifies()" );

$arg = 'ARGMOD_NULLOK(List_Item_Header alpha)';
@mods = ( 'FUNC_MODIFIES(*list)' );
$expected = [
'FUNC_MODIFIES(*list)',
'FUNC_MODIFIES(alpha)',
];
@mods_out = func_modifies($arg, \@mods);
is_deeply( \@mods_out, $expected,
"Got expected output of func_modifies()" );

eval {
$arg = 'ARGMOD_NULLOK(List_Item_Header)';
@mods = ( 'FUNC_MODIFIES(*list)' );
$expected = [
'FUNC_MODIFIES(*list)',
'FUNC_MODIFIES(alpha)',
];
@mods_out = func_modifies($arg, \@mods);
};
like($@, qr/Unable to figure out the modified/,
"Got expected error message for func_modifies()" );


# add_headerizer_markers
#{
# my $tdir = tempdir( CLEANUP => 1 );
Expand Down

0 comments on commit dd3c6c9

Please sign in to comment.