2 new revisions:

Revision: f9e7b3326b24
Author:   Pekka Klärck
Date:     Wed May 18 04:07:16 2011
Log:      Get Variable Value keyword...
http://code.google.com/p/robotframework/source/detail?r=f9e7b3326b24

Revision: aef3617303de
Author:   Pekka Klärck
Date:     Wed May 18 04:07:21 2011
Log:      regen
http://code.google.com/p/robotframework/source/detail?r=aef3617303de

==============================================================================
Revision: f9e7b3326b24
Author:   Pekka Klärck
Date:     Wed May 18 04:07:16 2011
Log:      Get Variable Value keyword

Update issue 844
Status: Done
Labels: Target-2.6
http://code.google.com/p/robotframework/source/detail?r=f9e7b3326b24

Added:
 /atest/robot/standard_libraries/builtin/get_variable_value.txt
 /atest/testdata/standard_libraries/builtin/get_variable_value.txt
Modified:
 /src/robot/libraries/BuiltIn.py

=======================================
--- /dev/null
+++ /atest/robot/standard_libraries/builtin/get_variable_value.txt Wed May 18 04:07:16 2011
@@ -0,0 +1,25 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY} standard_libraries/builtin/get_variable_value.txt
+Force Tags      regression  pybot  jybot
+Resource        atest_resource.txt
+
+*** Test Cases ***
+
+Get value when variable exists
+    Check Test Case  ${TESTNAME}
+
+Get value when variable doesn't exist
+    Check Test Case  ${TESTNAME}
+
+Default value contains variables
+    Check Test Case  ${TESTNAME}
+
+Use escaped variable syntaxes
+    Check Test Case  ${TESTNAME}
+
+List variables
+    Check Test Case  ${TESTNAME}
+
+Invalid variable syntax
+    Check Test Case  ${TESTNAME}
+
=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/builtin/get_variable_value.txt Wed May 18 04:07:16 2011
@@ -0,0 +1,45 @@
+*** Variables ***
+${VAR}   var table
+@{LIST}  1  2
+
+*** Test Cases ***
+
+Get value when variable exists
+    ${x} =  Get Variable Value  ${VAR}
+    Should be equal  ${x}  var table
+    ${y} =  Get Variable Value  ${x}  default value
+    Should be equal  ${y}  var table
+
+Get value when variable doesn't exist
+    ${x} =  Get Variable Value  ${nonex}
+    Should be equal  ${x}  ${None}
+    ${y} =  Get Variable Value  ${nonex}  default value
+    Should be equal  ${y}  default value
+
+Default value contains variables
+    ${x} =  Get Variable Value  ${nonex}  ${VAR}
+    Should be equal  ${x}  var table
+    ${y} =  Get Variable Value  ${nonex}  <${VAR.replace(' ', '')}>
+    Should be equal  ${y}  <vartable>
+    ${z} =  Get Variable Value  ${nonex}  ${42}
+    Should be equal  ${z}  ${42}
+
+Use escaped variable syntaxes
+    ${x} =  Get Variable Value  $VAR
+    Should be equal  ${x}  var table
+    ${y} =  Get Variable Value  \${VAR}
+    Should be equal  ${y}  var table
+    ${z} =  Get Variable Value  $NONEX  default
+    Should be equal  ${z}  default
+
+List variables
+    @{x} =  Get Variable Value  ${LIST}
+    Should be true  ${x} == ['1', '2']
+    @{y} =  Get Variable Value  ${nonex}  ${x}
+    Should be equal  ${y}  ${x}
+    @{z} =  Get Variable Value  ${nonex}
+    Should be empty  ${z}
+
+Invalid variable syntax
+    [Documentation]  FAIL Invalid variable syntax 'notvar'
+    Get Variable Value  notvar
=======================================
--- /src/robot/libraries/BuiltIn.py     Mon May 16 06:14:32 2011
+++ /src/robot/libraries/BuiltIn.py     Wed May 18 04:07:16 2011
@@ -670,6 +670,32 @@
"""Returns a dictionary containing all variables in the current scope."""
         return NAMESPACES.current.variables

+    def get_variable_value(self, name, default=None):
+ """Returns variable value or `default` if the variable does not exist.
+
+ The name of the variable can be given either as a normal variable name + (e.g. ${NAME}) or in escaped format (e.g. \\${NAME}). Notice that the
+        former has some limitations explained in `Set Suite Variable`.
+
+        Examples:
+        | ${x} = | Get Variable Value | ${a} | default |
+        | ${y} = | Get Variable Value | ${a} | ${b}    |
+        | ${z} = | Get Variable Value | ${z} |         |
+        =>
+ - ${x} gets value of ${a} if ${a} exists and string "default" otherwise + - ${y} gets value of ${a} if ${a} exists and value of ${b} otherwise
+        - ${z} is set to Python `None` if it does not exist previously
+
+ This keyword was added in Robot Framework 2.6. See `Set Variable If`
+        for another keyword to set variables dynamically.
+        """
+        name = self._get_var_name(name)
+        variables = self.get_variables()
+        try:
+            return variables[name]
+        except DataError:
+            return variables.replace_scalar(default)
+
     def log_variables(self, level='INFO'):
         """Logs all variables in the current scope with given log level."""
         variables = self.get_variables()
