Revision: 3285
Author: janne.t.harkonen
Date: Mon May 17 00:03:48 2010
Log: Added tests and implementation for test case and user keyword populators
http://code.google.com/p/robotframework/source/detail?r=3285

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

=======================================
--- /trunk/src/robot/parsing/populator.py       Wed May 12 05:19:58 2010
+++ /trunk/src/robot/parsing/populator.py       Mon May 17 00:03:48 2010
@@ -7,25 +7,28 @@

     def __init__(self, datafile):
         self._table = self._get_table(datafile)
-        self._current_populator = None
+        self._populator = None

     def add(self, row):
-        if self._is_continuing_row(row):
-            self._current_populator.add(self._values_from(row))
+        if self._is_continuing(row):
+            self._populator.add(self._values_from(row))
         else:
-            if self._current_populator:
-                self._current_populator.populate()
-            self._current_populator = self._get_populator(row)
-
-    def _is_continuing_row(self, row):
-        return row[0] == '...'
+            self.populate()
+            self._populator = self._get_populator(row)
+
+    def _values_from(self, row):
+        return row
+
+    def _is_continuing(self, row):
+        return row[0].strip() == self.row_continuation_marker

     def populate(self):
-        if self._current_populator:
-            self._current_populator.populate()
+        if self._populator:
+            self._populator.populate()


 class SettingTablePopulator(_TablePopulator):
