[Templates-cvs] cvs commit: TT3/t/directive my.t

cvs@template-toolkit.org cvs@template-toolkit.org
Wed, 01 Dec 2004 17:56:28 +0000


cvs         04/12/01 17:56:28

  Modified:    t/directive my.t
  Log:
  * added some more tests for MY variables
  
  Revision  Changes    Path
  1.2       +168 -11   TT3/t/directive/my.t
  
  Index: my.t
  ===================================================================
  RCS file: /template-toolkit/TT3/t/directive/my.t,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- my.t	2004/12/01 11:17:36	1.1
  +++ my.t	2004/12/01 17:56:28	1.2
  @@ -9,7 +9,7 @@
   # This is free software; you can redistribute it and/or modify it
   # under the same terms as Perl itself.
   #
  -# $Id: my.t,v 1.1 2004/12/01 11:17:36 abw Exp $
  +# $Id: my.t,v 1.2 2004/12/01 17:56:28 abw Exp $
   #
   #========================================================================
   
  @@ -19,38 +19,195 @@
   use lib qw( ./lib ../lib ../../lib );
   use Template::TT3::Compiler;
   use Template::Directive::My;
  -use Template::Test skip_all => 'test not working yet', tests => 12, import => ':all';
  +use Template::Context;
  +use Template::Resources;
  +use Template::Resource::Variable;
  +use Template::Test tests => 14, import => ':all';
   
   our $DEBUG = 
  +$Template::Parser::DEBUG =
   $Template::Directive::My::DEBUG =
  +$Template::Resource::Variable::DEBUG =
   grep(/^--?d(ebug)?/, @ARGV);
   
   
  +my $context = Template::Context->new({
  +    variables => {
  +        one    => 1,
  +        two    => 2,
  +        meta   => ['foo', 'bar', 'baz'],
  +        data   => {
  +            foo => 'The Foo Item',
  +            bar => 'The Bar Item',
  +        },
  +    },
  +    resources => 'Template::Resources',
  +}) || die Template::Context->error();
  +
   my $compiler = Template::TT3::Compiler->new()
       || die Template::TT3::Compiler->error();
   
   test_expect({
  -    handler => \&parse, 
  +    handler => \&compile, 
       debug   => $DEBUG,
   });
   
  -sub parse {
  +sub compile {
       my $test  = shift;
       my $input = $test->{ input };
  +    my $code  = $compiler->compile(\$input, name => "'$test->{ name }' test");
  +    my $result;
   
  -    return $compiler->compile(\$input, name => $test->{ name })
  -        || '<ERROR:' . $compiler->error() . '>';
  +    if (defined $code) {
  +        my $sub = eval $code;
  +        die "failed to compile code: $@\n$code\n" if $@;
  +        $result = &$sub($context);
  +        print STDERR "----------\n$code\n----------\n" 
  +            if $test->{ inflag }->{ show_code };
  +    }
  +    elsif (defined $code) {
  +        $result = '<DECLINE:' . $compiler->error() . '>';
  +    }
  +    else {
  +        $result = '<ERROR:' . $compiler->error() . '>';
  +    }
  +    return $result;
   }
   
  +
   __END__
  +
  +-- test no params --
  +[% MY %]
  +-- expect --
  +<ERROR:line 1: no variables set after MY>
  +
  +
  +-- test next directive ends params --
  +[% MY GET x %]
  +-- expect --
  +<ERROR:line 1: no variables set after MY>
   
  --- test set --
  -[% MY x = 10
  -      y = { foo => z, bar => x }
  -%]
  +-- test next directive foils expr --
  +[% MY x GET y %]
  +-- expect --
  +<ERROR:line 1: parser error - missing assignment after identifier 'x' (got 'GET')>
  +
  +-- test next directive after equals --
  +[% MY x = GET y %]
  +-- expect --
  +<ERROR:line 1: parser error - missing expression after '=' (got 'GET')>
  +
  +-- test dots not allowed --
  +[% MY x.y = 10 %]
  +-- expect --
  +<ERROR:line 1: parser error - missing assignment after identifier 'x' (got '.y')>
  +
  +-- test single x --
  +[% MY x = 10 -%]
   [% x %]
  +-- expect --
  +10
  +
  +-- test single y and z --
  +[% MY y = 20
  +      z = 30
  +-%]
  +y:[% y %] z:[% z %]
  +-- expect --
  +y:20 z:30
  +
  +-- test masking normal vars --
  +before: [% two %]
  +[% MY two = 'II' -%]
  +after: [% two %]
  +-- expect --
  +before: 2
  +after: II
  +
  +-- test my hash vars --
  +[% MY user = { 
  +        name  = 'Arthur Dent'
  +        email = 'dent@tt2.org'
  +      } 
  +-%]
  +[% x = user -%]
  +name: [% x.name %]
  +email: [% user.email %]
  +-- expect --
  +name: Arthur Dent
  +email: dent@tt2.org
  +
  +-- test my list vars --
  +[% MY numbers = [ 10, 20, 30 ] -%]
  +0: [% numbers.0 %]
   -- expect --
  -set stuff
  +0: 10
  +
  +-- test my vars from existing vars --
  +[% x = 10; 
  +   user = { name => 'Arthur Dent' };
  +
  +   MY y = x * 20
  +      z = user.name;
  +-%]
  +y: [% y %]
  +z: [% z %]
  +-- expect --
  +y: 200
  +z: Arthur Dent
  +
  +-- test my variables remain local to scope --
  +[% x = 10 -%]
  +outer x: [% x %]
  +[% IF x;
  +     MY x = 30;
  +     "inner x: $x\n";
  +   END
  +-%]
  +outer x: [% x %]
  +-- expect --
  +outer x: 10
  +inner x: 30
  +outer x: 10
  +
  +-- test my inside and out --
  +[% x = 10 -%]
  +outer x: [% x %]
  +[% MY x = 20 -%]
  +local x: [% x %]
  +[% IF x;
  +     MY x = 30;
  +     "inner x: $x\n";
  +   END
  +-%]
  +outer x: [% x %]
  +-- expect --
  +outer x: 10
  +local x: 20
  +inner x: 30
  +outer x: 20
  +
  +
  +# TODO: the 'MY' statement should remain local to the IF block as 
  +# if written [% IF 1; MY x = 40; END %] but it doesn't currently 
  +# because the side-effect handling doesn't force a side-effected
  +# expression to be contained within a block.  Hence the 
  +# generate_block() method isn't called and the context doesn't 
  +# get localised, and the MY variable x is added to the symbol table
  +# in the outer context.  It should be in an inner context.
  +
  +-- test side-effect set --
  +-- skip --
  +
  +[% x = 10 -%]
  +x: [% x %]
  +[% MY x = 40 IF 1 -%]
  +x: [% x %]
  +-- expect --
  +x: 10
  +x: 10
  +
   
   __END__