1
# -*- coding: utf-8 -*-
2
"""Automated tests (as opposed to human-verified test patterns)
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
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
18
from nose import SkipTest
19
from nose.tools import eq_
21
# This tests that __all__ is correct, since we use below everything that should
23
from blessings import *
26
TestTerminal = partial(Terminal, kind='xterm-256color')
30
"""Return the result of ``tigetstr`` except as Unicode."""
31
return tigetstr(cap).decode('utf-8')
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')
39
def test_capability():
40
"""Check that a capability lookup works.
42
Also test that Terminal grabs a reasonable default stream. This test
43
assumes it will be run from a tty.
47
sc = unicode_cap('sc')
49
eq_(t.save, sc) # Make sure caching doesn't screw it up.
52
def test_capability_without_tty():
53
"""Assert capability templates are '' when stream is not a tty."""
54
t = TestTerminal(stream=StringIO())
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'))
65
def test_parametrization():
66
"""Test parametrizing a capability."""
67
eq_(TestTerminal().cup(3, 4), unicode_parm('cup', 3, 4))
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)
77
def test_stream_attr():
78
"""Make sure Terminal exposes a ``stream`` attribute that defaults to something sane."""
79
eq_(Terminal().stream, sys.__stdout__)
83
"""Make sure ``location()`` does what it claims."""
84
t = TestTerminal(stream=StringIO(), force_styling=True)
86
with t.location(3, 4):
89
eq_(t.stream.getvalue(), unicode_cap('sc') +
90
unicode_parm('cup', 4, 3) +
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)
100
eq_(t.stream.getvalue(), unicode_cap('sc') +
101
unicode_parm('hpa', 5) +
105
def test_null_fileno():
106
"""Make sure ``Terminal`` works when ``fileno`` is ``None``.
108
This simulates piping output to another program.
113
t = TestTerminal(stream=out)
117
def test_mnemonic_colors():
118
"""Make sure color shortcuts work."""
120
return unicode_parm('setaf', num)
123
return unicode_parm('setab', num)
125
# Avoid testing red, blue, yellow, and cyan, since they might someday
126
# change depending on terminal type.
128
eq_(t.white, color(7))
129
eq_(t.green, color(2)) # Make sure it's different than white.
130
eq_(t.on_black, on_color(0))
131
eq_(t.on_green, on_color(2))
132
eq_(t.bright_black, color(8))
133
eq_(t.bright_green, color(10))
134
eq_(t.on_bright_black, on_color(8))
135
eq_(t.on_bright_green, on_color(10))
138
def test_callable_numeric_colors():
139
"""``color(n)`` should return a formatting wrapper."""
141
eq_(t.color(5)('smoo'), t.magenta + 'smoo' + t.normal)
142
eq_(t.color(5)('smoo'), t.color(5) + 'smoo' + t.normal)
143
eq_(t.on_color(2)('smoo'), t.on_green + 'smoo' + t.normal)
144
eq_(t.on_color(2)('smoo'), t.on_color(2) + 'smoo' + t.normal)
147
def test_null_callable_numeric_colors():
148
"""``color(n)`` should be a no-op on null terminals."""
149
t = TestTerminal(stream=StringIO())
150
eq_(t.color(5)('smoo'), 'smoo')
151
eq_(t.on_color(6)('smoo'), 'smoo')
154
def test_naked_color_cap():
155
"""``term.color`` should return a stringlike capability."""
157
eq_(t.color + '', t.setaf + '')
160
def test_number_of_colors_without_tty():
161
"""``number_of_colors`` should return 0 when there's no tty."""
162
# Hypothesis: once setupterm() has run and decided the tty supports 256
163
# colors, it never changes its mind.
166
t = TestTerminal(stream=StringIO())
167
eq_(t.number_of_colors, 0)
168
t = TestTerminal(stream=StringIO(), force_styling=True)
169
eq_(t.number_of_colors, 0)
172
def test_number_of_colors_with_tty():
173
"""``number_of_colors`` should work."""
175
eq_(t.number_of_colors, 256)
178
def test_formatting_functions():
179
"""Test crazy-ass formatting wrappers, both simple and compound."""
181
# By now, it should be safe to use sugared attributes. Other tests test those.
182
eq_(t.bold(u'hi'), t.bold + u'hi' + t.normal)
183
eq_(t.green('hi'), t.green + u'hi' + t.normal) # Plain strs for Python 2.x
184
# Test some non-ASCII chars, probably not necessary:
185
eq_(t.bold_green(u'boö'), t.bold + t.green + u'boö' + t.normal)
186
eq_(t.bold_underline_green_on_red('boo'),
187
t.bold + t.underline + t.green + t.on_red + u'boo' + t.normal)
188
# Don't spell things like this:
189
eq_(t.on_bright_red_bold_bright_green_underline('meh'),
190
t.on_bright_red + t.bold + t.bright_green + t.underline + u'meh' + t.normal)
193
def test_formatting_functions_without_tty():
194
"""Test crazy-ass formatting wrappers when there's no tty."""
195
t = TestTerminal(stream=StringIO())
196
eq_(t.bold(u'hi'), u'hi')
197
eq_(t.green('hi'), u'hi')
198
# Test non-ASCII chars, no longer really necessary:
199
eq_(t.bold_green(u'boö'), u'boö')
200
eq_(t.bold_underline_green_on_red('loo'), u'loo')
201
eq_(t.on_bright_red_bold_bright_green_underline('meh'), u'meh')
204
def test_nice_formatting_errors():
205
"""Make sure you get nice hints if you misspell a formatting wrapper."""
208
t.bold_misspelled('hey')
210
assert 'probably misspelled' in e.args[0]
213
t.bold_misspelled(u'hey') # unicode
215
assert 'probably misspelled' in e.args[0]
218
t.bold_misspelled(None) # an arbitrary non-string
220
assert 'probably misspelled' not in e.args[0]
223
t.bold_misspelled('a', 'b') # >1 string arg
225
assert 'probably misspelled' not in e.args[0]
228
def test_init_descriptor_always_initted():
229
"""We should be able to get a height and width even on no-tty Terminals."""
230
t = Terminal(stream=StringIO())
231
eq_(type(t.height), int)