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