File: Catmandu-Paged.t

package info (click to toggle)
libcatmandu-perl 1.0304-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,756 kB
  • ctags: 695
  • sloc: perl: 13,756; makefile: 34
file content (107 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;
use Role::Tiny;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Paged';
    use_ok $pkg;
}
require_ok $pkg;

{

    package T::PagedWithoutStart;
    use Moo;
    sub limit { }
    sub total { }

    package T::PagedWithoutLimit;
    use Moo;
    sub start { }
    sub total { }

    package T::PagedWithoutTotal;
    use Moo;
    sub start { }
    sub limit { }

    package T::Paged;
    use Moo;
    with $pkg;

    sub start {return 27;}
    sub limit {return 20;}
    sub total {return 32432;}

    package T2::Paged;
    use Moo;
    with $pkg;

    sub start {return 1;}
    sub limit {return 10;}
    sub total {return 127;}

    package T3::Paged;
    use Moo;
    with $pkg;

    sub start {return 0;}
    sub limit {return 10;}
    sub total {return 33;}
}

throws_ok {Role::Tiny->apply_role_to_package('T::PagedWithoutStart', $pkg)}
qr/missing start/;
throws_ok {Role::Tiny->apply_role_to_package('T::PagedWithoutLimit', $pkg)}
qr/missing limit/;
throws_ok {Role::Tiny->apply_role_to_package('T::PagedWithoutTotal', $pkg)}
qr/missing total/;

my $p = T::Paged->new;
can_ok $p, $_
    for
    qw/first_page page previous_page next_page first_on_page last_on_page last_page pages_in_spread/;

is $p->first_page,    1,    "first page ok";
is $p->page,          2,    "Page ok";
is $p->previous_page, 1,    "previous ok";
is $p->next_page,     3,    "next ok";
is $p->page_size,     20,   "page size ok";
is $p->first_on_page, 21,   "first on page ok";
is $p->last_on_page,  40,   "last on page ok";
is $p->last_page,     1622, "last page ok";
my @arr = (1, 2, 3, 4, 5, undef, 1622);
is_deeply \@{$p->pages_in_spread}, \@arr, "spread ok";

my $p2 = T2::Paged->new;
is $p2->first_page,    1,     "first page ok";
is $p2->page,          1,     "Page ok";
is $p2->previous_page, undef, "previous ok";
is $p2->next_page,     2,     "next ok";
is $p2->page_size,     10,    "page size ok";
is $p2->first_on_page, 1,     "first on page ok";
is $p2->last_on_page,  10,    "last on page ok";
is $p2->last_page,     13,    "last page ok";
my @arr2 = (1, 2, 3, 4, undef, 12, 13);
is_deeply \@{$p2->pages_in_spread}, \@arr2, "spread ok";

my $p3 = T3::Paged->new;
is $p3->first_page,    1,     "first page ok";
is $p3->page,          1,     "Page ok";
is $p3->previous_page, undef, "previous ok";
is $p3->next_page,     2,     "next ok";
is $p3->page_size,     10,    "page size ok";
is $p3->first_on_page, 1,     "first on page ok";
is $p3->last_on_page,  10,    "last on page ok";
is $p3->last_page,     4,     "last page ok";
my @arr3 = (1, 2, 3, 4);

is_deeply \@{$p3->pages_in_spread}, \@arr3, "spread ok";

done_testing 40;