-
Notifications
You must be signed in to change notification settings - Fork 2
/
cute.py
98 lines (81 loc) · 3.38 KB
/
cute.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
########################################################################################
# Cute is a minimal Cucumber feature/steps implementation, without pretty much anything
# that can be called 'implementation'. Parts borrowed from 'pea' by Tim Cuthbertson,
# removed dependencies to Nose, World and unittest (so in other words, cut it loose).
#
# Rudi Niemeijer - april 2018
# Todo:
# - Use colors
# - Catch the Fails and Successes of each RUN
# - Write output to log
########################################################################################
__all__ = [
'step',
'steps',
'Given',
'When',
'Then',
'And'
]
class stepCollection(object):
def __setattr__(self, attr, val):
if hasattr(self, attr):
raise RuntimeError("Step %s is already in use!" % (attr,))
return super(stepCollection, self).__setattr__(attr, val)
class object(object): pass
steps = stepCollection()
class stepCollectionWrapper(object):
def __init__(self, prefix):
self._prefix = prefix
def __getattr__(self, a):
attr = getattr(steps, a)
return attr(self._prefix)
Given = stepCollectionWrapper('Given')
When = stepCollectionWrapper('When')
Then = stepCollectionWrapper('Then')
And = stepCollectionWrapper('And')
def step(func):
#print('Added stepdescription %s' % (func.__name__))
setattr(steps, func.__name__, lambda prefix: withFormatting(prefix, func))
return func
def withFormatting(prefix, func):
def _run(*arguments, **kw):
name = func.__name__.replace('_', ' ')
def formattedStepResult():
formattedString = prefix + ' ' + name
if prefix == 'Given':
formattedString = '\n' + formattedString
if not 'undisclosed arguments' in name: #another crude hack to prevent long lists from being printed
if arguments:
a = 1
for arg in arguments:
if type(arg) is list:
argtext = "(list)"
elif type(arg) is tuple:
argtext = "(tuple)"
elif type(arg) is set:
argtext = "(set)"
elif type(arg) is dict:
argtext = "(dictionary)"
else:
argtext = str(arg)
if a == 2:
if len(arguments) == 2:
formattedString = formattedString + " with value '" + argtext + "'"
else:
formattedString = formattedString + " with values '" + argtext + "'"
if a > 2:
if ('credentials' in formattedString) and (a == 3): # Crude hack, but now we're KSP compliant
formattedString = formattedString + ", '********'"
else:
formattedString = formattedString + ", '" + argtext + "'"
a = a + 1
return formattedString
try:
returnValue = func(*arguments, **kw)
print(formattedStepResult())
return returnValue
except:
print(formattedStepResult())
raise
return _run