/
test_io.py
111 lines (90 loc) · 3.52 KB
/
test_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from pathlib import PosixPath
from snakemake.io import _wildcard_regex, expand
from snakemake.exceptions import WildcardError
def test_wildcard_regex():
def matches(text):
return [
(match.group("name"), match.group("constraint"))
for match in _wildcard_regex.finditer(text)
]
# without constraints
assert matches("") == []
assert matches("{") == []
assert matches("}") == []
assert matches("{}") == []
assert matches("{0}") == [("0", None)]
assert matches("{abc}") == [("abc", None)]
assert matches("abc{def}{ghi}") == [("def", None), ("ghi", None)]
# with constraints
assert matches("{w,constraint}") == [("w", "constraint")]
assert matches("{w , constraint}") == [("w", "constraint")]
# fails because constraint is detected as 'constraint '
# assert matches('{w,constraint }') == [('w', 'constraint')]
assert matches("abc { w , constraint} def") == [("w", "constraint")]
# multiple wildcards
assert matches("{a,1} {b,2} {c,3}") == [("a", "1"), ("b", "2"), ("c", "3")]
# more complicated constraints
assert matches(r"{w,([a-z]+|pat\|t*ern)}") == [("w", r"([a-z]+|pat\|t*ern)")]
assert matches(r"{w,([a-z]+|pat\|te{1,3}rn){5,7}}") == [
("w", r"([a-z]+|pat\|te{1,3}rn){5,7}")
]
# This used to be very slow with an older version of the regex
assert matches("{w, long constraint without closing brace") == []
def test_expand():
wildcards = {"a": [1, 2], "b": [3, 4], "c": [5]}
# each provided wildcard is used in the filepattern
assert sorted(expand("{a}{b}{c}", **wildcards)) == sorted(
["135", "145", "235", "245"]
)
# redundant wildcards are provided
assert sorted(expand("{a}{c}", **wildcards)) == sorted(["15", "25"])
# missing wildcards (should fail)
try:
expand("{a}{d}", **wildcards)
assert False
except WildcardError:
pass
# do not expand on strings and non iterables
assert expand("{x}{y}", **{"x": "Hello, ", "y": "world!"}) == ["Hello, world!"]
assert expand("{x}{y}", **{"x": 4, "y": 2}) == ["42"]
# format-minilang: field names
assert sorted(
expand("first letter of sample: {samples[0]}", samples=["A123", "B456", "C789"])
) == sorted(
[
"first letter of sample: A",
"first letter of sample: B",
"first letter of sample: C",
]
)
assert expand("{str.__class__}", str="") == ["<class 'str'>"]
# format-minilang: conversions
class ConvTest:
def __str__(self):
return "string"
def __repr__(self):
return "representation"
assert expand("{test!r}", test=ConvTest()) == ["representation"]
assert expand("{test!s}", test=ConvTest()) == ["string"]
# format-minilang: format specifications
assert sorted(
expand(
"The answer to life, the universe, and everything: {answer:f}",
answer=range(41, 43),
)
) == sorted(
[
"The answer to life, the universe, and everything: 41.000000",
"The answer to life, the universe, and everything: 42.000000",
]
)
# multiple filepatterns with different wildcards
assert sorted(
expand(["a: {a} + b: {b}", "c: {c}"], a="aa", b=["b", "bb"], c=["c", "cc"])
) == sorted(["a: aa + b: b", "a: aa + b: bb", "c: c", "c: cc"])
# expand on pathlib.Path objects
assert expand(
PosixPath() / "{x}" / "{y}",
x="Hello",
y="world",
) == ["Hello/world"]