Revision: 3204
Author: jussi.ao.malinen
Date: Thu May  6 00:07:22 2010
Log: Allow variable files' `get_variables` to return any mapping (Issue 427) implementation and tests.
http://code.google.com/p/robotframework/source/detail?r=3204

Added:
 /trunk/atest/robot/variables/getting_vars_from_dynamic_var_file.txt
 /trunk/atest/testdata/variables/dynamic_variable_files
 /trunk/atest/testdata/variables/dynamic_variable_files/dyn_vars.py
/trunk/atest/testdata/variables/dynamic_variable_files/getting_vars_from_dynamic_var_file.txt
Modified:
 /trunk/atest/robot/core/resource_and_variable_imports.html
 /trunk/src/robot/variables/variables.py

=======================================
--- /dev/null
+++ /trunk/atest/robot/variables/getting_vars_from_dynamic_var_file.txt Thu May 6 00:07:22 2010
@@ -0,0 +1,21 @@
+*** Settings ***
+Suite Setup Run Tests ${EMPTY} variables${/}dynamic_variable_files${/}getting_vars_from_dynamic_var_file.txt
+Default Tags      pybot  jybot  non-ready
+Resource        atest_resource.txt
+
+*** Test Cases ***
+Variables From Dict Should Be Loaded
+    Check Test Case  ${TEST NAME}
+
+Variables From My Dict Should Be Loaded
+    Check Test Case  ${TEST NAME}
+
+Variables From UserDict Should Be Loaded
+    Check Test Case  ${TEST NAME}
+
+Variables From My UserDict Should Be Loaded
+    Check Test Case  ${TEST NAME}
+
+Variables From Java Map Should Be Loaded
+    [tags]  jybot  non-ready
+    Check Test Case  ${TEST NAME}
=======================================
--- /dev/null
+++ /trunk/atest/testdata/variables/dynamic_variable_files/dyn_vars.py Thu May 6 00:07:22 2010
@@ -0,0 +1,37 @@
+import UserDict
+
+def get_variables(type):
+    return {'dict': get_dict,
+            'mydict': MyDict,
+            'UserDict': get_UserDict,
+            'MyUserDict': get_MyUserDict,
+            'JavaMap': get_JavaMap}[type]()
+
+
+def get_dict():
+    return {'from dict': 'This From Dict', 'from dict2': 2}
+
+class MyDict(dict):
+    def __init__(self):
+ dict.__init__(self, from_my_dict='This From My Dict', from_my_dict2=2)
+
+def get_UserDict():
+    userdict = UserDict.UserDict()
+ userdict.update({'from UserDict': 'This From UserDict', 'from UserDict2': 2})
+    return userdict
+
+class MyUserDict(UserDict.UserDict):
+    def __init__(self, dict):
+        self.data = {}
+        self.update(dict)
+
+def get_MyUserDict(*args):
+    return MyUserDict({'from MyUserDict': 'This From MyUserDict',
+                       'from MyUserDict2': 2})
+
+def get_JavaMap():
+    from java.util import HashMap
+    map = HashMap()
+    map.put('from Java Map', 'This From Java Map')
+    map.put('from Java Map2', 2)
+    return map
=======================================
--- /dev/null
+++ /trunk/atest/testdata/variables/dynamic_variable_files/getting_vars_from_dynamic_var_file.txt Thu May 6 00:07:22 2010
@@ -0,0 +1,27 @@
+*** Settings ***
+Variables  dyn_vars.py  dict
+Variables  dyn_vars.py  mydict
+Variables  dyn_vars.py  UserDict
+Variables  dyn_vars.py  MyUserDict
+Variables  dyn_vars.py  JavaMap
+
+*** Test Cases ***
+Variables From Dict Should Be Loaded
+    Should Be Equal  ${from dict}  This From Dict
+    Should Be Equal  ${from dict2}  ${2}
+
+Variables From My Dict Should Be Loaded
+    Should Be Equal  ${from my dict}  This From My Dict
+    Should Be Equal  ${from my dict2}  ${2}
+
+Variables From UserDict Should Be Loaded
+    Should Be Equal  ${from userdict}  This From UserDict
+    Should Be Equal  ${from userdict2}  ${2}
+
+Variables From My UserDict Should Be Loaded
+    Should Be Equal  ${from my userdict}  This From MyUserDict
+    Should Be Equal  ${from my userdict2}  ${2}
+
+Variables From Java Map Should Be Loaded
+    Should Be Equal  ${from Java Map}  This From Java Map
+    Should Be Equal  ${from Java Map2}  ${2}
=======================================
--- /trunk/atest/robot/core/resource_and_variable_imports.html Mon Apr 12 05:17:10 2010 +++ /trunk/atest/robot/core/resource_and_variable_imports.html Thu May 6 00:07:22 2010
@@ -287,7 +287,7 @@
 <td>My Check Stderr Contains</td>
<td>[ ERROR ] Error in file '${DATAPATH}' in table 'Setting' in element on row 10:</td> <td>Processing variable file '${path}' with arguments [ Two args returns None | which is invalid ] failed:</td>
-<td>get_variables returned 'NoneType', expected a dictionary</td>
+<td>get_variables returned 'NoneType', expected a mapping</td>
 </tr>
 <tr>
 <td class="name"></td>
=======================================
--- /trunk/src/robot/variables/variables.py     Tue Mar 30 23:37:45 2010
+++ /trunk/src/robot/variables/variables.py     Thu May  6 00:07:22 2010
@@ -12,10 +12,13 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.

-
 import re
 import os
-from types import DictionaryType
+from UserDict import UserDict
+if os.name == 'java':
+    from java.util import Map
+else:
+    class Map: pass

 from robot import utils
 from robot.errors import DataError
@@ -293,11 +296,13 @@
         except AttributeError:
             return None
         variables = get_variables(*args)
-        if type(variables) != DictionaryType:
-            raise DataError("%s returned '%s', expected a dictionary"
-                            % (get_variables.__name__,
-                               utils.type_as_str(variables)))
-        return variables.items()
+        if isinstance(variables, (dict, UserDict)):
+            return variables.items()
+        if isinstance(variables, Map):
+ return [(entry.key, entry.value) for entry in variables.entrySet()]
+        raise DataError("%s returned '%s', expected a mapping"
+                         % (get_variables.__name__,
+                           utils.type_as_str(variables)))

     def has_key(self, key):
         try:

Reply via email to