From c1589b3aab168d68a169750b9f378a59665d5ff5 Mon Sep 17 00:00:00 2001 From: Erik Rose Date: Tue, 22 Nov 2011 14:50:10 -0800 Subject: Get the remaining tests passing in Python 3. Mostly just dealt with unicode issues. Still ugly in some places. --- blessings/__init__.py | 18 +++++++------- blessings/tests.py | 69 +++++++++++++++++++++++++++++---------------------- 2 files changed, 48 insertions(+), 39 deletions(-) diff --git a/blessings/__init__.py b/blessings/__init__.py index 6a099ef..7cbfd19 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -108,7 +108,7 @@ class Terminal(object): # that.] At any rate, save redoing the work of _resolve_formatter(). self._codes = {} else: - self._codes = NullDict(lambda: NullCap('', self.encoding)) + self._codes = NullDict(lambda: NullCap(self.encoding)) self.stream = stream @@ -234,16 +234,16 @@ class Terminal(object): if all(f in COMPOUNDABLES for f in formatters): # It's a compound formatter, like "bold_green_on_red". Future # optimization: combine all formatting into a single escape - # sequence - return FormattingCap(''.join(self._resolve_formatter(s) - for s in formatters), - self) + # sequence. + return FormattingCap( + Capability().join(self._resolve_formatter(s) for s in formatters), + self) else: return ParametrizingCap(self._resolve_capability(attr)) def _resolve_capability(self, atom): """Return a terminal code for a capname or a sugary name, or ''.""" - return tigetstr(self._sugar.get(atom, atom)) or '' + return tigetstr(self._sugar.get(atom, atom)) or Capability() def _resolve_color(self, color): """Resolve a color like red or on_bright_green into a callable capability.""" @@ -327,12 +327,12 @@ class NullCap(Capability): """A dummy class to stand in for ``FormattingCap`` and ``ParametrizingCap`` A callable bytestring that returns ``''`` when called with an int and the - arg otherwise. We use this when tehre is no tty and so all capabilities are + arg otherwise. We use this when there is no tty and so all capabilities are blank. """ - def __new__(cls, cap, encoding): - new = Capability.__new__(cls, cap) + def __new__(cls, encoding): + new = Capability.__new__(cls, Capability()) new._encoding = encoding return new 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(): -- cgit v1.2.1