Revision: 4070
Author: jussi.ao.malinen
Date: Fri Sep 17 00:59:49 2010
Log: UserDict and Variables update now keeps internal key list updated.
http://code.google.com/p/robotframework/source/detail?r=4070

Modified:
 /trunk/atest/robot/core/invalid_syntax.txt
 /trunk/atest/robot/parsing/multirow.txt
 /trunk/src/robot/utils/normalizing.py
 /trunk/src/robot/variables/variables.py
 /trunk/utest/utils/test_normalizing.py
 /trunk/utest/variables/test_variables.py

=======================================
--- /trunk/atest/robot/core/invalid_syntax.txt  Wed May 26 07:30:39 2010
+++ /trunk/atest/robot/core/invalid_syntax.txt  Fri Sep 17 00:59:49 2010
@@ -30,7 +30,7 @@
     Verify Error  Non-existing setting 'Invalid Setting'  Setting

 Variables In Variable Table
- Verify Error Setting variable 'Invalid Variable Name' failed: Invalid variable name. Variable + Verify Error Setting variable 'Invalid Variable Name' failed: Invalid variable name 'Invalid Variable Name'. Variable
     Check Test Case  Check Valid Variable In Variable Table

 Non-Existing Variable In Test
=======================================
--- /trunk/atest/robot/parsing/multirow.txt     Wed Aug 18 05:22:12 2010
+++ /trunk/atest/robot/parsing/multirow.txt     Fri Sep 17 00:59:49 2010
@@ -52,7 +52,7 @@

 Invalid Multirow Usage
Check Multirow Error From Stderr Settings Non-existing setting '...'. - Check Multirow Error From Stderr Variables Setting variable '...' failed: Invalid variable name. + Check Multirow Error From Stderr Variables Setting variable '...' failed: Invalid variable name '...'.
     Check Test Case  \
     Check Test Case  Invalid Usage In Test And User Keyword

=======================================
--- /trunk/src/robot/utils/normalizing.py       Mon May 31 05:21:54 2010
+++ /trunk/src/robot/utils/normalizing.py       Fri Sep 17 00:59:49 2010
@@ -83,9 +83,21 @@
         for key, value in initial.items():
             self[key] = value

-    def __setitem__(self, key, value):
+    def update(self, dict=None, **kwargs):
+        if dict:
+            UserDict.update(self, dict)
+            for key in dict:
+                self._add_key(key)
+        if kwargs:
+            self.update(kwargs)
+
+    def _add_key(self, key):
         nkey = self._normalize(key)
         self._keys.setdefault(nkey, key)
+        return nkey
+
+    def __setitem__(self, key, value):
+        nkey = self._add_key(key)
         self.data[nkey] = value

     set = __setitem__
@@ -112,6 +124,9 @@
     def keys(self):
         return self._keys.values()

+    def __iter__(self):
+        return self._keys.itervalues()
+
     def items(self):
         return [ (key, self[key]) for key in self.keys() ]

=======================================
--- /trunk/src/robot/variables/variables.py     Tue Aug 31 04:06:38 2010
+++ /trunk/src/robot/variables/variables.py     Fri Sep 17 00:59:49 2010
@@ -47,8 +47,7 @@
         self._identifiers = identifiers

     def __setitem__(self, name, value, path=None, depr_warning=True):
-        if not is_var(name):
-            raise DataError("Invalid variable name '%s'." % name)
+        self._validate_var_name(name)
         if depr_warning:
             self._deprecation_warning_if_basename_already_used(name, path)
         utils.NormalizedDict.__setitem__(self, name, value)
@@ -67,9 +66,17 @@
# If path is not known we are executing keywords and can log message
         LOGGER.warn(msg, log=not path)

+    def update(self, dict=None, **kwargs):
+        if dict:
+            self._validate_var_dict(dict)
+            UserDict.update(self, dict)
+            for key in dict:
+                self._add_key(key)
+        if kwargs:
+            self.update(kwargs)
+
     def __getitem__(self, name):
