Skip to content

Commit efbcc2a

Browse files
committed
Implementing pytest and TravisCI; added docstrings
1 parent b2a09de commit efbcc2a

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea
22
build
33
*.egg-info
4-
*/__pycache__
4+
*/__pycache__
5+
.coverage

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: python
2+
python:
3+
- "3.5"
4+
- "3.6"
5+
- "3.7"
6+
- "3.8"
7+
# command to install dependencies
8+
install:
9+
- pip install -U coveralls pytest
10+
# command to run tests
11+
script:
12+
- coverage run --source=domdf_python_tools -m pytest
13+
after_success:
14+
- coveralls

domdf_python_tools/__init__.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,44 @@ def as_text(value):
5353

5454

5555
def str2tuple(input_string, sep=","):
56+
"""
57+
Convert a comma-seperated string of integers into a tuple
58+
59+
:param input_string: The string to be converted into a tuple
60+
:type input_string: str
61+
:param sep: The separator in the string, default ","
62+
:type sep: str
63+
64+
:rtype: tuple
65+
"""
66+
5667
return tuple(int(x) for x in input_string.split(sep))
5768

5869

5970
def tuple2str(input_tuple, sep=","):
60-
return sep.join(input_tuple)
71+
"""
72+
Convert a tuple into a comma-seperated string
73+
74+
:param input_tuple: The tuple to be joined into a string
75+
:type input_tuple: tuple
76+
:param sep: The separator in the string, default ","
77+
:type sep: str
78+
79+
:rtype: str
80+
"""
81+
82+
return sep.join([str(x) for x in input_tuple])
6183

6284

6385
def chunks(l, n):
64-
"""Yield successive n-sized chunks from l."""
86+
"""
87+
Yield successive n-sized chunks from l.
88+
89+
:param l:
90+
:param n:
91+
:return:
92+
"""
93+
6594
for i in range(0, len(l), n):
6695
yield l[i:i + n]
6796

tests/test_init.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
test_init
4+
~~~~~~~~~~~~~~~
5+
6+
Test functions in __init__.py
7+
8+
"""
9+
10+
import types
11+
12+
from domdf_python_tools import pyversion, str2tuple, tuple2str, chunks, list2str, list2string
13+
14+
def test_pyversion():
15+
assert isinstance(pyversion, int)
16+
17+
def test_str2tuple():
18+
assert isinstance(str2tuple("1,2,3"), tuple) # tests without spaces
19+
assert isinstance(str2tuple("1, 2, 3"), tuple) # tests with spaces
20+
assert isinstance(str2tuple("1; 2; 3", sep=";"), tuple) # tests with semicolon
21+
if str2tuple("1,2,3") == (1, 2, 3):
22+
assert 1
23+
else:
24+
assert 0
25+
26+
def test_tuple2str():
27+
assert isinstance(tuple2str(("1","2","3",)), str)
28+
if tuple2str((1, 2, 3)) == "1,2,3":
29+
assert 1
30+
else:
31+
assert 0
32+
33+
assert isinstance(tuple2str((1,2,3,), sep=";"), str) # tests with semicolon
34+
if tuple2str((1, 2, 3), sep=";") == "1;2;3":
35+
assert 1
36+
else:
37+
assert 0
38+
39+
def test_chunks():
40+
assert isinstance(chunks(list(range(100)), 5), types.GeneratorType)
41+
if list(chunks(list(range(100)), 5))[0] == [0,1,2,3,4]:
42+
assert 1
43+
else:
44+
assert 0
45+
46+
47+
def test_list2str():
48+
assert isinstance(list2str([1, 2, 3,]), str)
49+
if list2str([1, 2, 3]) == "1,2,3":
50+
assert 1
51+
else:
52+
assert 0
53+
54+
assert isinstance(list2str([1, 2, 3], sep=";"), str) # tests with semicolon
55+
if list2str((1, 2, 3), sep=";") == "1;2;3":
56+
assert 1
57+
else:
58+
assert 0
59+
60+
assert isinstance(list2string([1, 2, 3, ]), str)
61+
if list2string([1, 2, 3]) == "1,2,3":
62+
assert 1
63+
else:
64+
assert 0
65+
66+
assert isinstance(list2string([1, 2, 3], sep=";"), str) # tests with semicolon
67+
if list2string((1, 2, 3), sep=";") == "1;2;3":
68+
assert 1
69+
else:
70+
assert 0
71+
72+

0 commit comments

Comments
 (0)