Debian Codegraph @

test/test_apis.py

raw symbols SCIP
1 import re
2
3 import pytest
4
5 from linkify_it import LinkifyIt, SchemaError
6 from linkify_it.main import Match
7 from linkify_it.tlds import TLDS
8
9
10 def test_pretest_false():
11 linkifyit = LinkifyIt()
12 assert linkifyit.pretest("nolink") is False
13
14
15 def test_create_instance_with_schemas():
16 schemas = {"my:": {"validate": r"^\/\/[a-z]+"}}
17 linkifyit = LinkifyIt(schemas)
18
19 match = linkifyit.match("google.com. my:// my://asdf!")
20
21 assert match[0].text == "google.com"
22 assert match[1].text == "my://asdf"
23
24
25 def test_match_class():
26 linkifyit = LinkifyIt()
27 match = Match(linkifyit, 0)
28 assert (
29 match.__repr__()
30 == "linkify_it.main.Match({'schema': '', 'index': -1, 'last_index': -1, 'raw': '', 'text': '', 'url': ''})" # noqa: E501
31 )
32
33
34 def test_api_extend_tlds():
35 linkifyit = LinkifyIt()
36
37 assert linkifyit.test("google.myroot") is False
38
39 linkifyit.tlds("myroot", True)
40
41 assert linkifyit.test("google.myroot") is True
42 assert linkifyit.test("google.xyz") is False
43
44 # ref - http://data.iana.org/TLD/tlds-alpha-by-domain.txt
45 linkifyit.tlds(TLDS)
46
47 assert linkifyit.test("google.xyz") is True
48 assert linkifyit.test("google.myroot") is False
49
50
51 def test_api_add_rule_as_regex_with_default_normalizer():
52 linkifyit = LinkifyIt().add("my:", {"validate": re.compile(r"^\/\/[a-z]+")})
53
54 match = linkifyit.match("google.com. my:// my://asdf!")
55
56 assert match[0].text == "google.com"
57 assert match[1].text == "my://asdf"
58
59
60 def test_api_add_rule_as_regex_with_default_normalizer_with_no_compile():
61 linkifyit = LinkifyIt().add("my:", {"validate": r"^\/\/[a-z]+"})
62
63 match = linkifyit.match("google.com. my:// my://asdf!")
64
65 assert match[0].text == "google.com"
66 assert match[1].text == "my://asdf"
67
68
69 def test_api_add_rule_with_normalizer():
70 def func_normalize(self, m):
71 m.text = re.sub(r"^my://", "", m.text).upper()
72 m.url = m.url.upper()
73
74 linkifyit = LinkifyIt().add(
75 "my:", {"validate": re.compile(r"^\/\/[a-z]+"), "normalize": func_normalize}
76 )
77
78 match = linkifyit.match("google.com. my:// my://asdf!")
79
80 assert match[1].text == "ASDF"
81 assert match[1].url == "MY://ASDF"
82
83
84 def test_api_add_rule_with_normalizer_no_cimpile():
85 def func_normalize(self, m):
86 m.text = re.sub(r"^my://", "", m.text).upper()
87 m.url = m.url.upper()
88
89 linkifyit = LinkifyIt().add(
90 "my:", {"validate": r"^\/\/[a-z]+", "normalize": func_normalize}
91 )
92
93 match = linkifyit.match("google.com. my:// my://asdf!")
94
95 assert match[1].text == "ASDF"
96 assert match[1].url == "MY://ASDF"
97
98
99 def test_api_disable_rule():
100 linkifyit = LinkifyIt()
101
102 assert linkifyit.test("http://google.com")
103 assert linkifyit.test("[email protected]")
104 linkifyit.add("http:", None)
105 linkifyit.add("mailto:", None)
106 assert not linkifyit.test("http://google.com")
107 assert not linkifyit.test("[email protected]")
108
109
110 def test_api_add_bad_definition():
111 with pytest.raises(SchemaError):
112 linkifyit = LinkifyIt({"fuzzy_link": False})
113
114 linkifyit = LinkifyIt()
115
116 with pytest.raises(SchemaError):
117 linkifyit.add("test:", [])
118
119 linkifyit = LinkifyIt()
120
121 with pytest.raises(SchemaError):
122 linkifyit.add("test:", {"validate": []})
123
124 linkifyit = LinkifyIt()
125
126 with pytest.raises(SchemaError):
127
128 def func():
129 return False
130
131 linkifyit.add("test:", {"validate": func, "normalize": "bad"})
132
133
134 def test_api_at_position():
135 linkifyit = LinkifyIt()
136
137 assert linkifyit.test_schema_at("http://google.com", "http:", 5)
138 assert linkifyit.test_schema_at("http://google.com", "HTTP:", 5)
139 assert not linkifyit.test_schema_at("http://google.com", "http:", 6)
140
141 assert not linkifyit.test_schema_at("http://google.com", "bad_schema:", 6)
142
143
144 def test_api_correct_cache_value():
145 linkifyit = LinkifyIt()
146
147 match = linkifyit.match(".com. http://google.com google.com ftp://google.com")
148
149 assert match[0].text == "http://google.com"
150 assert match[1].text == "google.com"
151 assert match[2].text == "ftp://google.com"
152
153
154 def test_api_normalize():
155 linkifyit = LinkifyIt()
156
157 match = linkifyit.match("mailto:[email protected]")[0]
158
159 # assert match.text == "[email protected]"
160 assert match.url == "mailto:[email protected]"
161
162 match = linkifyit.match("[email protected]")[0]
163
164 # assert match.text == "[email protected]"
165 assert match.url == "mailto:[email protected]"
166
167
168 def test_api_twitter_rule():
169 linkifyit = LinkifyIt()
170
171 def validate(self, text, pos):
172 tail = text[pos:]
173
174 if not self.re.get("twitter"):
175 self.re["twitter"] = re.compile(
176 "^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + self.re["src_ZPCc"] + ")"
177 )
178 if self.re["twitter"].search(tail):
179 if pos > 2 and tail[pos - 2] == "@":
180 return False
181 return len(self.re["twitter"].search(tail).group())
182 return 0
183
184 def normalize(self, m):
185 m.url = "https://twitter.com/" + re.sub(r"^@", "", m.url)
186
187 linkifyit.add("@", {"validate": validate, "normalize": normalize})
188
189 assert linkifyit.match("hello, @gamajoba_!")[0].text == "@gamajoba_"
190 assert linkifyit.match(":@givi")[0].text == "@givi"
191 assert linkifyit.match(":@givi")[0].url == "https://twitter.com/givi"
192 assert not linkifyit.test("@@invalid")
193
194
195 def test_api_twitter_rule_no_compile():
196 linkifyit = LinkifyIt()
197
198 def validate(self, text, pos):
199 tail = text[pos:]
200
201 if not self.re.get("twitter"):
202 self.re["twitter"] = (
203 "^([a-zA-Z0-9_]){1,15}(?!_)(?=$|" + self.re["src_ZPCc"] + ")"
204 )
205 if re.search(self.re["twitter"], tail):
206 if pos > 2 and tail[pos - 2] == "@":
207 return False
208 return len(re.search(self.re["twitter"], tail).group())
209 return 0
210
211 def normalize(self, m):
212 m.url = "https://twitter.com/" + re.sub(r"^@", "", m.url)
213
214 linkifyit.add("@", {"validate": validate, "normalize": normalize})
215
216 assert linkifyit.match("hello, @gamajoba_!")[0].text == "@gamajoba_"
217 assert linkifyit.match(":@givi")[0].text == "@givi"
218 assert linkifyit.match(":@givi")[0].url == "https://twitter.com/givi"
219 assert not linkifyit.test("@@invalid")
220
221
222 def test_api_set_option_fuzzylink():
223 linkifyit = LinkifyIt(options={"fuzzy_link": False})
224
225 assert not linkifyit.test("google.com")
226
227 linkifyit.set({"fuzzy_link": True})
228
229 assert linkifyit.test("google.com")
230 assert linkifyit.match("google.com")[0].text == "google.com"
231
232
233 def test_api_set_option_fuzzyemail():
234 linkifyit = LinkifyIt(options={"fuzzy_email": False})
235
236 assert not linkifyit.test("[email protected]")
237
238 linkifyit.set({"fuzzy_email": True})
239
240 assert linkifyit.test("[email protected]")
241 assert linkifyit.match("[email protected]")[0].text == "[email protected]"
242
243
244 def test_api_set_option_fuzzyip():
245 linkifyit = LinkifyIt()
246
247 assert not linkifyit.test("1.1.1.1")
248
249 linkifyit.set({"fuzzy_ip": True})
250
251 assert linkifyit.test("1.1.1.1")
252 assert linkifyit.match("1.1.1.1")[0].text == "1.1.1.1"
253
254
255 def test_api_shoud_not_hang_in_fuzzy_mode_with_sequence_of_astrals():
256 linkifyit = LinkifyIt()
257
258 linkifyit.set({"fuzzy_link": True})
259
260 linkifyit.match(
261 "😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡😡 .com"
262 )
263
264
265 def test_api_shoud_accept_triple_minus():
266 linkifyit = LinkifyIt()
267
268 assert linkifyit.match("http://e.com/foo---bar")[0].text == "http://e.com/foo---bar"
269 assert linkifyit.match("[email protected]") is None
270
271 linkifyit = LinkifyIt(None, {"---": True})
272
273 assert linkifyit.match("http://e.com/foo---bar")[0].text == "http://e.com/foo"
274 assert linkifyit.match("[email protected]")[0].text == "[email protected]"
275
276
277 # issue #25. Schema key containing - not producing matches
278 @pytest.mark.parametrize(
279 "escape_str",
280 {".", "?", "*", "+", "^", "$", "[", "]", "\\", "(", ")", "{", "}", "|", "-"},
281 )
282 def test_api_add_alias_rule_with_excape_re_string(escape_str):
283 linkifyit = LinkifyIt()
284
285 linkifyit.add(f"foo{escape_str}bar:", "http:")
286 assert linkifyit.test(f"Check foo{escape_str}bar://test") is True
287
288
289 def test_api_blank_test_match_at_the_start():
290 linkifyit = LinkifyIt()
291
292 assert not linkifyit.match_at_start("")
293
294
295 def test_api_should_find_a_match_at_the_start():
296 linkifyit = LinkifyIt()
297
298 linkifyit = LinkifyIt(options={"fuzzy_link": True})
299 linkifyit.set({"fuzzy_link": True})
300
301 assert not linkifyit.test("@@invalid")
302 assert not linkifyit.match_at_start("google.com 123")
303 assert not linkifyit.match_at_start(" http://google.com 123")
304
305
306 def test_api_match_a_start_should_not_interfere_with_normal_match():
307 linkifyit = LinkifyIt()
308
309 str = "http://google.com http://google.com"
310 assert linkifyit.match_at_start(str)
311 assert len(linkifyit.match(str)) == 2
312
313 str = "aaa http://google.com http://google.com"
314 assert not linkifyit.match_at_start(str)
315 assert len(linkifyit.match(str)) == 2
316
317
318 def test_api_should_not_match_incomplete_links():
319 # regression test for https://github.com/markdown-it/markdown-it/issues/868
320 linkifyit = LinkifyIt()
321
322 assert not linkifyit.match_at_start("http://")
323 assert not linkifyit.match_at_start("https://")