#!/usr/bin/perl -w

use strict;

use Test::More qw(no_plan);

use lib "go/scratch/tools/";

use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

use_ok( 'GO::MsgLite' );

run_tests();

exit(0);

sub run_tests {

my $obj = GO::MsgLite->new();         # create an object
is( defined $obj, 1, 'object creation' );
isa_ok( $obj, 'GO::MsgLite');

# make a fake object with MsgLite as its base
{	package Object;
	use base qw(GO::MsgLite);
}

undef $obj;

$obj = Object->new();
is( defined $obj, 1, 'object creation' );
isa_ok( $obj, 'Object');
isa_ok( $obj, 'GO::MsgLite');

## make sure we can do all the methods we think we can

can_ok($obj, qw(
new
set_verbosity
info_msg
add_info
warning_msg
add_warning
warning_error
fatal_msg
add_fatal
fatal_error
add_message_list
add_errors
add_error_list
add_messages
add_msg_list
add_msgs
_add_message
get_all_msgs
get_all_errors
get_error_list
get_msg_list
get_last_msg
clear_all_msgs
clear_last_msg
get_msg_by_class
has_msgs
has_errors
has_error_list
has_n_msgs
has_n_errors
_printmsg
printerr
debugme
startme
endme));

## Let's try adding some messages
my $results;

$obj->info_msg();
$results = $obj->get_all_msgs;
isn't(defined $obj->has_msgs, 1, 'blank message');

print STDERR "has messages: ".Dumper($obj->has_msgs);

$obj->info_msg('This is an info message');
is($obj->has_msgs, 1, 'Add an info message');

#print STDERR "results: ".Dumper($obj->get_all_msgs)."\n";

$obj->warning_msg('This is a warning message');
$obj->warning_msg('This is another warning message');
$obj->warning_msg('This is a third warning message');

#$obj->fatal_msg('This is a fatal message');

eval { $obj->can('crap_msg') };
if ($@)
{	print STDERR "$@";
}

my $warnings = $obj->get_msg_by_class('warning');

# we should have three warning messages
is( scalar @{ $obj->get_msg_by_class('warning') }, 3, 'get message by class "warning"' );

print STDERR "\n\n\n";


## let's set the verbosity level
$obj->set_verbosity('0');

$obj->startme;
$obj->debugme("here's a test message");
$obj->printerr("here's an error message");
$obj->info_msg("here's an info message");
$obj->endme;

## let's set the verbosity level to low
$obj->set_verbosity('verbose');

$obj->startme;
$obj->debugme("here's a test message");
$obj->printerr("here's an error message");
$obj->info_msg("here's an info message");
$obj->endme;
print STDERR "\n\n\n";

$obj->set_verbosity('medium');
$obj->startme;
$obj->debugme("here's a test message");
$obj->info_msg("here's an info message");
$obj->endme;
print STDERR "\n\n\n";

## high verbosity
$obj->set_verbosity('high');

$obj->startme;
$obj->debugme("here's a test message");
$obj->printerr("here's an error message");
$obj->info_msg("here's an info message");
$obj->endme;


print STDERR "\n\n\n";

## get message by class - no class
#$warnings = ;
isn't ( defined $obj->get_msg_by_class, 1, 'get message by class - no class');

## get message by class - invalid class
$warnings = $obj->get_msg_by_class('crap');
isn't ( defined $obj->get_msg_by_class('crap'), 1, 'get message by class - invalid class');

## get message by class - no messages
$warnings = $obj->get_msg_by_class('fatal');
isn't (defined $obj->get_msg_by_class('fatal'), 1, 'get message by class - no messages');

## clear the messages
$obj->clear_all_msgs;
isn't (defined $obj->has_msgs, 1, 'Clear all messages');


## add three valid messages, one which is invalid
$obj->add_errors([ 'Message of unknown type', { class => 'bollocks', msg_html => '<p>A message with the type made up</p>' }, {class=>'info', msg=>'valid message' }, {class=>'info'}]);

## Get all the messages with the level indicated
my $msg_h = $obj->get_all_msgs('with_level');

is( ref $msg_h eq 'HASH' && $msg_h->{level} eq 'warning' && scalar @{$msg_h->{list}} == 3, 1, 'Testing get all messages with level');

# get the last message
is( ($obj->get_last_msg->{CLASS} eq 'info' && $obj->get_last_msg->{MSG} eq 'valid message'), 1, 'Testing get last message');


# clear the last message
$obj->clear_last_msg;

is( ($obj->get_last_msg->{CLASS} eq 'warning' && defined $obj->get_last_msg->{MSG_HTML}), 1, 'Testing clear last message');

$obj->fatal_error("this killed me!");
$obj->fatal_msg("but I was already dead");

is( $obj->has_n_msgs(), 4, 'Testing has_n_msgs' );
is( $obj->has_n_errors('fatal'), 2, 'Testing has_n_msgs with level' );
is( $obj->has_n_errors('crap'), undef, 'Testing has_n_msgs with invalid level' );
is( $obj->has_n_errors('info'), 0, 'Testing has_n_messages with missing level' );

# add_message_list
# get_all_msgs
# get_last_msg
# clear_all_msgs
# clear_last_msg
# get_msg_by_class
# has_msgs
# has_n_msgs
}
