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
|
use 5.008001;
use strict;
use warnings;
package Data::Fake;
# ABSTRACT: Declaratively generate fake structured data for testing
our $VERSION = '0.006';
use Import::Into 1.002005;
sub import {
my $class = shift;
for my $m (@_) {
my $module = "Data::Fake::$m";
$module->import::into( scalar caller );
}
}
1;
# vim: ts=4 sts=4 sw=4 et tw=75:
__END__
=pod
=encoding UTF-8
=head1 NAME
Data::Fake - Declaratively generate fake structured data for testing
=head1 VERSION
version 0.006
=head1 SYNOPSIS
use Data::Fake qw/Core Names Text Dates/;
my $hero_generator = fake_hash(
{
name => fake_name(),
battlecry => fake_sentences(1),
birthday => fake_past_datetime("%Y-%m-%d"),
friends => fake_array( fake_int(2,4), fake_name() ),
gender => fake_pick(qw/Male Female Other/),
}
);
my $hero = $hero_generator->();
### example output ###
# {
# 'name' => 'Antonio Nicholas Preston'
# 'battlecry' => 'Est ipsum corrupti quia voluptatibus.',
# 'birthday' => '2004-04-21',
# 'friends' => [
# 'Jaylin Lillianna Morgan',
# 'Colin Noel Estes',
# 'Justice Aron Hale',
# 'Franco Zane Oneil'
# ],
# 'gender' => 'Male',
# };
=head1 DESCRIPTION
This module generates randomized, fake structured data using declarative
syntax.
C<Data::Fake> is built on higher-order programming principles. It provides
users with "factory" functions, which create "generator" functions for
specific pieces of data.
Wherever randomized, fake data is desired, a generator code reference is
used as a placeholder for the desired output. Each time the top-level
generator is run, nested generators are recursively run to turn
placeholders into the desired randomized data.
For example, the SYNOPSIS declares the desired form of a "hero" using the
C<fake_hash> factory function. The input is a hash-reference, with nested
generators created as placeholders by the C<fake_name>, C<fake_sentences>,
etc. factory functions:
my $hero_generator = fake_hash(
{
name => fake_name(),
battlecry => fake_sentences(1),
birthday => fake_past_datetime("%Y-%m-%d"),
friends => fake_array( fake_int(2,4), fake_name() ),
gender => fake_pick(qw/Male Female Other/),
}
);
Every time C<$hero_generator> is run, a new hash is generated based
on the template and nested generators.
=head1 USAGE
=head2 Loading the right submodules
Factory functions are exported by submodules, loosely grouped by topic:
=over 4
=item *
L<Data::Fake::Core> – general purpose generators: hashes, arrays, numeric values, string templates
=item *
L<Data::Fake::Company> – company name, job title
=item *
L<Data::Fake::Dates> – past and future dates
=item *
L<Data::Fake::Internet> – domain names, email addresses
=item *
L<Data::Fake::Names> – first and last names
=item *
L<Data::Fake::Text> – characters, words, sentences, paragraphs
=back
Submodules can be loaded directly, to control exactly which functions are
exported, or they can be loaded by the C<Data::Fake> import function like
so:
use Data::Fake qw/Core Company Dates Names/;
In this case, the listed submodules are loaded and all their functions are
imported.
=head2 Constructing the desired output
Use the factory functions to construct a generator for your data. Start
with the highest level structure using
L<fake_hash|Data::Fake::Core/fake_hash> or
L<fake_array|Data::Fake::Core/fake_array>. Use other factory functions to
create placeholders with specific data types.
For example, a hash generator with placeholders for phone numbers:
$hash_generator = fake_hash(
{
home => fake_digits("###-###-####"),
work => fake_digits("###-###-####"),
cell => fake_digits("###-###-####"),
}
);
Or build them up piece by piece. For example, an array of 100 of the
previous hash references:
$array_generator = fake_array( 100, $hash_generator );
Then generate the hundred instances, each with three fake phone numbers:
$aoh = $array_generator->();
See L<Data::Fake::Examples> for ideas for how to use and combine
generators.
=head2 Using custom generators
Generators are just code references. You can use your own anywhere a
Data::Fake generator could be used:
$generator = fake_hash(
favorite_color => \&my_favorite_color_picker,
number_squared => sub { ( int(rand(10)) + 1 ) ** 2 },
);
You can (and probably should) write your own factory functions for
anything complex or that you'll use more than once. You can use
Data::Fake generators as part of these functions.
use Data::Fake qw/Core/;
sub fake_squared_int {
my $max_int = shift;
my $prng = fake_int( 1, $max_int );
return sub { $prng->() ** 2 };
}
$generator = fake_hash(
number_squared => fake_squared_int(10),
);
=head2 Caveats and special cases
Because many data structures are walked recursively looking for
code-references to replace, circular references will cause an infinite
loop.
If you need a code references as part of your output data structure, you
need to wrap it in a code reference.
$generator = fake_hash(
a_function => sub { \&some_function },
);
=for Pod::Coverage BUILD
=head1 CONTRIBUTING
If you have ideas for additional generator functions and think they would
be sensible additions to the main distribution, please open a support ticket
to discuss it. To be included in the main distribution, additional
generator functions should add few, if any, additional dependencies.
If you would like to release your own distributions in the C<Data::Fake::*>
namespace, please follow the conventions of the existing modules:
=over 4
=item *
factory function names start with "fake_"
=item *
export all factory functions by default
=item *
allow code-references where you would allow a sizing constant
=back
=head1 SEE ALSO
=over 4
=item *
L<Data::Faker> – similar but object oriented; doesn't do structured data; always loads all plugins
=item *
L<Data::Random> – generate several random types of data
=item *
L<Test::Sims> – generator for libraries generating random data
=item *
L<Text::Lorem> – just fake text
=back
=head1 AUTHOR
David Golden <dagolden@cpan.org>
=head1 CONTRIBUTORS
=for stopwords Chloé Kekoa José Joaquín Atria Ricardo Signes Stuart Skelton
=over 4
=item *
Chloé Kekoa <50083900+chloekek@users.noreply.github.com>
=item *
José Joaquín Atria <jjatria@gmail.com>
=item *
Ricardo Signes <rjbs@users.noreply.github.com>
=item *
Stuart Skelton <stuarts@broadbean.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2015 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|