1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
# -*- coding: utf-8 -*-
# Copyright 2017 - 2022 Avram Lubkin, All Rights Reserved
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
Test module for enlighten._util
"""
from textwrap import dedent
import blessed
from enlighten._util import format_time, Lookahead, HTMLConverter
from tests import TestCase, MockTTY
class TestFormatTime(TestCase):
"""
Test cases for :py:func:`_format_time`
"""
def test_seconds(self):
"""Verify seconds formatting"""
self.assertEqual(format_time(0), '00:00')
self.assertEqual(format_time(6), '00:06')
self.assertEqual(format_time(42), '00:42')
def test_minutes(self):
"""Verify minutes formatting"""
self.assertEqual(format_time(60), '01:00')
self.assertEqual(format_time(128), '02:08')
self.assertEqual(format_time(1684), '28:04')
def test_hours(self):
"""Verify hours formatting"""
self.assertEqual(format_time(3600), '1h 00:00')
self.assertEqual(format_time(43980), '12h 13:00')
self.assertEqual(format_time(43998), '12h 13:18')
def test_days(self):
"""Verify days formatting"""
self.assertEqual(format_time(86400), '1d 0h 00:00')
self.assertEqual(format_time(1447597), '16d 18h 06:37')
class TestLookahead(TestCase):
"""
Test cases for Lookahead
"""
def test_iteration(self):
"""Verify normal iteration"""
wrapped = Lookahead(iter(range(10)))
self.assertEqual([next(wrapped) for _ in range(10)], list(range(10)))
def test_getitem(self):
"""Verify __getitem__ behavior"""
wrapped = Lookahead(iter(range(10)))
self.assertEqual(wrapped[0], 0)
self.assertEqual(wrapped[4], 4)
self.assertEqual(wrapped[2: 4], [2, 3])
self.assertEqual(wrapped[8: 12], [8, 9])
with self.assertRaisesRegex(TypeError, 'Index or slice notation is required'):
wrapped['named_key'] # pylint: disable=pointless-statement
with self.assertRaisesRegex(ValueError, 'Negative indexes are not supported'):
wrapped[-1] # pylint: disable=pointless-statement
def test_buffer(self):
"""Output changes as iteration proceeds"""
wrapped = Lookahead(iter(range(10)))
self.assertEqual(next(wrapped), 0)
self.assertEqual(wrapped[0], 1)
self.assertEqual(next(wrapped), 1)
self.assertEqual(wrapped[4], 6)
self.assertEqual(next(wrapped), 2)
self.assertEqual(wrapped[2: 4], [5, 6])
self.assertEqual(next(wrapped), 3)
self.assertEqual(wrapped[8: 12], [])
def test_step_notation(self):
"""Slice notation is supported"""
wrapped = Lookahead(iter(range(10)))
self.assertEqual(wrapped[: 6: 2], [0, 2, 4])
class TestHTMLConverter(TestCase):
"""
Test cases for HTMLConverter
"""
# pylint: disable=protected-access
@classmethod
def setUpClass(cls):
cls.tty = MockTTY()
cls.term = blessed.Terminal(
stream=cls.tty.stdout, force_styling=True
)
cls.term.number_of_colors = 1 << 24
@classmethod
def tearDownClass(cls):
cls.tty.close()
def setUp(self):
self.converter = HTMLConverter(term=self.term)
def test_color(self):
"""Verify color conversion"""
# CGA color on RGB color
out = self.converter.to_html(self.term.blue_on_aquamarine('blue_on_aquam'))
self.assertEqual(
out,
u'<pre><span class="enlighten-fg-blue enlighten-bg-aquamarine">blue_on_aquam</span></pre>'
)
self.assertEqual(self.converter._styles['enlighten-fg-blue'], {'color': '#0000ee'})
self.assertEqual(
self.converter._styles['enlighten-bg-aquamarine'], {'background-color': '#7fffd4'}
)
# RGB color on CGA color
out = self.converter.to_html(self.term.aquamarine_on_blue('aquam_on_blue'))
self.assertEqual(
out,
u'<pre><span class="enlighten-fg-aquamarine enlighten-bg-blue">aquam_on_blue</span></pre>'
)
self.assertEqual(self.converter._styles['enlighten-fg-aquamarine'], {'color': '#7fffd4'})
self.assertEqual(
self.converter._styles['enlighten-bg-blue'], {'background-color': '#0000ee'}
)
# On RGB color
out = self.converter.to_html(self.term.on_color_rgb(80, 4, 13)('on_color_rgb'))
self.assertEqual(out, '<pre><span class="enlighten-bg-50040d">on_color_rgb</span></pre>')
self.assertEqual(
self.converter._styles['enlighten-bg-50040d'], {'background-color': '#50040d'}
)
# 256 Color
out = self.converter.to_html(self.term.color(90)('color_90'))
self.assertEqual(out, '<pre><span class="enlighten-fg-870087">color_90</span></pre>')
self.assertEqual(self.converter._styles['enlighten-fg-870087'], {'color': '#870087'})
# On 256 Color
out = self.converter.to_html(self.term.on_color(90)('on_color_90'))
self.assertEqual(out, '<pre><span class="enlighten-bg-870087">on_color_90</span></pre>')
self.assertEqual(
self.converter._styles['enlighten-bg-870087'], {'background-color': '#870087'}
)
# CGA Bright Color
out = self.converter.to_html(self.term.bright_red('bright_red'))
self.assertEqual(out, '<pre><span class="enlighten-fg-bright-red">bright_red</span></pre>')
self.assertEqual(self.converter._styles['enlighten-fg-bright-red'], {'color': '#ff0000'})
# On CGA Bright Color
out = self.converter.to_html(self.term.on_bright_red('on_bright_red'))
self.assertEqual(
out,
'<pre><span class="enlighten-bg-bright-red">on_bright_red</span></pre>'
)
self.assertEqual(
self.converter._styles['enlighten-bg-bright-red'], {'background-color': '#ff0000'}
)
def test_style(self):
"""Verify style conversion"""
# Italics
out = self.converter.to_html(self.term.italic('italic'))
self.assertEqual(out, '<pre><span class="enlighten-italic">italic</span></pre>')
self.assertEqual(self.converter._styles['enlighten-italic'], {'font-style': 'italic'})
# Bold
out = self.converter.to_html(self.term.bold('bold'))
self.assertEqual(out, '<pre><span class="enlighten-bold">bold</span></pre>')
self.assertEqual(self.converter._styles['enlighten-bold'], {'font-weight': 'bold'})
# Underline
out = self.converter.to_html(self.term.underline('underline'))
self.assertEqual(out, '<pre><span class="enlighten-underline">underline</span></pre>')
self.assertEqual(
self.converter._styles['enlighten-underline'], {'text-decoration': 'underline'}
)
def test_unsupported(self):
"""Verify unsupported does not produce classes"""
# Unsupported capability
out = self.converter.to_html(self.term.move(5, 6) + 'unsupported_move')
self.assertEqual(out, '<pre>unsupported_move</pre>')
# Unsupported text attribute
out = self.converter.to_html(self.term.reverse('unsupported_reverse'))
self.assertEqual(out, '<pre>unsupported_reverse</pre>')
def test_link(self):
"""Verify link creates hyperlink"""
out = self.converter.to_html(
self.term.link('https://pypi.org/project/enlighten/', 'enlighten')
)
self.assertEqual(
out,
'<pre><a href="https://pypi.org/project/enlighten/">enlighten<a></pre>'
)
def test_empty_span(self):
"""Empty Spans are ignored"""
out = self.converter.to_html(self.term.underline('') + 'empty')
self.assertEqual(out, '<pre>empty</pre>')
def test_class_not_unique(self):
"""Repeated classes are dropped within the same span"""
out = self.converter.to_html(self.term.blue_on_aquamarine(self.term.blue('blue_on_aquam')))
self.assertEqual(
out,
u'<pre><span class="enlighten-fg-blue enlighten-bg-aquamarine">blue_on_aquam</span></pre>'
)
def test_style_output(self):
"""Verify style section output"""
out = self.converter.to_html(self.term.red_on_slategrey('red_on_slategrey'))
self.assertEqual(
out,
u'<pre><span class="enlighten-fg-red enlighten-bg-slategray">red_on_slategrey</span></pre>'
)
style = '''\
<style>
.enlighten-fg-red {
color: #cd0000;
}
.enlighten-bg-slategray {
background-color: #708090;
}
</style>
'''
self.assertEqual(self.converter.style, dedent(style))
def test_blink(self):
"""Blink requires an additional style section"""
if not self.term.blink:
self.skipTest('blink is not supported by this terminal')
out = self.converter.to_html(self.term.blink('blink'))
self.assertEqual(out, '<pre><span class="enlighten-blink">blink</span></pre>')
self.assertEqual(
self.converter._additional_styles,
{'@keyframes enlighten-blink-animation {\n to {\n visibility: hidden;\n }\n}'}
)
self.assertEqual(
self.converter._styles['enlighten-blink'],
{'animation': 'enlighten-blink-animation 1s steps(5, start) infinite'}
)
style = '''\
<style>
.enlighten-blink {
animation: enlighten-blink-animation 1s steps(5, start) infinite;
}
@keyframes enlighten-blink-animation {
to {
visibility: hidden;
}
}
</style>
'''
self.assertEqual(self.converter.style, dedent(style))
|