[tor-commits] [nyx/master] Test _draw_entry()

atagar at torproject.org atagar at torproject.org
Mon Jul 4 22:27:02 UTC 2016


commit 4a2b61ffc237e0ebfe11404b229f8551c160ac03
Author: Damian Johnson <atagar at torproject.org>
Date:   Mon Jul 4 11:34:46 2016 -0700

    Test _draw_entry()
    
    Few quick tests for a log panel subfunction.
---
 nyx/panel/log.py       | 45 +++++++++++++++++++++++----------------------
 test/panel/__init__.py |  1 +
 test/panel/graph.py    |  2 +-
 test/panel/header.py   |  2 +-
 test/panel/log.py      | 45 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 24 deletions(-)

diff --git a/nyx/panel/log.py b/nyx/panel/log.py
index f68748a..29bb159 100644
--- a/nyx/panel/log.py
+++ b/nyx/panel/log.py
@@ -283,12 +283,12 @@ class LogPanel(nyx.panel.DaemonPanel):
     for day in sorted(day_to_entries.keys(), reverse = True):
       if day == today:
         for entry in day_to_entries[day]:
-          y = self._draw_entry(subwindow, x, y, subwindow.width, entry, show_duplicates)
+          y = _draw_entry(subwindow, x, y, entry, show_duplicates)
       else:
         original_y, y = y, y + 1
 
         for entry in day_to_entries[day]:
-          y = self._draw_entry(subwindow, x, y, subwindow.width, entry, show_duplicates)
+          y = _draw_entry(subwindow, x, y, entry, show_duplicates)
 
         subwindow.box(original_y, x - 1, subwindow.width - x + 1, y - original_y + 1, YELLOW, BOLD)
         time_label = time.strftime(' %B %d, %Y ', time.localtime(day_to_entries[day][0].timestamp))
@@ -342,26 +342,6 @@ class LogPanel(nyx.panel.DaemonPanel):
 
     subwindow.addstr(0, 0, title, HIGHLIGHT)
 
-  def _draw_entry(self, subwindow, x, y, width, entry, show_duplicates):
-    """
-    Presents a log entry with line wrapping.
-    """
-
-    min_x, msg = x + 2, entry.display_message
-    boldness = BOLD if 'ERR' in entry.type else NORMAL  # emphasize ERR messages
-    color = CONFIG['attr.log_color'].get(entry.type, WHITE)
-
-    for line in msg.splitlines():
-      x, y = subwindow.addstr_wrap(x, y, line, width, min_x, boldness, color)
-
-    if entry.duplicates and not show_duplicates:
-      duplicate_count = len(entry.duplicates) - 1
-      plural = 's' if duplicate_count > 1 else ''
-      duplicate_msg = ' [%i duplicate%s hidden]' % (duplicate_count, plural)
-      x, y = subwindow.addstr_wrap(x, y, duplicate_msg, width, min_x, GREEN, BOLD)
-
-    return y + 1
-
   def _update(self):
     """
     Redraws the display, coalescing updates if events are rapidly logged (for
@@ -399,3 +379,24 @@ class LogPanel(nyx.panel.DaemonPanel):
 
     if self._filter.match(event.display_message):
       self._has_new_event = True
+
+
+def _draw_entry(subwindow, x, y, entry, show_duplicates):
+  """
+  Presents a log entry with line wrapping.
+  """
+
+  color = CONFIG['attr.log_color'].get(entry.type, WHITE)
+  boldness = BOLD if entry.type in ('ERR', 'ERROR') else NORMAL  # emphasize ERROR messages
+  min_x = x + 2
+
+  for line in entry.display_message.splitlines():
+    x, y = subwindow.addstr_wrap(x, y, line, subwindow.width, min_x, boldness, color)
+
+  if entry.duplicates and not show_duplicates:
+    duplicate_count = len(entry.duplicates) - 1
+    plural = 's' if duplicate_count > 1 else ''
+    duplicate_msg = ' [%i duplicate%s hidden]' % (duplicate_count, plural)
+    x, y = subwindow.addstr_wrap(x, y, duplicate_msg, subwindow.width, min_x, GREEN, BOLD)
+
+  return y + 1
diff --git a/test/panel/__init__.py b/test/panel/__init__.py
index c86ba85..8980380 100644
--- a/test/panel/__init__.py
+++ b/test/panel/__init__.py
@@ -5,4 +5,5 @@ Unit tests for nyx's panel modules.
 __all__ = [
   'header',
   'graph',
+  'log',
 ]
diff --git a/test/panel/graph.py b/test/panel/graph.py
index 6df72c5..1420a00 100644
--- a/test/panel/graph.py
+++ b/test/panel/graph.py
@@ -40,7 +40,7 @@ Download:
 """.rstrip()
 
 
