Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #219 from usev6/master
Improve error message for sprintf
  • Loading branch information
moritz committed Jan 25, 2015
2 parents 4553a0e + 6fa84cc commit 5a33762
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 8 additions & 8 deletions src/HLL/sprintf.nqp
Expand Up @@ -59,14 +59,14 @@ my module sprintf {
my @statements;
@statements.push( $_.made ) for $<statement>;

if $assert_used_args && $*ARGS_USED < +@*ARGS_HAVE {
nqp::die("Too few directives: found $*ARGS_USED,"
~ " fewer than the " ~ +@*ARGS_HAVE ~ " arguments after the format string")
}
if $*ARGS_USED > +@*ARGS_HAVE {
nqp::die("Too many directives: found $*ARGS_USED, but "
~ (+@*ARGS_HAVE > 0 ?? "only " ~ +@*ARGS_HAVE !! "no")
~ " arguments after the format string")
if ($assert_used_args && $*ARGS_USED < +@*ARGS_HAVE) || ($*ARGS_USED > +@*ARGS_HAVE) {
nqp::die("Directives specify "
~ ($*ARGS_USED == 1 ?? "1 argument, but "
!! "$*ARGS_USED arguments, but ")
~ (+@*ARGS_HAVE < 1 ?? "no argument was"
!! +@*ARGS_HAVE == 1 ?? "1 argument was"
!! +@*ARGS_HAVE ~ " arguments were")
~ " supplied")
}
make nqp::join('', @statements);
}
Expand Down
6 changes: 3 additions & 3 deletions t/hll/06-sprintf.t
Expand Up @@ -32,15 +32,15 @@ is(nqp::sprintf('Peter %s', ['Bishop']), 'Peter Bishop', 'one %s directive' );
is(nqp::sprintf('%s %s', ['William', 'Bell']), 'William Bell', 'two %s directives' );

dies_ok({ nqp::sprintf('%s %s', ['Dr.', 'William', 'Bell']) }, 'arguments > directives' );
is($die_message, 'Too few directives: found 2, fewer than the 3 arguments after the format string',
is($die_message, 'Directives specify 2 arguments, but 3 arguments were supplied',
'arguments > directives error message' );

dies_ok({ nqp::sprintf('%s %s %s', ['Olivia', 'Dunham']) }, 'directives > arguments' );
is($die_message, 'Too many directives: found 3, but only 2 arguments after the format string',
is($die_message, 'Directives specify 3 arguments, but 2 arguments were supplied',
'directives > arguments error message' );

dies_ok({ nqp::sprintf('%s %s', []) }, 'directives > 0 arguments' );
is($die_message, 'Too many directives: found 2, but no arguments after the format string',
is($die_message, 'Directives specify 2 arguments, but no argument was supplied',
'directives > 0 arguments error message' );

is(nqp::sprintf('%% %% %%', []), '% % %', '%% escape' );
Expand Down

0 comments on commit 5a33762

Please sign in to comment.