Esempio n. 1
0
    def test_add_indent(self, mock_stdout):
        term = Terminal()

        i = 6
        expected_msg = ' ' * i + 'foo\n'

        term.add_indent(i)
        term.print('foo')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        j = 4
        expected_msg = ' ' * (i + j) + 'bar\n'

        term.add_indent(j)
        term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)
Esempio n. 2
0
    def test_add_indent(self):
        term = Terminal()
        term.add_indent(6)
        term.print('foo')

        # printed output at current indent location
        self.terminal_mock.location.assert_called_with(x=6)

        # an actual output has been generated
        self.print_mock.assert_called_with('foo')

        term.add_indent(4)
        term.print('bar')

        # printed output at current indent location
        self.terminal_mock.location.assert_called_with(x=10)

        # an actual output has been generated
        self.print_mock.assert_called_with('bar')
Esempio n. 3
0
class TerminalTestCase(unittest.TestCase):
    def setUp(self):
        self.maxDiff = 180
        # getting the bash-color-codes from the colorful module
        self.red = cf.red
        self.green = cf.green
        self.yellow = cf.yellow
        self.cyan = cf.cyan
        self.reset = cf.reset
        self.bold = cf.bold
        # every colors second value is the reset value ...
        self.term = Terminal()
        self.term.get_width = MagicMock(return_value=80)

    @patch('sys.stdout', new_callable=StringIO)
    def test_error(self, mock_stdout):
        status = '{} '.format(self.red(Signs.ERROR))
        msg = 'foo bar'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.error(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_fail(self, mock_stdout):
        status = '{} '.format(self.red(Signs.FAIL))
        msg = 'foo bar baz'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.fail(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_info(self, mock_stdout):
        status = '{} '.format(self.cyan(Signs.INFO))
        msg = 'foo bar'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.info(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_bold_info(self, mock_stdout):
        status = '{} '.format(self.cyan(Signs.INFO))
        msg = 'bold foo bar'

        expected_msg = (self.bold('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.bold_info(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_ok(self, mock_stdout):
        status = '{} '.format(self.green(Signs.OK))
        msg = 'foo bar'

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.ok(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_warning(self, mock_stdout):
        msg = 'foo bar'

        status = '{} '.format(self.yellow(Signs.WARNING))

        expected_msg = (self.reset('{}{}'.format(status, msg)).styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.warning(msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)

    @patch('sys.stdout', new_callable=StringIO)
    def test_print(self, mock_stdout):
        expected_msg = self.reset('  foo bar').styled_string + '\n'

        self.term.print('foo bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

    @patch('sys.stdout', new_callable=StringIO)
    def test_add_indent(self, mock_stdout):
        i = 6
        expected_msg = self.reset(' ' * i + 'foo').styled_string + '\n'

        self.term.add_indent(i - 2)
        self.term.print('foo')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        j = 4
        expected_msg = self.reset(' ' * (i + j) + 'bar').styled_string + '\n'

        self.term.add_indent(j)
        self.term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

    @patch('sys.stdout', new_callable=StringIO)
    def test_reset_indent(self, mock_stdout):
        i = 6
        expected_msg = self.reset(' ' * i + 'foo').styled_string + '\n'

        self.term.add_indent(i - 2)
        self.term.print('foo')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        expected_msg = self.reset('  bar').styled_string + '\n'

        self.term.reset_indent()
        self.term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

    @patch('sys.stdout', new_callable=StringIO)
    def test_with_indent(self, mock_stdout):
        expected_msg = self.reset('    foo').styled_string + '\n'

        with self.term.indent(2):
            self.term.print('foo')

            ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

        # clear the buffer
        mock_stdout.truncate(0)
        mock_stdout.seek(0)

        expected_msg = self.reset('  bar').styled_string + '\n'
        self.term.print('bar')

        ret = mock_stdout.getvalue()

        self.assertEqual(len(ret), len(expected_msg))
        self.assertEqual(ret, expected_msg)

    @patch('sys.stdout', new_callable=StringIO)
    def test_long_msg(self, mock_stdout):
        long_msg = (
            'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '
            'sed diam nonumy eirmod tempor invidunt ut labore et dolore magna'
            ' aliquyam erat, sed diam voluptua.')
        expected_msg = (self.reset(
            '  Lorem ipsum dolor sit amet, consetetur sadipscing elitr, '
            'sed diam nonumy eirmo\n  d tempor invidunt ut labore et'
            ' dolore magna aliquyam erat, sed diam voluptua.').styled_string +
                        '\n')
        expected_len = len(expected_msg)

        self.term.print(long_msg)

        ret = mock_stdout.getvalue()

        self.assertEqual(ret, expected_msg)
        self.assertEqual(len(ret), expected_len)