]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntp.t
read-only NNTP server
[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 done_testing();