-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09c3279
commit 6529e6e
Showing
3 changed files
with
174 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
"""Tests for paths.py""" | ||
|
||
from dol import StringTemplate | ||
|
||
def test_string_template(): | ||
|
||
def test_string_template_template_construction(): | ||
assert StringTemplate('{}.ext').template == '{_1}.ext' | ||
assert StringTemplate('{name}.ext').template == '{name}.ext' | ||
assert StringTemplate('{::\w+}.ext').template == '{_1}.ext' | ||
assert StringTemplate('{name::\w+}.ext').template == '{name}.ext' | ||
assert StringTemplate('{name::\w+}.ext').template == '{name}.ext' | ||
assert StringTemplate('{name:0.02f}.ext').template == '{name}.ext' | ||
assert StringTemplate('{name:0.02f:\w+}.ext').template == '{name}.ext' | ||
assert StringTemplate('{:0.02f:\w+}.ext').template == '{_1}.ext' | ||
|
||
|
||
def test_string_template_regex(): | ||
assert StringTemplate('{}.ext').regex.pattern == '(?P<_1>.*)\\.ext' | ||
assert StringTemplate('{name}.ext').regex.pattern == '(?P<name>.*)\\.ext' | ||
assert StringTemplate('{::\w+}.ext').regex.pattern == '(?P<_1>\\w+)\\.ext' | ||
assert StringTemplate('{name::\w+}.ext').regex.pattern == '(?P<name>\\w+)\\.ext' | ||
assert StringTemplate('{:0.02f:\w+}.ext').regex.pattern == '(?P<_1>\\w+)\\.ext' | ||
assert StringTemplate('{name:0.02f:\w+}.ext').regex.pattern == '(?P<name>\\w+)\\.ext' | ||
|
||
|
||
def test_string_template_simple(): | ||
from dol.paths import StringTemplate | ||
from collections import namedtuple | ||
|
||
st = StringTemplate( | ||
'{name} is {age} years old.', field_patterns={'name': r'\w+', 'age': r'\d+'} | ||
'root/{name}/v_{version}.json', | ||
field_patterns={'name': r'\w+', 'version': r'\d+'}, | ||
from_str_funcs={'version': int}, | ||
) | ||
|
||
assert st.str_to_dict('Alice is 30 years old.') == {'name': 'Alice', 'age': '30'} | ||
assert st.dict_to_str({'name': 'Alice', 'age': '30'}) == 'Alice is 30 years old.' | ||
assert st.dict_to_tuple({'name': 'Alice', 'age': '30'}) == ('Alice', '30') | ||
assert st.tuple_to_dict(('Alice', '30')) == {'name': 'Alice', 'age': '30'} | ||
assert st.str_to_tuple('Alice is 30 years old.') == ('Alice', '30') | ||
assert st.tuple_to_str(('Alice', '30')) == 'Alice is 30 years old.' | ||
assert st.str_to_dict('root/Alice/v_30.json') == {'name': 'Alice', 'version': 30} | ||
assert st.dict_to_str({'name': 'Alice', 'version': 30}) == 'root/Alice/v_30.json' | ||
assert st.dict_to_tuple({'name': 'Alice', 'version': 30}) == ('Alice', 30) | ||
assert st.tuple_to_dict(('Alice', 30)) == {'name': 'Alice', 'version': 30} | ||
assert st.str_to_tuple('root/Alice/v_30.json') == ('Alice', 30) | ||
assert st.tuple_to_str(('Alice', 30)) == 'root/Alice/v_30.json' | ||
|
||
VersionedFile = st.dict_to_namedtuple({'name': 'Alice', 'version': 30}) | ||
|
||
Person = st.dict_to_namedtuple({'name': 'Alice', 'age': '30'}) | ||
assert Person == namedtuple('Person', ['name', 'age'])('Alice', '30') | ||
assert st.namedtuple_to_dict(Person) == {'name': 'Alice', 'age': '30'} | ||
from collections import namedtuple | ||
assert VersionedFile == namedtuple('VersionedFile', ['name', 'version'])('Alice', 30) | ||
assert st.namedtuple_to_dict(VersionedFile) == {'name': 'Alice', 'version': 30} | ||
|
||
assert st.str_to_simple_str('Alice is 30 years old.', '-') == 'Alice-30' | ||
assert st.simple_str_to_str('Alice-30', '-') == 'Alice is 30 years old.' | ||
assert st.str_to_simple_str('root/Alice/v_30.json') == 'Alice,30' | ||
assert st.str_to_simple_str('root/Alice/v_30.json', '-') == 'Alice-30' | ||
assert st.simple_str_to_str('Alice-30', '-') == 'root/Alice/v_30.json' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters