-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMore.pm
More file actions
328 lines (282 loc) · 7.92 KB
/
More.pm
File metadata and controls
328 lines (282 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package Test::More;
use strict;
use warnings;
use Symbol 'qualify_to_ref';
use Data::Dumper;
# Load Test::Builder to make it available for legacy tests
use Test::Builder;
# Test::Builder singleton
our $Test_Builder;
our @EXPORT = qw(
plan ok is isnt like unlike cmp_ok can_ok isa_ok
pass fail diag note done_testing is_deeply subtest
use_ok require_ok BAIL_OUT
skip
eq_array eq_hash eq_set
);
our $Test_Count = 0;
our $Plan_Count;
our $Failed_Count = 0;
our $Test_Indent = "";
sub import {
my $package = shift;
my $caller = caller;
if (@_) {
plan(@_);
}
for my $symbol (@EXPORT) {
my $full_name = Symbol::qualify_to_ref($symbol, $caller);
*$full_name = \&{$symbol};
}
}
sub plan {
my ($directive, $arg) = @_;
if ($directive eq 'tests') {
$Plan_Count = $arg;
print "${Test_Indent}1..$Plan_Count\n";
}
elsif ($directive eq 'skip_all') {
print "${Test_Indent}1..0 # Skipped: $arg\n";
exit 0;
}
}
sub ok ($;$) {
my ($test, $name) = @_;
$Test_Count++;
my $result = $test ? "ok" : "not ok";
$Failed_Count++ unless $test;
print "$Test_Indent$result $Test_Count - $name\n";
return $test;
}
sub is ($$;$) {
my ($got, $expected, $name) = @_;
my $test = (!defined $got && !defined $expected) ||
(defined $got && defined $expected && $got eq $expected);
ok($test, $name);
unless ($test) {
diag(" got: " . (defined $got ? "'$got'" : "undef"));
diag(" expected: " . (defined $expected ? "'$expected'" : "undef"));
}
return $test;
}
sub isnt ($$;$) {
my ($got, $expected, $name) = @_;
my $test = (!defined $got && !defined $expected) ||
(defined $got && defined $expected && $got eq $expected);
$test = !$test;
ok($test, $name);
unless ($test) {
diag(" got: '$got'");
diag(" expected: anything else");
}
return $test;
}
sub like ($$;$) {
my ($got, $regex, $name) = @_;
my $test = defined $got && $got =~ /$regex/;
ok($test, $name);
unless ($test) {
diag(" '$got'");
diag(" doesn't match '$regex'");
}
return $test;
}
sub unlike ($$;$) {
my ($got, $regex, $name) = @_;
my $test = !defined $got || $got !~ /$regex/;
ok($test, $name);
unless ($test) {
diag(" '$got'");
diag(" matches '$regex'");
}
return $test;
}
sub cmp_ok ($$$;$){
my ($got, $op, $expected, $name) = @_;
my $test = eval "$got $op $expected";
ok($test, $name);
unless ($test) {
diag(" got: $got");
diag(" expected: $expected");
}
return $test;
}
sub can_ok ($@) {
my ($module, @methods) = @_;
my $test = 1;
for my $method (@methods) {
unless ($module->can($method)) {
$test = 0;
diag(" $module cannot '$method'");
}
}
ok($test, "$module can do everything we're asking");
return $test;
}
sub isa_ok ($$;$) {
my ($object, $class, $name) = @_;
$name ||= "The object";
my $test = UNIVERSAL::isa($object, $class);
ok($test, "$name isa $class");
return $test;
}
sub pass (;$) {ok(1, $_[0])}
sub fail (;$) {ok(0, $_[0])}
sub diag {
my ($message) = @_;
print STDERR "$Test_Indent# $message\n";
}
sub note {
my ($message) = @_;
return unless defined $message;
print "$Test_Indent# $message\n";
}
sub done_testing {
my ($count) = @_;
$count ||= $Test_Count;
if ($Plan_Count && $Plan_Count != $Test_Count) {
ok(0, "planned to run $Plan_Count but done_testing() expects $Test_Count");
diag(" Failed test 'planned to run $Plan_Count but done_testing() expects $Test_Count'");
diag(" at $0 line " . (caller)[2] . ".");
diag("Looks like you failed 1 test of $Plan_Count.");
return 0;
}
print "${Test_Indent}1..$count\n" unless $Plan_Count;
return $Failed_Count == 0;
}
sub is_deeply {
my ($got, $expected, $name) = @_;
local $Data::Dumper::Sortkeys = 1;
my $got_dumped = Dumper($got);
my $expected_dumped = Dumper($expected);
my $test = $got_dumped eq $expected_dumped;
ok($test, $name);
unless ($test) {
diag(" got: $got_dumped");
diag(" expected: $expected_dumped");
}
return $test;
}
sub subtest {
my ($name, $code) = @_;
print "# Subtest: $name\n";
my $result;
local $Plan_Count;
{
# Reset counters for subtest and set indent
local $Failed_Count = 0;
local $Test_Count = 0;
local $Test_Indent = $Test_Indent . " ";
# Run the subtest code
$code->();
# Print subtest plan
# print "${Test_Indent}1..$Test_Count\n";
$result = $Failed_Count == 0;
}
# Report subtest result
ok($result, $name);
return $result;
}
sub require_ok {
my ($module_or_file) = @_;
my $test_name;
# Determine if it's a module name or file path
if ($module_or_file =~ /\.pl$/ || $module_or_file =~ /\//) {
# It's a file
$test_name = "require '$module_or_file'";
} else {
# It's a module name
$test_name = "require $module_or_file";
# Convert module name to file path for require
my $file = $module_or_file;
$file =~ s/::/\//g;
$file .= '.pm';
$module_or_file = $file;
}
my $result = eval {
require $module_or_file;
1;
};
if ($result) {
ok(1, $test_name);
} else {
ok(0, $test_name);
my $error = $@ || 'Unknown error';
# Clean up the error message
$error =~ s/\n$//;
diag("Error loading $module_or_file: $error");
}
return $result;
}
sub use_ok {
my ($module, @imports) = @_;
my $test_name = "use $module";
# Check if first import is a version number
my $version;
if (@imports && $imports[0] =~ /^[\d\.]+$/) {
$version = shift @imports;
$test_name .= " $version";
}
if (@imports) {
$test_name .= " qw(" . join(' ', @imports) . ")";
}
# Build the use statement
my $use_statement = "package " . caller() . "; use $module";
$use_statement .= " $version" if $version;
$use_statement .= " qw(" . join(' ', @imports) . ")" if @imports;
my $result = eval $use_statement;
if (defined $result || $@ eq '') {
ok(1, $test_name);
return 1;
} else {
ok(0, $test_name);
my $error = $@ || 'Unknown error';
# Clean up the error message
$error =~ s/\n$//;
diag("Error loading $module: $error");
return 0;
}
}
sub BAIL_OUT {
my ($reason) = @_;
my $msg = "Bail out!";
$msg .= " $reason" if defined $reason && length $reason;
print "$msg\n";
exit 255;
}
sub skip($;$) {
my ($name, $count) = @_;
$count ||= 1;
for (1..$count) {
$Test_Count++;
my $result = "ok";
print "$Test_Indent$result $Test_Count # skip $name\n";
}
last SKIP;
}
# Legacy comparison functions - simple implementations using is_deeply
sub eq_array($$) {
my ($got, $expected) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
return is_deeply($got, $expected);
}
sub eq_hash($$) {
my ($got, $expected) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
return is_deeply($got, $expected);
}
sub eq_set($$) {
my ($got, $expected) = @_;
local $Test::Builder::Level = $Test::Builder::Level + 1;
# Set comparison: order doesn't matter
# Simple implementation: sort and compare
my @got_sorted = sort { (defined $a ? $a : '') cmp (defined $b ? $b : '') } @$got;
my @expected_sorted = sort { (defined $a ? $a : '') cmp (defined $b ? $b : '') } @$expected;
return is_deeply(\@got_sorted, \@expected_sorted);
}
# Provide access to Test::Builder for legacy tests
sub builder {
$Test_Builder ||= Test::Builder->new();
return $Test_Builder;
}
1;