Revision: 3320
Author: KariHusa
Date: Wed May 19 00:55:08 2010
Log: Started comment handling implementation
http://code.google.com/p/robotframework/source/detail?r=3320

Modified:
 /trunk/src/robot/parsing/populator.py
 /trunk/src/robot/parsing/settings.py
 /trunk/utest/parsing/test_populator.py

=======================================
--- /trunk/src/robot/parsing/populator.py       Tue May 18 05:28:18 2010
+++ /trunk/src/robot/parsing/populator.py       Wed May 19 00:55:08 2010
@@ -76,7 +76,7 @@
         return datafile.setting_table

     def _get_populator(self, row):
-        first_cell = row.head()
+        first_cell = row.head
         if self._is_metadata_with_olde_prefix(first_cell):
             return OldStyleMetadataPopulator(self._table.add_metadata)
         if self._is_import_or_metadata(first_cell):
@@ -158,7 +158,7 @@

     def _populate_declaration(self, row):
         if row.starts_for_loop() or row.is_continuing():
-            self._declaration.extend(row.tail())
+            self._declaration.extend(row.tail)
             return False
         return True

@@ -175,7 +175,7 @@

     def add(self, row):
         if not self._test_or_uk:
-            self._test_or_uk = self._test_or_uk_creator(row.head())
+            self._test_or_uk = self._test_or_uk_creator(row.head)
         dedented_row = row.dedent()
         if dedented_row:
             if not self._continues(dedented_row):
@@ -188,7 +188,7 @@

     def _get_populator(self, row):
         if row.starts_test_or_user_keyword_setting():
-            setter = self._setting_setter(row.head())
+            setter = self._setting_setter(row.head)
             return SettingPopulator(setter) if setter else NullPopulator()
         if row.starts_for_loop():
             return ForLoopPopulator(self._test_or_uk.add_for_loop)
@@ -239,12 +239,13 @@
     def __init__(self, setter):
         self._setter = setter
         self._value = []
+        self._comments = []


 class NameAndValuePropertyPopulator(_PropertyPopulator):

     def add(self, row):
-        self._value.extend(row.all())
+        self._value.extend(row.all)

     def populate(self):
         name, value = self._value[0], self._value[1:]
@@ -254,26 +255,27 @@
 class SettingPopulator(_PropertyPopulator):

     def add(self, row):
-        self._value.extend(row.tail())
+        self._value.extend(row.tail)
+        self._comments.extend(row.comments)

     def populate(self):
-        self._setter(self._value)
+        self._setter(self._value, self._comments)


 class SettingTableNameValuePopulator(NameAndValuePropertyPopulator):

     def add(self, row):
-        self._value.extend(row.tail())
+        self._value.extend(row.tail)


 class OldStyleMetadataPopulator(NameAndValuePropertyPopulator):
     olde_metadata_prefix = 'meta:'

     def add(self, row):
-        if self._is_metadata_with_olde_prefix(row.head()):
- values = self._extract_name_from_olde_style_meta_cell(row.head()) + row.tail()
+        if self._is_metadata_with_olde_prefix(row.head):
+ values = self._extract_name_from_olde_style_meta_cell(row.head) + row.tail
         else:
-            values = row.tail()
+            values = row.tail
         self._value.extend(values)

     def _extract_name_from_olde_style_meta_cell(self, first_cell):
@@ -286,7 +288,7 @@
 class StepPopulator(_PropertyPopulator):

     def add(self, row):
-        self._value.extend(row.all())
+        self._value.extend(row.all)

     def populate(self):
         if self._value:
@@ -349,55 +351,61 @@
     _whitespace_regexp = re.compile('\s+')

     def __init__(self, cells):
-        self.cells = self._data_cells(cells)
-
-    def head(self):
-        return self.cells[0]
-
-    def tail(self):
-        return self.cells[1:]
-
-    def all(self):
-        return self.cells
+        self.cells, self.comments = self._parse(cells)
+        self.head = self.cells[0] if self.cells else None
+        self.tail = self.cells[1:] if self.cells else None
+        self.all = self.cells

     def dedent(self):
-        return DataRow(self.tail())
+        return DataRow(self.tail)

     def startswith(self, value):
         return self.head() == value

     def starts_for_loop(self):
-        if not self.head().startswith(':'):
+        if not self.head.startswith(':'):
             return False
-        return self.head().replace(':', '').upper().strip() == 'FOR'
+        return self.head.replace(':', '').upper().strip() == 'FOR'

     def starts_test_or_user_keyword_setting(self):
-        head = self.head()
+        head = self.head
         return head and head[0] == '[' and head[-1] == ']'

     def is_indented(self):
-        return self.head() == ''
+        return self.head == ''

     def is_continuing(self):
