MarpaX-Languages-M4

 view release on metacpan or  search on metacpan

t/m4.t  view on Meta::CPAN

#!perl
use strict;
use warnings FATAL   => 'all';
use File::Temp qw/tempfile/;
use File::Spec;
use Test::More;
use Log::Handler;
use Log::Any::Adapter;
use Log::Any::Adapter::Handler;
use Data::Section -setup => {
  header_re => qr/
    \A                # start
      _+!             # __!
        \s*           # any whitespace
          ([^!]+?)    # this is the actual name of the section
        \s*           # any whitespace
      !_+             # !__
      [\x0d\x0a]{1,2} # possible cariage return for windows files
    \z                # end
  /x};

my $log = Log::Handler->new(
    screen => {
        log_to   => "STDOUT",
        maxlevel => "debug",
        minlevel => "error",
        dateformat     => "%Y-%m-%d",
        timeformat     => "%H:%M:%S",
        message_layout => "%D %T %L %m",
    }
    );
Log::Any::Adapter->set('Handler', logger => $log);

BEGIN {
    use_ok('MarpaX::Languages::M4')
        || print "Bail out!\n";
}

#
# Variables used for portability
#
my $fhfoo = File::Temp->new();
print $fhfoo "bar\n";
close($fhfoo);
my $echo = File::Spec->catfile('inc', 'echo.pl');
my $tmpfile = File::Spec->catfile(File::Spec->tmpdir(), 'fooXXXXXX');

foreach (grep {/: input/} sort {$a cmp $b} __PACKAGE__->section_data_names) {
  my $testName = $_;
  my $inputRef = __PACKAGE__->section_data($testName);
  local @ARGV = ();
  if ($testName =~ /: input\((.+)\)/) {
    @ARGV = eval "($1)";
  }
  $testName =~ s/: input.*//;
  my $testIsCmp = 0;
  my $outputRef = __PACKAGE__->section_data("$testName: ANYoutput");
  if (! defined($outputRef)) {
    $outputRef = __PACKAGE__->section_data("$testName: output");
    $testIsCmp = 1;
  }
  #
  # Take care: Data::Section does not reproduce data EXACTLY
  #
  # For instance, test 112, I MIGHT write: \\-a\-b\-c\- to have it read \-a\-b\-c\-
  # ...
  #
  if ($testName eq '112 patsubst - warning') {
      $outputRef = \"abc
abc
\\-a\\-b\\-c\\-
";
  }

  my $input = ${$inputRef};
  #
  # For the special foo temporary file
  #
  $input =~ s/%%%FOO%%%/$fhfoo/g;
  $input =~ s/%%%ECHO%%%/$echo/g;
  $input =~ s/%%%TMPFILE%%%/$tmpfile/g;
  my $m4 = MarpaX::Languages::M4->new_with_options();
  #
  # Our include directory
  #
  $m4->include(['inc']);
  #
  # Global test settings so that the test-suite works on all platforms:
  # the test suite below assumes LF line-ending.
  #
  $m4->cmdtounix(1);
  $m4->inctounix(1);

  $testName =~ s/\d+\s*//;
  if ($testIsCmp) {
    cmp_ok($m4->parse($input), 'eq', ${$outputRef}, $testName);
  } else {
    ok(length($m4->parse($input)) > 0, $testName);
  }
}

done_testing();

__DATA__
__! 001 empty input: input !__
__! 001 empty input: output !__
__! 002 define - Hello World: input !__
define(`foo', `Hello world.')
foo
__! 002 define - Hello World: output !__



( run in 1.484 second using v1.01-cache-2.11-cpan-800906f7e73 )