]> Sergey Matveev's repositories - public-inbox.git/blob - t/idx_stack.t
No ext_urls
[public-inbox.git] / t / idx_stack.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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 my $cmt_a = 'df8e4a0612545d53672036641e9f076efc94c2f6';
10 my $cmt_b = '3ba7c9fa4a083c439e768882c571c2026a981ca5';
11
12 my $stk = PublicInbox::IdxStack->new;
13 is($stk->read_prepare, $stk, 'nothing');
14 is($stk->num_records, 0, 'no records');
15 is($stk->pop_rec, undef, 'undef on empty');
16
17 $stk = PublicInbox::IdxStack->new;
18 $stk->push_rec('m', 1234, 5678, $oid_a, $cmt_a);
19 is($stk->read_prepare, $stk, 'read_prepare');
20 is($stk->num_records, 1, 'num_records');
21 is_deeply([$stk->pop_rec], ['m', 1234, 5678, $oid_a, $cmt_a], 'pop once');
22 is($stk->pop_rec, undef, 'undef on empty');
23
24 $stk = PublicInbox::IdxStack->new;
25 $stk->push_rec('m', 1234, 5678, $oid_a, $cmt_a);
26 $stk->push_rec('d', 1234, 5678, $oid_b, $cmt_b);
27 is($stk->read_prepare, $stk, 'read_prepare');
28 is($stk->num_records, 2, 'num_records');
29 is_deeply([$stk->pop_rec], ['d', 1234, 5678, $oid_b, $cmt_b], 'pop');
30 is_deeply([$stk->pop_rec], ['m', 1234, 5678, $oid_a, $cmt_a], 'pop-pop');
31 is($stk->pop_rec, undef, 'empty');
32
33 SKIP: {
34         $stk = undef;
35         my $nr = $ENV{TEST_GIT_LOG} or skip 'TEST_GIT_LOG unset', 3;
36         open my $fh, '-|', qw(git log --pretty=tformat:%at.%ct.%H), "-$nr" or
37                 die "git log: $!";
38         my @expect;
39         while (<$fh>) {
40                 chomp;
41                 my ($at, $ct, $H) = split(/\./);
42                 $stk //= PublicInbox::IdxStack->new;
43                 # not bothering to parse blobs here, just using commit OID
44                 # as a blob OID since they're the same size + format
45                 $stk->push_rec('m', $at + 0, $ct + 0, $H, $H);
46                 push(@expect, [ 'm', $at, $ct, $H, $H ]);
47         }
48         $stk or skip('nothing from git log', 3);
49         is($stk->read_prepare, $stk, 'read_prepare');
50         is($stk->num_records, scalar(@expect), 'num_records matches expected');
51         my @result;
52         while (my @tmp = $stk->pop_rec) {
53                 unshift @result, \@tmp;
54         }
55         is_deeply(\@result, \@expect, 'results match expected');
56 }
57
58 done_testing;