Skip to content

Commit

Permalink
add new rules and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Toru Yamaguchi committed Dec 6, 2011
1 parent 3facbee commit a04f9fd
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/Data/RuledFactory/Rule/Callback.pm
@@ -0,0 +1,78 @@
package Data::RuledFactory::Rule::Callback;

use strict;
use warnings;
use parent qw(Data::RuledFactory::Rule);

our $VERSION = '0.01';

sub new {
my $class = shift;
my $args = ref $_[0] ? $_[0] : { @_ };

%$args = (
callback => undef,
rows => undef,
cursor => 0,
%$args,
);

bless $args => $class;
}

sub has_next {
my $self = shift;

unless ($self->{callback}) {
return 0;
}

unless ( defined $self->{rows} ) {
return 1;
}

return $self->{cursor} < $self->{rows} ? 1 : 0;
}

sub _next {
my $self = shift;
return $self->{callback}->($self);
}

1;
__END__
=head1 NAME
Data::RuledFactory::Rule::Callback -
=head1 SYNOPSIS
use Data::RuledFactory::Rule::Callback;
=head1 DESCRIPTION
Data::RuledFactory::Rule::Callback is
=head1 AUTHOR
Toru Yamaguchi E<lt>zigorou@cpan.orgE<gt>
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# coding: utf-8-unix
# End:
#
# vim: expandtab shiftwidth=4:
73 changes: 73 additions & 0 deletions lib/Data/RuledFactory/Rule/Constant.pm
@@ -0,0 +1,73 @@
package Data::RuledFactory::Rule::Constant;

use strict;
use warnings;
use parent qw(Data::RuledFactory::Rule);

our $VERSION = '0.01';

sub new {
my $class = shift;
my $args = ref $_[0] ? $_[0] : { @_ };

%$args = (
const => undef,
rows => undef,
cursor => 0,
%$args,
);

bless $args => $class;
}

sub has_next {
my $self = shift;

unless ( defined $self->{rows} ) {
return 1;
}

return $self->{cursor} < $self->{rows} ? 1 : 0;
}

sub _next {
$_[0]->{const};
}

1;
__END__
=head1 NAME
Data::RuledFactory::Rule::Constant -
=head1 SYNOPSIS
use Data::RuledFactory::Rule::Constant;
=head1 DESCRIPTION
Data::RuledFactory::Rule::Constant is
=head1 AUTHOR
Toru Yamaguchi E<lt>zigorou@cpan.orgE<gt>
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# coding: utf-8-unix
# End:
#
# vim: expandtab shiftwidth=4:
40 changes: 40 additions & 0 deletions t/rule/callback.t
@@ -0,0 +1,40 @@
use strict;
use warnings;

use Test::More;
use Data::RuledFactory::Rule::Callback;

subtest 'default' => sub {
my $r = Data::RuledFactory::Rule::Callback->new(
callback => sub {
my $rule = shift;
return $rule->cursor * 5;
},
rows => 5,
);

my $i;

for $i (1..5) {
my $expect = 5 * $i;
ok $r->has_next, sprintf('has_next() is true (times: %d)', $i);
is $r->next, $expect, sprintf('next() equals %d (times: %d)', $expect, $i);
}

$i++;

ok !$r->has_next, sprintf('has_next() is false (times: %d)', $i);
is $r->next, undef, sprintf('next() is undef (times: %d)', $i);
};

done_testing;

# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# coding: utf-8-unix
# End:
#
# vim: expandtab shiftwidth=4:

36 changes: 36 additions & 0 deletions t/rule/constant.t
@@ -0,0 +1,36 @@
use strict;
use warnings;

use Test::More;
use Data::RuledFactory::Rule::Constant;

subtest 'default' => sub {
my $r = Data::RuledFactory::Rule::Constant->new(
const => 10,
rows => 5,
);

my $i;

for $i (1..5) {
ok $r->has_next, sprintf('has_next() is true (times: %d)', $i);
is $r->next, 10, 'next() equals 10';
}

$i++;

ok !$r->has_next, sprintf('has_next() is false (times: %d)', $i);
is $r->next, undef, sprintf('next() is undef (times: %d)', $i);
};

done_testing;

# Local Variables:
# mode: perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# coding: utf-8-unix
# End:
#
# vim: expandtab shiftwidth=4:

0 comments on commit a04f9fd

Please sign in to comment.