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

atagar at torproject.org atagar at torproject.org
Mon Apr 18 20:23:15 UTC 2016


commit 619cb2186fe67414c4ce1e49e7eb07821bdf24c8
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat Apr 9 21:48:37 2016 -0700

    Test _draw_platform_section()
    
    First test for our header panel. This requires our render() method to provide a
    draw context but other than that straight forward.
    
    I'm not a fan of the new _sampling() function. I'll change that when we add
    tests for get_sampling().
---
 nyx/panel/header.py    | 91 ++++++++++++++++++++++++++------------------------
 test/__init__.py       | 17 ++++++++--
 test/panel/__init__.py |  7 ++++
 test/panel/header.py   | 47 ++++++++++++++++++++++++++
 4 files changed, 116 insertions(+), 46 deletions(-)

diff --git a/nyx/panel/header.py b/nyx/panel/header.py
index 8efbf8e..f49efe1 100644
--- a/nyx/panel/header.py
+++ b/nyx/panel/header.py
@@ -45,7 +45,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
     threading.Thread.__init__(self)
     self.setDaemon(True)
 
-    self._vals = get_sampling()
+    self._vals = _get_sampling()
 
     self._last_width = 100
     self._pause_condition = threading.Condition()
@@ -181,7 +181,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
     left_width = max(subwindow.width / 2, 77) if is_wide else subwindow.width
     right_width = subwindow.width - left_width
 
-    self._draw_platform_section(subwindow, 0, 0, left_width, vals)
+    _draw_platform_section(subwindow, 0, 0, left_width, vals)
 
     if vals.is_connected:
       self._draw_ports_section(subwindow, 0, 1, left_width, vals)
@@ -212,35 +212,6 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
     else:
       subwindow.addstr(0, subwindow.height - 1, 'Paused', HIGHLIGHT)
 
-  def _draw_platform_section(self, subwindow, x, y, width, vals):
-    """
-    Section providing the user's hostname, platform, and version information...
-
-      nyx - odin (Linux 3.5.0-52-generic)        Tor 0.2.5.1-alpha-dev (unrecommended)
-      |------ platform (40 characters) ------|   |----------- tor version -----------|
-    """
-
-    initial_x, space_left = x, min(width, 40)
-
-    x = subwindow.addstr(x, y, vals.format('nyx - {hostname}', space_left))
-    space_left -= x - initial_x
-
-    if space_left >= 10:
-      subwindow.addstr(x, y, ' (%s)' % vals.format('{platform}', space_left - 3))
-
-    x, space_left = initial_x + 43, width - 43
-
-    if vals.version != 'Unknown' and space_left >= 10:
-      x = subwindow.addstr(x, y, vals.format('Tor {version}', space_left))
-      space_left -= x - 43 - initial_x
-
-      if space_left >= 7 + len(vals.version_status):
-        version_color = CONFIG['attr.version_status_colors'].get(vals.version_status, WHITE)
-
-        x = subwindow.addstr(x, y, ' (')
-        x = subwindow.addstr(x, y, vals.version_status, version_color)
-        subwindow.addstr(x, y, ')')
-
   def _draw_ports_section(self, subwindow, x, y, width, vals):
     """
     Section providing our nickname, address, and port information...
@@ -435,7 +406,7 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
 
   def _update(self):
     previous_height = self.get_height()
-    self._vals = get_sampling(self._vals)
+    self._vals = _get_sampling(self._vals)
 
     if self._vals.fd_used and self._vals.fd_limit != -1:
       fd_percent = 100 * self._vals.fd_used / self._vals.fd_limit
@@ -466,7 +437,24 @@ class HeaderPanel(nyx.panel.Panel, threading.Thread):
       self.redraw(True)  # just need to redraw ourselves
 
 
-def get_sampling(last_sampling = None):
+def _sampling(**attr):
+  class Sampling(collections.namedtuple('Sampling', attr.keys())):
+    def __init__(self, **attr):
+      super(Sampling, self).__init__(**attr)
+      self._attr = attr
+
+    def format(self, message, crop_width = None):
+      formatted_msg = message.format(**self._attr)
+
+      if crop_width:
+        formatted_msg = str_tools.crop(formatted_msg, crop_width)
+
+      return formatted_msg
+
+  return Sampling(**attr)
+
+
+def _get_sampling(last_sampling = None):
   controller = tor_controller()
   retrieved = time.time()
 
@@ -538,17 +526,34 @@ def get_sampling(last_sampling = None):
     'platform': '%s %s' % (os.uname()[0], os.uname()[2]),  # [platform name] [version]
   }
 
-  class Sampling(collections.namedtuple('Sampling', attr.keys())):
-    def __init__(self, **attr):
-      super(Sampling, self).__init__(**attr)
-      self._attr = attr
+  return _sampling(**attr)
 
-    def format(self, message, crop_width = None):
-      formatted_msg = message.format(**self._attr)
 
-      if crop_width:
-        formatted_msg = str_tools.crop(formatted_msg, crop_width)
+def _draw_platform_section(subwindow, x, y, width, vals):
+  """
+  Section providing the user's hostname, platform, and version information...
 
