Skip to content

Commit

Permalink
Item10187: updated the macro param expansion to take account of the e…
Browse files Browse the repository at this point in the history
…dge cases discussed; added a unit test

git-svn-id: http://svn.foswiki.org/trunk@12102 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Jul 1, 2011
1 parent 56bfda6 commit b1b7557
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
42 changes: 41 additions & 1 deletion UnitTestContrib/test/unit/VariableTests.pm
@@ -1,6 +1,6 @@
use strict;

# tests for the correct expansion of programmed variables (*not* FoswikiFns, which
# tests for the correct expansion of preferences (*not* FoswikiFns, which
# should have their own individual testcase)

package VariableTests;
Expand Down Expand Up @@ -131,4 +131,44 @@ END
$this->assert_str_equals( $xpect, $result );
}

sub test_macroParams {
my $this = shift;
# Check default on undefined macros
# Check default given, given but null, not given
# Check quotes and other standard expansions
# Check override of standard macros
$this->{session}->{prefs}->setSessionPreferences(
ARFLE => '%BARFLE{default="gloop"}%',
TING => '%DEFAULT% %DEFAULT{default="tong"}%',
ALING => '\'%DEFAULT%\' \'%DEFAULT{default="tong"}%\'',
TOOT => '\'%NOP%\'',
WOOF => '%MIAOW{default="$quot$percent$quot"}%');
my $input = <<'INPUT';
| gloop | %BARFLE{default="gloop"}% |
| mong | %ARFLE{BARFLE="mong"}% |
| %DEFAULT% tong | %TING% |
| ding ding | %TING{"ding"}% |
| '' '' | %ALING{""}% |
| 'sweet' | %TOOT{NOP="sweet"}% |
| "%" | %WOOF% |
| p"r"r | %WOOF{MIAOW="p$quot()r$quot()r"}% |
INPUT
my $topicObject =
Foswiki::Meta->new( $this->{session}, $this->{test_web},
$this->{test_topic}, $input );
my $result = $topicObject->expandMacros($input);
my $expected = <<'EXPECTED';
| gloop | gloop |
| mong | mong |
| %DEFAULT% tong | %DEFAULT% tong |
| ding ding | ding ding |
| '' '' | '' '' |
| 'sweet' | 'sweet' |
| "%" | "%" |
| p"r"r | p"r"r |
EXPECTED
$this->assert_str_equals( $expected, $result );

}

1;
42 changes: 23 additions & 19 deletions core/lib/Foswiki.pm
Expand Up @@ -3005,39 +3005,43 @@ sub _expandMacroOnTopicRendering {
my ( $this, $tag, $args, $topicObject ) = @_;

require Foswiki::Attrs;
my $attrs;

my $e = $this->{prefs}->getPreference($tag);
if ( defined $e ) {
if ( $args && $args =~ /\S/ ) {
my $attrs = new Foswiki::Attrs( $args, 0 );
$attrs = new Foswiki::Attrs( $args, 0 );
$attrs->{DEFAULT} = $attrs->{_DEFAULT};
$e = $this->_processMacros(
$e,
sub {
my ( $this, $tag, $args, $topicObject ) = @_;
return $attrs->{$tag};
return expandStandardEscapes($attrs->{$tag});
},
$topicObject,
1
);
}
}
else {
if ( exists( $macros{$tag} ) ) {
unless ( defined( $macros{$tag} ) ) {

# Demand-load the macro module
die $tag unless $tag =~ /([A-Z_:]+)/i;
$tag = $1;
eval "require Foswiki::Macros::$tag";
die $@ if $@;
$macros{$tag} = eval "\\&$tag";
die $@ if $@;
}

my $attrs = new Foswiki::Attrs( $args, $contextFreeSyntax{$tag} );

$e = &{ $macros{$tag} }( $this, $attrs, $topicObject );
}
elsif ( exists( $macros{$tag} ) ) {
unless ( defined( $macros{$tag} ) ) {

# Demand-load the macro module
die $tag unless $tag =~ /([A-Z_:]+)/i;
$tag = $1;
eval "require Foswiki::Macros::$tag";
die $@ if $@;
$macros{$tag} = eval "\\&$tag";
die $@ if $@;
}

$attrs = new Foswiki::Attrs( $args, $contextFreeSyntax{$tag} );
$e = &{ $macros{$tag} }( $this, $attrs, $topicObject );
} elsif ( $args && $args =~ /\S/ ) {
$attrs = new Foswiki::Attrs( $args );
if (defined $attrs->{default}) {
$e = expandStandardEscapes($attrs->{default});
}
}
return $e;
}
Expand Down

0 comments on commit b1b7557

Please sign in to comment.