Debian Codegraph @

t/scan/format.t

raw
1 use strict;
2 use warnings;
3 use FindBin;
4 use lib "$FindBin::Bin/../../";
5 use t::scan::Util;
6
7 test(<<'TEST'); # RANDERSON/HTTP-WebTest-1.02/WebTest.pm
8 my ($nbytes, $max_bytes, $min_bytes, $terse, $report, $num_fail,
9 $num_succeed) = @_;
10 my ($report_text, $result);
11 format WRITE_NBYTES =
12 @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<
13 $report_text, $result
14 .
15 if ($nbytes < 0) {
16 warn "Invalid value of nbytes ( = $nbytes )";
17 return 0;
18 }
19 TEST
20
21 test(<<'TEST'); # CNATION/Monkeywrench-1.0/lib/HTTP/Monkeywrench.pm
22 if (($click->{'sendcookie'}) && ($self->settings->{'show_cookies'})) {
23 my $cookie_to_print = $self->cookie_jar->as_string;
24 $~ = "COOKIES";
25 write;
26 format COOKIES =
27 Cookie:
28 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29 $cookie_to_print
30 .
31 }
32 my $failed = 0;
33 my $success = 0;
34
35 $content .= ' Code: ' . $res->code . ' ' . $res->message . "\n";
36 if ($res->is_redirect || $res->is_success) {
37 $content .= " Match Res:\n" if ($click->{'success_res'});
38 foreach my $sr (@{ $click->{'success_res'} }) {
39 my $result;
40 if ($res->content =~ $sr) {
41 $result = "PASS" if ($self->settings->{'match_detail'});
42 } else {
43 $result = "FAIL";
44 $failed++;
45 $totalerrs++;
46 }
47 pipe (RFH,WFH);
48 format WFH =
49 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>>>
50 $sr, $result
51 .
52 write WFH if ($result);
53 close WFH;
54 local $/ = undef;
55 $content .= <RFH>;
56 }
57
58 $content .= " Match Error:\n" if ($click->{'error_res'});
59 foreach my $er (@{ $click->{'error_res'} }) {
60 my $result;
61 if ($res->content =~ $er) {
62 $result = "FAIL";
63 $failed++;
64 $totalerrs++;
65 } else {
66 $result = "PASS" if ($self->settings->{'match_detail'});
67 }
68 pipe (ERR_RFH,ERR_WFH);
69 format ERR_WFH =
70 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>>>
71 $er, $result
72 .
73 write ERR_WFH if ($result);
74 close ERR_WFH;
75 local $/ = undef;
76 $content .= <ERR_RFH>;
77 }
78
79 } else {
80 $content .= " *** Request Failed ***\n"; #. $res->error_as_HTML;
81 }
82 TEST
83
84 test(<<'TEST'); # MLEHMANN/Games-Sokoban-1.01/Sokoban.pm
85 sub new_from_file {
86 my ($class, $path, $format) = @_;
87
88 open my $fh, "<:perlio", $path
89 or Carp::croak "$path: $!";
90 local $/;
91
92 $class->new (data => (scalar <$fh>), format => $format)
93 }
94
95 sub detect_format($) {
96 my ($data) = @_;
97
98 return "text" if $data =~ /^[ #\@\*\$\.\+\015\012\-_]+$/;
99
100 return "rle" if $data =~ /^[ #\@\*\$\.\+\015\012\-_|1-9]+$/;
101
102 my ($a, $b) = unpack "ww", $data;
103 return "binpack" if defined $a && defined $b;
104
105 Carp::croak "unable to autodetect sokoban level format";
106 }
107
108 =item $level->data ([$new_data, [$new_data_format]])
109
110 Sets the level from the given data.
111
112 =cut
113
114 sub data {
115 if (@_ > 1) {
116 my ($self, $data, $format) = @_;
117
118 $format ||= detect_format $data;
119
120 if ($format eq "text" or $format eq "rle") {
121 $data =~ y/-_|/ \n/;
122 $data =~ s/(\d)(.)/$2 x $1/ge;
123 my @lines = split /[\015\012]+/, $data;
124 my $w = List::Util::max map length, @lines;
125
126 $_ .= " " x ($w - length)
127 for @lines;
128
129 $self->{data} = join "\n", @lines;
130
131 } elsif ($format eq "binpack") {
132 (my ($w, $s), $data) = unpack "wwB*", $data;
133
134 my @enc = ('#', '$', '.', ' ', ' ', '###', '*', '# ');
135
136 $data = join "",
137 map $enc[$_],
138 unpack "C*",
139 pack "(b*)*",
140 unpack "(a3)*", $data;
141
142 # clip extra chars (max. 2)
143 my $extra = (length $data) % $w;
144 substr $data, -$extra, $extra, "" if $extra;
145
146 (substr $data, $s, 1) =~ y/ ./@+/;
147
148 $self->{data} =
149 join "\n",
150 map "#$_#",
151 "#" x $w,
152 (unpack "(a$w)*", $data),
153 "#" x $w;
154
155 } else {
156 Carp::croak "$format: unsupported sokoban level format requested";
157 }
158
159 $self->{format} = $format;
160 $self->update;
161 }
162
163 $_[0]{data}
164 }
165 TEST
166
167 done_testing;