Skip to content

Support for "extended assign" like ${var.attr} = Some Keyword #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spooning opened this issue Jun 29, 2014 · 9 comments
Closed

Support for "extended assign" like ${var.attr} = Some Keyword #1059

spooning opened this issue Jun 29, 2014 · 9 comments

Comments

@spooning
Copy link
Contributor

spooning commented Jun 29, 2014

Originally submitted to Google Code by corh... on 17 Feb 2012

Attempting to assign a property of a Python object using the extended variable syntax counter-intuitively creates a new test variable with the same name as the extended variable.

Robot Framework 2.6.3 (Python 2.7.2 on win32)

Test suite:

*** Settings ***
Variables         vars.py

*** Test Cases ***
Test Extended Variables
    Should Be Equal    ${foo.bar}    hello
    ${foo.bar}=    Set Variable    goodbye
    Should Be Equal    ${foo.bar}    goodbye
    Should Be Equal    ${foo.bar}    ${foo.getbar()}

vars.py:

class Obj(object):
    def **init**(self):
        self.bar = "hello"
    def getbar(self):
        return self.bar

foo = Obj()
# Test output:
# Extended Variables                                   

Test Extended Variables                      | FAIL |
goodbye != hello
@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 1 Mar 2012

I agree this would be handy and find it surprising nobody has requested this earlier. Because I got a feeling this would be pretty immediately useful at least in one project at NSN and implementing this is pretty straightforward, I try to get this into 2.7.

I actually already prototyped this and a simple patch that makes the the original example pass is attached. There are some open issues to be discussed before this can be fully implemented:

  1. When assigning to variable ${foo.bar}, what to do if variable ${foo.bar} already exists? Probably we should just reassign it.

  2. When assigning to variable ${foo.bar}, what to do if there is no ${foo.bar} but no no ${foo} either? Should we fail or create ${foo.bar} variable?

  3. How to handle ${foo.bar.zap}? It would probably be easiest to assign zap to ${foo.bar}.

@spooning spooning added this to the 2.7 milestone Jun 29, 2014
@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 3 Mar 2012

I'm going to implement this so that open questions above are resolved like this:

  1. If ${foo.bar} variable exists, assigning ${foo.bar} will reset it and not assign variable to possible ${foo}.

  2. If ${foo} does not exist, then assigning ${foo.bar} creates new variable ${foo.bar}.

  3. Assigning to ${foo.bar.zap} works so that it tries to find variable ${foo.bar} (which may be extended variable itself) and then sets zap attribute to it.

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 3 Mar 2012

This issue was updated by revision d778a2bf312f.

Implementation is pretty much done.

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 3 Mar 2012

This issue was updated by revision ba573f0359f2.

Decided to disable extended assign altogether if the base variable is string or integer. Assigning attributes to these immutable objects wouldn't work anyway and now this feature is less backwards-incompatible. For example, following example that does not use extended assign but instead uses the dot to create a logical namespace will continue to work:

${url} = Set Variable http://my.app
${url.home} = Set Variable ${url}/home
${url.index} = Set Variable ${url}/index

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 3 Mar 2012

This issue was updated by revision f529ad7b0301.

Refactored related code a little.

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 4 Mar 2012

This issue was updated by revision 898f9860d5e2.

Righter rules for using the new syntax. Mainly to avoid potential backwards-incompatibility problems.

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 4 Mar 2012

This issue was updated by revision 77b7d8d8a29b.

Now this feature is also documented in the User Guide.

Janne, could you review the code, tests, and docs?

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @pekkaklarck on 4 Mar 2012

This issue was updated by revision 09851621bb24.

One more refactoring: Better home for new VariableAssigner.

@spooning
Copy link
Contributor Author

Originally submitted to Google Code by @yanne on 5 Mar 2012

Reviewed, looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants