#!/bin/sh
# This is really a -*-perl-*- script

: ${PERL=perl}
: ${srcdir=.}

case "$PERL" in
  *'missing perl')
  echo 1>&2 "$0: configure didn't find a usable version of Perl,"
    "so can't run this test"
  # '
  exit 77
  ;;
esac

exec $PERL -w -I$srcdir/.. -MFetish -- - << \EOF
require 5.003;
use strict;

(my $program_name = $0) =~ s|.*/||;

# Turn off localisation of executable's ouput.
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;

my $prog = $ENV{PROG} || die "$0: \$PROG not specified in environment\n";

my @Tests =
    (
     ['a1',  qw('%s\\n' whale),			{OUT => "whale"}],
     ['a2',  qw('%% %s%%%%%%\\n' whale),	{OUT => "% whale%%%"}],
     ['b',   qw('%s %s\\n' whale fish),		{OUT => "whale fish"}],
     ['b1',  qw('%s\\n' whale fish),		{OUT => "whale\nfish"}],
     ['c',   qw('%s\\n' whale fish),		{OUT => "whale\nfish"}],
     ['d1',  qw('%9.5s\\n' whale),		{OUT => "    whale"}],
     ['d2',  qw('%9.4s\\n' whale),		{OUT => "     whal"}],
     ['d3',  qw('%9.4s\\n' whale),		{OUT => "     whal"}],
     ['e1',  qw('%4.4s\\n' whale),		{OUT => "whal"}],
     ['e2',  qw('%-9.5s\\n' whale),		{OUT => "whale    "}],
     ['e3',  qw('%-9.4s\\n' whale),		{OUT => "whal     "}],
     ['f1',  qw('%4.9s\\n' whale),		{OUT => "whale"}],
     ['f2',  qw('%-4.9s\\n' whale),		{OUT => "whale"}],
     ['g1',  qw('%*.*s\\n' 9 4 whale),		{OUT => "     whal"}],
     ['g2',  qw('%*.*s\\n' 4 9 whale),		{OUT => "whale"}],
     ['g3',  qw('%-*.*s\\n' 9 4 whale),		{OUT => "whal     "}],
     ['h1',  qw('%d %d\\n' 42 69),		{OUT => "42 69"}],
     ['h2',  qw('%3d %4d\\n' 42 69),		{OUT => " 42   69"}],
     ['h4',  qw('%03d\\n' 7),			{OUT => "007"}],
     ['h5',  qw('%3.4d\\n' 4269),		{OUT => "4269"}],
     ['i1',  qw('%3.4g\\n' 42.69),		{OUT => "42.69"}],
     ['i2',  qw('%4.3g\\n' 42.69),		{OUT => "42.7"}],

     ['m1',  qw('%1$s\\n' whale fish),		{OUT => "whale"}],
     ['m2',  qw('%% %1$s%%%%%%\\n' whale fish),	{OUT => "% whale%%%"}],
     ['n1',  qw('%1$s %2$s\\n' whale fish),	{OUT => "whale fish"}],
     ['n2',  qw('%1$s %2$s\\n' whale),		{OUT => "whale "},
		 {ERR => "$prog: 2: Argument number out of range.\n"}],
     ['o1',  qw('%2$s %1$s\\n' whale fish),	{OUT => "fish whale"}],
     ['o2',  qw('%2$s %1$s\\n' whale),		{OUT => " whale"},,
		 {ERR => "$prog: 2: Argument number out of range.\n"}],
     ['p1',  qw('%1$9.5s\\n' whale),		{OUT => "    whale"}],
     ['p2',  qw('%1$9.4s\\n' whale),		{OUT => "     whal"}],
     ['p3',  qw('%1$9.4s\\n' whale),		{OUT => "     whal"}],
     ['q1',  qw('%1$4.4s\\n' whale),		{OUT => "whal"}],
     ['q2',  qw('%1$-9.5s\\n' whale),		{OUT => "whale    "}],
     ['q3',  qw('%1$-9.4s\\n' whale),		{OUT => "whal     "}],
     ['r1',  qw('%1$4.9s\\n' whale),		{OUT => "whale"}],
     ['r2',  qw('%1$-4.9s\\n' whale),		{OUT => "whale"}],
     ['s1',  qw('%1$*3$.*2$s\\n' whale 4 9),	{OUT => "     whal"}],
     ['s2',  qw('%1$*3$.*2$s\\n' whale 9 4),	{OUT => "whale"}],
     ['s3',  qw('%2$-*1$.*3$s\\n' 9 whale 4),	{OUT => "whal     "}],

     ['warn-1', qw('%1$s%s\\n' '' ''),
		{ERR => "$prog: %s: All or no arguments must be numbered.\n"},
		{EXIT => '1'}],
     ['warn-2', qw('%4$s\\n' whale),
		{OUT => ""},
		{ERR => "$prog: 4: Argument number out of range.\n"},
		{EXIT => '0'}],

    );

# Append a newline to end of each expected `OUT' string.
my $t;
foreach $t (@Tests)
  {
    my $arg1 = $t->[1];
    my $e;
    foreach $e (@$t)
      {
	$e->{OUT} = "$e->{OUT}\n"
	  if ref $e eq 'HASH' and exists $e->{OUT};
      }
  }

my $save_temps = $ENV{SAVE_TEMPS};
my $verbose = $ENV{VERBOSE};

my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
exit $fail;
EOF
