Skip to content

Commit

Permalink
Item10346: render empty string instead of 0 if argument is empty; add…
Browse files Browse the repository at this point in the history
… unit tests for MAKETEXT

git-svn-id: http://svn.foswiki.org/trunk@10691 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
ArthurClemens authored and ArthurClemens committed Feb 12, 2011
1 parent b452f4a commit 2765732
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ test/unit/Fn_GROUPS.pm 0644
test/unit/Fn_ICON.pm 0644
test/unit/Fn_IF.pm 0644
test/unit/Fn_INCLUDE.pm 0644
test/unit/Fn_MAKETEXT.pm 0644
test/unit/Fn_NOP.pm 0644
test/unit/Fn_QUERY.pm 0644
test/unit/Fn_QUERYPARAMS.pm 0644
Expand Down
138 changes: 138 additions & 0 deletions UnitTestContrib/test/unit/Fn_MAKETEXT.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use strict;

# tests for the correct expansion of MAKETEXT

package Fn_MAKETEXT;

use FoswikiFnTestCase;
our @ISA = qw( FoswikiFnTestCase );

use Foswiki;
use Error qw( :try );

my $topicObject;

sub new {
my $self = shift()->SUPER::new( 'MAKETEXT', @_ );
return $self;
}

sub set_up {
my $this = shift;

$this->SUPER::set_up();

$topicObject =
Foswiki::Meta->new( $this->{session}, $this->{test_web}, 'WebHome' );
}

sub loadExtraConfig {
my $this = shift;
$this->SUPER::loadExtraConfig();
setLocalSite();
}

sub setLocalSite {
$Foswiki::cfg{WebMasterEmail} = 'a.b@c.org';
}

sub test_MAKETEXT_simple {
my $this = shift;

my $result = $topicObject->expandMacros('%MAKETEXT{"edit"}%');
$this->assert_str_equals( 'edit', $result );
}

sub test_MAKETEXT_doc_example_1 {
my $this = shift;

my $result = $topicObject->expandMacros('%MAKETEXT{string="Notes:"}%');
$this->assert_str_equals( 'Notes:', $result );
}

sub test_MAKETEXT_doc_example_2 {
my $this = shift;

my $result = $topicObject->expandMacros(
'%MAKETEXT{
"If you have any questions, please contact [_1]."
args="%WIKIWEBMASTER%"
}%'
);
$this->assert_str_equals(
'If you have any questions, please contact a.b@c.org.', $result );
}

sub test_MAKETEXT_doc_example_3 {
my $this = shift;

my $result = $topicObject->expandMacros(
'%MAKETEXT{"Did you want to [[[_1]][reset [_2]\'s password]]?" args="%SYSTEMWEB%.ResetPassword,%WIKIUSERNAME%"}%'
);

$this->assert_str_equals(
'Did you want to [[System.ResetPassword][reset TemporaryMAKETEXTUsersWeb.WikiGuest\'s password]]?',
$result
);
}

sub test_MAKETEXT_single_arg {
my $this = shift;

my $result =
$topicObject->expandMacros('%MAKETEXT{"edit [_1]" args="WebHome"}%');
$this->assert_str_equals( 'edit WebHome', $result );
}

sub test_MAKETEXT_expand_variables_in_args {
my $this = shift;

my $result =
$topicObject->expandMacros('%MAKETEXT{"edit [_1]" args="%HOMETOPIC%"}%');
$this->assert_str_equals( 'edit WebHome', $result );
}

sub test_MAKETEXT_multiple_args {
my $this = shift;

my $result = $topicObject->expandMacros(
'%MAKETEXT{"edit [_1] [_2]" args="WebHome, now"}%');
$this->assert_str_equals( 'edit WebHome now', $result );
}

sub test_MAKETEXT_multiple_args_one_empty {
my $this = shift;

my $result =
$topicObject->expandMacros('%MAKETEXT{"edit [_1][_2]" args="WebHome"}%');
$this->assert_str_equals( 'edit WebHome', $result );
}

sub test_MAKETEXT_multiple_args_forgot_to_reference_one {
my $this = shift;

my $result =
$topicObject->expandMacros('%MAKETEXT{"edit [_1]" args="WebHome, now"}%');
$this->assert_str_equals( 'edit WebHome', $result );
}

sub test_MAKETEXT_underscore {
my $this = shift;

# name starts with underscore: error
my $result = $topicObject->expandMacros('%MAKETEXT{"_edit"}%');
$this->assert_str_equals(
'<span class="foswikiAlert">Error: MAKETEXT argument\'s can\'t start with an underscore ("_").</span>',
$result
);
}

sub test_MAKETEXT_access_key {
my $this = shift;

my $result = $topicObject->expandMacros('%MAKETEXT{"ed&it"}%');
$this->assert_str_equals( 'ed<span class=\'foswikiAccessKey\'>i</span>t',
$result );
}

1;
2 changes: 1 addition & 1 deletion core/lib/Foswiki/I18N.pm
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ sub maketext {
import CGI();

return CGI::span(
{ -style => 'color:red;' },
{ -class => 'foswikiAlert' },
"Error: MAKETEXT argument's can't start with an underscore (\"_\")."
);
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/Macros/MAKETEXT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ s/~\[(\*,\_(\d+),[^,]+(,([^,]+))?)~\]/ $max = $2 if ($2 > $max); "[$1]"/ge;

my @args = split( /\s*,\s*/, $argsStr );

# fill omitted args with zeros
# fill omitted args with empty strings
while ( ( scalar @args ) < $max ) {
push( @args, 0 );
push( @args, '' );
}

# do the magic:
Expand Down

0 comments on commit 2765732

Please sign in to comment.