Revision: 6b3d79444347
Author:   Robot Framework Developers <[email protected]>
Date:     Tue Dec 20 05:00:36 2011
Log:      writer: escape pipes and handle empty celss in pipe format
http://code.google.com/p/robotframework/source/detail?r=6b3d79444347

Modified:
 /src/robot/writer/formatters.py
 /src/robot/writer/tableformatters.py
 /utest/writer/test_formatters.py

=======================================
--- /src/robot/writer/formatters.py     Tue Dec 20 04:09:25 2011
+++ /src/robot/writer/formatters.py     Tue Dec 20 05:00:36 2011
@@ -44,11 +44,10 @@


 class TsvFormatter(_TestDataFileFormatter):
-    _padding = ''

     def __init__(self, cols=8):
         self._cols = cols
-        self._formatter = RowSplittingFormatter(self._padding, self._cols)
+        self._formatter = RowSplittingFormatter(self._cols)

     def _variable_table_formatter(self):
         return self._formatter
@@ -70,11 +69,10 @@

     def _pad(self, row):
         row = [cell.replace('\n', ' ') for cell in row]
-        return row + [self._padding] * (self._cols - len(row))
+        return row + [''] * (self._cols - len(row))


 class TxtFormatter(_TestDataFileFormatter):
-    _padding = ''
     _FIRST_ROW_LENGTH = 18
     _SETTING_NAME_WIDTH = 14

@@ -82,11 +80,10 @@
         self._cols = cols

     def _variable_table_formatter(self):
-        return RowSplittingFormatter(self._padding, self._cols)
+        return RowSplittingFormatter(self._cols)

     def _setting_table_formatter(self):
-        return SettingTableAligner(self._padding, self._cols,
-                                   self._SETTING_NAME_WIDTH)
+        return SettingTableAligner(self._cols, self._SETTING_NAME_WIDTH)

     def _test_table_formatter(self, tests):
         return self._indented_table_formatter(tests)
@@ -103,7 +100,7 @@
     def _indented_table_formatter(self, table):
         if self._should_align_columns(table):
             return ColumnAligner(self._FIRST_ROW_LENGTH, table)
-        return RowSplittingFormatter(self._padding, self._cols)
+        return RowSplittingFormatter(self._cols)

     def _should_align_columns(self, table):
         return bool(table.header[1:])
@@ -112,21 +109,45 @@
         return self._escape(row)

     def _escape(self, row):
+        return self._escape_consecutive_whitespace(
+            self._escape_empty_cell_from_start(row))
+
+    def _escape_empty_cell_from_start(self, row):
         if len(row) >= 2 and row[0] == '' and row[1] == '':
             row[1] = '\\'
+        return row
+
+    def _escape_consecutive_whitespace(self, row):
         return [re.sub('\s\s+(?=[^\s])',
lambda match: '\\'.join(match.group(0)), item.replace('\n', ' ')) for item in row]


 class PipeFormatter(TxtFormatter):
-    _padding = '  '
+
+    def _escape(self, row):
+        row = self._format_empty_cells(row)
+        return self._escape_consecutive_whitespace(self._escape_pipes(row))
+
+    def _format_empty_cells(self, row):
+        return ['  ' if not cell else cell for cell in row]
+
+    def _escape_pipes(self, row):
+        return [self._escape_pipes_from_cell(cell) for cell in row]
+
+    def _escape_pipes_from_cell(self, cell):
+        cell = cell.replace(' | ', ' \\| ')
+        if cell.startswith('| '):
+            cell = '\\' + cell
+        if cell.endswith(' |'):
+            cell = cell[:-1] + '\\|'
+        return cell


 class HtmlFormatter(_TestDataFileFormatter):

     def __init__(self):
         self._cols = 5
-        self._formatter = SplittingHtmlFormatter('', self._cols)
+        self._formatter = SplittingHtmlFormatter(self._cols)

     def empty_row(self):
         return [NameCell('')] + [HtmlCell('') for _ in range(self._cols-1)]
=======================================
--- /src/robot/writer/tableformatters.py        Tue Dec 20 04:09:25 2011
+++ /src/robot/writer/tableformatters.py        Tue Dec 20 05:00:36 2011
@@ -46,10 +46,9 @@

 class RowSplittingFormatter(_Formatter):

