Algorithm-CouponCode

 view release on metacpan or  search on metacpan

lib/Algorithm/CouponCode.pm  view on Meta::CPAN

=item *

The 4th character of each part is a checkdigit, so client-side scripting can
be used to highlight parts which have been mis-typed, before the code is even
submitted to the application's back-end validation.

=item *

The checkdigit algorithm takes into account the position of the part being
keyed.  So for example '1K7Q' might be valid in the first part but not in the
second so if a user typed the parts in the wrong boxes then their error could
be highlighted.

=item *

The code generation algorithm avoids 'undesirable' codes. For example any code
in which transposed characters happen to result in a valid checkdigit will be
skipped.  Any generated part which happens to spell an 'inappropriate' 4-letter
word (e.g.: 'P00P') will also be skipped.

=back

t/01-generate.t  view on Meta::CPAN


can_ok(__PACKAGE__, 'cc_generate');

my $code1 = cc_generate(plaintext => '1234567890');

ok($code1, 'generated a code from a static plaintext');
like($code1, qr{^[0-9A-Z-]+$}, 'code comprises uppercase letter, digits and dashes');
like($code1, qr{^\w{4}-\w{4}-\w{4}$}, 'pattern is XXXX-XXXX-XXXX');

my $code2 = cc_generate(plaintext => '123456789A');
ok($code2, 'generated a second code from a static plaintext');
like($code2, qr{^[0-9A-Z-]+$}, 'code2 comprises uppercase letter, digits and dashes');
like($code2, qr{^\w{4}-\w{4}-\w{4}$}, 'pattern is XXXX-XXXX-XXXX');

isnt($code1, $code2, 'second code differs from first');

my $code3 = cc_generate(plaintext => '1234567890');
is($code1, $code3, 'third code is same as first');

my $code4 = cc_generate();
my $code5 = cc_generate();
isnt($code4, $code5, 'two codes without plaintexts supplied are different');

is($code1, '1K7Q-CTFM-LMTC', '$code1 is exactly as expected');
is($code2, 'X730-KCV1-MA2G', '$code2 is exactly as expected');



( run in 0.781 second using v1.01-cache-2.11-cpan-39bf76dae61 )