]> Sergey Matveev's repositories - public-inbox.git/blob - t/linkify.t
www: drop --subject from "git send-email" instructions
[public-inbox.git] / t / linkify.t
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Linkify;
7
8 {
9         my $l = PublicInbox::Linkify->new;
10         my $u = 'http://example.com/url-with-trailing-period';
11         my $s = $u . '.';
12         $s = $l->linkify_1($s);
13         $s = $l->linkify_2($s);
14         is($s, qq(<a\nhref="$u">$u</a>.), 'trailing period not in URL');
15 }
16
17 {
18         my $l = PublicInbox::Linkify->new;
19         my $u = 'http://i-forgot-trailing-slash.example.com';
20         my $s = $u;
21         $s = $l->linkify_1($s);
22         $s = $l->linkify_2($s);
23         is($s, qq(<a\nhref="$u">$u</a>), 'missing trailing slash OK');
24 }
25
26 # handle URLs in parenthesized statements
27 {
28         my $l = PublicInbox::Linkify->new;
29         my $u = 'http://example.com/';
30         my $s = "(see: $u)";
31         $s = $l->linkify_1($s);
32         $s = $l->linkify_2($s);
33         is($s, qq{(see: <a\nhref="$u">$u</a>)}, 'trailing ) not in URL');
34 }
35
36 {
37         my $l = PublicInbox::Linkify->new;
38         my $u = 'http://example.com/url-with-trailing-semicolon';
39         my $s = $u . ';';
40         $s = $l->linkify_1($s);
41         $s = $l->linkify_2($s);
42         is($s, qq(<a\nhref="$u">$u</a>;), 'trailing semicolon not in URL');
43 }
44
45 {
46         my $l = PublicInbox::Linkify->new;
47         my $u = 'http://example.com/url-with-(parens)';
48         my $s = "hello $u world";
49         $s = $l->linkify_1($s);
50         $s = $l->linkify_2($s);
51         is($s, qq(hello <a\nhref="$u">$u</a> world), 'URL preserved');
52
53         $s = "$u. hi";
54         $s = $l->linkify_1($s);
55         $s = $l->linkify_2($s);
56         is($s, qq(<a\nhref="$u">$u</a>. hi), 'paired () in URL OK');
57
58         $u .= "?query=a";
59         $s = "hello $u world";
60         $s = $l->linkify_1($s);
61         $s = $l->linkify_2($s);
62         is($s, qq(hello <a\nhref="$u">$u</a> world), 'query preserved');
63
64         $u .= "#fragment";
65         $s = "hello $u world";
66         $s = $l->linkify_1($s);
67         $s = $l->linkify_2($s);
68         is($s, qq(hello <a\nhref="$u">$u</a> world),
69           'query + fragment preserved');
70
71         $u = "http://example.com/";
72         $s = "hello $u world";
73         $s = $l->linkify_1($s);
74         $s = $l->linkify_2($s);
75         is($s, qq(hello <a\nhref="$u">$u</a> world), "root URL preserved");
76
77         $u = "http://example.com/#fragment";
78         $s = "hello $u world";
79         $s = $l->linkify_1($s);
80         $s = $l->linkify_2($s);
81         is($s, qq(hello <a\nhref="$u">$u</a> world), "root + fragment");
82 }
83
84 # Markdown compatibility
85 {
86         my $l = PublicInbox::Linkify->new;
87         my $u = 'http://example.com/';
88         my $s = "[markdown]($u)";
89         $s = $l->linkify_1($s);
90         $s = $l->linkify_2($s);
91         is($s, qq![markdown](<a\nhref="$u">$u</a>)!, 'Markdown-compatible');
92
93         $s = qq![markdown]($u "title")!;
94         $s = $l->linkify_1($s);
95         $s = $l->linkify_2($s);
96         is($s, qq![markdown](<a\nhref="$u">$u</a> "title")!,
97                 'Markdown title compatible');
98
99         $s = qq![markdown]($u).!;
100         $s = $l->linkify_1($s);
101         $s = $l->linkify_2($s);
102         is($s, qq![markdown](<a\nhref="$u">$u</a>).!,
103                 'Markdown-compatible end of sentence');
104 }
105
106 # Perl and Ruby code compatibility
107 {
108         my $l = PublicInbox::Linkify->new;
109         my $u = 'http://example.com/';
110         foreach my $q ("'%s'", '"%s"', 'q!%s!', 'q(%s)') {
111                 # Perl
112                 my $s = sprintf("my \$var = $q;", $u);
113                 $s = $l->linkify_1($s);
114                 $s = $l->linkify_2($s);
115                 like($s, qr/>\Q$u\E</, "no quote($q) in URL");
116
117                 # applies to Ruby, too
118                 $s = sprintf("$q,", $u);
119                 $s = $l->linkify_1($s);
120                 $s = $l->linkify_2($s);
121                 like($s, qr/>\Q$u\E</, "no quote($q) in URL array");
122         }
123 }
124
125 # dangling ')'  cf. see MaintNotes in git.git todo branch
126 {
127         my $l = PublicInbox::Linkify->new;
128         my $s = '(see http://example.com/).';
129         $s = $l->linkify_1($s);
130         $s = $l->linkify_2($s);
131         like($s, qr!\(see <a[^>]+>http://example\.com/</a>\)\.!s,
132                 'punctuation with unpaired ) OK')
133 }
134
135 if ('IDN example: <ACDB98F4-178C-43C3-99C4-A1D03DD6A8F5@sb.org>') {
136         my $hc = '&#26376;';
137         my $u = "http://www.\x{6708}.example.com/";
138         my $s = $u;
139         my $l = PublicInbox::Linkify->new;
140         $s = $l->linkify_1($s);
141         $s = $l->linkify_2($s);
142         my $expect = qq{<a
143 href="http://www.$hc.example.com/">http://www.$hc.example.com/</a>};
144         is($s, $expect, 'IDN message escaped properly');
145 }
146
147 done_testing();