File: interpreter.py

package info (click to toggle)
nyx 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,732 kB
  • sloc: python: 7,055; makefile: 7; sh: 3
file content (119 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Unit tests for nyx.panel.interpreter.
"""

import unittest

import nyx.curses
import nyx.panel.interpreter
import test

from test import require_curses

try:
  # added in python 3.3
  from unittest.mock import patch
except ImportError:
  from mock import patch

EXPECTED_PANEL = """
Control Interpreter:
>>> to use this panel press enter
""".strip()

EXPECTED_PANEL_INPUT_MODE = """
Control Interpreter (enter "/help" for usage or a blank line to stop):
>>>
""".strip()

EXPECTED_MULTILINE_PANEL = """
Control Interpreter:
>>> GETINFO version
250-version=0.2.4.27 (git-412e3f7dc9c6c01a)
>>> to use this panel press enter
""".strip()

EXPECTED_WITH_SCROLLBAR = """
Control Interpreter:
 |>>> GETINFO version
 |250-version=0.2.4.27 (git-412e3f7dc9c6c01a)
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
 |
-+
""".strip()


class TestInterpreter(unittest.TestCase):
  def test_format_prompt_input_with_interperter_command(self):
    output = nyx.panel.interpreter._format_prompt_input('/help')
    self.assertEqual(2, len(output))
    self.assertEqual(('>>> ', ('Green', 'Bold')), output[0])
    self.assertEqual(('/help', ('Magenta', 'Bold')), output[1])

  def test_format_prompt_input_with_command(self):
    output = nyx.panel.interpreter._format_prompt_input('GETINFO')
    self.assertEqual(2, len(output))
    self.assertEqual(('>>> ', ('Green', 'Bold')), output[0])
    self.assertEqual(('GETINFO ', ('Green', 'Bold')), output[1])

  def test_format_prompt_input_with_command_and_arg(self):
    output = nyx.panel.interpreter._format_prompt_input('GETINFO version')
    self.assertEqual(3, len(output))
    self.assertEqual(('>>> ', ('Green', 'Bold')), output[0])
    self.assertEqual(('GETINFO ', ('Green', 'Bold')), output[1])
    self.assertEqual(('version', ('Cyan', 'Bold')), output[2])

  @require_curses
  @patch('nyx.panel.interpreter.tor_controller')
  def test_blank_panel(self, tor_controller_mock):
    tor_controller_mock()._handle_event = lambda event: None

    panel = nyx.panel.interpreter.InterpreterPanel()
    self.assertEqual(EXPECTED_PANEL, test.render(panel._draw).content)

    panel._is_input_mode = True
    self.assertEqual(EXPECTED_PANEL_INPUT_MODE, test.render(panel._draw).content)

  @require_curses
  @patch('nyx.panel.interpreter.tor_controller')
  def test_multiline_panel(self, tor_controller_mock):
    tor_controller_mock()._handle_event = lambda event: None

    panel = nyx.panel.interpreter.InterpreterPanel()
    panel._add_line([('>>> ', ('Green', 'Bold')), ('GETINFO', ('Green', 'Bold')), (' version', ('Cyan',))])
    panel._add_line([('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', ('Blue',))])

    self.assertEqual(EXPECTED_MULTILINE_PANEL, test.render(panel._draw).content)

  @require_curses
  @patch('nyx.panel.interpreter.tor_controller')
  def test_scrollbar(self, tor_controller_mock):
    tor_controller_mock()._handle_event = lambda event: None

    panel = nyx.panel.interpreter.InterpreterPanel()
    panel._add_line([('>>> ', ('Green', 'Bold')), ('GETINFO', ('Green', 'Bold')), (' version', ('Cyan',))])
    panel._add_line([('250-version=0.2.4.27 (git-412e3f7dc9c6c01a)', ('Blue',))])

    for i in range(panel.get_height() - 2):
      panel._add_line([])

    self.assertEqual(EXPECTED_WITH_SCROLLBAR, test.render(panel._draw).content)