Permalink
Browse files

Fixed up DEBUG option to accept list of text strings, e.g. "undef,var…

…s,etc"
  • Loading branch information...
1 parent b57685a commit bdcc005af21b048aebc311586b33c4c9af5cbc49 @abw committed Feb 10, 2012
Showing with 69 additions and 67 deletions.
  1. +0 −5 lib/Template/TT2.pm
  2. +23 −57 lib/Template/TT2/Constants.pm
  3. +1 −1 lib/Template/TT2/Context.pm
  4. +28 −2 lib/Template/TT2/Hub.pm
  5. +1 −1 lib/Template/TT2/Templates.pm
  6. +16 −1 t/stash/stash2.t
View
@@ -94,11 +94,6 @@ sub init {
$self->{ throw } = $config->{ THROW };
$self->{ config } = $config;
- # TODO: debug flags
- # convert textual DEBUG flags to number
-# $config->{ DEBUG } = debug_flags($self, $debug)
-# if defined $debug && ! looks_like_number $debug;
-
# Trigger initialisation of service and context so that we can sample
# any package variable defaults now before they go changing. This is
# unlikely to affect anyone in the Real World, but we rely on this
@@ -40,7 +40,8 @@ use Badger::Class
debug => 'DEBUG_OFF DEBUG_ON DEBUG_UNDEF DEBUG_VARS
DEBUG_DIRS DEBUG_STASH DEBUG_CONTEXT DEBUG_PARSER
DEBUG_TEMPLATES DEBUG_PLUGINS DEBUG_FILTERS
- DEBUG_SERVICE DEBUG_ALL DEBUG_CALLER DEBUG_FLAGS',
+ DEBUG_SERVICE DEBUG_ALL DEBUG_CALLER DEBUG_FLAGS
+ @DEBUG_VALUES $DEBUG_OPTIONS',
cache => 'CACHE_UNLIMITED',
flow => 'FLOW_STOP',
stash => 'STASH_PRIVATE STASH_IMPORT STASH_UNDEF',
@@ -140,65 +141,30 @@ use Badger::Class
STASH_PRIVATE => qr/^[_.]/, # private members begin with _ or .
};
-1;
-
-__END__
-our $DEBUG_FLAGS = {
- off => DEBUG_OFF,
- on => DEBUG_ON,
- undef => DEBUG_UNDEF,
- vars => DEBUG_VARS,
- dirs => DEBUG_DIRS,
- stash => DEBUG_STASH,
- context => DEBUG_CONTEXT,
- parser => DEBUG_PARSER,
- provider => DEBUG_PROVIDER,
- plugins => DEBUG_PLUGINS,
- filters => DEBUG_FILTERS,
- service => DEBUG_SERVICE,
- all => DEBUG_ALL,
- caller => DEBUG_CALLER,
+our @DEBUG_VALUES = qw(
+ DEBUG_OFF DEBUG_ON DEBUG_UNDEF DEBUG_VARS DEBUG_DIRS DEBUG_STASH
+ DEBUG_CONTEXT DEBUG_PARSER DEBUG_TEMPLATES DEBUG_PLUGINS DEBUG_FILTERS
+ DEBUG_SERVICE DEBUG_ALL DEBUG_CALLER
+);
+
+our $DEBUG_OPTIONS = {
+ off => DEBUG_OFF,
+ on => DEBUG_ON,
+ undef => DEBUG_UNDEF,
+ vars => DEBUG_VARS,
+ dirs => DEBUG_DIRS,
+ stash => DEBUG_STASH,
+ context => DEBUG_CONTEXT,
+ parser => DEBUG_PARSER,
+ templates => DEBUG_TEMPLATES,
+ plugins => DEBUG_PLUGINS,
+ filters => DEBUG_FILTERS,
+ service => DEBUG_SERVICE,
+ all => DEBUG_ALL,
+ caller => DEBUG_CALLER,
};
-sub debug_flags {
- my ($self, $debug) = @_;
- my (@flags, $flag, $value);
- $debug = $self unless defined($debug) || ref($self);
-
- if ($debug =~ /^\d+$/) {
- foreach $flag (@DEBUG) {
- next if $flag =~ /^DEBUG_(OFF|ALL|FLAGS)$/;
-
- # don't trash the original
- my $copy = $flag;
- $flag =~ s/^DEBUG_//;
- $flag = lc $flag;
- return $self->error("no value for flag: $flag")
- unless defined($value = $DEBUG_OPTIONS->{ $flag });
- $flag = $value;
-
- if ($debug & $flag) {
- $value = $DEBUG_OPTIONS->{ $flag };
- return $self->error("no value for flag: $flag") unless defined $value;
- push(@flags, $value);
- }
- }
- return wantarray ? @flags : join(', ', @flags);
- }
- else {
- @flags = split(/\W+/, $debug);
- $debug = 0;
- foreach $flag (@flags) {
- $value = $DEBUG_OPTIONS->{ $flag };
- return $self->error("unknown debug flag: $flag") unless defined $value;
- $debug |= $value;
- }
- return $debug;
- }
-}
-
-
1;
__END__
@@ -110,7 +110,7 @@ sub init {
# CONFIG - original configuration hash
# EXPOSE_BLOCKS - make blocks visible as pseudo-files
# DEBUG_FORMAT - format for generating template runtime debugging messages
- # DEBUG - format for generating template runtime debugging messages
+ # DEBUG - internal flag based on DEBUG_CONTEXT being set
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$self->{ RECURSION } = $config->{ RECURSION } || 0;
$self->{ EVAL_PERL } = $config->{ EVAL_PERL } || 0;
View
@@ -4,9 +4,9 @@ use Template::TT2::Class
version => 0.01,
debug => 0,
base => 'Template::TT2::Modules',
- utils => 'blessed',
+ utils => 'blessed numlike',
filesystem => 'VFS FS',
- constants => 'HASH ARRAY CODE GLOB SCALAR',
+ constants => 'HASH ARRAY CODE GLOB SCALAR :debug',
constant => {
PRINT_METHOD => 'print',
},
@@ -56,6 +56,11 @@ sub found_object {
sub init {
my ($self, $config) = @_;
+ my $debug = $config->{ DEBUG };
+
+ # convert textual DEBUG flags to number
+ $config->{ DEBUG } = $self->debug_flags($debug)
+ if defined $debug;
# TODO: QUIET => ON_WARN
$self->configure($config);
@@ -205,6 +210,27 @@ sub output_file {
}
+sub debug_flags {
+ my ($self, $debug) = @_;
+
+ return $debug
+ if defined $debug && numlike $debug;
+
+ my @flags = split(/\W+/, lc $debug);
+ my $result = 0;
+ my ($flag, $value);
+
+ foreach $flag (@flags) {
+ $flag =~ s/^DEBUG_//i;
+ $value = $DEBUG_OPTIONS->{ $flag };
+ return $self->error_msg( invalid => 'debug flag', $flag )
+ unless defined $value;
+ $result |= $value;
+ }
+ return $result;
+}
+
+
sub destroy {
my $self = shift;
@@ -12,7 +12,7 @@ use Template::TT2::Class
accessors => 'hub',
filesystem => 'FS VFS Cwd Dir File',
constants =>
- 'SCALAR ARRAY HASH GLOB BLANK UNICODE DEBUG_TEMPLATES DEBUG_FLAGS
+ 'SCALAR ARRAY HASH GLOB BLANK UNICODE DEBUG_TEMPLATES
TT2_DOCUMENT TT2_CACHE TT2_STORE TT2_PARSER MSWIN32',
constant => {
INPUT_TEXT => 'input text',
View
@@ -20,7 +20,7 @@ use Badger
lib => '../lib ../../lib ../../blib/lib ../../blib/arch';
use Template::TT2::Test
- tests => 10,
+ tests => 12,
debug => 'Template::TT2::Stash',
args => \@ARGV;
@@ -48,6 +48,8 @@ my $data = {
my $engines = {
'default' => ENGINE->new(),
'warn' => ENGINE->new( DEBUG => DEBUG_UNDEF, DEBUG_FORMAT => '' ),
+ 'warn2' => ENGINE->new( DEBUG => 'DEBUG_UNDEF', DEBUG_FORMAT => '' ),
+ 'warn3' => ENGINE->new( DEBUG => 'undef, vars', DEBUG_FORMAT => '' ),
'strict' => ENGINE->new( STRICT => 1 ),
};
@@ -73,6 +75,19 @@ a:
-- expect --
ERROR: undef error - Undefined variable: a
+-- test ditto with DEBUG_UNDEF specified as string --
+-- use warn2 --
+[% TRY; a; CATCH; "ERROR: $error"; END %]
+-- expect --
+ERROR: undef error - Undefined variable: a
+
+
+-- test and again with undef specified as string, among others --
+-- use warn3 --
+[% TRY; a; CATCH; "ERROR: $error"; END %]
+-- expect --
+ERROR: undef error - Undefined variable: a
+
-- test undefined variable warning via STRICT --
-- use strict --
[% TRY; a; CATCH; "ERROR: $error"; END %]

0 comments on commit bdcc005

Please sign in to comment.