-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
119 lines (103 loc) · 3.6 KB
/
test.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
import argparse
import textwrap
import sys
from commandline_parsable import parsable_base
from commandline_parsable import split_by_outerlevel_character as resplit
import commandline_parsable
@parsable_base(base_instantiable=True, required_kwargs=["hallo"], factory_function=None,)
class ClassA(object):
"""
This is the Base-Class
"""
def __init__(self, hallo):
self._hallo = hallo
class ClassB(ClassA):
"""
The is B-B-B-Better
"""
pass
class ClassC(ClassA):
"""
The C-C-C-C-classical option
"""
pass
class ClassD(ClassB):
"""
This is D-D-D-D-delicate
"""
pass
@parsable_base(base_instantiable=True, factory_function="build")
class AnotherClass(object):
"""
This is another Base-Class
"""
@classmethod
def build(cls, arg1, arg2):
instance = cls()
instance.arg1 = arg1
instance.arg2 = arg2
return instance
class AnotherSubClass(AnotherClass):
"""
This is another Sub-Class
"""
@classmethod
def build(cls, arg1, arg2, arg3):
instance = cls()
instance.arg1 = arg1
instance.arg2 = arg2
instance.arg3 = arg3
return instance
def test_helpmessage():
parser = argparse.ArgumentParser(prog="EXECUTABLE", formatter_class=argparse.RawTextHelpFormatter)
ClassA.add_to_parser(parser, "--option1", "Choose the class you like to use")
helpmessage = parser.format_help()
expectedHelp = """\
usage: EXECUTABLE [-h] [--option1 OPTION1]
optional arguments:
-h, --help show this help message and exit
--option1 OPTION1 Choose the class you like to use. One of the following: `ClassA`: This is the Base-Class, `ClassB`: The is B-B-B-Better, `ClassD`: This is D-D-D-D-delicate, `ClassC`: The C-C-C-C-classical option """
assert helpmessage.strip() == textwrap.dedent(expectedHelp).strip()
def test_correct_class_instantiated():
instances = ClassA.from_string("ClassD", hallo=12)
assert type(instances[0]) == ClassD
assert instances[0]._hallo == 12
def test_correct_class_instantiated_multiple():
instances = ClassA.from_string("ClassD,ClassB", hallo=12)
assert type(instances[0]) == ClassD
assert type(instances[1]) == ClassB
def test_nested_arguments():
instances = AnotherClass.from_string("AnotherClass[aaa[b],bbb],AnotherSubClass[cd[e[f,g,h],i]bla,gh[i],jkl]")
assert instances[0].arg1 == "aaa[b]"
assert instances[0].arg2 == "bbb"
assert instances[1].arg1 == "cd[e[f,g,h],i]bla"
assert instances[1].arg2 == "gh[i]"
assert instances[1].arg3 == "jkl"
def test_split_by_outerlevel_character():
assert resplit("a,[b,v[f],de[e,t[f,]]],www,2e2[323,2]", ",")==["a","[b,v[f],de[e,t[f,]]]", "www", "2e2[323,2]"]
assert resplit("ab,cd[3,e.]..e,,f", ",.") == ["ab","cd[3,e.]","e","f"]
assert resplit("ab[p,k]") == ["ab[p,k]"]
def test_call_error_reporting():
def aFunnyFunction(arg1, arg2, arg3):
pass
try:
commandline_parsable.call(aFunnyFunction, 1,2)
except TypeError as e:
print(e)
assert "Tried to call" in str(e)
assert "aFunnyFunction" in str(e)
else:
assert False
def test_call_error_reporting2():
def aFunnyFunction(arg1, arg2, arg3):
pass
def bFunction(a, c, d=12):
aFunnyFunction(a)
try:
commandline_parsable.call(bFunction, 1,2)
except TypeError as e:
print(e)
assert "Tried to call" not in str(e)
assert "bFunction" not in str(e)
else:
assert False