Exemplo n.º 1
0
 def testInit(self, mockWin32):
     mockAttr = Mock()
     mockAttr.wAttributes = 7 + 6 * 16 + 8
     mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr
     term = WinTerm()
     self.assertEqual(term._fore, 7)
     self.assertEqual(term._back, 6)
     self.assertEqual(term._style, 8)
Exemplo n.º 2
0
    def assert_autoresets(self, convert, autoreset=True):
        stream = AnsiToWin32(Mock())
        stream.convert = convert
        stream.reset_all = Mock()
        stream.autoreset = autoreset
        stream.winterm = Mock()

        stream.write('abc')

        self.assertEqual(stream.reset_all.called, autoreset)
Exemplo n.º 3
0
    def testWriteStripsAnsi(self):
        mockStdout = Mock()
        stream = AnsiToWin32(mockStdout)
        stream.wrapped = Mock()
        stream.write_and_convert = Mock()
        stream.strip = True

        stream.write('abc')

        self.assertFalse(stream.wrapped.write.called)
        self.assertEqual(stream.write_and_convert.call_args, (('abc', ), {}))
Exemplo n.º 4
0
    def testSetConsole(self, mockWin32):
        mockAttr = Mock()
        mockAttr.wAttributes = 0
        mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr
        term = WinTerm()
        term.windll = Mock()

        term.set_console()

        self.assertEqual(mockWin32.SetConsoleTextAttribute.call_args,
                         ((mockWin32.STDOUT, term.get_attrs()), {}))
Exemplo n.º 5
0
 def testWriteAndConvertCallsWin32WithParamsAndCommand(self):
     stream = AnsiToWin32(Mock())
     stream.convert = True
     stream.call_win32 = Mock()
     stream.extract_params = Mock(return_value='params')
     data = {
         'abc\033[adef': ('a', 'params'),
         'abc\033[;;bdef': ('b', 'params'),
         'abc\033[0cdef': ('c', 'params'),
         'abc\033[;;0;;Gdef': ('G', 'params'),
         'abc\033[1;20;128Hdef': ('H', 'params'),
     }
     for datum, expected in data.items():
         stream.call_win32.reset_mock()
         stream.write_and_convert(datum)
         self.assertEqual(stream.call_win32.call_args[0], expected)
Exemplo n.º 6
0
 def testProxyNoContextManager(self):
     mockStream = MagicMock()
     mockStream.__enter__.side_effect = AttributeError()
     mockConverter = Mock()
     with self.assertRaises(AttributeError) as excinfo:
         with StreamWrapper(mockStream, mockConverter) as wrapper:
             wrapper.write('hello')
Exemplo n.º 7
0
    def testResetAll(self, mockWin32):
        mockAttr = Mock()
        mockAttr.wAttributes = 1 + 2 * 16 + 8
        mockWin32.GetConsoleScreenBufferInfo.return_value = mockAttr
        term = WinTerm()

        term.set_console = Mock()
        term._fore = -1
        term._back = -1
        term._style = -1

        term.reset_all()

        self.assertEqual(term._fore, 1)
        self.assertEqual(term._back, 2)
        self.assertEqual(term._style, 8)
        self.assertEqual(term.set_console.called, True)
Exemplo n.º 8
0
    def testBack(self):
        term = WinTerm()
        term.set_console = Mock()
        term._back = 0

        term.back(5)

        self.assertEqual(term._back, 5)
        self.assertEqual(term.set_console.called, True)
Exemplo n.º 9
0
    def testFore(self):
        term = WinTerm()
        term.set_console = Mock()
        term._fore = 0

        term.fore(5)

        self.assertEqual(term._fore, 5)
        self.assertEqual(term.set_console.called, True)
Exemplo n.º 10
0
    def testStyle(self):
        term = WinTerm()
        term.set_console = Mock()
        term._style = 0

        term.style(22)

        self.assertEqual(term._style, 22)
        self.assertEqual(term.set_console.called, True)
