Skip to content

Commit ee10341

Browse files
authored
Merge pull request #309 from sash2104/fix/list_embedded_urls
Fix broken URL detection
2 parents 1375e51 + 9face36 commit ee10341

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

onlinejudge_verify/languages/special_comments.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ def list_embedded_urls(path: pathlib.Path) -> List[str]:
5959
urls = []
6060
for url in pattern.findall(content):
6161
# The URL may be written like `"https://atcoder.jp/"`. In this case, we need to remove `"`s around the URL.
62+
# We also need to remove trailing superfluous chars in a case like `{"url":"https://atcoder.jp/"}`.
6263
for quote in ("'", '"', '`'):
63-
if len(url) >= 2 and url.startswith(quote) and url.endswith(quote):
64-
url = url[1:-1]
64+
if url.startswith(quote):
65+
end_quote_pos = url.rfind(quote)
66+
if end_quote_pos == 0:
67+
# Remove opening quote from the URL like `"https://atcoder.jp/`
68+
url = url[1:]
69+
else:
70+
# Remove quotes and trailing superfluous chars around the URL
71+
url = url[1:end_quote_pos]
6572
break
6673
urls.append(url)
6774
return sorted(set(urls))

tests/test_special_comments.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import textwrap
2+
import unittest
3+
from typing import *
4+
5+
import onlinejudge_verify.languages.special_comments as special_comments
6+
import tests.utils
7+
8+
9+
class TestSpecialComments(unittest.TestCase):
10+
"""Unit tests for languages/special_comments.py
11+
"""
12+
def test_list_embedded_urls(self) -> None:
13+
files = {
14+
'main.cpp': textwrap.dedent("""\
15+
// URL with quotes
16+
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
17+
// url='https://atcoder.jp/'
18+
// `https://atcoder.jp/contests/abc001`
19+
//
20+
// URL without quotes
21+
// @see https://atcoder.jp/contests/abc002
22+
//
23+
// URL with quotes and extra characters
24+
// {"url": "https://atcoder.jp/contests/abc003"}
25+
// ('https://atcoder.jp/contests/abc004')
26+
//
27+
// URL with opening quote and without closing quote
28+
// "https://atcoder.jp/contests/abc005
29+
""").encode(),
30+
}
31+
expected = [
32+
'http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A',
33+
'https://atcoder.jp/',
34+
'https://atcoder.jp/contests/abc001',
35+
'https://atcoder.jp/contests/abc002',
36+
'https://atcoder.jp/contests/abc003',
37+
'https://atcoder.jp/contests/abc004',
38+
'https://atcoder.jp/contests/abc005',
39+
]
40+
41+
with tests.utils.load_files(files) as tempdir:
42+
with tests.utils.chdir(tempdir):
43+
file_path = tempdir / 'main.cpp'
44+
actual = sorted(special_comments.list_embedded_urls(file_path))
45+
self.assertEqual(actual, expected)

0 commit comments

Comments
 (0)