-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtest_choice.py
172 lines (121 loc) · 4.27 KB
/
test_choice.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from dataclasses import dataclass, field
from enum import Enum
from typing import Union
import pytest
from simple_parsing import choice
from .testutils import TestSetup, raises
@dataclass
class A(TestSetup):
color: str = choice("red", "green", "blue", default="red")
colors: list[str] = choice("red", "green", "blue", default_factory=["red"].copy)
def test_choice_default():
a = A.setup("")
assert a.color == "red"
assert a.colors == ["red"]
def test_value_not_in_choices_throws_error():
with raises(SystemExit):
A.setup("--color orange")
with raises(SystemExit):
A.setup("--colors red orange")
def test_passed_value_works_fine():
a = A.setup("--color red")
assert a.color == "red"
a = A.setup("--color green")
assert a.color == "green"
a = A.setup("--color blue")
assert a.color == "blue"
a = A.setup("--colors red blue")
assert a.colors == ["red", "blue"]
a = A.setup("--colors blue red green red")
assert a.colors == ["blue", "red", "green", "red"]
@dataclass
class Base:
value: str = "hello base"
@dataclass
class AA(Base):
value: str = "hello a"
@dataclass
class BB(Base):
value: str = "hello b"
def test_choice_with_dict():
@dataclass
class C(TestSetup):
option: Union[AA, BB, float] = choice(
{"a": AA("aaa"), "b": BB("bbb"), "bob": AA("bobobo"), "f": 1.23},
default="a",
)
options: list[Union[AA, BB, float]] = choice(
{"a": AA("aaa"), "b": BB("bbb"), "bob": AA("bobobo"), "f": 1.23},
default_factory=["a"].copy,
)
c = C.setup("")
assert c.option == AA("aaa")
assert c.options == [AA("aaa")]
c = C.setup("--option a --options a a")
assert c.option == AA("aaa")
assert c.options == [AA("aaa"), AA("aaa")]
c = C.setup("--option bob --options bob a")
assert c.option == AA("bobobo")
assert c.options == [AA("bobobo"), AA("aaa")]
c = C.setup("--option f --options a f a b")
assert c.option == 1.23
assert c.options == [AA("aaa"), 1.23, AA("aaa"), BB("bbb")]
def test_choice_with_default_instance():
@dataclass
class D(TestSetup):
option: Union[AA, BB, float] = choice(
{
"a": [AA("aa1"), AA("aa2")],
"b": 1.23,
"bob": BB("bobobo"),
},
default="a",
)
@dataclass
class Parent(TestSetup):
d: D = field(default_factory=lambda: D(option=AA("parent")))
p = Parent.setup("")
assert p.d.option == AA("parent")
class Color(Enum):
blue: str = "BLUE"
red: str = "RED"
green: str = "GREEN"
orange: str = "ORANGE"
def test_passing_enum_to_choice():
@dataclass
class Something(TestSetup):
favorite_color: Color = choice(Color, default=Color.green)
colors: list[Color] = choice(Color, default_factory=[Color.green].copy)
s = Something.setup("")
assert s.favorite_color == Color.green
assert s.colors == [Color.green]
s = Something.setup("--colors blue red")
assert s.colors == [Color.blue, Color.red]
# TODO: Add a test to check how the enum arguments are shown on the help string.
def test_passing_enum_to_choice_no_default_makes_required_arg():
@dataclass
class Something(TestSetup):
favorite_color: Color = choice(Color)
with raises(SystemExit):
s = Something.setup("")
s = Something.setup("--favorite_color blue")
assert s.favorite_color == Color.blue
def test_passing_enum_to_choice_with_key_as_default():
with pytest.warns(UserWarning):
@dataclass
class Something(TestSetup):
favorite_color: Color = choice(Color, default="blue")
def test_passing_enum_to_choice_is_same_as_enum_attr():
@dataclass
class Something1(TestSetup):
favorite_color: Color = Color.orange
@dataclass
class Something2(TestSetup):
favorite_color: Color = choice(Color, default=Color.orange)
s1 = Something1.setup("--favorite_color green")
s2 = Something2.setup("--favorite_color green")
assert s1.favorite_color == s2.favorite_color
s = Something1.setup("--favorite_color blue")
assert s.favorite_color == Color.blue
s = Something2.setup("--favorite_color blue")
assert s.favorite_color == Color.blue