diff --git a/launch/launch/utilities/type_utils.py b/launch/launch/utilities/type_utils.py index 17e1a7341..9fd795e3a 100644 --- a/launch/launch/utilities/type_utils.py +++ b/launch/launch/utilities/type_utils.py @@ -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]' diff --git a/launch/test/launch/substitutions/test_boolean_substitution.py b/launch/test/launch/substitutions/test_boolean_substitution.py index edd39cfb2..86725ffc1 100644 --- a/launch/test/launch/substitutions/test_boolean_substitution.py +++ b/launch/test/launch/substitutions/test_boolean_substitution.py @@ -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) @@ -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): @@ -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): diff --git a/launch/test/launch/utilities/test_type_utils.py b/launch/test/launch/utilities/test_type_utils.py index 788d9b070..1e7f9cafe 100644 --- a/launch/test/launch/utilities/test_type_utils.py +++ b/launch/test/launch/utilities/test_type_utils.py @@ -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'] @@ -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])