AI-Gene-Sequence
view release on metacpan or search on metacpan
demo/Regexgene.pm view on Meta::CPAN
As we want to be able to fill our regular expression at the same
time as we create it and because we will want to nest sequences
we will need some way to know how deep we are, then a different
B<new> method is needed.
If called as an object method (C<$obj->new>), this decreases the depth
count by one from the invoking object. It also adds tokens to the regular
expression and uses the B<valid_gene> method to ensure we stay sane
from the start.
As can be seen, we use array offsets above $self->[1] to store information
which is specific to our implementation.
=cut
sub new {
my $gene = ['',[], ref($_[0]) ? $_[0]->[2]-1 : 3 ]; # limit recursion
bless $gene, ref($_[0]) || $_[0];
my $length = $_[1] || 5;
for (1..$length) {
my @token = $gene->generate_token();
use strict;
use warnings;
# Gtest is a small package used to test the AI::Gene::Sequence
# package. It provides a generate_token method and a seed_gene
# method, the first is highly deterministic (so tests of a module
# which hinge on randomness can work) and the second sets up a gene
# ready for a test.
# Also is a new method, which creates the gene and seeds it and
# 'd' and 'g' methods, which return (stringified) versions of the
# sequence ($self->[0]) and gene (@{$self->[1]}) respectively.
package GTest;
our (@ISA);
use AI::Gene::Sequence;
@ISA = qw(AI::Gene::Sequence);
sub g {
my $self = shift;
return join('', @{$self->[1]});
}
package main;
use Test;
# see above for a small package ( GTest ) used to test G::G::S
BEGIN {plan tests => 111, todo =>[]}
my $hammer = 30; # set big to bash at methods with randomness
{ # test1
# first of all, does our testing package behave
my $gene = GTest->new;
die "$0: Broken render" unless $gene->d eq 'abcdefghij'
and $gene->g eq 'abcdefghij';
die "$0: Broken generate" unless ($gene->generate_token('a'))[1] eq 'A'
and ($gene->generate_token())[0] eq 'n';
ok(1);
}
use strict;
use warnings;
# GtestS is a small package used to test the AI::Gene::Simple
# package. It provides a generate_token method and a seed_gene
# method, the first is highly deterministic (so tests of a module
# which hinge on randomness can work) and the second sets up a gene
# ready for a test.
# Also is a new method, which creates the gene and seeds it and
# a 'd' method, which returns a stringified version of $self->[0]
package GTestS;
our (@ISA);
use AI::Gene::Simple;
@ISA = qw(AI::Gene::Simple);
sub d {
my $self = shift;
return join('',@{$self->[0]});
}
package main;
use Test;
# see above for a small package ( GTest ) used to test G::G::S
BEGIN {plan tests => 101, todo =>[]}
my $hammer = 30; # set big to bash at methods with randomness
{ # test1
# first of all, does our testing package behave
my $gene = GTestS->new;
die "$0: Broken render" unless $gene->d eq 'abcdefghij';
die "$0: Broken generate" unless $gene->generate_token('a') eq 'A'
and $gene->generate_token eq 'N';
ok(1);
}
my $main = GTestS->new;
( run in 0.566 second using v1.01-cache-2.11-cpan-49f99fa48dc )