Skip to content

Collections: New keywords for matching list items using glob/regexp patterns optionally case/space sensitively #1724

Closed

Description

@spooning

Originally submitted to Google Code by Marcin.Koperski on 2 Jun 2014

It would nice to have option to find item which containing specific subset of string with option to be cases insensitive or add option to use wild cards in value

List Should Contain Value Containing Value
[ list_ | value | msg=None |caseSensitive=True]

${list} Create List Value1
List Should Contain Value ${list} Value List doesnt have item
List Should Contain Value ${list} value List doesnt have item True

Activity

  1. spooning commented on Jun 30, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 6 Jun 2014

    I would rather enhance the current List Should Contain Value. It could easily get new case_insensitive=False argument to make matching case-insensitive. Specifying should matching be exact or should patterns be used there are two possibilities:

    1. Add separate pattern=False or match=False argument. A problem here is that there could be need for both glob style patterns (foo*) and regexps (foo\d+). We could either add separate match=False and regexp=False arguments or add just single match_style='exact' argument with possible values like exact, match, and regexp.

    2. Add matching style to the value with a prefix like match=foo* or regexp=foo\d+.

    Are you interested in implementing this or sponsoring someone else to get this? Notice that for consistency reasons, same functionality should be added also to most other should keywords.

  2. spooning commented on Jun 30, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by Marcin.Koperski on 6 Jun 2014

    I think first solution is better, since it would more compatible friendly.

    Unfortunately, I don't think I can help with development,but I can help to test this functionality or add some tests.

  3. spooning commented on Jun 30, 2014

    @spooning
    ContributorAuthor

    Originally submitted to Google Code by @pekkaklarck on 25 Jun 2014

    Case-insensitive matching could be also used with keywords like Remote Duplicates, Remove Values From Lists.

  4. guykisel commented on Jul 3, 2014

    @guykisel
    Contributor

    I can implement this. I suggest adding three arguments: case_insensitive (boolean, default=False), glob (string, default=None), and regex (string, default=None)

    If glob or regex have a value, then use glob or regex matching as appropriate. If case_insensitive is True, do exact (but case insensitive) matching. If all three arguments are left at their default value, do exact case sensitive matching.

    Glob and regex matching can be implemented using the glob and re packages from the standard library, and case insensitive matching can be implemented by converting all strings to lowercase. One question is, what should be done with non-string list values? Should they be converted to strings during the search? Should they simply be ignored?

  5. pekkaklarck commented on Jul 3, 2014

    @pekkaklarck
    Member

    @guykisel Great that you are interested to implement this!

    I agree adding case_insensitive=False is a good idea but I'm not sure about adding separate arguments for regexp and glob. Two other alternatives:

    1. Add separate argument like match_type='exact' that can get values exact (default), regexp or glob:
    | List Should Contain Value | ${list} | foo | case_insensitive=yes |
    | List Should Contain Value | ${list} | f*  | case_insensitive=yes | match_type=glob |
    | List Should Contain Value | ${list} | f.* | case_insensitive=yes | match_type=regexp |
    
    1. Make it possible to add regexp= or glob= prefix for the current value argument:
    | List Should Contain Value | ${list} | foo        | case_insensitive=yes |
    | List Should Contain Value | ${list} | glob=f*    | case_insensitive=yes |
    | List Should Contain Value | ${list} | regexp=f.* | case_insensitive=yes |
    

    I kind of like the last alternative best. What about others?

  6. pekkaklarck commented on Jul 3, 2014

    @pekkaklarck
    Member

    To which keywords would you like to implement this support? It would at least potentially be useful with many.

  7. guykisel commented on Jul 3, 2014

    @guykisel
    Contributor

    For 2) I would suggest also allowing (but not requiring) an explicit match=, as in:

    | List Should Contain Value | ${list} | foo        | case_insensitive=yes |
    | List Should Contain Value | ${list} | glob=f*    | case_insensitive=yes |
    | List Should Contain Value | ${list} | regexp=f.* | case_insensitive=yes |
    | List Should Contain Value | ${list} | match=foo  | case_insensitive=yes |
    

    I think this matching logic could be implemented in a util function, and then it could be easily applied to any keyword that does matching on iterables.

    Keywords to use this logic in:
    BuiltIn.Should Contain
    BuiltIn.Should Not Contain
    BuiltIn.Should Contain X Times
    BuiltIn.Get Count
    Collections.List Should Contain Value
    Collections.List Should Not Contain Value
    Collections.Dictionary Should Contain Key
    Collections.Dictionary Should Not Contain Key
    Collections.Dictionary Should Contain Item
    Collections.Dictionary Should Not Contain Item
    Collections.Dictionary Should Contain Value
    Collections.Dictionary Should Not Contain Value

    This will result in some redundant keywords, since some of the libraries already have keywords for matching patterns or regexps.

  8. guykisel commented on Jul 10, 2014

    @guykisel
    Contributor

    @pekkaklarck I've implemented this functionality in Collections and I'm fairly happy with it, but there are still the BuiltIn keywords. I have a few options:

    1. Don't implement this logic for BuiltIn keywords
    2. Move the logic for this functionality into robot.utils, then import it in Collections and BuiltIn
    3. Import Collections in BuiltIn
    4. Move the logic to BuiltIn and import BuiltIn in Collections

    What do you think?

  9. guykisel commented on Jul 10, 2014

    @guykisel
    Contributor

    It looks like the BuiltIn.Should Match keyword (and the other similar keywords) already support some pattern matching. I could do a backwards-incompatible rewrite of these keywords with the new matching pattern functionality, but I'm not sure if this is desirable.

  10. pekkaklarck commented on Jul 12, 2014

    @pekkaklarck
    Member

    After discussing this issue with @guykisel on IRC and thinking about it a bit more, I think it's better to add new keywords for finding/counting matches from lists. It has two main benefits:

    • Less work than updating all keywords that operate with lists in one way or other. In a comment above @guykisel lists altogether 12 keywords that could get this support.
    • No risk for breaking backwards compatibility.

    The keywords that could be added:

    • Should Contain Match
    • Should Not Contain Match
    • Get Matches
    • Get Match Count

    All of them should take arguments list, pattern, case_insensitive=False. Pattern should be a glob pattern by default, and support also regexps by using regexp= prefix. Also glob= prefix should be supported. It would allow having a glob pattern starting with regexp= text (e.g. glob=regexp=*).

    I'm not certain should these new keywords be added to BuiltIn or to Collections. The former has related Should (Not) Contain and Get Count keywords (although they work also with strings) but the latter has more list related keywords. I don't want to make BuiltIn huge so perhaps Collections is better.

  11. guykisel commented on Jul 14, 2014

    @guykisel
    Contributor

    @pekkaklarck that all sounds good to me. I'll add them to Collections for now, but it's easy to move the keywords if we decide otherwise later.

    I'll use Matcher's glob parser as opposed to using Python's built in glob functions, for consistency with other RF matching/contains keywords.

  12. pekkaklarck commented on Jul 14, 2014

    @pekkaklarck
    Member

    Sounds good! I'd also consider enhancing Matcher to accept regexp patterns too. Ought to be easy to implement with a help of few new unit tests and ought to also ease further implementation considerably.

  13. added this to the 2.8.6 milestone on Jul 31, 2014
  14. self-assigned this
    on Jul 31, 2014
  15. pekkaklarck commented on Jul 31, 2014

    @pekkaklarck
    Member

    @guykisel has been working with a great pull request so this is soon implemented. Will be included in the forthcoming 2.8.6 release.

  16. changed the title [-]Collections: Support case-insensitive matching and pattern matching[/-] [+]Collections: New keywords for matching list items using glob/regexp patterns optionally case/space sensitively[/+] on Jul 31, 2014
  17. pekkaklarck commented on Jul 31, 2014

    @pekkaklarck
    Member

    This was implemented by pull request #1756 by @guykisel.

    The newly added keywords are:

    • Should Contain Match
    • Should Not Contain Match
    • Get Matches
    • Get Match Count
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