#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

use FileHandle;
use IPC::Open2;

# colours for output
our $norm   = "";
#our $bold   = "";
our $green  = "";
our $red    = "";
#our $yellow = "";


our $tests = 0;

our $test_input;
our $test_output;
our $expected_output;

sub read_input() {
  my $text = "";
  while (<>) {
    last if /^RESULT:/;
    $text .= $_;
  }
  return $text;
}

sub read_output() {
  my $text = "";
  while (<>) {
    last if /^DONE/;
    $text .= $_;
  }
  return $text;
}

while (<>) {
  if (/^TEST:/) {
    $test_input = read_input;
    $expected_output = read_output;

    my($rdr, $wdr) = (FileHandle->new, FileHandle->new);
    my $pid = open2($rdr, $wdr, "../dprog | indent -gnu -i4 --no-tabs");
    print $wdr $test_input;
    close($wdr) or die "Error executing $tests!";

    $test_output = "";
    while (<$rdr>) {
      $test_output .= $_;
    }
    close($rdr) or die "Error executing $tests!)";

    if ($test_output ne $expected_output) {
      print "Testing:\n$test_input";
      print "\t\t=>[${red}FAILURE${norm}]\n";
      print "Expected output \n$expected_output"; 
      print "differs from actual output \n$test_output";
      print "in test $tests\n";
      exit(1);
    }
  }
}

print "\t\t\t\t\t[${green}SUCCESS${norm}]\n";




syntax highlighted by Code2HTML, v. 0.9.1