Commit | Line | Data |
6aaee015 |
1 | ### make sure we can find our conf.pl file |
2 | BEGIN { |
3 | use FindBin; |
4 | require "$FindBin::Bin/inc/conf.pl"; |
5 | } |
6 | |
7 | use strict; |
8 | use Test::More 'no_plan'; |
9 | use Data::Dumper; |
10 | use FileHandle; |
11 | use CPANPLUS::Error; |
12 | |
13 | my $conf = gimme_conf(); |
14 | |
15 | my $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(); |
37 | unlink $file; # just in case |
38 | |
39 | local $CPANPLUS::Error::MSG_FH = output_handle(); |
40 | local $CPANPLUS::Error::ERROR_FH = output_handle(); |
41 | |
42 | ok( -e $file, "Output redirect file exists" ); |
43 | ok( !-s $file, " Output file is empty" ); |
44 | |
45 | ### print a msg & error ### |
46 | for my $name ( keys %$map ) { |
47 | my $sub = __PACKAGE__->can( $name ); |
48 | |
49 | $sub->( $map->{$name}->[0], 1 ); |
50 | } |
51 | |
52 | ### must close it for Win32 tests! |
53 | close output_handle; |
54 | |
55 | ok( -s $file, " Output file now has size" ); |
56 | |
57 | my $fh = FileHandle->new( $file ); |
58 | ok( $fh, "Opened output file for reading " ); |
59 | |
60 | my $contents = do { local $/; <$fh> }; |
61 | my $string = CPANPLUS::Error->stack_as_string; |
62 | my $trace = CPANPLUS::Error->stack_as_string(1); |
63 | |
64 | ok( $contents, " Got the file contents" ); |
65 | ok( $string, "Got the error stack as string" ); |
66 | |
67 | |
68 | for my $type ( keys %$map ) { |
69 | my $tag = $type; $tag =~ s/.+?_//g; |
70 | |
71 | for my $str (@{ $map->{$type} } ) { |
72 | like( $contents, qr/\U\Q$tag/, |
73 | " Contents matches for '$type'" ); |
74 | like( $contents, qr/\Q$str/, |
75 | " Contents matches for '$type'" ); |
76 | |
77 | like( $string, qr/\U\Q$tag/, |
78 | " String matches for '$type'" ); |
79 | like( $string, qr/\Q$str/, |
80 | " String matches for '$type'" ); |
81 | |
82 | like( $trace, qr/\U\Q$tag/, |
83 | " Trace matches for '$type'" ); |
84 | like( $trace, qr/\Q$str/, |
85 | " Trace matches for '$type'" ); |
86 | |
87 | ### extra trace tests ### |
88 | like( $trace, qr/\Q$str\E.*?\Q$str/s, |
89 | " Trace holds proper traceback" ); |
90 | like( $trace, qr/\Q$0/, |
91 | " Trace holds program name" ); |
92 | like( $trace, qr/line/, |
93 | " Trace holds line number information" ); |
94 | } |
95 | } |
96 | |
97 | ### check the stack, flush it, check again ### |
98 | is( scalar(()=CPANPLUS::Error->stack), scalar(keys(%$map)), |
99 | "All items on stack" ); |
100 | is( scalar(()=CPANPLUS::Error->flush), scalar(keys(%$map)), |
101 | "All items flushed" ); |
102 | is( scalar(()=CPANPLUS::Error->stack), 0, |
103 | "No items on stack" ); |
104 | |
105 | } |
106 | |
107 | |
108 | # Local variables: |
109 | # c-indentation-style: bsd |
110 | # c-basic-offset: 4 |
111 | # indent-tabs-mode: nil |
112 | # End: |
113 | # vim: expandtab shiftwidth=4: |