Skip to content

Support inline flags for configuring custom embedded argument patterns #5242

Closed

Description

@pekkaklarck

At the moment we explicitly disallow using any kind of regular expression extensions using format (?...), but it would be a good idea to support iniline flags. For more details about regexp extensions and inline flags see the regular expression syntax.

Probably the only flag that's actually relevant for us is case-insensitivity i.e. (?i), but we can easily accept them all. Why case-insensitivity is important is illustrate by the following example.

Assume we have the following keyword that allows selecting an animal so that animals are limited to cat, dog and cow.

from robot.api.deco import keyword


@keyword('Select ${animal:cat|dog|cow}')
def select(animal):
    print(animal)

Let's then have the following tests using this keywod.

*** Test Cases ***
Works fine
    Select dog

Works but causes deprecation warning
    Select Dog

As the test case names already illustrate, the first one using one of the listed animals exactly works fine. The second test where one of the animals is used with a different case also passes, but there's the following deprecation warning:

Embedded argument 'animal' got value 'Dog' that does not match custom pattern 'cat|dog|cow'. The argument is still accepted, but this behavior will change in Robot Framework 8.0.

The deprecation warning is caused by #4069. It was added because we want to start validating that arguments match also when they are given as variables, and it hadn't occurred to me earlier that there's a similar situation also if there's a case-insensitive match. The reason there's the warning in this particular case is that:

  • The initial keyword matching is case-insensitive and thus both dog and Dog match. This check is done before variables are resolved.
  • After variables are resolved, there's a separate check that arguments match custom patterns. In this check Dog doesn't match anymore.

I think custom pattern matching being case-sensitive is fine, but there should be an option to make explicitly make it case-insensitive. A natural syntax with regexps is using inline flags as in the example below, but at the moment that doesn't work.

@keyword('Select ${animal:(?i)cat|dog|cow}')
def select(animal):
    print(animal)

I believe it's important to do this before RF 8.0 where #4069 is planned to be implemented.

Activity

  1. added this to the v7.2 milestone on Oct 18, 2024
  2. self-assigned this
    on Oct 18, 2024
  3. pekkaklarck commented on Oct 18, 2024

    @pekkaklarck
    MemberAuthor

    This issue is a spinoff from #5231 that contains a generic keyword that is supposed to match multiple keywords and that could be created like this:

    @keyword('${name:Keyword 1|Keyword 2}')
    def call(name, *args):
        print('Keyword', name, 'called with', args)

    and used like

    *** Test Cases ***
    Example
        Keyword 1
        Keyword 2    some    args

    The above works, but it requires the used keyword name to be example Keyword 1 or Keyword 2. Using it case-insensitively like keyword 1 causes the same deprecation warning as in the description. Prefixing the pattern with the (?i) flag would be a natural way to make the match case-insensitive.

  4. added a commit that references this issue on Oct 18, 2024
    f6af9a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions