File: 40-search.t

package info (click to toggle)
librt-client-rest-perl 1%3A0.56-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 568 kB
  • sloc: perl: 4,205; makefile: 9
file content (67 lines) | stat: -rw-r--r-- 1,186 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
58
59
60
61
62
63
64
65
66
67
#!perl
# vim:ft=perl:

use strict;
use warnings;

package Mock;
use base 'RT::Client::REST::Object';

sub new {
    my $class = shift;
    bless {@_}, ref($class) || $class;
}

sub retrieve { shift }

sub id { shift->{id} }

package main;

use Test::More tests => 20;
use Test::Exception;

use constant METHODS => (
    'new', 'count', 'get_iterator',
);

BEGIN {
    use_ok('RT::Client::REST::SearchResult');
}

for my $method (METHODS) {
    can_ok('RT::Client::REST::SearchResult', $method);
}

my $search;
my @ids = (1 .. 9);
lives_ok {
    $search = RT::Client::REST::SearchResult->new(
        ids => \@ids,
        object => sub { Mock->new(id => shift) },
    );
};

ok($search->count == 9);

my $iter;
lives_ok {
    $iter = $search->get_iterator;
} "'get_iterator' call OK";

ok('CODE' eq ref($iter), "'get_iterator' returns a coderef");

my @results = &$iter;
ok(9 == @results, "Got 9 results in list context");
@results = &$iter;
ok(0 == @results, "Got 0 results in list context second time around");

$iter = $search->get_iterator;
my $i = 0;
while (my $obj = &$iter) {
    ++$i;
    ok($i == $obj->id, "id as expected");
}

ok(9 == $i, "Iterated 9 times (as expected)");