forked from psss/did
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
151 lines (110 loc) · 3.65 KB
/
test_utils.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# coding: utf-8
import pytest
import os
import did
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Constants
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_email_re():
''' Confirm regex works as we would expect for extracting
name, login and email from standard email strings'''
from did.utils import EMAIL_REGEXP
# good
x = '"Chris Ward" <cward@redhat.com>'
groups = EMAIL_REGEXP.search(x).groups()
assert len(groups) == 2
assert groups[0] == 'Chris Ward'
assert groups[1] == 'cward@redhat.com'
x = 'cward@redhat.com'
groups = EMAIL_REGEXP.search(x).groups()
assert len(groups) == 2
assert groups[0] is None
assert groups[1] == 'cward@redhat.com'
# bad
x = 'cward'
groups = EMAIL_REGEXP.search(x)
assert groups is None
# ugly
x = '"" <>'
groups = EMAIL_REGEXP.search(x)
assert groups is None
def test_log():
from did.utils import log
assert log
log.name == 'did'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Utils
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_import_success():
import sys
from did.utils import _import
s = _import("sys", True)
assert s is sys
s = _import("blah", True)
assert s is None
def test_find_base():
top = os.path.dirname(os.path.dirname(did.__file__))
assert top == did.utils._find_base(__file__)
assert top == did.utils._find_base(os.path.dirname(__file__))
def test_load_components():
top = os.path.dirname(did.__file__)
assert did.utils.load_components(top) > 0
assert did.utils.load_components("did.plugins") > 0
def test_import_failure():
from did.utils import _import
with pytest.raises(ImportError):
_import("blah", False)
def test_header():
from did.utils import header
assert header
def test_shorted():
from did.utils import shorted
assert shorted
def test_item():
from did.utils import item
assert item
def test_pluralize():
from did.utils import pluralize
assert pluralize
assert pluralize("word") == "words"
assert pluralize("bounty") == "bounties"
assert pluralize("mass") == "masses"
def test_listed():
from did.utils import listed
assert listed
assert listed(range(1)) == "0"
assert listed(range(2)) == "0 and 1"
assert listed(range(3), quote='"') == '"0", "1" and "2"'
assert listed(range(4), max=3) == "0, 1, 2 and 1 more"
assert listed(range(5), 'number', max=3) == "0, 1, 2 and 2 more numbers"
assert listed(range(6), 'category') == "6 categories"
assert listed(7, "leaf", "leaves") == "7 leaves"
assert listed([], "item", max=0) == "0 items"
def test_ascii():
from did.utils import ascii
assert ascii
assert ascii("ěščřžýáíé") == "escrzyaie"
assert ascii(0) == "0"
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Logging
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_eprint():
from did.utils import eprint
assert eprint
def test_info():
from did.utils import info
assert info
info("something")
info("no-new-line", newline=False)
def test_Logging():
from did.utils import Logging
assert Logging
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Coloring
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_Coloring():
from did.utils import Coloring
assert Coloring
def test_color():
from did.utils import color
assert color