summaryrefslogtreecommitdiff
path: root/blessings/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'blessings/tests.py')
-rw-r--r--blessings/tests.py69
1 files changed, 39 insertions, 30 deletions
diff --git a/blessings/tests.py b/blessings/tests.py
index d8b5fcd..5fcfb18 100644
--- a/blessings/tests.py
+++ b/blessings/tests.py
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement # Make 2.5-compatible
-from StringIO import StringIO
+try:
+ from io import BytesIO
+except ImportError:
+ from StringIO import StringIO as BytesIO
from curses import tigetstr, tparm
import sys
@@ -33,14 +36,14 @@ def test_capability():
def test_capability_without_tty():
"""Assert capability templates are '' when stream is not a tty."""
- t = Terminal(stream=StringIO())
- eq_(t.save, Capability(''))
- eq_(t.red, Capability(''))
+ t = Terminal(stream=BytesIO())
+ eq_(t.save, Capability(''.encode('utf-8')))
+ eq_(t.red, Capability(''.encode('utf-8')))
def test_capability_with_forced_tty():
"""If we force styling, capabilities had better not (generally) be empty."""
- t = Terminal(stream=StringIO(), force_styling=True)
+ t = Terminal(stream=BytesIO(), force_styling=True)
assert len(t.save) > 0
@@ -63,19 +66,19 @@ def test_stream_attr():
def test_location():
"""Make sure ``location()`` does what it claims."""
- t = Terminal(stream=StringIO(), force_styling=True)
+ t = Terminal(stream=BytesIO(), force_styling=True)
with t.location(3, 4):
- t.stream.write('hi')
+ t.stream.write('hi'.encode(t.encoding))
eq_(t.stream.getvalue(), tigetstr('sc') +
tparm(tigetstr('cup'), 4, 3) +
- 'hi' + # TODO: Encode with Terminal's encoding.
+ 'hi'.encode(t.encoding) +
tigetstr('rc'))
def test_horizontal_location():
"""Make sure we can move the cursor horizontally without changing rows."""
- t = Terminal(stream=StringIO(), force_styling=True)
+ t = Terminal(stream=BytesIO(), force_styling=True)
with t.location(x=5):
pass
eq_(t.stream.getvalue(), t.save + tparm(tigetstr('hpa'), 5) + t.restore)
@@ -87,50 +90,56 @@ def test_null_fileno():
This simulates piping output to another program.
"""
- out = stream=StringIO()
+ out = stream=BytesIO()
out.fileno = None
t = Terminal(stream=out)
- bytes_eq(t.save, '')
+ eq_(t.save, ''.encode('utf-8'))
def test_mnemonic_colors():
"""Make sure color shortcuts work."""
+ def color(num):
+ return tparm(tigetstr('setaf'), num)
+
+ def on_color(num):
+ return tparm(tigetstr('setab'), num)
+
# Avoid testing red, blue, yellow, and cyan, since they might someday
# chance depending on terminal type.
t = Terminal()
- bytes_eq(t.white, '\x1b[37m')
- bytes_eq(t.green, '\x1b[32m') # Make sure it's different than white.
- bytes_eq(t.on_black, '\x1b[40m')
- bytes_eq(t.on_green, '\x1b[42m')
- bytes_eq(t.bright_black, '\x1b[90m')
- bytes_eq(t.bright_green, '\x1b[92m')
- bytes_eq(t.on_bright_black, '\x1b[100m')
- bytes_eq(t.on_bright_green, '\x1b[102m')
+ eq_(t.white, color(7))
+ bytes_eq(t.green, color(2)) # Make sure it's different than white.
+ bytes_eq(t.on_black, on_color(0))
+ bytes_eq(t.on_green, on_color(2))
+ bytes_eq(t.bright_black, color(8))
+ bytes_eq(t.bright_green, color(10))
+ bytes_eq(t.on_bright_black, on_color(8))
+ bytes_eq(t.on_bright_green, on_color(10))
def test_formatting_functions():
"""Test crazy-ass formatting wrappers, both simple and compound."""
t = Terminal(encoding='utf-8')
- eq_(t.bold('hi'), t.bold + 'hi' + t.normal)
- eq_(t.green('hi'), t.green + 'hi' + t.normal)
+ eq_(t.bold('hi'), t.bold + 'hi'.encode('utf-8') + t.normal)
+ eq_(t.green('hi'), t.green + 'hi'.encode('utf-8') + t.normal)
# Test encoding of unicodes:
- eq_(t.bold_green(u'boö'), t.bold + t.green + u'boö'.encode('utf-8') + t.normal)
+ eq_(t.bold_green('boö'), t.bold + t.green + 'boö'.encode('utf-8') + t.normal)
eq_(t.bold_underline_green_on_red('boo'),
- t.bold + t.underline + t.green + t.on_red + 'boo' + t.normal)
+ t.bold + t.underline + t.green + t.on_red + 'boo'.encode('utf-8') + t.normal)
# Don't spell things like this:
eq_(t.on_bright_red_bold_bright_green_underline('meh'),
- t.on_bright_red + t.bold + t.bright_green + t.underline + 'meh' + t.normal)
+ t.on_bright_red + t.bold + t.bright_green + t.underline + 'meh'.encode('utf-8') + t.normal)
def test_formatting_functions_without_tty():
"""Test crazy-ass formatting wrappers when there's no tty."""
- t = Terminal(stream=StringIO())
- eq_(t.bold('hi'), 'hi')
- eq_(t.green('hi'), 'hi')
+ t = Terminal(stream=BytesIO())
+ eq_(t.bold('hi'), 'hi'.encode('utf-8'))
+ eq_(t.green('hi'), 'hi'.encode('utf-8'))
# Test encoding of unicodes:
- eq_(t.bold_green(u'boö'), u'boö'.encode('utf-8')) # unicode
- eq_(t.bold_underline_green_on_red('boo'), 'boo')
- eq_(t.on_bright_red_bold_bright_green_underline('meh'), 'meh')
+ eq_(t.bold_green('boö'), 'boö'.encode('utf-8')) # unicode
+ eq_(t.bold_underline_green_on_red('boo'), 'boo'.encode('utf-8'))
+ eq_(t.on_bright_red_bold_bright_green_underline('meh'), 'meh'.encode('utf-8'))
def test_nice_formatting_errors():