Skip to content

Commit d7f0857

Browse files
committed
Add doctest and tests for clearcode coordinate helpers
1 parent 469c506 commit d7f0857

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

clearcode/cdutils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,14 @@ def str2coord(s):
524524
URL: "cd:/gem/rubygems/-/mocha/1.7.0"
525525
URN: "urn:gem:rubygems:-:mocha:revision:1.7.0:tool:scancode:3.1.0"
526526
plain: /gem/rubygems/foo/mocha/1.7.0"
527+
528+
>>> str2coord("cd:/gem/rubygems/-/mocha/1.7.0")
529+
{'type': 'gem', 'provider': 'rubygems', 'namespace': '-', 'name': 'mocha', 'revision': '1.7.0'}
530+
>>> str2coord("urn:gem:rubygems:-:mocha:revision:1.7.0:tool:scancode:3.1.0")
531+
{'type': 'gem', 'provider': 'rubygems', 'namespace': '-', 'name': 'mocha', 'revision': '1.7.0'}
532+
>>> str2coord("/gem/rubygems/foo/mocha/1.7.0")
533+
{'type': 'gem', 'provider': 'rubygems', 'namespace': 'foo', 'name': 'mocha', 'revision': '1.7.0'}
527534
"""
528-
# TODO: Add doctest
529535
is_urn = s.startswith("urn")
530536
is_url = s.startswith("cd:")
531537
splitter = ":" if is_urn else "/"

clearcode/tests/test_cdutils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
#
4+
from clearcode.cdutils import coord2str
5+
from clearcode.cdutils import str2coord
6+
7+
8+
def test_str2coord_from_cd_url():
9+
assert str2coord("cd:/gem/rubygems/-/mocha/1.7.0") == {
10+
"type": "gem",
11+
"provider": "rubygems",
12+
"namespace": "-",
13+
"name": "mocha",
14+
"revision": "1.7.0",
15+
}
16+
17+
18+
def test_str2coord_from_urn_ignores_extra_segments():
19+
assert str2coord("urn:gem:rubygems:-:mocha:revision:1.7.0:tool:scancode:3.1.0") == {
20+
"type": "gem",
21+
"provider": "rubygems",
22+
"namespace": "-",
23+
"name": "mocha",
24+
"revision": "1.7.0",
25+
}
26+
27+
28+
def test_coord2str_preserves_missing_namespace_as_dash():
29+
assert coord2str(
30+
{
31+
"type": "git",
32+
"provider": "github",
33+
"namespace": None,
34+
"name": "license-expression",
35+
"revision": "70277cdfc186466667cb58ec9f9c7281e68a221b",
36+
}
37+
) == "git/github/-/license-expression/70277cdfc186466667cb58ec9f9c7281e68a221b"

0 commit comments

Comments
 (0)