Skip to content

Better support for bytes with Should Contain #5054

Description

@WisniewskiP

To use Should Contains with bytes, item that is searched needs to be first converted to bytes.

Test
    ${binaryVar} =    Convert To Bytes    \x00\x01\x02\xA0\xB0
    Should Contain    ${binary1Var}    \xA0

result:

TypeError: a bytes-like object is required, not 'str'

for int the value is converted automatically:

Test Should Contain int
    ${binary1Var} =    Convert To Bytes    \x00\x01\x02\xA0\xB0
    ${item} =    Convert To Bytes    \xC0

    Should Contain    ${binary1Var}    ${1000}

this fails with:

ValueError: byte must be in range(0, 256)

this is expected.

Activity

  1. changed the title [-]Enhence Should Contains for bytes[/-] [+]Enhance Should Contains for bytes[/+] on Feb 14, 2024
  2. added this to the v7.1 milestone on Mar 26, 2024
  3. pekkaklarck commented on Mar 26, 2024

    @pekkaklarck
    Member

    We could easily enhance Should Contain so that if the container (i.e. the first argument) is bytes and the item to check (i.e. the second argument) is a string, we convert the latter to bytes. In practice it would just require something like below (plus tests and docs):

    if isinstance(container, bytes) and isinstance(item, str):
        try:
            item = item.encode('latin-1')
        except UnicodeEncodeError:
            raise ValueError(...)
  4. droeland commented on Mar 29, 2024

    @droeland
    Contributor

    Can I take this one up?

  5. pekkaklarck commented on May 29, 2024

    @pekkaklarck
    Member

    Thanks for the PR @droeland. It looks pretty good, but there are some smallish things that could be still enhanced. See the PR review for more details.

  6. pekkaklarck commented on Jul 3, 2024

    @pekkaklarck
    Member

    Are you @droeland planning to fix the small issues I mentioned in the PR review? If you don't have time, I can do that myself as well. I'm now more actively working with RF 7.1 and would like to get this issue closed.

  7. added a commit that references this issue on Aug 8, 2024
    842a387
  8. Abhilash-du commented on Aug 8, 2024

    @Abhilash-du

    Hi, I have created a pull request that addresses this issue by enhancing the should_contain keyword to handle bytes and improve error handling. The changes include:

    • Automatically encoding strings to bytes using 'latin-1' when the container is a bytes object.
    • Checking if an integer item is within the range 0-255 before converting it to bytes.
    • Raising a ValueError with a clear message if the integer is out of range.
    • Adding tests to validate the new functionality.

    You can view and review the pull request here: #5176

    Thank you!

  9. pekkaklarck commented on Aug 26, 2024

    @pekkaklarck
    Member

    Thanks for the PR @Abhilash-du. Similarly as the earlier #5097 it has some issues that would need to be fixed. As I explained in the comments of your PR, I want this issue closed to get RF 7.1 out soon and I needed to decide which PR to use as the base. I decided to go with the earlier one simply because I had reviewed it in detail earlier and making required changes to it is thus very fast for me. Hopefully you are interested to contribute in the future anyway!

  10. added
    acknowledge
    on Aug 26, 2024
  11. changed the title [-]Enhance Should Contains for bytes[/-] [+]Better support for bytes with `Should Contain`[/+] on Aug 26, 2024
  12. pekkaklarck commented on Aug 26, 2024

    @pekkaklarck
    Member

    This issue is now done. PR #5097 by @droeland was used as the base and I then made some enhancements (incl. support for bytearray in addition to bytes) in 409cff4.

  13. added a commit that references this issue on Aug 27, 2024
    f5e7a01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledgeeffort: smallgood first issuepriority: mediumrc 1

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions