| 1 |
from pytest import skip, raises |
| 2 |
|
| 3 |
from invoke.parser import Argument |
| 4 |
|
| 5 |
|
| 6 |
class Argument_: |
| 7 |
class init: |
| 8 |
"__init__" |
| 9 |
|
| 10 |
def may_take_names_list(self): |
| 11 |
names = ("--foo", "-f") |
| 12 |
a = Argument(names=names) |
| 13 |
# herp a derp |
| 14 |
for name in names: |
| 15 |
assert name in a.names |
| 16 |
|
| 17 |
def may_take_name_arg(self): |
| 18 |
assert "-b" in Argument(name="-b").names |
| 19 |
|
| 20 |
def must_get_at_least_one_name(self): |
| 21 |
with raises(TypeError): |
| 22 |
Argument() |
| 23 |
|
| 24 |
def default_arg_is_name_not_names(self): |
| 25 |
assert "b" in Argument("b").names |
| 26 |
|
| 27 |
def can_declare_positional(self): |
| 28 |
assert Argument(name="foo", positional=True).positional is True |
| 29 |
|
| 30 |
def positional_is_False_by_default(self): |
| 31 |
assert Argument(name="foo").positional is False |
| 32 |
|
| 33 |
def can_set_attr_name_to_control_name_attr(self): |
| 34 |
a = Argument("foo", attr_name="bar") |
| 35 |
assert a.name == "bar" # not 'foo' |
| 36 |
|
| 37 |
class repr: |
| 38 |
"__repr__" |
| 39 |
|
| 40 |
def shows_useful_info(self): |
| 41 |
arg = Argument(names=("name", "nick1", "nick2")) |
| 42 |
expected = "<Argument: {} ({})>".format("name", "nick1, nick2") |
| 43 |
assert repr(arg) == expected |
| 44 |
|
| 45 |
def does_not_show_nickname_parens_if_no_nicknames(self): |
| 46 |
assert repr(Argument("name")) == "<Argument: name>" |
| 47 |
|
| 48 |
def shows_positionalness(self): |
| 49 |
arg = Argument("name", positional=True) |
| 50 |
assert repr(arg) == "<Argument: name *>" |
| 51 |
|
| 52 |
def shows_optionalness(self): |
| 53 |
arg = Argument("name", optional=True) |
| 54 |
assert repr(arg) == "<Argument: name ?>" |
| 55 |
|
| 56 |
def positionalness_and_optionalness_stick_together(self): |
| 57 |
# TODO: but do these even make sense on the same argument? For now, |
| 58 |
# best to have a nonsensical test than a missing one... |
| 59 |
arg = Argument("name", optional=True, positional=True) |
| 60 |
assert repr(arg) == "<Argument: name *?>" |
| 61 |
|
| 62 |
def shows_kind_if_not_str(self): |
| 63 |
assert repr(Argument("age", kind=int)) == "<Argument: age [int]>" |
| 64 |
|
| 65 |
def all_the_things_together(self): |
| 66 |
arg = Argument( |
| 67 |
names=("meh", "m"), kind=int, optional=True, positional=True |
| 68 |
) |
| 69 |
assert repr(arg) == "<Argument: meh (m) [int] *?>" |
| 70 |
|
| 71 |
class kind_kwarg: |
| 72 |
"'kind' kwarg" |
| 73 |
|
| 74 |
def is_optional(self): |
| 75 |
Argument(name="a") |
| 76 |
Argument(name="b", kind=int) |
| 77 |
|
| 78 |
def defaults_to_str(self): |
| 79 |
assert Argument("a").kind == str |
| 80 |
|
| 81 |
def non_bool_implies_value_needed(self): |
| 82 |
assert Argument(name="a", kind=int).takes_value |
| 83 |
assert Argument(name="b", kind=str).takes_value |
| 84 |
assert Argument(name="c", kind=list).takes_value |
| 85 |
|
| 86 |
def bool_implies_no_value_needed(self): |
| 87 |
assert not Argument(name="a", kind=bool).takes_value |
| 88 |
|
| 89 |
def bool_implies_default_False_not_None(self): |
| 90 |
# Right now, parsing a bool flag not given results in None |
| 91 |
# TODO: may want more nuance here -- False when a --no-XXX flag is |
| 92 |
# given, True if --XXX, None if not seen? |
| 93 |
# Only makes sense if we add automatic --no-XXX stuff (think |
| 94 |
# ./configure) |
| 95 |
skip() |
| 96 |
|
| 97 |
def may_validate_on_set(self): |
| 98 |
with raises(ValueError): |
| 99 |
Argument("a", kind=int).value = "five" |
| 100 |
|
| 101 |
def list_implies_initial_value_of_empty_list(self): |
| 102 |
assert Argument("mylist", kind=list).value == [] |
| 103 |
|
| 104 |
class names: |
| 105 |
def returns_tuple_of_all_names(self): |
| 106 |
assert Argument(names=("--foo", "-b")).names == ("--foo", "-b") |
| 107 |
assert Argument(name="--foo").names == ("--foo",) |
| 108 |
|
| 109 |
def is_normalized_to_a_tuple(self): |
| 110 |
assert isinstance(Argument(names=("a", "b")).names, tuple) |
| 111 |
|
| 112 |
class name: |
| 113 |
def returns_first_name(self): |
| 114 |
assert Argument(names=("a", "b")).name == "a" |
| 115 |
|
| 116 |
class nicknames: |
| 117 |
def returns_rest_of_names(self): |
| 118 |
assert Argument(names=("a", "b")).nicknames == ("b",) |
| 119 |
|
| 120 |
class takes_value: |
| 121 |
def True_by_default(self): |
| 122 |
assert Argument(name="a").takes_value |
| 123 |
|
| 124 |
def False_if_kind_is_bool(self): |
| 125 |
assert not Argument(name="-b", kind=bool).takes_value |
| 126 |
|
| 127 |
class value_set: |
| 128 |
"value=" |
| 129 |
|
| 130 |
def available_as_dot_raw_value(self): |
| 131 |
"available as .raw_value" |
| 132 |
a = Argument("a") |
| 133 |
a.value = "foo" |
| 134 |
assert a.raw_value == "foo" |
| 135 |
|
| 136 |
def untransformed_appears_as_dot_value(self): |
| 137 |
"untransformed, appears as .value" |
| 138 |
a = Argument("a", kind=str) |
| 139 |
a.value = "foo" |
| 140 |
assert a.value == "foo" |
| 141 |
|
| 142 |
def transformed_appears_as_dot_value_with_original_as_raw_value(self): |
| 143 |
"transformed, modified value is .value, original is .raw_value" |
| 144 |
a = Argument("a", kind=int) |
| 145 |
a.value = "5" |
| 146 |
assert a.value == 5 |
| 147 |
assert a.raw_value == "5" |
| 148 |
|
| 149 |
def list_kind_triggers_append_instead_of_overwrite(self): |
| 150 |
# TODO: when put this way it makes the API look pretty strange; |
| 151 |
# maybe a sign we should switch to explicit setter methods |
| 152 |
# (selected on kind, perhaps) instead of using an implicit setter |
| 153 |
a = Argument("mylist", kind=list) |
| 154 |
assert a.value == [] |
| 155 |
a.value = "val1" |
| 156 |
assert a.value == ["val1"] |
| 157 |
a.value = "val2" |
| 158 |
assert a.value == ["val1", "val2"] |
| 159 |
|
| 160 |
def incrementable_True_triggers_increment_of_default(self): |
| 161 |
a = Argument("verbose", kind=int, default=0, incrementable=True) |
| 162 |
assert a.value == 0 |
| 163 |
# NOTE: parser currently just goes "Argument.takes_value is false? |
| 164 |
# Gonna stuff True/False in there." So this looks pretty silly out |
| 165 |
# of context (as with list-types above.) |
| 166 |
a.value = True |
| 167 |
assert a.value == 1 |
| 168 |
for _ in range(4): |
| 169 |
a.value = True |
| 170 |
assert a.value == 5 |
| 171 |
|
| 172 |
class value: |
| 173 |
def returns_default_if_not_set(self): |
| 174 |
a = Argument("a", default=25) |
| 175 |
assert a.value == 25 |
| 176 |
|
| 177 |
class raw_value: |
| 178 |
def is_None_when_no_value_was_actually_seen(self): |
| 179 |
a = Argument("a", kind=int) |
| 180 |
assert a.raw_value is None |
| 181 |
|
| 182 |
class got_value: |
| 183 |
def non_list_kind_tests_for_None_value(self): |
| 184 |
arg = Argument("a") |
| 185 |
assert not arg.got_value |
| 186 |
arg.value = "something" |
| 187 |
assert arg.got_value |
| 188 |
|
| 189 |
def list_kind_test_for_empty_list_value(self): |
| 190 |
arg = Argument("a", kind=list) |
| 191 |
assert not arg.got_value |
| 192 |
arg.value = "append-me" |
| 193 |
assert arg.got_value |
| 194 |
|
| 195 |
class set_value: |
| 196 |
def casts_by_default(self): |
| 197 |
a = Argument("a", kind=int) |
| 198 |
a.set_value("5") |
| 199 |
assert a.value == 5 |
| 200 |
|
| 201 |
def allows_setting_value_without_casting(self): |
| 202 |
a = Argument("a", kind=int) |
| 203 |
a.set_value("5", cast=False) |
| 204 |
assert a.value == "5" |