]> Sergey Matveev's repositories - public-inbox.git/blob - t/idx_stack.t
public-inbox 1.6.1 - minor bugfix release
[public-inbox.git] / t / idx_stack.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use Test::More;
6 use_ok 'PublicInbox::IdxStack';
7 my $oid_a = '03c21563cf15c241687966b5b2a3f37cdc193316';
8 my $oid_b = '963caad026055ab9bcbe3ee9550247f9d8840feb';
9
10 my $stk = PublicInbox::IdxStack->new;
11 is($stk->read_prepare, $stk, 'nothing');
12 is($stk->num_records, 0, 'no records');
13 is($stk->pop_rec, undef, 'undef on empty');
14
15 $stk = PublicInbox::IdxStack->new;
16 $stk->push_rec('m', 1234, 5678, $oid_a);
17 is($stk->read_prepare, $stk, 'read_prepare');
18 is($stk->num_records, 1, 'num_records');
19 is_deeply([$stk->pop_rec], ['m', 1234, 5678, $oid_a], 'pop once');
20 is($stk->pop_rec, undef, 'undef on empty');
21
22 $stk = PublicInbox::IdxStack->new;
23 $stk->push_rec('m', 1234, 5678, $oid_a);
24 $stk->push_rec('d', 1234, 5678, $oid_b);
25 is($stk->read_prepare, $stk, 'read_prepare');
26 is($stk->num_records, 2, 'num_records');
27 is_deeply([$stk->pop_rec], ['d', 1234, 5678, $oid_b], 'pop');
28 is_deeply([$stk->pop_rec], ['m', 1234, 5678, $oid_a], 'pop-pop');
29 is($stk->pop_rec, undef, 'empty');
30
31 SKIP: {
32         $stk = undef;
33         my $nr = $ENV{TEST_GIT_LOG} or skip 'TEST_GIT_LOG unset', 3;
34         open my $fh, '-|', qw(git log --pretty=tformat:%at.%ct.%H), "-$nr" or
35                 die "git log: $!";
36         my @expect;
37         while (<$fh>) {
38                 chomp;
39                 my ($at, $ct, $H) = split(/\./);
40                 $stk //= PublicInbox::IdxStack->new($H);
41                 # not bothering to parse blobs here, just using commit OID
42                 # as a blob OID since they're the same size + format
43                 $stk->push_rec('m', $at + 0, $ct + 0, $H);
44                 push(@expect, [ 'm', $at, $ct, $H ]);
45         }
46         $stk or skip('nothing from git log', 3);
47         is($stk->read_prepare, $stk, 'read_prepare');
48         is($stk->num_records, scalar(@expect), 'num_records matches expected');
49         my @result;
50         while (my @tmp = $stk->pop_rec) {
51                 unshift @result, \@tmp;
52         }
53         is_deeply(\@result, \@expect, 'results match expected');
54 }
55
56 done_testing;