/
Sims.pm
369 lines (248 loc) · 7.91 KB
/
Sims.pm
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package Test::Sims;
use strict;
use warnings;
our $VERSION = "20090630";
=head1 NAME
Test::Sims - Helps build semi-random data for testing
=head1 SYNOPSIS
package My::Sims;
use Test::Sims;
# Creates rand_name() and exported on demand.
make_rand name => [
qw(Mal Zoe Jayne Kaylee Inara River Simon Wash Zoe Book)
];
# Automatically exported
sub sim_character {
my %defaults = (
name => rand_name(),
series => "Firefly",
);
require Character;
return Character->new(
%defaults, @_;
);
}
=head1 DESCRIPTION
B<THIS IS AN EARLY RELEASE>! While very well tested behaviors may
change. The interface is not stable.
This is a module to help building semi-random data for testing and to
create large, nested, interesting data structures.
This module contains no new assertions, but it does tie in with
Test::Builder.
It does two things. It contains functions which make generating
random data easier and it allows you to write repeatable, yet random,
test data.
=head2 make_rand()
my $code = make_rand $name => \@list;
my $code = make_rand $name => sub { ... };
Creates a subroutine called C<<rand_$name>> and exports it on request.
If a @list is given it will generate a subroutine which returns
elements out of @list at random. It takes C<min> and C<max> arguments
to control how many.
my @items = rand_$name(
min => $min_random_items,
max => $max_random_items
);
C<min> and C<max> both default to 1. So by default you get 1 item.
If a subroutine is given it will simply give that routine a name.
This is just to get the convenience of adding it to the exports.
Also adds it to a "rand" export tag.
{
package Sim::Firefly;
make_rand crew => [
qw(Zoe Wash Mal River Simon Book Jayne Kaylee Inara)
];
}
...later...
{
use Sim::Firefly ":rand";
my $crew = rand_crew; # 1 name
my @crew = rand_crew( max => 3 ); # 1, 2 or 3 names
}
=head2 export_sims()
export_sims();
A utility function which causes your module to export all the
functions called C<<sims_*>>. It also creates an export tag called
"sims".
You should call this at the end of your Sim package.
=head2 Controlling randomness
You can control the random seed used by Test::Sims by setting the
C<TEST_SIMS_SEED> environment variable. This is handy to make test runs
repeatable.
TEST_SIMS_SEED=12345 perl -Ilib t/some_test.t
Test::Sims will output the seed used at the end of each test run. If
the test failed it will be visible to the user, otherwise it will be a
TAP comment and only visible if the test is run verbosely.
=head2 C<sim> functions
Test::Sims doesn't do anything with functions named C<sim_*> but
export them. Generally we recommend they're written like so:
sub sim_thing {
my %defaults = (
name => rand_name(),
age => rand_age(),
motto => rand_text(),
picture => rand_image(),
);
return Thing->new( %defaults, @_ );
}
This way you can get a completely random Thing.
my $thing = sim_thing();
Or you can lock down the bits you need leaving the rest to float free.
# Joe's motto and picture remain random
my $joe = sim_thing(
name => "Joe",
age => 64
);
=cut
use base qw(Exporter);
our @EXPORT = qw(make_rand export_sims);
# Yes, its not a great seed but it doesn't have to be secure.
my $Seed = defined $ENV{TEST_SIMS_SEED} ? $ENV{TEST_SIMS_SEED} : time ^ $$;
# XXX If something else calls srand() we're in trouble
srand $Seed;
## no critic (Subroutines::RequireArgUnpacking)
sub import {
my $class = shift;
my $caller = caller;
{
no strict 'refs';
unshift @{ $caller . "::ISA" }, "Exporter" unless $caller->isa("Exporter");
}
return __PACKAGE__->export_to_level( 1, $class, @_ );
}
sub make_rand {
my $name = shift;
my $thing = shift;
my $items = ref $thing eq "ARRAY" ? $thing : [];
my $caller = caller;
my $code = ref $thing eq 'CODE' ? $thing : sub {
my %args = @_;
$args{min} = 1 unless defined $args{min};
$args{max} = 1 unless defined $args{max};
my $max = int rand( $args{max} - $args{min} + 1 ) + $args{min};
my @return;
for( 1 .. $max ) {
push @return, $items->[ rand @$items ];
}
return @return == 1 ? $return[0] : @return;
};
my $func = "rand_$name";
_alias( $caller, $func, $code );
_add_to_export_ok( $caller, $func );
_add_to_export_tags( $caller, $func, 'rand' );
return $code;
}
sub export_sims {
my $caller = caller;
my $symbols = do {
no strict 'refs';
\%{ $caller . '::' };
};
my @sim_funcs = grep { *{ $symbols->{$_} }{CODE} }
grep /^sim_/, keys %$symbols;
for my $func (@sim_funcs) {
_add_to_export( $caller, $func );
_add_to_export_tags( $caller, $func, 'sims' );
}
return;
}
sub _add_to_export_ok {
my( $package, $func ) = @_;
no strict 'refs';
push @{ $package . '::EXPORT_OK' }, $func;
return;
}
sub _add_to_export {
my( $package, $func ) = @_;
no strict 'refs';
push @{ $package . '::EXPORT' }, $func;
return;
}
sub _add_to_export_tags {
my( $package, $func, $tag ) = @_;
no strict 'refs';
my $export_tags = \%{ $package . '::EXPORT_TAGS' };
push @{ $export_tags->{$tag} }, $func;
return;
}
sub _alias {
my( $package, $func, $code ) = @_;
no strict 'refs';
*{ $package . '::' . $func } = $code;
return;
}
sub _display_seed {
my $tb = shift;
my $ok = $tb->summary && !( grep !$_, $tb->summary );
my $msg = "TEST_SIMS_SEED=$Seed";
$ok ? $tb->note($msg) : $tb->diag($msg);
return;
}
END {
require Test::Builder;
my $tb = Test::Builder->new;
if( defined $tb->has_plan ) {
_display_seed($tb);
}
}
1;
=head1 EXAMPLE
Here's an example of making a simple package to generate random dates.
package Sim::Date;
use strict;
use warnings;
use DateTime;
use Test::Sims;
# Create rand_year(), rand_month(), etc...
# All exportable on demand or with the :rand tag
make_rand year => [1800..2100];
make_rand month => [1..12];
make_rand day => [1..31];
make_rand hour => [0..23];
make_rand minute=> [0..59];
make_rand second=> [0..59];
sub sim_datetime {
my %defaults = (
year => rand_year(),
month => rand_month(),
day => rand_day(),
hour => rand_hour(),
minute => rand_minute(),
second => rand_second(),
);
return DateTime->new(
%defaults, @_
);
}
# Export sim_datetime()
export_sims();
And then using it.
use Sim::Date;
# Random date.
my $date = sim_datetime;
# Random date in the year 2009
my $date = sim_datetime(
year => 2009
);
=head1 SEE ALSO
"Generating Test Data with The Sims"
L<http://schwern.org/talks/Generating%20Test%20Data%20With%20The%20Sims.pdf>
is a set of slides outlining the Sims testing technique which this
module is supporting.
L<Data::Random> for common rand_* routines.
L<Data::Generate> to generate random data from a set of rules.
=head1 SOURCE
The source code repository can be found at
L<http://github.com/schwern/Test-Sims>.
The latest release can be found at
L<http://search.cpan.org/dist/Test-Sims>.
=head1 BUGS
Please report bugs, problems, rough corners, feedback and suggestions
to L<http://github.com/schwern/Test-Sims/issues>.
Report early, report often.
=head1 LICENSE and COPYRIGHT
Copyright 2009 Michael G Schwern E<gt>schwern@pobox.comE<lt>
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
See F<http://www.perl.com/perl/misc/Artistic.html>
=cut