view Lib/IMPL/Test.pm @ 166:4267a2ac3d46

Added Class::Template, Rewritten SQL::Schema 'use parent' directive instead of 'use base'
author wizard
date Sat, 23 Apr 2011 23:12:06 +0400
parents 76515373dac0
children 029c9610528c
line wrap: on
line source

package IMPL::Test;
use strict;
use warnings;

require IMPL::Test::SkipException;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(&test &shared &failed &cmparray &skip &run_plan &assert);

require IMPL::Test::Unit;
require IMPL::Test::Plan;
require IMPL::Test::TAPListener;
use IMPL::Class::Member;

sub test($$) {
    my ($name,$code) = @_;
    my $class = caller;
    
    $class->set_meta(
        new IMPL::Test::Unit::TestInfo( $name, $code )
    );
}

sub shared($) {
    my ($propInfo) = @_;
    
    my $class = caller;
    
    die new IMPL::Exception("Only properties could be declared as shared",$propInfo->Name) unless eval {$propInfo->isa('IMPL::Class::PropertyInfo')};
    die new IMPL::Exception("You can't mark the readonly property as shared",$propInfo->Name) unless $propInfo->canSet;
    die new IMPL::Exception("Only public properties could be declared as shared",$propInfo->Name) unless $propInfo->Access == IMPL::Class::Member::MOD_PUBLIC;
    
    $class->set_meta(new IMPL::Test::Unit::SharedData($propInfo->Name));
}

sub failed($;@) {
    die new IMPL::Test::FailException(@_);
}

sub assert {
	my ($condition,@params) = @_;
	
	die new IMPL::Test::FailException(@params ? @params : ("Assertion failed" , _GetSourceLine( (caller)[1,2] )) ) unless $condition;
}

sub skip($;@) {
	die new IMPL::Test::SkipException(@_);
}

sub cmparray {
    my ($a,$b) = @_;
    
    return 0 unless @$a == @$b;
    
    for (my $i=0; $i < @$a; $i++ ) {
        return 0 unless $a->[$i] eq $b->[$i];
    }
    
    return 1;
}

sub _GetSourceLine {
	my ($file,$line) = @_;
	
	open my $hFile, $file or return "failed to open file: $file: $!";
	
	my $text;
	$text = <$hFile> for ( 1 .. $line);
	chomp $text;
	$text =~ s/^\s+//;
	return "line $line: $text";
}

sub run_plan {
	my (@units) = @_;
	
	my $plan = new IMPL::Test::Plan(@units);
	
	$plan->Prepare;
	$plan->AddListener(new IMPL::Test::TAPListener);
	$plan->Run;
}
1;