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
|
import unittest
import os
import sys
from unittest.mock import MagicMock
from asciimatics.renderers import ImageFile, ColourImageFile
from asciimatics.screen import Screen, Canvas
if sys.platform != "win32":
import curses
class TestRendererImages(unittest.TestCase):
def test_image_files(self):
"""
Check that the ImageFile renderer works.
"""
renderer = ImageFile(
os.path.join(os.path.dirname(__file__), "globe.gif"), height=10)
# Check renderer got all images from the file.
count = 0
for image in renderer.images:
count += 1
self.assertIsNotNone(image)
self.assertIsNotNone(len(image) <= renderer.max_height)
self.assertEqual(count, 11)
# Check an image looks plausible
image = next(renderer.images)
self.maxDiff = None
self.assertEqual(
image,
[' sA3h3h3Hr2 ',
' ;:;G#99G@&3;;;r ',
' :::#9&&@@G;rrrr;;3 ',
'.:;;A&@AAGsssssrr;#H',
'::;;;r39@srssssrr;A2',
'.::;;rrrrr@@@@9;r;;A',
's::;;;;rr2@@@@@@#;; ',
' s:::;;;;;;9&&&3;: ',
' .:::;;;;9#r::2 ',
' s.::r:; '])
def test_image_files_256(self):
"""
Check that the ImageFile renderer works.
"""
renderer = ImageFile(
os.path.join(os.path.dirname(__file__), "globe.gif"), height=10, colours=256)
# Check an image looks plausible
image = next(renderer.images)
self.maxDiff = None
self.assertEqual(
image,
[' sA3h3h3Hr2 ',
' ;:;G#99G@&3;;;r ',
' :::#9&&@@G;rrrr;;3 ',
'.:;;A&@AAGsssssrr;#H',
'::;;;r39@srssssrr;A2',
'.::;;rrrrr@@@@9;r;;A',
's::;;;;rr2@@@@@@#;; ',
' s:::;;;;;;9&&&3;: ',
' .:::;;;;9#r::2 ',
' s.::r:; '])
def test_colour_image_file(self):
"""
Check that the ColourImageFile renderer works.
"""
def internal_checks(screen):
# Check the original FG only rendering
renderer = ColourImageFile(
screen,
os.path.join(os.path.dirname(__file__), "globe.gif"),
height=10)
# Check renderer got all images from the file.
count = 0
for image in renderer.images:
count += 1
self.assertIsNotNone(image)
self.assertIsNotNone(len(image) <= renderer.max_height)
self.assertEqual(count, 11)
# Check an image looks plausible
image = next(renderer.images)
self.maxDiff = None
self.assertEqual(
image,
[' ########## ',
' ############### ',
' ################## ',
'####################',
'####################',
'####################',
'################### ',
' ################# ',
' ############## ',
' ####### '])
# Also check the BG rendering
renderer2 = ColourImageFile(
screen,
os.path.join(os.path.dirname(__file__), "globe.gif"),
fill_background=True,
height=10)
# Check BG rendering doesn't change the visible text output.
# Note that BG rendering needs to print dots for some terminals.
image2 = [x.replace(".", " ") for x in next(renderer2.images)]
self.assertEqual(image, image2)
# Check BG rendering gives same colours for FG and BG as original
# rendering
for a, b in zip(renderer.rendered_text[1],
renderer2.rendered_text[1]):
for attr1, attr2 in zip(a, b):
if attr1[0] is None:
self.assertEqual(0, attr2[0])
self.assertEqual(0, attr2[2])
else:
self.assertEqual(attr1[0], attr2[0])
self.assertEqual(attr2[0], attr2[2])
# Mock screen for better coverage.
screen = MagicMock(spec=Screen, colours=16, unicode_aware=True, palette=Screen._8_palette)
canvas = Canvas(screen, 15, 40, 0, 0)
internal_checks(screen)
def test_uni_image_files(self):
"""
Check that the unicode ColourImageFile rendering works.
"""
def internal_checks(screen):
# Check the original FG only rendering
renderer = ColourImageFile(
screen,
os.path.join(os.path.dirname(__file__), "globe.gif"),
height=10, uni=True, dither=True)
# Check renderer got all images from the file.
count = 0
for image in renderer.images:
count += 1
self.assertIsNotNone(image)
self.assertIsNotNone(len(image) <= renderer.max_height)
self.assertEqual(count, 11)
# Check an image looks plausible
image = next(renderer.images)
self.assertEqual(
image,
['....▄▄▄▄▄▄▄▄▄▄▄▄.....',
'..▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄...',
'.▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄..',
'▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄.',
'▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄.',
'▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄.',
'▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄..',
'.▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄..',
'..▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄....',
'.....▄▄▄▄▄▄▄▄▄▄......'])
# Mock screen for better coverage.
screen = MagicMock(spec=Screen, colours=16, unicode_aware=True, palette=Screen._8_palette)
canvas = Canvas(screen, 15, 40, 0, 0)
internal_checks(screen)
if __name__ == '__main__':
unittest.main()
|