Support templates in test_tei2korapxml

Change-Id: Ibe8981d8b5919661ef10a5b68b9c53bfb4da1af3
diff --git a/lib/Test/KorAP/XML/TEI.pm b/lib/Test/KorAP/XML/TEI.pm
index 7e82b9c..fa94273 100644
--- a/lib/Test/KorAP/XML/TEI.pm
+++ b/lib/Test/KorAP/XML/TEI.pm
@@ -66,7 +66,7 @@
 #   of the test suite.
 sub test_tei2korapxml {
   my ($file, $script, $pattern);
-  my ($env, $param) = ('', '');
+  my ($env, $param, $fh) = ('', '');
 
   if (@_ == 1) {
     $file = shift;
@@ -79,6 +79,17 @@
     $param   = $hash{param}  if $hash{param};
     $env     = $hash{env}    if $hash{env};
     $pattern = $hash{tmp}    if $hash{tmp};
+
+    # Handle template instead of hash
+    if ($hash{template} && !$file) {
+      my $pattern = delete $hash{template}->{pattern};
+      my $tmpl = i5_template(%{$hash{template}});
+
+      # Write template to file
+      ($fh, $file) = korap_tempfile($pattern);
+      print $fh $tmpl;
+      close($fh);
+    }
   };
 
   # Assume script in test environment
@@ -110,7 +121,7 @@
 
   # Read from written file
   my $stdout = '';
-  if (open(my $fh, '<', $fn)) {
+  if (open($fh, '<', $fn)) {
     binmode($fh);
     $stdout .= <$fh> while !eof($fh);
     close($fh);
diff --git a/t/test.t b/t/test.t
index de0c65e..a549e1e 100644
--- a/t/test.t
+++ b/t/test.t
@@ -8,7 +8,8 @@
   unshift @INC, "$FindBin::Bin/../lib";
 };
 
-use_ok('Test::KorAP::XML::TEI','korap_tempfile', 'i5_template');
+use_ok('Test::KorAP::XML::TEI','korap_tempfile', 'i5_template', 'test_tei2korapxml');
+
 
 subtest 'korap_tempfile' => sub {
   my ($fh, $filename) = korap_tempfile('test');
@@ -26,6 +27,7 @@
   like($filename, qr!KorAP-XML-TEI_.+?\.tmp$!, 'Filename pattern');
 };
 
+
 subtest 'i5_template' => sub {
   my $tpl = i5_template();
   my $t = Test::XML::Loy->new($tpl);
@@ -50,4 +52,24 @@
     ;
 };
 
+
+subtest 'test_tei2korapxml_i5_template' => sub {
+  test_tei2korapxml(
+    template => {
+      text => 'Das ist ein gutes Beispiel',
+      korpusSigle => 'a',
+      dokumentSigle => 'a/b',
+      textSigle => 'a/b.1'
+    },
+    param => '-ti'
+  )
+    ->stderr_like(qr!tei2korapxml: .*? text_id=a_b\.1!)
+    ->file_exists('a/b/1/header.xml')
+    ->file_exists('a/b/header.xml')
+    ->file_exists('a/header.xml')
+    ->unzip_xml('a/b/1/data.xml')
+    ->attr_is('raw_text', 'docid', 'a_b.1')
+    ->text_is('text', 'Das ist ein gutes Beispiel');
+};
+
 done_testing;