+    row_continuation_marker = '...'
     attrs_by_name = utils.NormalizedDict({'Documentation': 'doc',
                                           'Document': 'doc',
                                           'Suite Setup': 'suite_setup',
@@ -62,6 +65,7 @@


 class VariableTablePopulator(_TablePopulator):
+    row_continuation_marker = '...'

     def _get_table(self, datafile):
         return datafile.variable_table
@@ -69,12 +73,86 @@
     def _get_populator(self, row):
         return VariablePopulator(self._table.add, row)

-    def _values_from(self, row):
-        return row
-
-
-class TestTablePopulator(SettingTablePopulator): pass
-class KeywordTablePopulator(SettingTablePopulator): pass
+
+class TestTablePopulator(_TablePopulator):
+    row_continuation_marker = ''
+
+    def _get_table(self, datafile):
+        return datafile.testcase_table
+
+    def _get_populator(self, row):
+        return TestCasePopulator(self._table.add, row)
+
+
+class KeywordTablePopulator(_TablePopulator):
+    row_continuation_marker = ''
+
+    def _get_table(self, datafile):
+        return datafile.keyword_table
+
+    def _get_populator(self, row):
+        return UserKeywordPopulator(self._table.add, row)
+
+
+class _TestCaseUserKeywordPopulator(object):
+    row_continuation_marker = ('', '...')
+
+    def __init__(self, setter, row):
+        self._test_or_uk = setter(row[0])
+        self._populator = self._get_populator(row)
+
+    def add(self, row):
+        if self._is_continuing_step(row):
+            self._populator.add(row[2:])
+        else:
+            self._populator.populate()
+            self._populator = self._get_populator(row)
+
+    def _get_populator(self, row):
+        first_cell = self._first_cell_value(row)
+        if self._is_setting(first_cell):
+ return PropertyPopulator(self._setting_setter(first_cell), row[2:])
+        return StepPopulator(self._test_or_uk.add_step, row[1:])
+
+    def _first_cell_value(self, row):
+        return row[1].strip() if len(row) > 1 else ''
+
+    def _is_setting(self, cell):
+        return cell and cell[0] == '[' and cell[-1] == ']'
+
+    def _setting_setter(self, cell):
+        attr_name = self.attrs_by_name[self._setting_name(cell)]
+        return getattr(self._test_or_uk, attr_name).set
+
+    def _setting_name(self, cell):
+        return cell[1:-1].strip()
+
+    def _is_continuing_step(self, row):
+ return (row[0].strip(), row[1].strip()) == self.row_continuation_marker
+
+    def populate(self):
+        if self._populator:
+            self._populator.populate()
+
+
+class TestCasePopulator(_TestCaseUserKeywordPopulator):
+    attrs_by_name = utils.NormalizedDict({'Documentation': 'doc',
+                                          'Document': 'doc',
+                                          'Setup': 'setup',
+                                          'Precondition': 'setup',
+                                          'Teardown': 'teardown',
+                                          'Postcondition': 'teardown',
+                                          'Tags': 'tags',
+                                          'Timeout': 'timeout'})
+
+
+
+class UserKeywordPopulator(_TestCaseUserKeywordPopulator):
+    attrs_by_name = utils.NormalizedDict({'Documentation': 'doc',
+                                          'Document': 'doc',
+                                          'Arguments': 'args',
+                                          'Return': 'return_',
+                                          'Timeout': 'timeout'})


 class PropertyPopulator(object):
@@ -95,6 +173,19 @@
         self._setter(self._value[0], self._value[1:])


+class StepPopulator(object):
+
+    def __init__(self, setter, row):
+        self._setter = setter
+        self._current_row = row
+
+    def add(self, row):
+        self._current_row.extend(row)
+
+    def populate(self):
+        if self._current_row:
+            self._setter(self._current_row)
+

 class Populator(object):
     _whitespace_regexp = re.compile('\s+')
=======================================
--- /trunk/utest/parsing/test_populator.py      Wed May 12 05:19:53 2010
+++ /trunk/utest/parsing/test_populator.py      Mon May 17 00:03:48 2010
@@ -61,6 +61,68 @@
         self._populator.eof()
         assert_equals(len(self._datafile.setting_table.imports), 4)

+    def test_test_case_populating(self):
+        self._start_table('Test cases')
+        self._populator.add(['My test name'])
+        self._populator.add(['', 'No operation'])
+        self._populator.add(['Another test'])
+        self._populator.add(['', 'Log', 'quux'])
+        self._populator.eof()
+        assert_equals(len(self._datafile.testcase_table.tests), 2)
+        test = self._datafile.testcase_table.tests[0]
+        assert_equals(len(test.steps), 1)
+        assert_equals(test.steps[0].keyword, 'No operation')
+        test = self._datafile.testcase_table.tests[1]
+        assert_equals(len(test.steps), 1)
+
+    def test_case_name_and_first_step_on_same_row(self):
+        self._start_table('Test cases')
+        self._populator.add(['My test name', 'No Operation'])
+        self._populator.eof()
+        test = self._datafile.testcase_table.tests[0]
+        assert_equals(len(test.steps), 1)
+
+    def test_continuing_row_in_test(self):
+        self._start_table('Test cases')
+        self._populator.add(['My test name', 'Log Many', 'foo'])
+        self._populator.add(['', '...', 'bar', 'quux'])
+        self._populator.add(['Another test'])
+        self._populator.add(['', 'Log Many', 'quux'])
+        self._populator.add(['', '...', 'fooness'])
+        self._populator.add(['', 'Log', 'barness'])
+        self._populator.eof()
+        test = self._datafile.testcase_table.tests[0]
+        assert_equals(len(test.steps), 1)
+        test = self._datafile.testcase_table.tests[1]
+        assert_equals(len(test.steps), 2)
+
+    def test_test_settings(self):
+        self._start_table('Test cases')
+        self._populator.add(['My test name'])
+ self._populator.add(['', '[Documentation]', 'This is domumentation for the test case'])
+        self._populator.add(['', '[  Tags  ]', 'ankka', 'kameli'])
+        self._populator.add(['', '... ', 'aasi'])
+        self._populator.add(['', 'Log', 'barness'])
+        self._populator.eof()
+        test = self._datafile.testcase_table.tests[0]
+        assert_equals(len(test.steps), 1)
+ assert_equals(test.doc.value, 'This is domumentation for the test case')
+        assert_equals(test.tags.value, ['ankka', 'kameli', 'aasi'])
+
+    def test_creating_user_keywords(self):
+        self._start_table('Keywords')
+        self._populator.add(['My User Keyword'])
+        self._populator.add(['', '[Arguments]', '${foo}', '${bar}'])
+        self._populator.add(['', 'Log Many', '${foo}'])
+        self._populator.add(['', '...', 'bar'])
+        self._populator.add(['', 'No Operation'])
+        self._populator.add(['', '[Return]', 'ankka', 'kameli'])
+        self._populator.eof()
+        uk = self._datafile.keyword_table.keywords[0]
+        assert_equals(len(uk.steps), 2)
+        assert_equals(uk.args.value, ['${foo}', '${bar}'])
+        assert_equals(uk.return_.value, ['ankka', 'kameli'])
+
     def _assert_setting(self, setting_name, exp_value):
assert_equals(getattr(self._datafile.setting_table, setting_name).value,
                       exp_value)

Reply via email to