-      return formatted_msg
+    nyx - odin (Linux 3.5.0-52-generic)        Tor 0.2.5.1-alpha-dev (unrecommended)
+    |------ platform (40 characters) ------|   |----------- tor version -----------|
+  """
 
-  return Sampling(**attr)
+  initial_x, space_left = x, min(width, 40)
+
+  x = subwindow.addstr(x, y, vals.format('nyx - {hostname}', space_left))
+  space_left -= x - initial_x
+
+  if space_left >= 10:
+    subwindow.addstr(x, y, ' (%s)' % vals.format('{platform}', space_left - 3))
+
+  x, space_left = initial_x + 43, width - 43
+
+  if vals.version != 'Unknown' and space_left >= 10:
+    x = subwindow.addstr(x, y, vals.format('Tor {version}', space_left))
+    space_left -= x - 43 - initial_x
+
+    if space_left >= 7 + len(vals.version_status):
+      version_color = CONFIG['attr.version_status_colors'].get(vals.version_status, WHITE)
+
+      x = subwindow.addstr(x, y, ' (')
+      x = subwindow.addstr(x, y, vals.version_status, version_color)
+      subwindow.addstr(x, y, ')')
diff --git a/test/__init__.py b/test/__init__.py
index b606b4a..23dda15 100644
--- a/test/__init__.py
+++ b/test/__init__.py
@@ -3,6 +3,7 @@ Unit tests for nyx.
 """
 
 import collections
+import inspect
 import time
 import unittest
 
@@ -55,7 +56,8 @@ def require_curses(func):
 def render(func, *args, **kwargs):
   """
   Runs the given curses function, providing content that's rendered on the
-  screen.
+  screen. If the function starts with an argument named 'subwindow' then it's
+  provided one through :func:`~nyx.curses.draw`.
 
   :param function func: draw function to be invoked
 
@@ -67,9 +69,18 @@ def render(func, *args, **kwargs):
   def draw_func():
     nyx.curses.disable_acs()
     nyx.curses.CURSES_SCREEN.erase()
-
     start_time = time.time()
-    attr['return_value'] = func(*args, **kwargs)
+
+    func_args = inspect.getargspec(func).args
+
+    if func_args and func_args[0] == 'subwindow':
+      def _draw(subwindow):
+        return func(subwindow, *args, **kwargs)
+
+      attr['return_value'] = nyx.curses.draw(_draw)
+    else:
+      attr['return_value'] = func(*args, **kwargs)
+
     attr['runtime'] = time.time() - start_time
     attr['content'] = nyx.curses.screenshot()
 
diff --git a/test/panel/__init__.py b/test/panel/__init__.py
new file mode 100644
index 0000000..0050223
--- /dev/null
+++ b/test/panel/__init__.py
@@ -0,0 +1,7 @@
+"""
+Unit tests for nyx's panel modules.
+"""
+
+__all__ = [
+  'header',
+]
diff --git a/test/panel/header.py b/test/panel/header.py
new file mode 100644
index 0000000..ee18eb6
--- /dev/null
+++ b/test/panel/header.py
@@ -0,0 +1,47 @@
+"""
+Unit tests for nyx.panel.header.
+"""
+
+import unittest
+
+import nyx.panel.header
+import test
+
+from test import require_curses
+
+
+class TestHeader(unittest.TestCase):
+  @require_curses
+  def test_draw_platform_section(self):
+    vals = nyx.panel.header._sampling(
+      hostname = 'odin',
+      platform = 'Linux 3.5.0-54-generic',
+      version = '0.2.8.1-alpha-dev',
+      version_status = 'unrecommended',
+    )
+
+    test_input = {
+      80: 'nyx - odin (Linux 3.5.0-54-generic)        Tor 0.2.8.1-alpha-dev',
+      70: 'nyx - odin (Linux 3.5.0-54-generic)        Tor 0.2.8.1-alpha-dev',
+      60: 'nyx - odin (Linux 3.5.0-54-generic)        Tor 0.2.8.1-al...',
+      50: 'nyx - odin (Linux 3.5.0-54-generic)',
+      40: 'nyx - odin (Linux 3.5.0-54-generic)',
+      30: 'nyx - odin (Linux 3.5.0-54...)',
+      20: 'nyx - odin (Linu...)',
+      10: 'nyx - odin',
+      0: 'nyx - odin',
+    }
+
+    for width, expected in test_input.items():
+      self.assertEqual(expected, test.render(nyx.panel.header._draw_platform_section, 0, 0, width, vals).content)
+
+  @require_curses
+  def test_draw_platform_section_without_version(self):
+    vals = nyx.panel.header._sampling(
+      hostname = 'odin',
+      platform = 'Linux 3.5.0-54-generic',
+      version = 'Unknown',
+    )
+
+    rendered = test.render(nyx.panel.header._draw_platform_section, 0, 0, 80, vals)
+    self.assertEqual('nyx - odin (Linux 3.5.0-54-generic)', rendered.content)





More information about the tor-commits mailing list