-        if not is_var(name):
-            raise DataError("Invalid variable name '%s'." % name)
+        self._validate_var_name(name)
         try: return utils.NormalizedDict.__getitem__(self, name)
         except KeyError:
             try: return self._get_number_var(name)
@@ -80,6 +87,14 @@
                     except ValueError:
raise DataError("Non-existing variable '%s'." % name)

+    def _validate_var_name(self, name):
+        if not is_var(name):
+            raise DataError("Invalid variable name '%s'." % name)
+
+    def _validate_var_dict(self, dict):
+        for name in dict:
+            self._validate_var_name(name)
+
     def _get_list_var_as_scalar(self, name):
         if is_scalar_var(name):
             try:
@@ -130,7 +145,7 @@
         return results

     def _replace_variables_inside_possible_list_var(self, item):
-        if not (isinstance(item, basestring)
+        if not (isinstance(item, basestring)
                 and item.startswith('@{') and item.endswith('}')):
             return None
         var = VariableSplitter(item, self._identifiers)
@@ -254,7 +269,7 @@
         for variable in variable_table:
             try:
name, value = self._get_var_table_name_and_value(variable.name, - variable.value, + variable.value, variable_table.source) # self.has_key would also match if name matches extended syntax
                 if not utils.NormalizedDict.has_key(self, name):
@@ -264,8 +279,7 @@
% (variable.name, unicode(err)))

     def _get_var_table_name_and_value(self, name, value, path=None):
-        if not is_var(name):
-            raise DataError("Invalid variable name.")
+        self._validate_var_name(name)
value = [ self._unescape_leading_trailing_spaces(cell) for cell in value ]
         if name[0] == '@':
             return name, self.replace_list(value)
=======================================
--- /trunk/utest/utils/test_normalizing.py      Fri Sep 17 00:58:20 2010
+++ /trunk/utest/utils/test_normalizing.py      Fri Sep 17 00:59:49 2010
@@ -168,6 +168,28 @@
         nd = NormalizedDict({'a': 1, 'B': 1})
         assert_equals(str(nd), "{'a': 1, 'B': 1}")

+    def test_update(self):
+        nd = NormalizedDict({'a': 1})
+        nd.update({'b': 2})
+        assert_equals(nd['b'], 2)
+        assert_true('b' in nd.keys())
+
+    def test_update_with_kwargs(self):
+        nd = NormalizedDict({'a': 0, 'c': 1})
+        nd.update({'b': 2, 'c': 3}, b=4, d=5)
+        for k, v in [('a', 0), ('b', 4), ('c', 3), ('d', 5)]:
+            assert_equals(nd[k], v)
+            assert_true(k in nd)
+            assert_true(k in nd.keys())
+
+    def test_iter(self):
+        nd = NormalizedDict({'a': 0, 'B': 1, 'c': 2})
+        assert_equals(sorted(list(nd)), ['B', 'a', 'c'])
+        keys = []
+        for key in nd:
+            keys.append(key)
+        assert_equals(sorted(keys), ['B', 'a', 'c'])
+

 if __name__ == '__main__':
     unittest.main()
=======================================
--- /trunk/utest/variables/test_variables.py    Mon Aug  3 05:47:27 2009
+++ /trunk/utest/variables/test_variables.py    Fri Sep 17 00:59:49 2010
@@ -83,7 +83,19 @@
                 self.varz.clear()
         self.varz['${myvar}'] = ''
         assert_equals(self.varz['${myvar}'], '')
-
+
+    def test_update(self):
+        self.varz['${a}'] = 1
+        self.varz.update({'${b}':2})
+        for k, v in [('${a}', 1), ('${b}', 2)]:
+            assert_true(k in self.varz)
+            assert_true(k in self.varz.keys())
+            assert_equals(self.varz[k], v)
+
+    def test_update_invalid(self):
+        self.varz['${a}'] = 1
+ assert_raises(DataError, self.varz.update, {'invalid variable name':2})
+
     def test_set_list(self):
         for var in LISTS:
             for value in [ [], [''], ['str'], [10], ['hi','u'], ['hi',2],

Reply via email to