~ubuntu-branches/ubuntu/trusty/blessings/trusty

« back to all changes in this revision

Viewing changes to blessings/tests.py

  • Committer: Package Import Robot
  • Author(s): David Villa Alises
  • Date: 2013-01-17 01:17:05 UTC
  • Revision ID: package-import@ubuntu.com-20130117011705-9y48hwmnx97h59tv
Tags: upstream-1.5
Import upstream version 1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""Automated tests (as opposed to human-verified test patterns)
 
3
 
 
4
It was tempting to mock out curses to get predictable output from ``tigetstr``,
 
5
but there are concrete integration-testing benefits in not doing so. For
 
6
instance, ``tigetstr`` changed its return type in Python 3.2.3. So instead, we
 
7
simply create all our test ``Terminal`` instances with a known terminal type.
 
8
All we require from the host machine is that a standard terminfo definition of
 
9
xterm-256color exists.
 
10
 
 
11
"""
 
12
from __future__ import with_statement  # Make 2.5-compatible
 
13
from curses import tigetstr, tparm
 
14
from functools import partial
 
15
from StringIO import StringIO
 
16
import sys
 
17
 
 
18
from nose import SkipTest
 
19
from nose.tools import eq_
 
20
 
 
21
# This tests that __all__ is correct, since we use below everything that should
 
22
# be imported:
 
23
from blessings import *
 
24
 
 
25
 
 
26
TestTerminal = partial(Terminal, kind='xterm-256color')
 
27
 
 
28
 
 
29
def unicode_cap(cap):
 
30
    """Return the result of ``tigetstr`` except as Unicode."""
 
31
    return tigetstr(cap).decode('utf-8')
 
32
 
 
33
 
 
34
def unicode_parm(cap, *parms):
 
35
    """Return the result of ``tparm(tigetstr())`` except as Unicode."""
 
36
    return tparm(tigetstr(cap), *parms).decode('utf-8')
 
37
 
 
38
 
 
39
def test_capability():
 
40
    """Check that a capability lookup works.
 
41
 
 
42
    Also test that Terminal grabs a reasonable default stream. This test
 
43
    assumes it will be run from a tty.
 
44
 
 
45
    """
 
46
    t = TestTerminal()
 
47
    sc = unicode_cap('sc')
 
48
    eq_(t.save, sc)
 
49
    eq_(t.save, sc)  # Make sure caching doesn't screw it up.
 
50
 
 
51
 
 
52
def test_capability_without_tty():
 
53
    """Assert capability templates are '' when stream is not a tty."""
 
54
    t = TestTerminal(stream=StringIO())
 
55
    eq_(t.save, u'')
 
56
    eq_(t.red, u'')
 
57
 
 
58
 
 
59
def test_capability_with_forced_tty():
 
60
    """If we force styling, capabilities had better not (generally) be empty."""
 
61
    t = TestTerminal(stream=StringIO(), force_styling=True)
 
62
    eq_(t.save, unicode_cap('sc'))
 
63
 
 
64
 
 
65
def test_parametrization():
 
66
    """Test parametrizing a capability."""
 
67
    eq_(TestTerminal().cup(3, 4), unicode_parm('cup', 3, 4))
 
68
 
 
69
 
 
70
def height_and_width():
 
71
    """Assert that ``height_and_width()`` returns ints."""
 
72
    t = TestTerminal()  # kind shouldn't matter.
 
73
    assert isinstance(int, t.height)
 
74
    assert isinstance(int, t.width)
 
75
 
 
76
 
 
77
def test_stream_attr():
 
78
    """Make sure Terminal exposes a ``stream`` attribute that defaults to something sane."""
 
79
    eq_(Terminal().stream, sys.__stdout__)
 
80
 
 
81
 
 
82
def test_location():
 
83
    """Make sure ``location()`` does what it claims."""
 
84
    t = TestTerminal(stream=StringIO(), force_styling=True)
 
85
 
 
86
    with t.location(3, 4):
 
87
        t.stream.write(u'hi')
 
88
 
 
89
    eq_(t.stream.getvalue(), unicode_cap('sc') +
 
90
                             unicode_parm('cup', 4, 3) +
 
91
                             u'hi' +
 
92
                             unicode_cap('rc'))
 
93
 
 
94
 
 
95
def test_horizontal_location():
 
96
    """Make sure we can move the cursor horizontally without changing rows."""
 
97
    t = TestTerminal(stream=StringIO(), force_styling=True)
 
98
    with t.location(x=5):
 
99
        pass
 
100
    eq_(t.stream.getvalue(), unicode_cap('sc') +
 
101
                             unicode_parm('hpa', 5) +
 
102
                             unicode_cap('rc'))
 
103
 
 
104
 
 
105
def test_null_location():
 
106
    """Make sure ``location()`` with no args just does position restoration."""
 
107
    t = TestTerminal(stream=StringIO(), force_styling=True)
 
108
    with t.location():
 
109
        pass
 
110
    eq_(t.stream.getvalue(), unicode_cap('sc') +
 
111
                             unicode_cap('rc'))
 
112
 
 
113
 
 
114
def test_zero_location():
 
115
    """Make sure ``location()`` pays attention to 0-valued args."""
 
116
    t = TestTerminal(stream=StringIO(), force_styling=True)
 
117
    with t.location(0, 0):
 
118
        pass
 
119
    eq_(t.stream.getvalue(), unicode_cap('sc') +
 
120
                             unicode_parm('cup', 0, 0) +
 
121
                             unicode_cap('rc'))
 
122
 
 
123
 
 
124
def test_null_fileno():
 
125
    """Make sure ``Terminal`` works when ``fileno`` is ``None``.
 
