# ExampleTests.pm
#
# Copyright (c) 2002 Cunningham & Cunningham, Inc.
# Released under the terms of the GNU General Public License version 2 or later.
#
# Perl translation by Dave W. Smith <dws@postcognitive.com>

package ExampleTests;

use lib qw(../Fit);

use base qw(ColumnFixture);
use strict;


sub new {
    my $pkg = shift;

    return $pkg->SUPER::new(file => undef,
                            wiki => undef,
                            input => undef,
                            tables => undef,
                            fixture => undef,
                            runCounts => new Counts(),
                            reports => {},
                            @_);
}

sub run {
    my $self = shift;

    $self->{'input'} = $self->read($self->{'file'});
    $self->{'fixture'} = new Fixture();
    if ( $self->{'wiki'} eq "true" ) {
	$self->{'tables'} = new Parse($self->{'input'},
				      qw(wiki table tr td));
	$self->{'fixture'}->doTables($self->{'tables'}->parts());
    } else {
	$self->{'tables'} = new Parse($self->{'input'});
        $self->{'fixture'}->doTables($self->{'tables'});
    }
    $self->{'reports'}->{$self->{'file'}} = $self->{'tables'};
    $self->{'runCounts'}->tally($self->{'fixture'}->counts());
    $self->{'summary'}->{"counts run"} = $self->{'runCounts'};
}

sub right {
    my $self = shift;

    return $self->SUPER::right(@_) if @_;	#HACK deal with accidental overloading of "right"

    $self->run();
    return $self->{'fixture'}->counts()->{'right'};
}

sub wrong {
    my $self = shift;

    return $self->SUPER::wrong(@_) if @_;

    return $self->{'fixture'}->counts()->{'wrong'};
}

sub ignores {
    my $self = shift;

    return $self->SUPER::ignores(@_) if @_;

    return $self->{'fixture'}->counts()->{'ignores'};
}

sub exceptions {
    my $self = shift;

    return $self->SUPER::exceptions(@_) if @_;

    return $self->{'fixture'}->counts()->{'exceptions'};
}

sub read {
    my $self = shift;
    my($path) = @_;

    CORE::open(FILE, "<", $path) or die "$path: $!\n";
    my $chars = do {local $/; <FILE>};
    close(FILE);
    return $chars;
}



1;

__END__
// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Read license.txt in this directory.

package eg;

import fit.*;
import java.io.*;
import java.util.*;

public class ExampleTests extends ColumnFixture {

    public String file;
    public boolean wiki;

    protected String input;
    protected Parse tables;
    protected Fixture fixture;
    protected Counts runCounts = new Counts();
    protected Map reports = new HashMap();

    protected void run() throws Exception {
        input = read(new File("Documents/"+file));
        fixture = new Fixture();
        if (wiki) {
            tables = new Parse(input, new String[]{"wiki", "table", "tr", "td"});
            fixture.doTables(tables.parts);
        } else {
            tables = new Parse(input, new String[]{"table", "tr", "td"});
            fixture.doTables(tables);
        }
        reports.put(file, tables);
        runCounts.tally(fixture.counts);
        summary.put("counts run", runCounts);
    }


    public int right() throws Exception {
        run();
        return fixture.counts.right;
    }

    public int wrong() {
        return fixture.counts.wrong;
    }

    public int ignores() {
        return fixture.counts.ignores;
    }

    public int exceptions() {
        return fixture.counts.exceptions;
    }

    protected String read(File input) throws IOException {
        char chars[] = new char[(int)(input.length())];
        FileReader in = new FileReader(input);
        in.read(chars);
        in.close();
        return new String(chars);
    }

}

