]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntp.t
tests: fixup requirements for tests
[public-inbox.git] / t / nntp.t
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 use Data::Dumper;
7
8 foreach my $mod (qw(DBD::SQLite Search::Xapian Danga::Socket)) {
9         eval "require $mod";
10         plan skip_all => "$mod missing for nntp.t" if $@;
11 }
12
13 use_ok 'PublicInbox::NNTP';
14
15 {
16         sub quote_str {
17                 my (undef, $s) = split(/ = /, Dumper($_[0]), 2);
18                 $s =~ s/;\n//;
19                 $s;
20         }
21
22         sub wm_prepare {
23                 my ($wm) = @_;
24                 my $orig = qq{'$wm'};
25                 PublicInbox::NNTP::wildmat2re($_[0]);
26                 my $new = quote_str($_[0]);
27                 ($orig, $new);
28         }
29
30         sub wildmat_like {
31                 my ($str, $wm) = @_;
32                 my ($orig, $new) = wm_prepare($wm);
33                 like($str, $wm, "$orig matches '$str' using $new");
34         }
35
36         sub wildmat_unlike {
37                 my ($str, $wm, $check_ex) = @_;
38                 if ($check_ex) {
39                         use re 'eval';
40                         my $re = qr/$wm/;
41                         like($str, $re, "normal re with $wm matches, but ...");
42                 }
43                 my ($orig, $new) = wm_prepare($wm);
44                 unlike($str, $wm, "$orig does not match '$str' using $new");
45         }
46
47         wildmat_like('[foo]', '[\[foo\]]');
48         wildmat_like('any', '*');
49         wildmat_unlike('bar.foo.bar', 'foo.*');
50
51         # no code execution
52         wildmat_unlike('HI', '(?{"HI"})', 1);
53         wildmat_unlike('HI', '[(?{"HI"})]', 1);
54 }
55
56 {
57         sub ngpat_like {
58                 my ($str, $pat) = @_;
59                 my $orig = $pat;
60                 PublicInbox::NNTP::ngpat2re($pat);
61                 like($str, $pat, "'$orig' matches '$str' using $pat");
62         }
63
64         ngpat_like('any', '*');
65         ngpat_like('a.s.r', 'a.t,a.s.r');
66         ngpat_like('a.s.r', 'a.t,a.s.*');
67 }
68
69 {
70         use POSIX qw(strftime);
71         sub time_roundtrip {
72                 my ($date, $time, $gmt) = @_;
73                 my $m = join(' ', @_);
74                 my $ts = PublicInbox::NNTP::parse_time(@_);
75                 my @t = gmtime($ts);
76                 my ($d, $t);
77                 if (length($date) == 8) {
78                         ($d, $t) = split(' ', strftime('%Y%m%d %H%M%S', @t));
79                 } else {
80                         ($d, $t) = split(' ', strftime('%g%m%d %H%M%S', @t));
81                 }
82                 is_deeply([$d, $t], [$date, $time], "roundtripped: $m");
83                 $ts;
84         }
85         my $x1 = time_roundtrip(qw(20141109 060606 GMT));
86         my $x2 = time_roundtrip(qw(141109 060606 GMT));
87         my $x3 = time_roundtrip(qw(930724 060606 GMT));
88
89         SKIP: {
90                 skip('YYMMDD test needs updating', 2) if (time > 0x7fffffff);
91                 # our world probably ends in 2038, but if not we'll try to
92                 # remember to update the test then
93                 is($x1, $x2, 'YYYYMMDD and YYMMDD parse identically');
94                 is(strftime('%Y', gmtime($x3)), '1993', '930724 was in 1993');
95         }
96 }
97
98 done_testing();