126
 
 
127
    This simulates piping output to another program.
 
128
 
 
129
    """
 
130
    out = StringIO()
 
131
    out.fileno = None
 
132
    t = TestTerminal(stream=out)
 
133
    eq_(t.save, u'')
 
134
 
 
135
 
 
136
def test_mnemonic_colors():
 
137
    """Make sure color shortcuts work."""
 
138
    def color(num):
 
139
        return unicode_parm('setaf', num)
 
140
 
 
141
    def on_color(num):
 
142
        return unicode_parm('setab', num)
 
143
 
 
144
    # Avoid testing red, blue, yellow, and cyan, since they might someday
 
145
    # change depending on terminal type.
 
146
    t = TestTerminal()
 
147
    eq_(t.white, color(7))
 
148
    eq_(t.green, color(2))  # Make sure it's different than white.
 
149
    eq_(t.on_black, on_color(0))
 
150
    eq_(t.on_green, on_color(2))
 
151
    eq_(t.bright_black, color(8))
 
152
    eq_(t.bright_green, color(10))
 
153
    eq_(t.on_bright_black, on_color(8))
 
154
    eq_(t.on_bright_green, on_color(10))
 
155
 
 
156
 
 
157
def test_callable_numeric_colors():
 
158
    """``color(n)`` should return a formatting wrapper."""
 
159
    t = TestTerminal()
 
160
    eq_(t.color(5)('smoo'), t.magenta + 'smoo' + t.normal)
 
161
    eq_(t.color(5)('smoo'), t.color(5) + 'smoo' + t.normal)
 
162
    eq_(t.on_color(2)('smoo'), t.on_green + 'smoo' + t.normal)
 
163
    eq_(t.on_color(2)('smoo'), t.on_color(2) + 'smoo' + t.normal)
 
164
 
 
165
 
 
166
def test_null_callable_numeric_colors():
 
167
    """``color(n)`` should be a no-op on null terminals."""
 
168
    t = TestTerminal(stream=StringIO())
 
169
    eq_(t.color(5)('smoo'), 'smoo')
 
170
    eq_(t.on_color(6)('smoo'), 'smoo')
 
171
 
 
172
 
 
173
def test_naked_color_cap():
 
174
    """``term.color`` should return a stringlike capability."""
 
175
    t = TestTerminal()
 
176
    eq_(t.color + '', t.setaf + '')
 
177
 
 
178
 
 
179
def test_number_of_colors_without_tty():
 
180
    """``number_of_colors`` should return 0 when there's no tty."""
 
