| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
|
| 3 |
from __future__ import absolute_import |
| 4 |
from behave.userdata import parse_user_define, UserData, UserDataNamespace |
| 5 |
from behave._types import Unknown |
| 6 |
import pytest |
| 7 |
|
| 8 |
|
| 9 |
class TestParseUserDefine(object): |
| 10 |
"""Test parse_user_define() function.""" |
| 11 |
|
| 12 |
def test_parse__name_value(self): |
| 13 |
parts = parse_user_define("person=Alice") |
| 14 |
assert parts == ("person", "Alice") |
| 15 |
|
| 16 |
def test_parse__name_only_for_boolean_flag(self): |
| 17 |
parts = parse_user_define("boolean_flag") |
| 18 |
assert parts == ("boolean_flag", "true") |
| 19 |
|
| 20 |
|
| 21 |
@pytest.mark.parametrize("text", [ |
| 22 |
" person=Alice", |
| 23 |
"person=Alice ", |
| 24 |
"person = Alice", |
| 25 |
]) |
| 26 |
def test_parse__name_value_with_padded_whitespace(self, text): |
| 27 |
parts = parse_user_define(text) |
| 28 |
assert parts == ("person", "Alice") |
| 29 |
|
| 30 |
|
| 31 |
@pytest.mark.parametrize("text", [ |
| 32 |
'"person=Alice and Bob"', |
| 33 |
"'person=Alice and Bob'", |
| 34 |
]) |
| 35 |
def test_parse__name_value_with_quoted_name_value_pair(self, text): |
| 36 |
parts = parse_user_define(text) |
| 37 |
assert parts == ("person", "Alice and Bob") |
| 38 |
|
| 39 |
@pytest.mark.parametrize("text", [ |
| 40 |
'person="Alice and Bob"', |
| 41 |
"person='Alice and Bob'", |
| 42 |
]) |
| 43 |
def test_parse__name_value_with_quoted_value(self, text): |
| 44 |
parts = parse_user_define(text) |
| 45 |
assert parts == ("person", "Alice and Bob") |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class TestUserData(object): |
| 51 |
"""Test UserData class.""" |
| 52 |
|
| 53 |
def test_userdata_is_dictlike(self): |
| 54 |
userdata = UserData(name="Foo", number=42) |
| 55 |
value1 = userdata["name"] |
| 56 |
value2 = userdata.get("number") |
| 57 |
value3 = userdata.get("unknown", Unknown) |
| 58 |
assert isinstance(userdata, dict) |
| 59 |
assert value1 == "Foo" |
| 60 |
assert value2 == 42 |
| 61 |
assert value3 is Unknown |
| 62 |
|
| 63 |
def test_getas__with_known_param_and_valid_text(self): |
| 64 |
userdata = UserData(param="42") |
| 65 |
assert "param" in userdata, "ENSURE: known param" |
| 66 |
|
| 67 |
value = userdata.getas(int, "param") |
| 68 |
assert isinstance(value, int) |
| 69 |
assert value == 42 |
| 70 |
|
| 71 |
def test_getas__with_known_param_and_invalid_text_raises_ValueError(self): |
| 72 |
userdata = UserData(param="__BAD_NUMBER__") |
| 73 |
assert "param" in userdata, "ENSURE: known param" |
| 74 |
with pytest.raises(ValueError): |
| 75 |
userdata.getas(int, "param") |
| 76 |
|
| 77 |
def test_getas__with_known_param_and_preconverted_value(self): |
| 78 |
userdata = UserData(param=42) |
| 79 |
assert "param" in userdata, "ENSURE: known param" |
| 80 |
|
| 81 |
value = userdata.getas(int, "param") |
| 82 |
assert isinstance(value, int) |
| 83 |
assert value == 42 |
| 84 |
|
| 85 |
def test_getas__with_known_param_and_preconverted_value_and_valuetype(self): |
| 86 |
userdata = UserData(param=42) |
| 87 |
assert "param" in userdata, "ENSURE: known param" |
| 88 |
|
| 89 |
def parse_int(text): |
| 90 |
return int(text) |
| 91 |
|
| 92 |
value = userdata.getas(parse_int, "param", valuetype=int) |
| 93 |
assert isinstance(value, int) |
| 94 |
assert value == 42 |
| 95 |
|
| 96 |
def test_getas__with_unknown_param_without_default_returns_none(self): |
| 97 |
userdata = UserData() |
| 98 |
assert "param" not in userdata, "ENSURE: unknown param" |
| 99 |
|
| 100 |
value = userdata.getas(int, "param") |
| 101 |
assert value is None |
| 102 |
|
| 103 |
def test_getas__with_unknown_param_returns_default_value(self): |
| 104 |
userdata = UserData() |
| 105 |
assert "param" not in userdata, "ENSURE: unknown param" |
| 106 |
|
| 107 |
value = userdata.getas(int, "param", 123) |
| 108 |
assert isinstance(value, int) |
| 109 |
assert value == 123 |
| 110 |
|
| 111 |
|
| 112 |
def test_getint__with_known_param_and_valid_text(self): |
| 113 |
userdata = UserData(param="42") |
| 114 |
value = userdata.getint("param") |
| 115 |
assert isinstance(value, int) |
| 116 |
assert value == 42 |
| 117 |
|
| 118 |
def test_getint__with_known_param_and_invalid_text_raises_ValueError(self): |
| 119 |
userdata = UserData(param="__BAD_NUMBER__") |
| 120 |
with pytest.raises(ValueError): |
| 121 |
userdata.getint("param") |
| 122 |
|
| 123 |
def test_getint__with_unknown_param_without_default_returns_zero(self): |
| 124 |
userdata = UserData() |
| 125 |
value = userdata.getint("param") |
| 126 |
assert value == 0 |
| 127 |
|
| 128 |
def test_getint__with_unknown_param_returns_default_value(self): |
| 129 |
userdata = UserData() |
| 130 |
value = userdata.getint("param", 123) |
| 131 |
assert isinstance(value, int) |
| 132 |
assert value == 123 |
| 133 |
|
| 134 |
def test_getfloat__with_known_param_and_valid_text(self): |
| 135 |
for valid_text in ["1.2", "2", "-1E+3", "+2.34E-5"]: |
| 136 |
userdata = UserData(param=valid_text) |
| 137 |
value = userdata.getfloat("param") |
| 138 |
assert isinstance(value, float) |
| 139 |
assert value == float(valid_text) |
| 140 |
|
| 141 |
def test_getfloat__with_known_param_and_invalid_text_raises_ValueError(self): |
| 142 |
userdata = UserData(param="__BAD_NUMBER__") |
| 143 |
with pytest.raises(ValueError): |
| 144 |
userdata.getfloat("param") |
| 145 |
|
| 146 |
def test_getfloat__with_unknown_param_without_default_returns_zero(self): |
| 147 |
userdata = UserData() |
| 148 |
value = userdata.getfloat("param") |
| 149 |
assert value == 0.0 |
| 150 |
|
| 151 |
def test_getfloat__with_unknown_param_returns_default_value(self): |
| 152 |
userdata = UserData() |
| 153 |
value = userdata.getint("param", 1.2) |
| 154 |
assert isinstance(value, float) |
| 155 |
assert value == 1.2 |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.parametrize("text", [ |
| 159 |
"true", "TRUE", "True", "yes", "on", "1" |
| 160 |
]) |
| 161 |
def test_getbool__with_known_param_and_valid_true_text(self, text): |
| 162 |
true_text = text |
| 163 |
userdata = UserData(param=true_text) |
| 164 |
value = userdata.getbool("param") |
| 165 |
assert isinstance(value, bool), "text=%s" % true_text |
| 166 |
assert value is True |
| 167 |
|
| 168 |
@pytest.mark.parametrize("text", [ |
| 169 |
"false", "FALSE", "False", "no", "off", "0" |
| 170 |
]) |
| 171 |
def test_getbool__with_known_param_and_valid_false_text(self, text): |
| 172 |
false_text = text |
| 173 |
userdata = UserData(param=false_text) |
| 174 |
value = userdata.getbool("param") |
| 175 |
assert isinstance(value, bool), "text=%s" % false_text |
| 176 |
assert value is False |
| 177 |
|
| 178 |
def test_getbool__with_known_param_and_invalid_text_raises_ValueError(self): |
| 179 |
userdata = UserData(param="__BAD_VALUE__") |
| 180 |
with pytest.raises(ValueError): |
| 181 |
userdata.getbool("param") |
| 182 |
|
| 183 |
def test_getbool__with_unknown_param_without_default_returns_false(self): |
| 184 |
userdata = UserData() |
| 185 |
value = userdata.getfloat("param") |
| 186 |
assert value == False |
| 187 |
|
| 188 |
def test_getbool__with_unknown_param_returns_default_value(self): |
| 189 |
userdata = UserData() |
| 190 |
value = userdata.getint("param", 1.2) |
| 191 |
assert isinstance(value, float) |
| 192 |
assert value == 1.2 |
| 193 |
|
| 194 |
|
| 195 |
class TestUserDataNamespace(object): |
| 196 |
|
| 197 |
def test_make_scoped(self): |
| 198 |
scoped_name = UserDataNamespace.make_scoped("my.scope", "param") |
| 199 |
assert scoped_name == "my.scope.param" |
| 200 |
|
| 201 |
def test_make_scoped__with_empty_scope(self): |
| 202 |
scoped_name = UserDataNamespace.make_scoped("", "param") |
| 203 |
assert scoped_name == "param" |
| 204 |
|
| 205 |
def test_ctor__converts_dict_into_userdata(self): |
| 206 |
userdata = {"my.scope.param1": 12} |
| 207 |
config = UserDataNamespace("my.scope", userdata) |
| 208 |
assert isinstance(config.data, UserData) |
| 209 |
assert config.data is not userdata |
| 210 |
assert config.data == userdata |
| 211 |
|
| 212 |
def test_ctor__converts_items_into_userdata(self): |
| 213 |
userdata = {"my.scope.param1": 12} |
| 214 |
config = UserDataNamespace("my.scope", userdata.items()) |
| 215 |
assert isinstance(config.data, UserData) |
| 216 |
assert config.data is not userdata |
| 217 |
assert config.data == userdata |
| 218 |
|
| 219 |
def test_ctor__can_assign_userdata_afterwards(self): |
| 220 |
userdata = UserData({"my.scope.param1": 12}) |
| 221 |
config = UserDataNamespace("my.scope") |
| 222 |
config.data = userdata |
| 223 |
assert isinstance(config.data, UserData) |
| 224 |
assert config.data is userdata |
| 225 |
|
| 226 |
def test_get__retrieves_value_when_scoped_param_exists(self): |
| 227 |
userdata = UserData({"my.scope.param1": 12}) |
| 228 |
config = UserDataNamespace("my.scope", userdata) |
| 229 |
assert config.get("param1") == 12 |
| 230 |
assert config["param1"] == 12 |
| 231 |
|
| 232 |
def test_get__returns_default_when_scoped_param_not_exists(self): |
| 233 |
config = UserDataNamespace("my.scope", UserData({})) |
| 234 |
assert config.get("UNKNOWN_PARAM", "DEFAULT1") == "DEFAULT1" |
| 235 |
assert config.get("UNKNOWN_PARAM", "DEFAULT2") == "DEFAULT2" |
| 236 |
|
| 237 |
def test_getint__retrieves_value_when_scoped_param_exists(self): |
| 238 |
userdata = UserData({"my.scope.param1": "12"}) |
| 239 |
config = UserDataNamespace("my.scope", userdata) |
| 240 |
assert config.getint("param1") == 12 |
| 241 |
|
| 242 |
def test_getint__retrieves_value_when_scoped_param_exists(self): |
| 243 |
userdata = UserData({"my.scope.param2": "12"}) |
| 244 |
config = UserDataNamespace("my.scope", userdata) |
| 245 |
assert config.getint("param2") == 12 |
| 246 |
|
| 247 |
def test_getint__returns_default_when_scoped_param_not_exists(self): |
| 248 |
userdata = UserData({}) |
| 249 |
config = UserDataNamespace("my.scope", userdata) |
| 250 |
assert config.getint("UNKNOWN_PARAM", 123) == 123 |
| 251 |
assert config.getint("UNKNOWN_PARAM", 321) == 321 |
| 252 |
|
| 253 |
def test_getbool__retrieves_value_when_scoped_param_exists(self): |
| 254 |
userdata = UserData({"my.scope.param3": "yes"}) |
| 255 |
config = UserDataNamespace("my.scope", userdata) |
| 256 |
assert config.getbool("param3") == True |
| 257 |
|
| 258 |
def test_getbool__returns_default_when_scoped_param_not_exists(self): |
| 259 |
userdata = UserData({}) |
| 260 |
config = UserDataNamespace("my.scope", userdata) |
| 261 |
assert config.getint("UNKNOWN_PARAM", True) == True |
| 262 |
assert config.getint("UNKNOWN_PARAM", False) == False |
| 263 |
|
| 264 |
def test_contains__when_scoped_param_exists(self): |
| 265 |
userdata = UserData({"my.scope.param": 12}) |
| 266 |
config = UserDataNamespace("my.scope", userdata) |
| 267 |
assert "param" in config |
| 268 |
assert not("param" not in config) |
| 269 |
|
| 270 |
def test_contains__when_scoped_param_not_exists(self): |
| 271 |
userdata = UserData({"my.scope.param": 12}) |
| 272 |
config = UserDataNamespace("my.scope", userdata) |
| 273 |
assert "UNKNOWN_PARAM" not in config |
| 274 |
assert not ("UNKNOWN_PARAM" in config) |
| 275 |
|
| 276 |
def test_getitem__returns_value_when_param_exists(self): |
| 277 |
userdata = UserData({"my.scope.param": "123"}) |
| 278 |
config = UserDataNamespace("my.scope", userdata) |
| 279 |
assert config["param"] == "123" |
| 280 |
|
| 281 |
def test_getitem__raises_error_when_param_not_exists(self): |
| 282 |
userdata = UserData({"my.scope.param": "123"}) |
| 283 |
config = UserDataNamespace("my.scope", userdata) |
| 284 |
with pytest.raises(KeyError): |
| 285 |
_ = config["UNKNOWN_PARAM"] |
| 286 |
|
| 287 |
def test_setitem__stores_value(self): |
| 288 |
userdata = UserData({"my.scope.param1": "123"}) |
| 289 |
config = UserDataNamespace("my.scope", userdata) |
| 290 |
scoped_name = "my.scope.new_param" |
| 291 |
config["new_param"] = 1234 |
| 292 |
assert "new_param" in config |
| 293 |
assert config["new_param"] == 1234 |
| 294 |
assert scoped_name in config.data |
| 295 |
assert config.data[scoped_name] == 1234 |
| 296 |
|
| 297 |
def test_length__returns_zero_without_params(self): |
| 298 |
userdata = UserData({"my.other_scope.param1": "123"}) |
| 299 |
config = UserDataNamespace("my.scope", userdata) |
| 300 |
assert len(config) == 0 |
| 301 |
|
| 302 |
def test_length__with_scoped_params(self): |
| 303 |
userdata1 = UserData({"my.scope.param1": "123"}) |
| 304 |
userdata2 = UserData({ |
| 305 |
"my.other_scope.param1": "123", |
| 306 |
"my.scope.param1": "123", |
| 307 |
"my.scope.param2": "456", |
| 308 |
}) |
| 309 |
userdata3 = UserData({ |
| 310 |
"my.other_scope.param1": "123", |
| 311 |
"my.scope.param1": "123", |
| 312 |
"my.scope.param2": "456", |
| 313 |
"my.scope.param3": "789", |
| 314 |
"my.other_scope.param2": "123", |
| 315 |
}) |
| 316 |
config = UserDataNamespace("my.scope") |
| 317 |
config.data = userdata1 |
| 318 |
assert len(config) == 1 |
| 319 |
config.data = userdata2 |
| 320 |
assert len(config) == 2 |
| 321 |
config.data = userdata3 |
| 322 |
assert len(config) == 3 |
| 323 |
|
| 324 |
def test_scoped_keys__with_scoped_params(self): |
| 325 |
userdata = UserData({ |
| 326 |
"my.other_scope.param1": "123", |
| 327 |
"my.scope.param1": "123", |
| 328 |
"my.scope.param2": "456", |
| 329 |
"my.other_scope.param2": "123", |
| 330 |
}) |
| 331 |
config = UserDataNamespace("my.scope", userdata) |
| 332 |
assert sorted(config.scoped_keys()) == ["my.scope.param1", "my.scope.param2"] |
| 333 |
|
| 334 |
def test_keys__with_scoped_params(self): |
| 335 |
userdata = UserData({ |
| 336 |
"my.other_scope.param1": "__OTHER1__", |
| 337 |
"my.scope.param1": "123", |
| 338 |
"my.scope.param2": "456", |
| 339 |
"my.other_scope.param2": "__OTHER2__", |
| 340 |
}) |
| 341 |
config = UserDataNamespace("my.scope", userdata) |
| 342 |
assert sorted(config.keys()) == ["param1", "param2"] |
| 343 |
|
| 344 |
def test_values__with_scoped_params(self): |
| 345 |
userdata = UserData({ |
| 346 |
"my.other_scope.param1": "__OTHER1__", |
| 347 |
"my.scope.param1": "123", |
| 348 |
"my.scope.param2": "456", |
| 349 |
"my.other_scope.param2": "__OTHER2__", |
| 350 |
}) |
| 351 |
config = UserDataNamespace("my.scope", userdata) |
| 352 |
assert sorted(config.values()) == ["123", "456"] |
| 353 |
|
| 354 |
def test_items__with_scoped_params(self): |
| 355 |
userdata = UserData({ |
| 356 |
"my.other_scope.param1": "__OTHER1__", |
| 357 |
"my.scope.param1": "123", |
| 358 |
"my.scope.param2": "456", |
| 359 |
"my.other_scope.param2": "__OTHER2__", |
| 360 |
}) |
| 361 |
config = UserDataNamespace("my.scope", userdata) |
| 362 |
assert sorted(config.items()) == [("param1", "123"), ("param2", "456")] |