-    def __init__(self, padding, cols):
+    def __init__(self, cols):
         self._cols = cols
-        self._padding = padding
-        self._row_splitter = RowSplitter(padding, cols)
+        self._row_splitter = RowSplitter(cols)

     def format_simple_table(self, table):
         for row in self._rows_from_simple_table(table):
@@ -77,8 +76,8 @@

 class SettingTableAligner(_Aligner):

-    def __init__(self, padding, cols, first_column_width):
-        self._row_splitter = RowSplitter(padding, cols)
+    def __init__(self, cols, first_column_width):
+        self._row_splitter = RowSplitter(cols)
         self._widths = [first_column_width]

     def format_simple_table(self, table):
@@ -151,7 +150,7 @@

     def _format_documentation(self, doc, indent):
         if indent:
-            start = [NameCell(self._padding), HtmlCell(doc.setting_name)]
+            start = [NameCell(), HtmlCell(doc.setting_name)]
             value = doc.as_list()[1:]
             if len(value) == 1:
return [start + [DocumentationCell(doc.value, self._cols-1-indent)]]
@@ -162,12 +161,12 @@
     def _format_row(self, row):
         if row and not isinstance(row[0], basestring):
             return row
-        return self._pad([NameCell(row[0])] + [Cell(c) for c in row[1:]])
+ return self._pad([NameCell(row[0])] + [HtmlCell(c) for c in row[1:]])

     def _pad(self, row, colspan=False, indent=0):
         if colspan:
             return row
- return row + [HtmlCell(self._padding)] * (self._cols - len(row) - indent)
+        return row + [HtmlCell()] * (self._cols - len(row) - indent)


 class HtmlCell(object):
@@ -191,7 +190,7 @@

 class NameCell(HtmlCell):

-    def __init__(self, name, attributes=None):
+    def __init__(self, name='', attributes=None):
         HtmlCell.__init__(self, name, attributes)
         self.attributes.update({'class': 'name'})

@@ -226,9 +225,8 @@
     _empty_cell_escape = '${EMPTY}'
     _line_continuation = '...'

-    def __init__(self, padding='', cols=8):
+    def __init__(self, cols=8):
         self._cols = cols
-        self._padding = padding

     def split(self, row, indent):
         self._in_comment = False
@@ -263,4 +261,4 @@
         return data

     def _indent(self, row, indent):
-        return [self._padding]*indent + row
+        return [''] * indent + row
=======================================
--- /utest/writer/test_formatters.py    Tue Dec 20 04:09:25 2011
+++ /utest/writer/test_formatters.py    Tue Dec 20 05:00:36 2011
@@ -1,7 +1,7 @@
 import unittest
 from robot.parsing.model import TestCaseTable, TestCaseFileSettingTable

-from robot.writer.formatters import TxtFormatter, HtmlFormatter, TsvFormatter +from robot.writer.formatters import TxtFormatter, HtmlFormatter, TsvFormatter, PipeFormatter
 from robot.writer.tableformatters import RowSplitter, HtmlCell
 from robot.utils.asserts import assert_equals

@@ -29,6 +29,21 @@
         assert_equals(TxtFormatter()._escape(['so\nme']), ['so me'])


+class TestPipeFormatter(unittest.TestCase):
+
+    def test_escaping_pipes(self):
+        assert_equals(PipeFormatter()._escape(['so | me']), ['so \\| me'])
+        assert_equals(PipeFormatter()._escape(['|so|me|']), ['|so|me|'])
+        assert_equals(PipeFormatter()._escape(['so |']), ['so \\|'])
+        assert_equals(PipeFormatter()._escape(['| so']), ['\\| so'])
+
+    def test_empty_cell(self):
+        settings = TestCaseFileSettingTable(None)
+        settings.force_tags.value = ['f1', '', 'f3']
+        assert_equals(list(PipeFormatter().setting_rows(settings))[0],
+                      ['Force Tags    ', 'f1', '  ', 'f3'])
+
+
 class TestTsvFormatter(unittest.TestCase):

     def test_replacing_newlines(self):

Reply via email to