181
    # Hypothesis: once setupterm() has run and decided the tty supports 256
 
182
    # colors, it never changes its mind.
 
183
    raise SkipTest
 
184
 
 
185
    t = TestTerminal(stream=StringIO())
 
186
    eq_(t.number_of_colors, 0)
 
187
    t = TestTerminal(stream=StringIO(), force_styling=True)
 
188
    eq_(t.number_of_colors, 0)
 
189
 
 
190
 
 
191
def test_number_of_colors_with_tty():
 
192
    """``number_of_colors`` should work."""
 
193
    t = TestTerminal()
 
194
    eq_(t.number_of_colors, 256)
 
195
 
 
196
 
 
197
def test_formatting_functions():
 
198
    """Test crazy-ass formatting wrappers, both simple and compound."""
 
199
    t = TestTerminal()
 
200
    # By now, it should be safe to use sugared attributes. Other tests test those.
 
201
    eq_(t.bold(u'hi'), t.bold + u'hi' + t.normal)
 
202
    eq_(t.green('hi'), t.green + u'hi' + t.normal)  # Plain strs for Python 2.x
 
203
    # Test some non-ASCII chars, probably not necessary:
 
204
    eq_(t.bold_green(u'boö'), t.bold + t.green + u'boö' + t.normal)
 
205
    eq_(t.bold_underline_green_on_red('boo'),
 
206
        t.bold + t.underline + t.green + t.on_red + u'boo' + t.normal)
 
207
    # Don't spell things like this:
 
208
    eq_(t.on_bright_red_bold_bright_green_underline('meh'),
 
209
        t.on_bright_red + t.bold + t.bright_green + t.underline + u'meh' + t.normal)
 
210
 
 
211
 
 
212
def test_formatting_functions_without_tty():
 
213
    """Test crazy-ass formatting wrappers when there's no tty."""
 
214
    t = TestTerminal(stream=StringIO())
 
215
    eq_(t.bold(u'hi'), u'hi')
 
216
    eq_(t.green('hi'), u'hi')
 
217
    # Test non-ASCII chars, no longer really necessary:
 
218
    eq_(t.bold_green(u'boö'), u'boö')
 
219
    eq_(t.bold_underline_green_on_red('loo'), u'loo')
 
220
    eq_(t.on_bright_red_bold_bright_green_underline('meh'), u'meh')
 
221
 
 
222
 
 
223
def test_nice_formatting_errors():
 
224
    """Make sure you get nice hints if you misspell a formatting wrapper."""
 
225
    t = TestTerminal()
 
226
    try:
 
227
        t.bold_misspelled('hey')
 
228
    except TypeError, e:
 
229
        assert 'probably misspelled' in e.args[0]
 
230
 
 
231
    try:
 
232
        t.bold_misspelled(u'hey')  # unicode
 
233
    except TypeError, e:
 
234
        assert 'probably misspelled' in e.args[0]
 
235
 
 
236
    try:
 
237
        t.bold_misspelled(None)  # an arbitrary non-string
 
238
    except TypeError, e:
 
239
        assert 'probably misspelled' not in e.args[0]
 
240
 
 
241
    try:
 
242
        t.bold_misspelled('a', 'b')  # >1 string arg
 
243
    except TypeError, e:
 
244
        assert 'probably misspelled' not in e.args[0]
 
245
 
 
246
 
 
247
def test_init_descriptor_always_initted():
 
248
    """We should be able to get a height and width even on no-tty Terminals."""
 
249
    t = Terminal(stream=StringIO())
 
250
    eq_(type(t.height), int)
 
251
 
 
252
 
 
253
def test_force_styling_none():
 
254
    """If ``force_styling=None`` is passed to the constructor, don't ever do styling."""
 
255
    t = TestTerminal(force_styling=None)
 
256
    eq_(t.save, '')