@@ -1121,6 +1147,9 @@
         | ...      | ${rc} == 2      | two               |
         | ...      | ${rc} > 2       | greater than two  |
         | ...      | ${rc} < 0       | less than zero    |
+
+        Use `Get Variable Value` if you need to set variables
+        dynamically based on whether a variable exist or not.
         """
         values = self._verify_values_for_set_variable_if(list(values))
         if self._is_true(condition):
@@ -1790,6 +1819,7 @@
for name in [ attr for attr in dir(_RunKeyword) if not attr.startswith('_') ]:
     register_run_keyword('BuiltIn', getattr(_RunKeyword, name))
for name in ['set_test_variable', 'set_suite_variable', 'set_global_variable',
-             'variable_should_exist', 'variable_should_not_exist', 'comment']:
+             'variable_should_exist', 'variable_should_not_exist', 'comment',
+             'get_variable_value']:
     register_run_keyword('BuiltIn', name, 0)
 del name, attr

==============================================================================
Revision: aef3617303de
Author:   Pekka Klärck
Date:     Wed May 18 04:07:21 2011
Log:      regen
http://code.google.com/p/robotframework/source/detail?r=aef3617303de

Modified:
 /doc/libraries/BuiltIn.html

=======================================
--- /doc/libraries/BuiltIn.html Mon May 16 06:30:22 2011
+++ /doc/libraries/BuiltIn.html Wed May 18 04:07:21 2011
@@ -131,6 +131,8 @@
 &nbsp;&middot;&nbsp;
<a href="#Get Time" title="Returns the given time in the requested format.">Get&nbsp;Time</a>
 &nbsp;&middot;&nbsp;
+<a href="#Get Variable Value" title="Returns variable value or `default` if the variable does not exist.">Get&nbsp;Variable&nbsp;Value</a>
+&nbsp;&middot;&nbsp;
<a href="#Get Variables" title="Returns a dictionary containing all variables in the current scope.">Get&nbsp;Variables</a>
 &nbsp;&middot;&nbsp;
<a href="#Import Library" title="Imports a library with the given name and optional arguments.">Import&nbsp;Library</a>
@@ -769,6 +771,41 @@
 - @{time} = ['16', '08', '24']</td>
 </tr>
 <tr>
+  <td class="kw"><a name="Get Variable Value"></a>Get Variable Value</td>
+  <td class="arg">name, default=None</td>
+ <td class="doc">Returns variable value or <span class="name">default</span> if the variable does not exist.<br />
+<br />
+The name of the variable can be given either as a normal variable name (e.g. ${NAME}) or in escaped format (e.g. \${NAME}). Notice that the former has some limitations explained in <a href="#Set Suite Variable" class="name">Set Suite Variable</a>.<br />
+<br />
+Examples:<br />
+<table border="1" class="doc">
+<tr>
+<td>${x} =</td>
+<td>Get Variable Value</td>
+<td>${a}</td>
+<td>default</td>
+</tr>
+<tr>
+<td>${y} =</td>
+<td>Get Variable Value</td>
+<td>${a}</td>
+<td>${b}</td>
+</tr>
+<tr>
+<td>${z} =</td>
+<td>Get Variable Value</td>
+<td>${z}</td>
+<td></td>
+</tr>
+</table>
+=&gt;<br />
+- ${x} gets value of ${a} if ${a} exists and string "default" otherwise<br />
+- ${y} gets value of ${a} if ${a} exists and value of ${b} otherwise<br />
+- ${z} is set to Python <span class="name">None</span> if it does not exist previously<br />
+<br />
+This keyword was added in Robot Framework 2.6. See <a href="#Set Variable If" class="name">Set Variable If</a> for another keyword to set variables dynamically.</td>
+</tr>
+<tr>
   <td class="kw"><a name="Get Variables"></a>Get Variables</td>
   <td class="arg"></td>
<td class="doc">Returns a dictionary containing all variables in the current scope.</td>
@@ -1487,7 +1524,8 @@
 <td></td>
 </tr>
 </table>
-</td>
+<br />
+Use <a href="#Get Variable Value" class="name">Get Variable Value</a> if you need to set variables dynamically based on whether a variable exist or not.</td>
 </tr>
 <tr>
   <td class="kw"><a name="Should Be Empty"></a>Should Be Empty</td>
@@ -1928,9 +1966,9 @@
 </tr>
 </table>
 <p id="footer">
-Altogether 79 keywords.<br />
+Altogether 80 keywords.<br />
Generated by <a href="http://code.google.com/p/robotframework/wiki/LibraryDocumentationTool";>libdoc.py</a>
-on 2011-05-16 16:30:01.
+on 2011-05-18 14:05:51.
 </p>
 </body>
 </html>

Reply via email to