Skip to content

Commit

Permalink
Fixed rt56956 with test case
Browse files Browse the repository at this point in the history
DIV operator broken (Agent Zhang)
  • Loading branch information
robertkrimen committed Apr 27, 2010
1 parent f817706 commit 5390bec
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
---
version: 0.261
date: Tuesday April 27 09:35:06 PDT 2010
changes:
- Fixed rt56956 with test case - DIV operator broken (Agent Zhang)

---
version: 0.260
date: Monday April 26 12:03:40 PDT 2010
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ t/9bug/53453-make-Jemplate-slice-like-TT-slice.t
t/9bug/53454-array-range-operator.t
t/9bug/37570-list-for-lists.t
t/9bug/43809-capture-implementation.t
t/9bug/56965-DIV-operator-broken.t
tests/Makefile
tests/README
tests/bin/daemon
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION
Version 0.260
Version 0.261

NAME
Jemplate - JavaScript Templating with Template Toolkit
Expand Down
39 changes: 36 additions & 3 deletions bin/jemplate
Original file line number Diff line number Diff line change
Expand Up @@ -10300,6 +10300,16 @@ $block
...
}

sub _attempt_range_expand_val ($) {
my $val = shift;
return $val unless
my ( $from, $to ) = $val =~ m/\s*\[\s*(\S+)\s*\.\.\s*(\S+)\s*\]/;

die "Range expansion is current supported for positive/negative integer values only (e.g. [ 1 .. 10 ])\nCannot expand: $val" unless $from =~ m/^-?\d+$/ && $to =~ m/^-?\d+$/;

return join '', '[', join( ',', $from .. $to ), ']';
}


sub textblock {
my ($class, $text) = @_;
Expand Down Expand Up @@ -10356,6 +10366,7 @@ sub assign {
$var = '[' . join(', ', @$var) . ']';
}
}
$val = _attempt_range_expand_val $val;
$val .= ', 1' if $default;
return "stash.set($var, $val)";
}
Expand Down Expand Up @@ -10494,6 +10505,8 @@ sub foreach {
$loop_restore = 'stash = context.delocalise()';
}

$list = _attempt_range_expand_val $list;

return <<EOF;

// FOREACH
Expand Down Expand Up @@ -10806,7 +10819,27 @@ EOF
}

sub capture {
return "throw('CAPTURE not yet supported in Jemplate');";
my ($class, $name, $block) = @_;

if (ref $name) {
if (scalar @$name == 2 && ! $name->[1]) {
$name = $name->[0];
}
else {
$name = '[' . join(', ', @$name) . ']';
}
}

return <<EOF;

// CAPTURE
(function() {
var output = '';
$block
stash.set($name, output);
})();
EOF

}

BEGIN {
Expand Down Expand Up @@ -16692,7 +16725,7 @@ sub
[#Rule 137
'expr', 3,
sub
{ "int($_[1] / $_[3])" }
{ "Math.floor($_[1] / $_[3])" }
],
[#Rule 138
'expr', 3,
Expand Down Expand Up @@ -19843,7 +19876,7 @@ use Template 2.14;
use Getopt::Long;


our $VERSION = '0.25';
our $VERSION = '0.260';

use Jemplate::Parser;

Expand Down
4 changes: 2 additions & 2 deletions lib/Jemplate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use Getopt::Long;

=head1 VERSION
Version 0.260
Version 0.261
=cut

our $VERSION = '0.260';
our $VERSION = '0.261';

use Jemplate::Parser;

Expand Down
2 changes: 1 addition & 1 deletion lib/Jemplate/Grammar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6010,7 +6010,7 @@ sub
'expr', 3,
sub
#line 358 "Parser.yp"
{ "int($_[1] / $_[3])" }
{ "Math.floor($_[1] / $_[3])" }
],
[#Rule 138
'expr', 3,
Expand Down
2 changes: 1 addition & 1 deletion src/parser/Parser.yp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ item: IDENT { "'$_[1]'" }
expr: expr BINOP expr { "$_[1] $_[2] $_[3]" }
| expr '/' expr { "$_[1] $_[2] $_[3]" }
| expr '+' expr { "$_[1] $_[2] $_[3]" }
| expr DIV expr { "int($_[1] / $_[3])" }
| expr DIV expr { "Math.floor($_[1] / $_[3])" }
| expr MOD expr { "$_[1] % $_[3]" }
| expr CMPOP expr { "$_[1] $CMPOP{ $_[2] } $_[3]" }
| expr CAT expr { "$_[1] + $_[3]" }
Expand Down
36 changes: 36 additions & 0 deletions t/9bug/56965-DIV-operator-broken.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

BEGIN {
plan skip_all => "JavaScript::V8x::TestMoreish not available" unless eval { require JavaScript::V8x::TestMoreish };
}

plan qw/no_plan/;

use Jemplate;
use Jemplate::Runtime;

use JavaScript::V8x::TestMoreish;

my $jemplate = Jemplate->new;
my @js;

push @js, $jemplate->compile_template_content( <<_END_, 't0' );
[% BLOCK t1 %][% 3 DIV 2 %][% END %]
[% BLOCK t2 %][% 4 DIV 2 %][% END %]
[% BLOCK t3 %][% 5 DIV 2 %][% END %]
_END_

test_js_eval( Jemplate::Runtime->kernel );
test_js_eval( join "\n", @js, "1;" );
test_js <<'_END_';
areEqual( Jemplate.process( 't1' ), 1 );
areEqual( Jemplate.process( 't2' ), 2 );
areEqual( Jemplate.process( 't3' ), 2 );
_END_

0 comments on commit 5390bec

Please sign in to comment.