Re: tests for change #22539
[p5sagit/p5-mst-13.2.git] / ext / B / t / concise.t
CommitLineData
c517cc47 1#!./perl
2
3BEGIN {
4 chdir 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
724aa791 9plan tests => 38;
c517cc47 10
11require_ok("B::Concise");
12
13$out = runperl(switches => ["-MO=Concise"], prog => '$a', stderr => 1);
14
15# If either of the next two tests fail, it probably means you need to
16# fix the section labeled 'fragile kludge' in Concise.pm
17
c33fe613 18($op_base) = ($out =~ /^(\d+)\s*<0>\s*enter/m);
c517cc47 19
c33fe613 20is($op_base, 1, "Smallest OP sequence number");
c517cc47 21
c27ea44e 22($op_base_p1, $cop_base)
23 = ($out =~ /^(\d+)\s*<;>\s*nextstate\(main (-?\d+) /m);
c517cc47 24
c33fe613 25is($op_base_p1, 2, "Second-smallest OP sequence number");
26
27is($cop_base, 1, "Smallest COP sequence number");
62e36f8a 28
29# test that with -exec B::Concise navigates past logops (bug #18175)
30
31$out = runperl(
32 switches => ["-MO=Concise,-exec"],
33 prog => q{$a//=$b && print q/foo/},
34 stderr => 1,
35);
36
724aa791 37like($out, qr/print/, "'-exec' option output has print opcode");
38
39######## API tests v.60
40
41use Config; # used for perlio check
42B::Concise->import(qw(set_style set_style_standard add_callback
43 add_style walk_output));
44
45## walk_output argument checking
46
47# test that walk_output accepts a HANDLE arg
48foreach my $foo (\*STDOUT, \*STDERR) {
49 eval { walk_output($foo) };
50 is ($@, '', "walk_output() accepts STD* " . ref $foo);
51}
52
53# test that walk_output rejects non-HANDLE args
54foreach my $foo (undef, 0, "string",[], {}) {
55 eval { walk_output($foo) };
56 isnt ($@, '', "walk_output() rejects arg '$foo'");
57 $@=''; # clear the fail for next test
58}
59
60{ # any object that can print should be ok for walk_output
61 package Hugo;
62 sub new { my $foo = bless {} };
63 sub print { CORE::print @_ }
64}
65my $foo = new Hugo; # suggested this API fix
66eval { walk_output($foo) };
67is ($@, '', "walk_output() accepts obj that can print");
68
69# now test a ref to scalar
70eval { walk_output(\my $junk) };
71is ($@, '', "walk_output() accepts ref-to-sprintf target");
72
73$junk = "non-empty";
74eval { walk_output(\$junk) };
75is ($@, '', "walk_output() accepts ref-to-non-empty-scalar");
76
77## add_style
78my @stylespec;
79$@='';
80eval { add_style ('junk_B' => @stylespec) };
81like ($@, 'expecting 3 style-format args',
82 "add_style rejects insufficient args");
83
84@stylespec = (0,0,0); # right length, invalid values
85$@='';
86eval { add_style ('junk' => @stylespec) };
87is ($@, '', "add_style accepts: stylename => 3-arg-array");
88
89$@='';
90eval { add_style (junk => @stylespec) };
91like ($@, qr/style 'junk' already exists, choose a new name/,
92 "add_style correctly disallows re-adding same style-name" );
93
94# test new arg-checks on set_style
95$@='';
96eval { set_style (@stylespec) };
97is ($@, '', "set_style accepts 3 style-format args");
98
99@stylespec = (); # bad style
100
101eval { set_style (@stylespec) };
102like ($@, qr/expecting 3 style-format args/,
103 "set_style rejects bad style-format args");
104
105
106#### for content with doc'd options
107
108set_style_standard('concise'); # MUST CALL b4 output needed
109my $func = sub{ $a = $b+42 };
110
111@options = qw(
112 -basic -exec -tree -compact -loose -vt -ascii -main
113 -base10 -bigendian -littleendian
114 );
115foreach $opt (@options) {
116 walk_output(\my $out);
117 my $treegen = B::Concise::compile($opt, $func);
118 $treegen->();
119 #print "foo:$out\n";
120 isnt($out, '', "got output with option $opt");
121}
122
123## test output control via walk_output
124
125my $treegen = B::Concise::compile('-basic', $func); # reused
126
127{ # test output into a package global string (sprintf-ish)
128 our $thing;
129 walk_output(\$thing);
130 $treegen->();
131 ok($thing, "walk_output to our SCALAR, output seen");
132}
133
134{ # test output to GLOB, using perlio feature directly
135 skip 1, "no perlio on this build" unless $Config{useperlio};
136 open (my $fh, '>', \my $buf);
137 walk_output($fh);
138 $treegen->();
139 ok($buf, "walk_output to GLOB, output seen");
140}
141
142## Test B::Concise::compile error checking
143
144# call compile on non-CODE ref items
145foreach my $ref ([], {}) {
146 my $typ = ref $ref;
147 walk_output(\my $out);
148 eval { B::Concise::compile('-basic', $ref)->() };
149 like ($@, qr/^err: not a coderef: $typ/,
150 "compile detects $typ-ref where expecting subref");
151 # is($out,'', "no output when errd"); # announcement prints
152}
153
154# test against a bogus autovivified subref.
155# in debugger, it should look like:
156# 1 CODE(0x84840cc)
157# -> &CODE(0x84840cc) in ???
158sub nosuchfunc;
159eval { B::Concise::compile('-basic', \&nosuchfunc)->() };
160like ($@, qr/^err: coderef has no START/,
161 "compile detects CODE-ref w/o actual code");
162
163foreach my $opt (qw( -concise -exec )) {
164 eval { B::Concise::compile($opt,'non_existent_function')->() };
165 like ($@, qr/unknown function \(main::non_existent_function\)/,
166 "'$opt' reports non-existent-function properly");
167}