Exemplo n.º 11
0
 def testExtractParams(self):
     stream = AnsiToWin32(Mock())
     data = {
         '': (0, ),
         ';;': (0, ),
         '2': (2, ),
         ';;002;;': (2, ),
         '0;1': (0, 1),
         ';;003;;456;;': (3, 456),
         '11;22;33;44;55': (11, 22, 33, 44, 55),
     }
     for datum, expected in data.items():
         self.assertEqual(stream.extract_params('m', datum), expected)
Exemplo n.º 12
0
 def testCallWin32UsesLookup(self):
     listener = Mock()
     stream = AnsiToWin32(listener)
     stream.win32_calls = {
         1: (lambda *_, **__: listener(11),
             ),
         2: (lambda *_, **__: listener(22),
             ),
         3: (lambda *_, **__: listener(33),
             ),
     }
     stream.call_win32('m', (3, 1, 99, 2))
     self.assertEqual([a[0][0] for a in listener.call_args_list],
                      [33, 11, 22])
Exemplo n.º 13
0
 def testWriteAndConvertStripsAllValidAnsi(self):
     stream = AnsiToWin32(Mock())
     stream.call_win32 = Mock()
     data = [
         'abc\033[mdef',
         'abc\033[0mdef',
         'abc\033[2mdef',
         'abc\033[02mdef',
         'abc\033[002mdef',
         'abc\033[40mdef',
         'abc\033[040mdef',
         'abc\033[0;1mdef',
         'abc\033[40;50mdef',
         'abc\033[50;30;40mdef',
         'abc\033[Adef',
         'abc\033[0Gdef',
         'abc\033[1;20;128Hdef',
     ]
     for datum in data:
         stream.wrapped.write.reset_mock()
         stream.write_and_convert(datum)
         self.assertEqual(
             [args[0] for args in stream.wrapped.write.call_args_list],
             [('abc', ), ('def', )])
Exemplo n.º 14
0
 def testIsAProxy(self):
     mockStream = Mock()
     wrapper = StreamWrapper(mockStream, None)
     self.assertTrue(wrapper.random_attr is mockStream.random_attr)
Exemplo n.º 15
0
 def testWriteAndConvertSkipsEmptySnippets(self):
     stream = AnsiToWin32(Mock())
     stream.call_win32 = Mock()
     stream.write_and_convert('\033[40m\033[41m')
     self.assertFalse(stream.wrapped.write.called)
Exemplo n.º 16
0
 def testStripIsFalseOffWindows(self):
     with osname('posix'):
         mockStdout = Mock(closed=False)
         stream = AnsiToWin32(mockStdout)
         self.assertFalse(stream.strip)
Exemplo n.º 17
0
 def testStripIsTrueOnWindows(self):
     with osname('nt'):
         mockStdout = Mock()
         stream = AnsiToWin32(mockStdout)
         self.assertTrue(stream.strip)
Exemplo n.º 18
0
 def testWriteAndConvertWritesPlainText(self):
     stream = AnsiToWin32(Mock())
     stream.write_and_convert('abc')
     self.assertEqual(stream.wrapped.write.call_args, (('abc', ), {}))
Exemplo n.º 19
0
 def testDelegatesContext(self):
     mockConverter = Mock()
     s = StringIO()
     with StreamWrapper(s, mockConverter) as fp:
         fp.write(u'hello')
     self.assertTrue(s.closed)
Exemplo n.º 20
0
 def testDelegatesWrite(self):
     mockStream = Mock()
     mockConverter = Mock()
     wrapper = StreamWrapper(mockStream, mockConverter)
     wrapper.write('hello')
     self.assertTrue(mockConverter.write.call_args, (('hello', ), {}))
Exemplo n.º 21
0
 def testInit(self):
     mockStdout = Mock()
     auto = Mock()
     stream = AnsiToWin32(mockStdout, autoreset=auto)
     self.assertEqual(stream.wrapped, mockStdout)
     self.assertEqual(stream.autoreset, auto)
Exemplo n.º 22
0
def redirected_output():
    orig = sys.stdout
    sys.stdout = Mock()
    sys.stdout.isatty = lambda: False
    yield
    sys.stdout = orig