-        return self.head() == self._row_continuation_marker
-
-    def _data_cells(self, row):
-        cells = [ self._collapse_whitespace(cell)
-                  for cell in self._cells_without_comments(row) ]
-        while cells and not cells[-1]:
-            cells.pop()
-        return cells
+        return self.head == self._row_continuation_marker
+
+    def _parse(self, row):
+        return self._purge_empty_cells(self._extract_data(row)), \
+            self._extract_comments(row)

     def _collapse_whitespace(self, value):
         return self._whitespace_regexp.sub(' ', value).strip()

-    def _cells_without_comments(self, row):
-        filtered = []
+    def _extract_comments(self, row):
+        comments = []
+        for c in row:
+            if c.startswith('#') and not comments:
+                comments.append(c[1:])
+            elif comments:
+                comments.append(c)
+        return comments
+
+    def _extract_data(self, row):
+        data = []
         for c in row:
             if c.startswith('#'):
-                return filtered
-            filtered.append(c)
-        return filtered
+                return data
+            data.append(c)
+        return data
+
+    def _purge_empty_cells(self, data):
+        data = [ self._collapse_whitespace(cell) for cell in data]
+        while data and not data[-1]:
+            data.pop()
+        return data

     def __nonzero__(self):
         return self.cells != []
=======================================
--- /trunk/src/robot/parsing/settings.py        Tue May 18 03:43:02 2010
+++ /trunk/src/robot/parsing/settings.py        Wed May 19 00:55:08 2010
@@ -17,8 +17,13 @@

     def __init__(self):
         self.value = []
-
-    def set(self, value):
+        self.comment = ''
+
+    def set(self, value, comment=''):
+        self._set(value)
+        self.comment = self._string_value(comment)
+
+    def _set(self, value):
         self.value = value

     def _string_value(self, value):
@@ -30,7 +35,7 @@
     def __init__(self):
         self.value = ''

-    def set(self, value):
+    def _set(self, value):
         self.value = self._string_value(value)


@@ -40,7 +45,7 @@
         self.name = None
         self.args = []

-    def set(self, value):
+    def _set(self, value):
         self.name = value[0] if value else ''
         self.args = value[1:]

@@ -51,7 +56,7 @@
         self.value = None
         self.message = ''

-    def set(self, value):
+    def _set(self, value):
         self.value = value[0] if value else ''
         self.message = ' '.join(value[1:])

=======================================
--- /trunk/utest/parsing/test_populator.py      Tue May 18 04:26:03 2010
+++ /trunk/utest/parsing/test_populator.py      Wed May 19 00:55:08 2010
@@ -21,8 +21,27 @@
     def value(self):
         return self._output.getvalue()

-
-class TestCaseFilePopulatingTest(unittest.TestCase):
+class _PopulatorTest(unittest.TestCase):
+
+    def _start_table(self, name):
+        return self._populator.start_table(name)
+
+    def _create_table(self, name, rows, eof=True):
+        self._start_table(name)
+        for r  in rows:
+            self._populator.add(r)
+        if eof:
+            self._populator.eof()
+
+    def _assert_setting(self, setting_name, exp_value, exp_comment=None):
+        assert_equals(self._setting_with(setting_name).value, exp_value)
+        if exp_comment:
+ assert_equals(self._setting_with(setting_name).comment, exp_comment)
+
+    def _setting_with(self, name):
+        return getattr(self._datafile.setting_table, name)
+
+class TestCaseFilePopulatingTest(_PopulatorTest):

     def setUp(self):
         self._datafile = TestCaseFile()
@@ -243,17 +262,11 @@
         assert_equals(len(self._datafile.testcase_table.tests), 1)
         assert_equals(len(self._nth_uk(0).steps), 1)

-    def _assert_setting(self, setting_name, exp_value):
-        assert_equals(self._setting_with(setting_name).value, exp_value)
-
     def _assert_meta(self, index, exp_name, exp_value):
         meta = self._setting_with('metadata')[index]
         assert_equals(meta.name, exp_name)
         assert_equals(meta.value, exp_value)

-    def _setting_with(self, name):
-        return getattr(self._datafile.setting_table, name)
-
     def _assert_fixture(self, fixture_name, exp_name, exp_args):
         fixture = self._setting_with(fixture_name)
         self._assert_name_and_args(fixture, exp_name, exp_args)
@@ -289,5 +302,18 @@
         assert_equals(len(test.steps), expected_steps)


+class TestPopulatingComments(_PopulatorTest):
+
+    def setUp(self):
+        self._datafile = TestCaseFile()
+        self._populator = TestDataPopulator(self._datafile)
+
+    def test_end_of_line_setting_comment(self):
+ self._create_table('settings', [['Force Tags', 'Foo', 'Bar', '#comment'],
+                                        ['#comment between rows'],
+                                        ['Default Tags', 'Quux']])
+        self._assert_setting('force_tags', ['Foo', 'Bar'], 'comment')
+ #self._assert_setting('default_tags', ['Quux'], 'comment between rows')
+
 if __name__ == '__main__':
     unittest.main()

Reply via email to