NAME

Test::QuickGen - Utilities for generating random test data

SYNOPSIS

use Test::QuickGen qw(:all);

my $id = id();
my $ascii = ascii_string(10);
my $alphanum = alphanumeric_string(10);
my $utf8 = utf8_string(20);
my $clean = utf8_sanitized(15);

my $rand = between(1, 100);
my $opt = nullable("value");
my $item = pick(qw(a b c));

my $words = words(\&ascii_string, 5);

DESCRIPTION

Test::QuickGen provides a set of utility functions for generating random data, primarily intended for testing purposes. These generators are simple, fast, and have minimal dependencies.

COMMAND LINE TOOL

This module comes bundled with an optional test runner, see quicktest for more details.

IMPORTING

Nothing is exported by default.

Import functions explicitly:

use Test::QuickGen qw(id ascii_string);

Import groups of functions using tags:

use Test::QuickGen qw(:all);
use Test::QuickGen qw(:ascii);
use Test::QuickGen qw(:utf8);
use Test::QuickGen qw(:basic);

See source for exact composition of the imports.

FUNCTIONS

id

my $id1 = id();
my $id2 = id();

# $id1 != $id2

Returns a monotonically increasing integer starting from 0.

The counter is process-local and resets each time the program runs.

string_of($n, @chars)

my $str = string_of(10, qw(a b c));

Generates a random string of length $n using the provided list of characters @chars.

ascii_string($n)

my $str = ascii_string(10);

Generates a random ASCII string length $n.

The character set includes all visible ASCII symbols and characters (in the range 33 to 126).

alphanumeric_string($n)

my $str = alphanumeric_string($n);

Generates a random ASCII string of only alphanumericeric characters of length $n.

utf8_string($n)

my $str = utf8_string(10);

Generates a random UTF-8 string of $n characters.

The generator:

Note: Because characters may vary in byte length, this function targets character count (not byte length).

utf8_sanitized($n)

my $clean = utf8_sanitized(10);

Generates a UTF-8 string of length $n and removes all non-alphanumericeric characters, retaining only:

If all characters are filtered out, the function retries until a non-empty string is produced.

words($gen, $n, $max_len = 70)

my $str = words(\&string_generator, 5);

Generates a string made up of $n space-separated "words".

Each word is produced by calling the generator function $gen.

Example:

words(\&ascii_string, 3);
# might return: "aZ3 kLm92 Q"

between($min, $max)

my $n = between(1, 10);

Returns a random integer between $min and $max (inclusive).

$min must be <= $max.

nullable($val)

my $value = nullable("data");

Returns either the given value or undef.

25% chance of returning undef, 75% chance of returning the original value. Useful for testing optional fields.

pick(@items)

my $item = pick(qw(a b c));

Returns a random element from the provided list.

If provided an empty list, will return undef.

NOTES

AUTHOR

Antonis Kalou kalouantonis@protonmail.com

CONTRIBUTORS

bas080: https://github.com/bas080

Penfold: Mike Whitaker pendfold@cpan.org

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See LICENSE for details.