~frankban/juju-quickstart/check-locale

« back to all changes in this revision

Viewing changes to quickstart/tests/cli/test_base.py

  • Committer: Francesco Banconi
  • Date: 2014-04-24 14:47:18 UTC
  • Revision ID: francesco.banconi@canonical.com-20140424144718-f6snuob5b3ze7v7v
Done.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from __future__ import unicode_literals
20
20
 
 
21
from contextlib import contextmanager
21
22
import unittest
22
23
 
 
24
import mock
23
25
import urwid
24
26
 
25
27
from quickstart.cli import base
26
28
from quickstart.tests.cli import helpers as cli_helpers
27
29
 
28
30
 
 
31
class TestCheckEncoding(unittest.TestCase):
 
32
 
 
33
    @contextmanager
 
34
    def patch_urwid(self, supports_unicode=True):
 
35
        """Patch the urwid.set_encoding and urwid.supports_unicode calls.
 
36
 
 
37
        Ensure the mock functions are called once with the expected arguments.
 
38
 
 
39
        Make the latter return the given supports_unicode argument.
 
40
        """
 
41
        mock_supports_unicode = mock.Mock(return_value=supports_unicode)
 
42
        with mock.patch('urwid.set_encoding') as mock_set_encoding:
 
43
            with mock.patch('urwid.supports_unicode', mock_supports_unicode):
 
44
                yield
 
45
        mock_set_encoding.assert_called_once_with('utf-8')
 
46
        mock_supports_unicode.assert_called_once_with()
 
47
 
 
48
    def test_unicode_supported(self):
 
49
        # The utf-8 encoding is properly set and the function exits without
 
50
        # errors.
 
51
        with self.patch_urwid(supports_unicode=True):
 
52
            base._check_encoding()
 
53
 
 
54
    def test_unicode_not_supported(self):
 
55
        # If unicode is not supported, the program exits with an error.
 
56
        with self.patch_urwid(supports_unicode=False):
 
57
            with self.assertRaises(SystemExit) as context_manager:
 
58
                base._check_encoding()
 
59
        self.assertIn(
 
60
            b'Error: your terminal does not seem to support UTF-8 encoding.',
 
61
            bytes(context_manager.exception))
 
62
 
 
63
 
29
64
class TestMainLoop(unittest.TestCase):
30
65
 
31
66
    def setUp(self):