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