Skip to content

Commit 33857d6

Browse files
committed
Added robot.utils.is_noney.
Also added docs and tests for is_truthy and is_falsy. Fixes robotframework#2646.
1 parent 87a2ce2 commit 33857d6

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

src/robot/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
secs_to_timestr, timestamp_to_secs, timestr_to_secs,
6666
parse_time)
6767
from .robottypes import (is_bytes, is_dict_like, is_falsy, is_integer,
68-
is_list_like, is_number, is_string, is_truthy,
69-
is_unicode, type_name)
68+
is_list_like, is_noney, is_number, is_string,
69+
is_truthy, is_unicode, type_name)
7070
from .setter import setter, SetterAwareType
7171
from .sortable import Sortable
7272
from .text import (cut_long_message, format_assign_message,

src/robot/utils/robottypes.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,39 @@
2626

2727

2828
def is_truthy(item):
29+
"""Returns `True` or `False` depending is the item considered true or not.
30+
31+
Validation rules:
32+
33+
- If the value is a string, it is considered `True` if it is not `FALSE`,
34+
`NO` or and empty string, case-insensitively.
35+
- Other values are handled by using the standard `bool()` function.
36+
37+
Designed to be used also by external test libraries that want to handle
38+
Boolean values similarly as Robot Framework itself. See also
39+
:func:`is_falsy` and :func:`is_noney`.
40+
"""
2941
if is_string(item):
3042
return item.upper() not in ('FALSE', 'NO', '')
3143
return bool(item)
3244

3345

3446
def is_falsy(item):
47+
"""Opposite of :func:`is_truthy`."""
3548
return not is_truthy(item)
49+
50+
51+
def is_noney(item):
52+
"""Returns `True` or `False` depending is the item considered `None` or not.
53+
54+
Validation rules:
55+
56+
- If the value is a string, it is considered `None` if it is equal to
57+
`NONE`, case-insensitively.
58+
- Otherwise the value is considerd `None` only if it is the actual `None`.
59+
60+
Designed to be used also by external test libraries that want to handle
61+
`NONE` similarly as Robot Framework itself. New in Robot Framework 3.0.3.
62+
See also :func:`is_truthy` and :func:`is_falsy`.
63+
"""
64+
return item is None or is_string(item) and item.upper() == 'NONE'

utest/utils/test_robottypes.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
except ImportError:
1717
pass
1818

19-
from robot.utils import (is_bytes, is_dict_like, is_list_like, is_string,
20-
type_name, JYTHON, PY3)
21-
from robot.utils.asserts import assert_equal
19+
from robot.utils import (is_bytes, is_falsy, is_dict_like, is_list_like,
20+
is_noney, is_string, is_truthy, type_name, JYTHON, PY3)
21+
from robot.utils.asserts import assert_equal, assert_true
2222

2323

2424
if PY3:
@@ -156,5 +156,22 @@ def test_java_object(self):
156156
assert_equal(type_name(item), exp)
157157

158158

159+
class TestIsY(unittest.TestCase):
160+
161+
def test_is_truthy_and_is_falsy(self):
162+
for item in [True, 1, [False], unittest.TestCase, 'foo', 'True']:
163+
assert_true(is_truthy(item) is True)
164+
assert_true(is_falsy(item) is False)
165+
for item in [False, 0, [], None, 'False', 'FALSE', 'No', 'nO', '']:
166+
assert_true(is_truthy(item) is False)
167+
assert_true(is_falsy(item) is True)
168+
169+
def test_noney(self):
170+
for item in [None, 'None', 'NONE', 'none']:
171+
assert_true(is_noney(item) is True)
172+
for item in [True, False, 1, '', 'xxx']:
173+
assert_true(is_noney(item) is False)
174+
175+
159176
if __name__ == '__main__':
160177
unittest.main()

0 commit comments

Comments
 (0)