]> Sergey Matveev's repositories - public-inbox.git/blob - t/linkify.t
linkify: handle URLs in parenthesized statements
[public-inbox.git] / t / linkify.t
1 # Copyright (C) 2016 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 # handle URLs in parenthesized statements
18 {
19         my $l = PublicInbox::Linkify->new;
20         my $u = 'http://example.com/';
21         my $s = "(see: $u)";
22         $s = $l->linkify_1($s);
23         $s = $l->linkify_2($s);
24         is($s, qq{(see: <a\nhref="$u">$u</a>)}, 'trailing ) not in URL');
25 }
26
27 {
28         my $l = PublicInbox::Linkify->new;
29         my $u = 'http://example.com/url-with-trailing-semicolon';
30         my $s = $u . ';';
31         $s = $l->linkify_1($s);
32         $s = $l->linkify_2($s);
33         is($s, qq(<a\nhref="$u">$u</a>;), 'trailing semicolon not in URL');
34 }
35
36 {
37         my $l = PublicInbox::Linkify->new;
38         my $u = 'http://example.com/url-with-(parens)';
39         my $s = "hello $u world";
40         $s = $l->linkify_1($s);
41         $s = $l->linkify_2($s);
42         is($s, qq(hello <a\nhref="$u">$u</a> world), 'URL preserved');
43
44         $u .= "?query=a";
45         $s = "hello $u world";
46         $s = $l->linkify_1($s);
47         $s = $l->linkify_2($s);
48         is($s, qq(hello <a\nhref="$u">$u</a> world), 'query preserved');
49
50         $u .= "#fragment";
51         $s = "hello $u world";
52         $s = $l->linkify_1($s);
53         $s = $l->linkify_2($s);
54         is($s, qq(hello <a\nhref="$u">$u</a> world),
55           'query + fragment preserved');
56
57         $u = "http://example.com/";
58         $s = "hello $u world";
59         $s = $l->linkify_1($s);
60         $s = $l->linkify_2($s);
61         is($s, qq(hello <a\nhref="$u">$u</a> world), "root URL preserved");
62
63         $u = "http://example.com/#fragment";
64         $s = "hello $u world";
65         $s = $l->linkify_1($s);
66         $s = $l->linkify_2($s);
67         is($s, qq(hello <a\nhref="$u">$u</a> world), "root + fragment");
68 }
69
70 # Markdown compatibility
71 {
72         my $l = PublicInbox::Linkify->new;
73         my $u = 'http://example.com/';
74         my $s = "[markdown]($u)";
75         $s = $l->linkify_1($s);
76         $s = $l->linkify_2($s);
77         is($s, qq![markdown](<a\nhref="$u">$u</a>)!, 'Markdown-compatible');
78
79         $s = qq![markdown]($u "title")!;
80         $s = $l->linkify_1($s);
81         $s = $l->linkify_2($s);
82         is($s, qq![markdown](<a\nhref="$u">$u</a> "title")!,
83                 'Markdown title compatible');
84
85         $s = qq![markdown]($u).!;
86         $s = $l->linkify_1($s);
87         $s = $l->linkify_2($s);
88         is($s, qq![markdown](<a\nhref="$u">$u</a>).!,
89                 'Markdown-compatible end of sentence');
90 }
91
92 done_testing();