-class TestGraph(unittest.TestCase):
+class TestGraphPanel(unittest.TestCase):
   def test_x_axis_labels(self):
     test_inputs = {
       0: {},
diff --git a/test/panel/header.py b/test/panel/header.py
index e8502d9..3e47afa 100644
--- a/test/panel/header.py
+++ b/test/panel/header.py
@@ -67,7 +67,7 @@ def test_sampling():
   )
 
 
-class TestHeader(unittest.TestCase):
+class TestHeaderPanel(unittest.TestCase):
   @require_curses
   @patch('nyx.controller.get_controller')
   @patch('nyx.panel.header.tor_controller')
diff --git a/test/panel/log.py b/test/panel/log.py
new file mode 100644
index 0000000..0fad2f1
--- /dev/null
+++ b/test/panel/log.py
@@ -0,0 +1,45 @@
+"""
+Unit tests for nyx.panel.log.
+"""
+
+import unittest
+
+import nyx.panel.log
+import test
+
+from nyx.log import LogEntry
+from test import require_curses
+
+EXPECTED_WRAPPED_MSG = """\
+[NOTICE] ho hum, ho hum, ho hum, ho hum, ho hum, ho hum, ho hum, ho
+  hum, ho hum, ho hum, ho hum, ho hum, ho hum, ho hum, ho hum, ho hum, ho hum,
+  ho hum, ho hum, ho hum, ho hum...
+""".rstrip()
+
+
+class TestLogPanel(unittest.TestCase):
+  @require_curses
+  def test_draw_entry(self):
+    entry = LogEntry(1467656897.08663, 'NOTICE', 'feeding sulfur to baby dragons is just mean...')
+    rendered = test.render(nyx.panel.log._draw_entry, 0, 0, entry, True)
+    self.assertEqual('[NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content.split(' ', 1)[1])
+
+  @require_curses
+  def test_draw_entry_that_wraps(self):
+    entry = LogEntry(1467656897.08663, 'NOTICE', 'ho hum%s...' % (', ho hum' * 20))
+    rendered = test.render(nyx.panel.log._draw_entry, 0, 0, entry, True)
+    self.assertEqual(EXPECTED_WRAPPED_MSG, rendered.content.split(' ', 1)[1])
+
+  @require_curses
+  def test_draw_entry_with_duplicates(self):
+    entry = LogEntry(1467656897.08663, 'NOTICE', 'feeding sulfur to baby dragons is just mean...')
+    entry.duplicates = [1, 2]  # only care about the count, not the content
+    rendered = test.render(nyx.panel.log._draw_entry, 0, 0, entry, True)
+    self.assertEqual('[NOTICE] feeding sulfur to baby dragons is just mean...', rendered.content.split(' ', 1)[1])
+
+    rendered = test.render(nyx.panel.log._draw_entry, 0, 0, entry, False)
+    self.assertEqual('[NOTICE] feeding sulfur to baby dragons is just mean... [1 duplicate\n  hidden]', rendered.content.split(' ', 1)[1])
+
+    entry.duplicates = [1, 2, 3, 4, 5, 6]
+    rendered = test.render(nyx.panel.log._draw_entry, 0, 0, entry, False)
+    self.assertEqual('[NOTICE] feeding sulfur to baby dragons is just mean... [5 duplicates\n  hidden]', rendered.content.split(' ', 1)[1])





More information about the tor-commits mailing list