Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (101 sloc) 3.211 kb
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/context.t
4 #
5 # Test the Template::Context.pm module.
6 #
7 # Written by Andy Wardley <abw@kfs.org>
8 #
9 # Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
10 # Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
11 #
12 # This is free software; you can redistribute it and/or modify it
13 # under the same terms as Perl itself.
14 #
15 # $Id$
16 #
17 #========================================================================
18
19 use strict;
20 use lib qw( ./lib ../lib );
21 use Template::Test;
22
23 #ntests(3);
24
25 my $tt = Template->new({
26 INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
27 TRIM => 1,
28 POST_CHOMP => 1,
29 });
30
31 my $ttperl = Template->new({
32 INCLUDE_PATH => [ qw( t/test/lib test/lib ) ],
33 TRIM => 1,
34 EVAL_PERL => 1,
35 POST_CHOMP => 1,
36 });
37
38 # test we created a context object and check internal values
39 my $context = $tt->service->context();
40 ok( $context );
41 ok( $context->trim() );
42 ok( ! $context->eval_perl() );
43
44 ok( $context = $ttperl->service->context() );
45 ok( $context->trim() );
46 ok( $context->eval_perl() );
47
48 # test we can fetch a template via template()
49 my $template = $context->template('header');
50 ok( $template );
51 ok( UNIVERSAL::isa($template, 'Template::Document') );
52
53 # test that non-existance of a template is reported
54 $template = $context->template('no_such_template');
55 ok( ! $template );
56 ok( $context->error() eq 'no_such_template: template not found' );
57
58 # check that template() returns CODE and Template::Document refs intact
59 my $code = sub { return "this is a hard-coded template" };
60 $template = $context->template($code);
61 ok( $template eq $code );
62
63 my $doc = "this is a document";
64 $doc = bless \$doc, 'Template::Document';
65 $template = $context->template($doc);
66 ok( $template eq $doc );
67 ok( $$doc = 'this is a document' );
68
69 # check the use of visit() and leave() to add temporary BLOCK lookup
70 # tables to the context's search space
71 my $blocks1 = {
72 some_block_1 => 'hello',
73 };
74 my $blocks2 = {
75 some_block_2 => 'world',
76 };
77
78 ok( ! $context->template('some_block_1') );
79 $context->visit($blocks1);
80 ok( $context->template('some_block_1') eq 'hello' );
81 ok( ! $context->template('some_block_2') );
82 $context->visit($blocks2);
83 ok( $context->template('some_block_1') eq 'hello' );
84 ok( $context->template('some_block_2') eq 'world' );
85 $context->leave();
86 ok( $context->template('some_block_1') eq 'hello' );
87 ok( ! $context->template('some_block_2') );
88 $context->leave();
89 ok( ! $context->template('some_block_1') );
90 ok( ! $context->template('some_block_2') );
91
92 # test that reset() clears all blocks
93 $context->visit($blocks1);
94 ok( $context->template('some_block_1') eq 'hello' );
95 ok( ! $context->template('some_block_2') );
96 $context->visit($blocks2);
97 ok( $context->template('some_block_1') eq 'hello' );
98 ok( $context->template('some_block_2') eq 'world' );
99 $context->reset();
100 ok( ! $context->template('some_block_1') );
101 ok( ! $context->template('some_block_2') );
102
103 #test_expect(\*DATA, $tt, &callsign);
104
105 __DATA__
106
107
108
109 __END__
110 Methods:
111 template() - ok
112 plugin()
113 filter()
114 process()
115 include()
116 throw()
117 catch()
118 localise()/delocalise()
119 visit()/leave()/reset - ok
Something went wrong with that request. Please try again.