File: cache.t

package info (click to toggle)
libmojolicious-perl 9.31%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,260 kB
  • sloc: perl: 10,139; makefile: 31; javascript: 1
file content (57 lines) | stat: -rw-r--r-- 2,058 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use Mojo::Base -strict;

use Test::More;
use Mojo::Cache;

subtest 'Basics' => sub {
  my $cache = Mojo::Cache->new(max_keys => 2);
  is $cache->get('foo'), undef, 'no result';
  $cache->set(foo => 'bar');
  is $cache->get('foo'), 'bar', 'right result';
  $cache->set(bar => 'baz');
  is $cache->get('foo'), 'bar', 'right result';
  is $cache->get('bar'), 'baz', 'right result';
  $cache->set(baz => 'yada');
  is $cache->get('foo'), undef,  'no result';
  is $cache->get('bar'), 'baz',  'right result';
  is $cache->get('baz'), 'yada', 'right result';
  $cache->set(yada => 23);
  is $cache->get('foo'),  undef,  'no result';
  is $cache->get('bar'),  undef,  'no result';
  is $cache->get('baz'),  'yada', 'right result';
  is $cache->get('yada'), 23,     'right result';
  $cache->max_keys(1)->set(one => 1)->set(two => 2);
  is $cache->get('one'), undef, 'no result';
  is $cache->get('two'), 2,     'right result';
};

subtest 'Bigger cache' => sub {
  my $cache = Mojo::Cache->new(max_keys => 3);
  is $cache->get('foo'),                    undef, 'no result';
  is $cache->set(foo => 'bar')->get('foo'), 'bar', 'right result';
  $cache->set(bar => 'baz');
  is $cache->get('foo'), 'bar', 'right result';
  is $cache->get('bar'), 'baz', 'right result';
  $cache->set(baz => 'yada');
  is $cache->get('foo'), 'bar',  'right result';
  is $cache->get('bar'), 'baz',  'right result';
  is $cache->get('baz'), 'yada', 'right result';
  $cache->set(yada => 23);
  is $cache->get('foo'),  undef,  'no result';
  is $cache->get('bar'),  'baz',  'right result';
  is $cache->get('baz'),  'yada', 'right result';
  is $cache->get('yada'), 23,     'right result';
};

subtest 'Cache disabled' => sub {
  my $cache = Mojo::Cache->new(max_keys => 0);
  is $cache->get('foo'),                    undef, 'no result';
  is $cache->set(foo => 'bar')->get('foo'), undef, 'no result';

  $cache = Mojo::Cache->new(max_keys => -1);
  is $cache->get('foo'), undef, 'no result';
  $cache->set(foo => 'bar');
  is $cache->get('foo'), undef, 'no result';
};

done_testing();