Commit | Line | Data |
---|---|---|
3a5d7bb8 JH |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
5 | @INC = '../lib'; | |
6 | } | |
7 | ||
8 | use Test::More tests => 15; | |
9 | ||
10 | use_ok( 'B::Terse' ); | |
11 | ||
12 | # indent should return a string indented four spaces times the argument | |
1212f7d8 JH |
13 | is( B::Terse::indent(2), ' ' x 8, 'indent with an argument' ); |
14 | is( B::Terse::indent(), '', 'indent with no argument' ); | |
3a5d7bb8 JH |
15 | |
16 | # this should fail without a reference | |
17 | eval { B::Terse::terse('scalar') }; | |
1212f7d8 | 18 | like( $@, qr/not a reference/, 'terse() fed bad parameters' ); |
3a5d7bb8 JH |
19 | |
20 | # now point it at a sub and see what happens | |
21 | sub foo {} | |
22 | ||
23 | my $sub; | |
24 | eval{ $sub = B::Terse::compile('', 'foo') }; | |
1212f7d8 JH |
25 | is( $@, '', 'compile()' ); |
26 | ok( defined &$sub, 'valid subref back from compile()' ); | |
3a5d7bb8 JH |
27 | |
28 | # and point it at a real sub and hope the returned ops look alright | |
29 | my $out = tie *STDOUT, 'TieOut'; | |
30 | $sub = B::Terse::compile('', 'bar'); | |
31 | $sub->(); | |
32 | ||
33 | # now build some regexes that should match the dumped ops | |
34 | my ($hex, $op) = ('\(0x[a-f0-9]+\)', '\s+\w+'); | |
35 | my %ops = map { $_ => qr/$_ $hex$op/ } | |
36 | qw ( OP COP LOOP PMOP UNOP BINOP LOGOP LISTOP ); | |
37 | ||
38 | # split up the output lines into individual ops (terse is, well, terse!) | |
39 | # use an array here so $_ is modifiable | |
40 | my @lines = split(/\n+/, $out->read); | |
41 | foreach (@lines) { | |
42 | next unless /\S/; | |
43 | s/^\s+//; | |
44 | if (/^([A-Z]+)\s+/) { | |
45 | my $op = $1; | |
46 | next unless exists $ops{$op}; | |
1212f7d8 | 47 | like( $_, $ops{$op}, "$op " ); |
3a5d7bb8 JH |
48 | delete $ops{$op}; |
49 | s/$ops{$op}//; | |
50 | redo if $_; | |
51 | } | |
52 | } | |
53 | ||
54 | warn "# didn't find " . join(' ', keys %ops) if keys %ops; | |
55 | ||
56 | # XXX: | |
57 | # this tries to get at all tersified optypes in B::Terse | |
58 | # if you add AV, NULL, PADOP, PVOP, or SPECIAL, add it to the regex above too | |
59 | # | |
60 | use vars qw( $a $b ); | |
61 | sub bar { | |
62 | # OP SVOP COP IV here or in sub definition | |
63 | my @bar = (1, 2, 3); | |
64 | ||
65 | # got a GV here | |
66 | my $foo = $a + $b; | |
67 | ||
68 | # NV here | |
69 | $a = 1.234; | |
70 | ||
71 | # this is awful, but it gives a PMOP | |
72 | my $boo = split('', $foo); | |
73 | ||
74 | # PMOP | |
75 | LOOP: for (1 .. 10) { | |
76 | last LOOP if $_ % 2; | |
77 | } | |
78 | ||
79 | # make a PV | |
80 | $foo = "a string"; | |
f3be9b72 RGS |
81 | |
82 | # make an OP_SUBSTCONT | |
83 | $foo =~ s/(a)/$1/; | |
3a5d7bb8 JH |
84 | } |
85 | ||
4f9392f7 JH |
86 | SKIP: { |
87 | use Config; | |
63141bc4 GS |
88 | skip("- B::Terse won't grok RVs under ithreads yet", 1) |
89 | if $Config{useithreads}; | |
4f9392f7 JH |
90 | # Schwern's example of finding an RV |
91 | my $path = join " ", map { qq["-I$_"] } @INC; | |
e69a2255 | 92 | $path = '-I::lib -MMac::err=unix' if $^O eq 'MacOS'; |
4f9392f7 JH |
93 | my $redir = $^O eq 'MacOS' ? '' : "2>&1"; |
94 | my $items = qx{$^X $path "-MO=Terse" -le "print \\42" $redir}; | |
95 | like( $items, qr/RV $hex \\42/, 'RV' ); | |
96 | } | |
3a5d7bb8 JH |
97 | |
98 | package TieOut; | |
99 | ||
100 | sub TIEHANDLE { | |
101 | bless( \(my $out), $_[0] ); | |
102 | } | |
103 | ||
104 | sub PRINT { | |
105 | my $self = shift; | |
106 | $$self .= join('', @_); | |
107 | } | |
108 | ||
109 | sub PRINTF { | |
110 | my $self = shift; | |
111 | $$self .= sprintf(@_); | |
112 | } | |
113 | ||
114 | sub read { | |
115 | my $self = shift; | |
116 | return substr($$self, 0, length($$self), ''); | |
117 | } |