Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion launch/launch/utilities/type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,12 @@ def convert_as_yaml(value, error_msg):
else:
raise

if type_obj is not bool:
if type_obj is bool: # For bool coercion, allow '1' and '0' to be truthy and falsy
if value == '1':
value = 'true'
elif value == '0':
value = 'false'
else:
raise ValueError(
'data_type is invalid. Expected one of: '
'int, float, str, bool, List[int], List[float], List[str], List[bool]'
Expand Down
34 changes: 34 additions & 0 deletions launch/test/launch/substitutions/test_boolean_substitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def test_not_substitution():
lc = LaunchContext()
assert NotSubstitution('true').perform(lc) == 'false'
assert NotSubstitution('false').perform(lc) == 'true'
assert NotSubstitution('1').perform(lc) == 'false'
assert NotSubstitution('0').perform(lc) == 'true'
with pytest.raises(SubstitutionFailure):
NotSubstitution('not-condition-expression').perform(lc)

Expand All @@ -38,6 +40,22 @@ def test_and_substitution():
assert AndSubstitution('true', 'false').perform(lc) == 'false'
assert AndSubstitution('false', 'true').perform(lc) == 'false'
assert AndSubstitution('false', 'false').perform(lc) == 'false'

assert AndSubstitution('true', '1').perform(lc) == 'true'
assert AndSubstitution('true', '0').perform(lc) == 'false'
assert AndSubstitution('false', '1').perform(lc) == 'false'
assert AndSubstitution('false', '0').perform(lc) == 'false'

assert AndSubstitution('1', 'true').perform(lc) == 'true'
assert AndSubstitution('1', 'false').perform(lc) == 'false'
assert AndSubstitution('0', 'true').perform(lc) == 'false'
assert AndSubstitution('0', 'false').perform(lc) == 'false'

assert AndSubstitution('1', '1').perform(lc) == 'true'
assert AndSubstitution('1', '0').perform(lc) == 'false'
assert AndSubstitution('0', '1').perform(lc) == 'false'
assert AndSubstitution('0', '0').perform(lc) == 'false'

with pytest.raises(SubstitutionFailure):
AndSubstitution('not-condition-expression', 'true').perform(lc)
with pytest.raises(SubstitutionFailure):
Expand All @@ -50,6 +68,22 @@ def test_or_substitution():
assert OrSubstitution('true', 'false').perform(lc) == 'true'
assert OrSubstitution('false', 'true').perform(lc) == 'true'
assert OrSubstitution('false', 'false').perform(lc) == 'false'

assert OrSubstitution('true', '1').perform(lc) == 'true'
assert OrSubstitution('true', '0').perform(lc) == 'true'
assert OrSubstitution('false', '1').perform(lc) == 'true'
assert OrSubstitution('false', '0').perform(lc) == 'false'

assert OrSubstitution('1', 'true').perform(lc) == 'true'
assert OrSubstitution('1', 'false').perform(lc) == 'true'
assert OrSubstitution('0', 'true').perform(lc) == 'true'
assert OrSubstitution('0', 'false').perform(lc) == 'false'

assert OrSubstitution('1', '1').perform(lc) == 'true'
assert OrSubstitution('1', '0').perform(lc) == 'true'
assert OrSubstitution('0', '1').perform(lc) == 'true'
assert OrSubstitution('0', '0').perform(lc) == 'false'

with pytest.raises(SubstitutionFailure):
OrSubstitution('not-condition-expression', 'true').perform(lc)
with pytest.raises(SubstitutionFailure):
Expand Down
4 changes: 2 additions & 2 deletions launch/test/launch/utilities/test_type_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ def test_coercions_given_specific_type(coerce_to_type_impl):
assert coerce_to_type_impl('on', data_type=bool) is True
assert coerce_to_type_impl('off', data_type=bool) is False
assert coerce_to_type_impl('True', data_type=bool) is True
assert coerce_to_type_impl('1', data_type=bool) is True
assert coerce_to_type_impl('0', data_type=bool) is False

assert coerce_to_type_impl('[.2, .1, .1]', data_type=List[float]) == [.2, .1, .1]
assert coerce_to_type_impl('[asd, bsd, csd]', data_type=List[str]) == ['asd', 'bsd', 'csd']
Expand Down Expand Up @@ -283,8 +285,6 @@ def test_coercion_raises_value_error(coerce_to_type_impl):
coerce_to_type_impl('', data_type=bool)
with pytest.raises(ValueError):
coerce_to_type_impl('Bsd', data_type=bool)
with pytest.raises(ValueError):
coerce_to_type_impl('1', data_type=bool)

with pytest.raises(ValueError):
coerce_to_type_impl('', data_type=List[float])
Expand Down