From 5cf62322294f2ff5fd92996115030823eadc0e49 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Fri, 16 Aug 2019 11:43:00 -0400 Subject: [PATCH] bt2: add `StringValue.__contains__()` method This is useful to use the `in` Python operator like we do with regular Python `str` objects: if 'my_env_var' in env_string_value: ... Signed-off-by: Francis Deslauriers Change-Id: I8f7f5c02d71bc089e14c3b8f2ffa81045a9e8f15 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1954 Reviewed-by: Philippe Proulx Tested-by: jenkins --- src/bindings/python/bt2/bt2/value.py | 3 +++ tests/bindings/python/bt2/test_value.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/src/bindings/python/bt2/bt2/value.py b/src/bindings/python/bt2/bt2/value.py index 8c8ef60c..3acabe79 100644 --- a/src/bindings/python/bt2/bt2/value.py +++ b/src/bindings/python/bt2/bt2/value.py @@ -404,6 +404,9 @@ class StringValue(collections.abc.Sequence, _Value): def __len__(self): return len(self._value) + def __contains__(self, item): + return self._value_to_str(item) in self._value + def __iadd__(self, value): curvalue = self._value curvalue += self._value_to_str(value) diff --git a/tests/bindings/python/bt2/test_value.py b/tests/bindings/python/bt2/test_value.py index d5051f90..c73e397d 100644 --- a/tests/bindings/python/bt2/test_value.py +++ b/tests/bindings/python/bt2/test_value.py @@ -1535,6 +1535,15 @@ class StringValueTestCase(_TestCopySimple, unittest.TestCase): s1 = bt2.StringValue('allo') self.assertGreaterEqual('bateau', s1) + def test_in_string(self): + s1 = bt2.StringValue('beau grand bateau') + self.assertIn('bateau', s1) + + def test_in_vstring(self): + s1 = bt2.StringValue('beau grand bateau') + s2 = bt2.StringValue('bateau') + self.assertIn(s2, s1) + def test_bool_op(self): self.assertEqual(bool(self._def), bool(self._def_value)) -- 2.34.1