r1659@llin: dpavlin | 2007-11-20 23:07:47 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 20 Nov 2007 22:07:45 +0000 (22:07 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 20 Nov 2007 22:07:45 +0000 (22:07 +0000)
 re-insert spaces in AST after sub to create valid normalize
 source if we are using sub name {} in normalize files [2.33]

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1061 07558da8-63fa-0310-ba24-9fe276d99e06

Makefile.PL
TODO
lib/WebPAC/Parser.pm
t/2-parse.t
t/conf/normalize/sub.pl [new file with mode: 0644]
t/conf/sub.yml [new file with mode: 0644]

index 0af8011..51f7a34 100644 (file)
@@ -5,7 +5,7 @@ use inc::Module::Install;
 
 
 name   'WebPAC';
-version        '2.32';
+version        '2.33';
 license        'GPL';
 
 requires       'YAML';
diff --git a/TODO b/TODO
index 8f1af4a..df6ce18 100644 (file)
--- a/TODO
+++ b/TODO
@@ -48,6 +48,7 @@
 + marc_template must use marc_indicators data
 + indicator(field,nr) nr=1|2 for MARC input -> rec(field,'i1') or rec(field,'i2')
 + validator don't accept 0 as valid subfield
++ subroutines gets mungled in normalization files [2.33]
 - rewrite WebPAC::Input to be based on Module::Pluggable
 - check usage of config in normalize file (database, input name?)
 - load_ds/save_ds should use on-disk hash to preserve inodes
index 0bbde1a..bdd79ce 100644 (file)
@@ -332,6 +332,13 @@ sub _parse_source {
                        my ($Document,$Element) = @_;
 
                        $Element->isa('PPI::Token::Word') or return '';
+
+                       if ( $Element->content eq 'sub' ) {
+                               # repair demage done by prune of whitespace
+                               $Element->insert_after( PPI::Token::Whitespace->new(' ') );
+                               return '';
+                       }
+
                        $Element->content eq 'lookup' or return '';
 
                        $log->debug("expansion: ", $Element->snext_sibling);
@@ -406,7 +413,7 @@ sub _parse_source {
                        }
 
                        $e[7]->remove;
-                       $e[8]->insert_before( new PPI::Token::Quote::Single( "'$key'" ) );
+                       $e[8]->insert_before( PPI::Token::Quote::Single->new( "'$key'" ) );
                        $e[8]->remove;
 
 
index 8f1f334..1c24231 100755 (executable)
@@ -3,7 +3,7 @@
 use strict;
 use blib;
 
-use Test::More tests => 55;
+use Test::More tests => 60;
 
 use YAML qw/LoadFile/;
 
@@ -61,7 +61,7 @@ ok(my $n = $parser->{_normalize_source}, "_normalize_source");
 diag "_normalize_source = ",dump($n) if ($debug);
 foreach my $db (keys %$n) {
        foreach my $i (keys %{$n->{$db}}) {
-               ok(my $r = $parser->normalize_rules($db,$i), "normalize_source($db/$i)");
+               ok(my $r = $parser->normalize_rules($db,$i), "normalize_rules($db/$i)");
                diag "normalize_rules($db,$i) = $r" if ($debug);
                cmp_ok($n->{$db}->{$i}, 'eq', $r, "same");
        }
@@ -111,7 +111,7 @@ $config_path = "$abs_path/conf/marc.yml";
 ok(-e $config_path, "$config_path exists");
 
 ok(
-       my $parser = new WebPAC::Parser(
+       $parser = new WebPAC::Parser(
                config => new WebPAC::Config( path => $config_path ),
                base_path => $abs_path,
                %LOG,
@@ -132,3 +132,27 @@ is_deeply($marc, {
        marc_repeatable_subfield        => 1,
 }, 'catched all marc_*');
 
+
+$config_path = "$abs_path/conf/sub.yml";
+
+ok(-e $config_path, "$config_path exists");
+
+ok(
+       $parser = new WebPAC::Parser(
+               config => new WebPAC::Config( path => $config_path ),
+               base_path => $abs_path,
+               %LOG,
+), "new");
+
+ok(my $rules = $parser->normalize_rules('sub','sub-input'), "normalize_rules(sub)");
+
+diag "rules: $rules" if $debug;
+
+our @test;
+eval $rules;
+diag "test = ",dump( @test ) if $debug;
+
+ok(! $@, "eval: $@" );
+
+is_deeply( [ @test ], [ "foo", "foo", "bar >>2<<", "bar >>42<<" ], 'sub executed' );
+
diff --git a/t/conf/normalize/sub.pl b/t/conf/normalize/sub.pl
new file mode 100644 (file)
index 0000000..c810355
--- /dev/null
@@ -0,0 +1,11 @@
+sub foo {
+       push @test, 'foo';
+}
+
+sub bar($) {
+       push @test, "bar >>" . shift(@_) . "<<";
+}
+
+foo();
+bar(foo());
+bar(42);
diff --git a/t/conf/sub.yml b/t/conf/sub.yml
new file mode 100644 (file)
index 0000000..bf8987a
--- /dev/null
@@ -0,0 +1,12 @@
+--- #YAML:1.0
+# DO NOT USE TABS FOR INDENTATION OR label/value SEPARATION!!!
+
+databases:
+  sub:
+    name: 'sub name'
+    input:
+      name: 'sub-input'
+      path: '/foo/bar/baz'
+      normalize:
+        path: 'conf/normalize/sub.pl'
+