Move CPANPLUS from lib/ to ext/
[p5sagit/p5-mst-13.2.git] / ext / CPANPLUS / t / 10_CPANPLUS-Error.t
CommitLineData
6aaee015 1### make sure we can find our conf.pl file
2BEGIN {
3 use FindBin;
4 require "$FindBin::Bin/inc/conf.pl";
5}
6
7use strict;
8use Test::More 'no_plan';
9use Data::Dumper;
10use FileHandle;
11use CPANPLUS::Error;
12
13my $conf = gimme_conf();
14
15my $map = {
16 cp_msg => ["This is just a test message"],
17 msg => ["This is just a test message"],
18 cp_error => ["This is just a test error"],
19 error => ["This is just a test error"],
20};
21
22### check if CPANPLUS::Error can do what we expect
23{ for my $name ( keys %$map ) {
24 can_ok('CPANPLUS::Error', $name);
25 can_ok('main', $name); # did it get exported?
26 }
27}
28
29### make sure we start with an empty stack
30{ CPANPLUS::Error->flush;
31 is( scalar(()=CPANPLUS::Error->stack), 0,
32 "Starting with empty stack" );
33}
34
35### global variables test ###
36{ my $file = output_file();
6aaee015 37
983ffab6 38 ### this *has* to be set, as we're testing the contents of the file
39 ### to see if it matches what's stored in the buffer.
6aaee015 40 local $CPANPLUS::Error::MSG_FH = output_handle();
41 local $CPANPLUS::Error::ERROR_FH = output_handle();
42
43 ok( -e $file, "Output redirect file exists" );
44 ok( !-s $file, " Output file is empty" );
45
46 ### print a msg & error ###
47 for my $name ( keys %$map ) {
48 my $sub = __PACKAGE__->can( $name );
49
50 $sub->( $map->{$name}->[0], 1 );
51 }
52
53 ### must close it for Win32 tests!
54 close output_handle;
55
56 ok( -s $file, " Output file now has size" );
57
58 my $fh = FileHandle->new( $file );
59 ok( $fh, "Opened output file for reading " );
60
61 my $contents = do { local $/; <$fh> };
62 my $string = CPANPLUS::Error->stack_as_string;
63 my $trace = CPANPLUS::Error->stack_as_string(1);
64
65 ok( $contents, " Got the file contents" );
66 ok( $string, "Got the error stack as string" );
67
68
69 for my $type ( keys %$map ) {
70 my $tag = $type; $tag =~ s/.+?_//g;
71
72 for my $str (@{ $map->{$type} } ) {
73 like( $contents, qr/\U\Q$tag/,
74 " Contents matches for '$type'" );
75 like( $contents, qr/\Q$str/,
76 " Contents matches for '$type'" );
77
78 like( $string, qr/\U\Q$tag/,
79 " String matches for '$type'" );
80 like( $string, qr/\Q$str/,
81 " String matches for '$type'" );
82
83 like( $trace, qr/\U\Q$tag/,
84 " Trace matches for '$type'" );
85 like( $trace, qr/\Q$str/,
86 " Trace matches for '$type'" );
87
88 ### extra trace tests ###
89 like( $trace, qr/\Q$str\E.*?\Q$str/s,
90 " Trace holds proper traceback" );
91 like( $trace, qr/\Q$0/,
92 " Trace holds program name" );
93 like( $trace, qr/line/,
94 " Trace holds line number information" );
95 }
96 }
97
98 ### check the stack, flush it, check again ###
99 is( scalar(()=CPANPLUS::Error->stack), scalar(keys(%$map)),
100 "All items on stack" );
101 is( scalar(()=CPANPLUS::Error->flush), scalar(keys(%$map)),
102 "All items flushed" );
103 is( scalar(()=CPANPLUS::Error->stack), 0,
104 "No items on stack" );
105
106}
107
108
109# Local variables:
110# c-indentation-style: bsd
111# c-basic-offset: 4
112# indent-tabs-mode: nil
113# End:
114# vim: expandtab shiftwidth=4: