Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
plicease committed Nov 16, 2023
1 parent 96c6035 commit 87b19c2
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/Perl/Critic/Role/Cacheable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ The name of the file where the cache will be stored.
}

sub _cacheable_config_digest ($self) {
return $self->{_cacheable}->{rc_digest};
return $self->{_cacheable}->{config_digest};
}

sub _cacheable_digest_ok ($self) {
Expand Down Expand Up @@ -156,7 +156,8 @@ critiqued and had no violations will not be checked again.

around critique => sub ($orig, $self, $source_code) {

my $filename = !is_ref $source_code ? Path::Tiny->new($source_code)->absolute->stringify : undef;
$DB::single = 1;
my $filename = !Ref::Util::is_ref $source_code ? Path::Tiny->new($source_code)->absolute->stringify : undef;
if($filename) {
return () if $self->_cacheable_check_cache_ok($filename);
}
Expand Down
161 changes: 158 additions & 3 deletions t/perl_critic_role_cacheable.t
Original file line number Diff line number Diff line change
@@ -1,22 +1,177 @@
use Test2::V0 -no_srand => 1;
use experimental qw( signatures postderef );
use Perl::Critic::Role::Cacheable;
use Path::Tiny;

package Perl::Critic::Cacheable {
my @faux_violations;
my $call_count;

package MyCritic {
use parent 'Perl::Critic';
use Role::Tiny::With ();

Role::Tiny::With::with 'Perl::Critic::Role::Cacheable';

sub critique ($self, $source_code ) {
$call_count++;
my @old = @faux_violations;
@faux_violations = ();
return @old;
}

sub new ($self, @args) {
$call_count = 0;
return $self->SUPER::new(@args);
}
}

subtest 'very basic' => sub {

my $critic = Perl::Critic::Cacheable->new;
my $critic = MyCritic->new;
isa_ok $critic, 'Perl::Critic';
ok $critic->can('new'), 'has new method';
ok !$critic->can('around'), 'does not have around method';

};

done_testing;
subtest 'cache' => sub {
my $root = Path::Tiny->tempdir;

my @perl_source = (map { $root->child($_) } qw( source1.pl source2.pm));
$perl_source[0]->spew('foo');
$perl_source[1]->spew('bar');

my $profile = $root->child('profile');
$profile->spew('');

my $cache = $root->child('cache');

subtest 'first run (no cache)' => sub {
my $critic = MyCritic->new(
-profile => "$profile",
'-cacheable-filename' => "$cache"
);

is(
[$critic->critique("$perl_source[0]")],
[],
"call \$critic->critique(\"$perl_source[0]\") = []",
);

@faux_violations = ('a','b','x');

is(
[$critic->critique("$perl_source[1]")],
['a','b','x'],
"call \$critic->critique(\"$perl_source[1]\") = [a,b,x]",
);

is $call_count, 2, 'expected call count';
$critic->cacheable_save;

ok -r $cache, 'created cache file';
};

subtest 'second run (with cache)' => sub {
my $critic = MyCritic->new(
-profile => "$profile",
'-cacheable-filename' => "$cache"
);

is(
[$critic->critique("$perl_source[0]")],
[],
"call \$critic->critique(\"$perl_source[0]\") = []",
);

@faux_violations = ('a','b','x');

is(
[$critic->critique("$perl_source[1]")],
['a','b','x'],
"call \$critic->critique(\"$perl_source[1]\") = [a,b,x]",
);

is $call_count, 1, 'expected call count';
};

subtest 'third run with change to source file' => sub {
$perl_source[0]->spew('baz');

my $critic = MyCritic->new(
-profile => "$profile",
'-cacheable-filename' => "$cache"
);

is(
[$critic->critique("$perl_source[0]")],
[],
"call \$critic->critique(\"$perl_source[0]\") = []",
);

@faux_violations = ('a','b','x');

is(
[$critic->critique("$perl_source[1]")],
['a','b','x'],
"call \$critic->critique(\"$perl_source[1]\") = [a,b,x]",
);

is $call_count, 2, 'expected call count';
};

subtest 'forth run with change to profile' => sub {
$perl_source[0]->spew('foo');
$profile->spew('; just a comment');

my $critic = MyCritic->new(
-profile => "$profile",
'-cacheable-filename' => "$cache"
);

is(
[$critic->critique("$perl_source[0]")],
[],
"call \$critic->critique(\"$perl_source[0]\") = []",
);

@faux_violations = ('a','b','x');

is(
[$critic->critique("$perl_source[1]")],
['a','b','x'],
"call \$critic->critique(\"$perl_source[1]\") = [a,b,x]",
);

is $call_count, 2, 'expected call count';
};

subtest 'forth run with change to arguments' => sub {
my $profile = $root->child('profile2');
$profile->spew('');

my $critic = MyCritic->new(
-profile => "$profile",
'-cacheable-filename' => "$cache"
);

is(
[$critic->critique("$perl_source[0]")],
[],
"call \$critic->critique(\"$perl_source[0]\") = []",
);

@faux_violations = ('a','b','x');

is(
[$critic->critique("$perl_source[1]")],
['a','b','x'],
"call \$critic->critique(\"$perl_source[1]\") = [a,b,x]",
);

is $call_count, 2, 'expected call count';
};

};

done_testing;

0 comments on commit 87b19c2

Please sign in to comment.