Skip to content

Fixes #5054: Enhance Should Contains for bytes - #5097

Merged
pekkaklarck merged 2 commits into
robotframework:masterfrom
droeland:nez
Aug 26, 2024
Merged

pekkaklarck merged 2 commits into
robotframework:masterfrom
droeland:nez

Conversation

@droeland

@droeland droeland commented Mar 29, 2024 •

Copy link
Copy Markdown
Contributor

Implemented as described in the issue.
Fixes #5054.

@droeland droeland changed the title Implementation for issue 5054: Enhance Should Contains for bytes Fixes #5054 Mar 29, 2024
@droeland droeland changed the title Fixes #5054 Fixes #5054: Enhance Should Contains for bytes Mar 30, 2024

@pekkaklarck pekkaklarck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good. See the inline comments for things that could be still enhanced. At least the documentation change needs to be done, other's are just "nice to have".

@@ -1042,7 +1042,7 @@ def should_contain(self, container, item, msg=None, values=True,
ignore_case=False, strip_spaces=False, collapse_spaces=False):
"""Fails if ``container`` does not contain ``item`` one or more times.

Works with strings, lists, and anything that supports Python's ``in``
Works with strings, lists, bytes, and anything that supports Python's ``in``

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be explained somewhere in the docs that the item is converted from sting to bytes automatically f the container is bytes. There should also be a note that this functionality is new in RF 7.1.

[Template] Should Contain
${BYTES} ${1000}
${BYTES} ${UNICODE_NON_LATIN}
${BYTES} \xA0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • It doesn't really matter, but it would be kind of better to have passing examples first.
  • It would probably be a good idea to add a passing example where the item that's search for is an integer like ${1}.
  • An example where the item itself is bytes would be good too. Could use the inline Python evaluation syntax like ${{b'\x02\xa0'}}.
  • An example that fails so that there's no conversion error would be good too.
  • Not sure does it make sense to have all these examples in a single test. There could, for example, be one test where the item is bytes, another where it's an integer, and one where it's string. You can decide.

try:
item = item.encode('latin-1')
except UnicodeEncodeError:
raise ValueError(f"{item} can not be encoded into bytes.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You added a test where the item is 1000 and it fails for a ValueError. Should we actually validate that, if the item is an integer, it is in range 0-255 and provide a nicer error than the current byte must be in range(0, 256) that Python gives us? That would also avoid the issue with the error possibly changing in some future Python version.

@pekkaklarck

Copy link
Copy Markdown
Member

This PR hasn't been updated, but I decided to merge it anyway. It is a good enough base and I can do required changes myself.

@pekkaklarck
pekkaklarck merged commit f5e7a01 into robotframework:master Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better support for bytes with Should Contain

3 participants