OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 ''' |
| 3 PEXPECT LICENSE |
| 4 |
| 5 This license is approved by the OSI and FSF as GPL-compatible. |
| 6 http://opensource.org/licenses/isc-license.txt |
| 7 |
| 8 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 |
| 20 ''' |
| 21 |
| 22 import sys |
| 23 |
| 24 from pexpect import screen |
| 25 import unittest |
| 26 from . import PexpectTestCase |
| 27 |
| 28 PY3 = (sys.version_info[0] >= 3) |
| 29 |
| 30 fill1_target='XXXXXXXXXX\n' + \ |
| 31 'XOOOOOOOOX\n' + \ |
| 32 'XO::::::OX\n' + \ |
| 33 'XO:oooo:OX\n' + \ |
| 34 'XO:o..o:OX\n' + \ |
| 35 'XO:o..o:OX\n' + \ |
| 36 'XO:oooo:OX\n' + \ |
| 37 'XO::::::OX\n' + \ |
| 38 'XOOOOOOOOX\n' + \ |
| 39 'XXXXXXXXXX' |
| 40 fill2_target = 'XXXXXXXXXXX\n' + \ |
| 41 'XOOOOOOOOOX\n' + \ |
| 42 'XO:::::::OX\n' + \ |
| 43 'XO:ooooo:OX\n' + \ |
| 44 'XO:o...o:OX\n' + \ |
| 45 'XO:o.+.o:OX\n' + \ |
| 46 'XO:o...o:OX\n' + \ |
| 47 'XO:ooooo:OX\n' + \ |
| 48 'XO:::::::OX\n' + \ |
| 49 'XOOOOOOOOOX\n' + \ |
| 50 'XXXXXXXXXXX' |
| 51 put_target = '\\.3.5.7.9/\n' + \ |
| 52 '.........2\n' + \ |
| 53 '3.........\n' + \ |
| 54 '.........4\n' + \ |
| 55 '5...\\/....\n' + \ |
| 56 '..../\\...6\n' + \ |
| 57 '7.........\n' + \ |
| 58 '.........8\n' + \ |
| 59 '9.........\n' + \ |
| 60 '/2.4.6.8.\\' |
| 61 scroll_target = '\\.3.5.7.9/\n' + \ |
| 62 '\\.3.5.7.9/\n' + \ |
| 63 '\\.3.5.7.9/\n' + \ |
| 64 '\\.3.5.7.9/\n' + \ |
| 65 '5...\\/....\n' + \ |
| 66 '..../\\...6\n' + \ |
| 67 '/2.4.6.8.\\\n' + \ |
| 68 '/2.4.6.8.\\\n' + \ |
| 69 '/2.4.6.8.\\\n' + \ |
| 70 '/2.4.6.8.\\' |
| 71 insert_target = 'ZXZZZZZZXZ\n' +\ |
| 72 '.........2\n' +\ |
| 73 '3.........\n' +\ |
| 74 '.........4\n' +\ |
| 75 'Z5...\\/...\n' +\ |
| 76 '..../Z\\...\n' +\ |
| 77 '7.........\n' +\ |
| 78 '.........8\n' +\ |
| 79 '9.........\n' +\ |
| 80 'ZZ/2.4.6ZZ' |
| 81 get_region_target = ['......', '.\\/...', './\\...', '......'] |
| 82 |
| 83 unicode_box_unicode_result = u'\u2554\u2557\n\u255A\u255D' |
| 84 unicode_box_pretty_result = u'''\ |
| 85 +--+ |
| 86 |\u2554\u2557| |
| 87 |\u255A\u255D| |
| 88 +--+ |
| 89 ''' |
| 90 unicode_box_ascii_bytes_result = b'??\n??' |
| 91 unicode_box_cp437_bytes_result = b'\xc9\xbb\n\xc8\xbc' |
| 92 unicode_box_utf8_bytes_result = b'\xe2\x95\x94\xe2\x95\x97\n\xe2\x95\x9a\xe2\x95
\x9d' |
| 93 |
| 94 class screenTestCase (PexpectTestCase.PexpectTestCase): |
| 95 def make_screen_with_put (self): |
| 96 s = screen.screen(10,10) |
| 97 s.fill ('.') |
| 98 for r in range (1,s.rows + 1): |
| 99 if r % 2: |
| 100 s.put_abs (r, 1, str(r)) |
| 101 else: |
| 102 s.put_abs (r, s.cols, str(r)) |
| 103 for c in range (1,s.cols + 1): |
| 104 if c % 2: |
| 105 s.put_abs (1, c, str(c)) |
| 106 else: |
| 107 s.put_abs (s.rows, c, str(c)) |
| 108 s.put_abs(1,1, '\\') |
| 109 s.put_abs(1,s.cols, '/') |
| 110 s.put_abs(s.rows,1,'/') |
| 111 s.put_abs(s.rows, s.cols, '\\') |
| 112 s.put_abs(5,5,'\\') |
| 113 s.put_abs(5,6,'/') |
| 114 s.put_abs(6,5,'/') |
| 115 s.put_abs(6,6,'\\') |
| 116 return s |
| 117 |
| 118 def test_fill (self): |
| 119 s = screen.screen (10,10) |
| 120 s.fill_region (10,1,1,10,'X') |
| 121 s.fill_region (2,2,9,9,'O') |
| 122 s.fill_region (8,8,3,3,':') |
| 123 s.fill_region (4,7,7,4,'o') |
| 124 s.fill_region (6,5,5,6,'.') |
| 125 assert str(s) == fill1_target |
| 126 |
| 127 s = screen.screen (11,11) |
| 128 s.fill_region (1,1,11,11,'X') |
| 129 s.fill_region (2,2,10,10,'O') |
| 130 s.fill_region (9,9,3,3,':') |
| 131 s.fill_region (4,8,8,4,'o') |
| 132 s.fill_region (7,5,5,7,'.') |
| 133 s.fill_region (6,6,6,6,'+') |
| 134 assert str(s) == fill2_target |
| 135 def test_put (self): |
| 136 s = self.make_screen_with_put() |
| 137 assert str(s) == put_target |
| 138 def test_get_region (self): |
| 139 s = self.make_screen_with_put() |
| 140 r = s.get_region (4,4,7,9) |
| 141 assert r == get_region_target |
| 142 |
| 143 def test_cursor_save (self): |
| 144 s = self.make_screen_with_put() |
| 145 s.cursor_home (5,5) |
| 146 c = s.get() |
| 147 s.cursor_save() |
| 148 s.cursor_home() |
| 149 s.cursor_forward() |
| 150 s.cursor_down() |
| 151 s.cursor_unsave() |
| 152 assert s.cur_r == 5 and s.cur_c == 5 |
| 153 assert c == s.get() |
| 154 def test_scroll (self): |
| 155 s = self.make_screen_with_put() |
| 156 s.scroll_screen_rows (1,4) |
| 157 s.scroll_down(); s.scroll_down(); s.scroll_down() |
| 158 s.scroll_down(); s.scroll_down(); s.scroll_down() |
| 159 s.scroll_screen_rows (7,10) |
| 160 s.scroll_up(); s.scroll_up(); s.scroll_up() |
| 161 s.scroll_up(); s.scroll_up(); s.scroll_up() |
| 162 assert str(s) == scroll_target |
| 163 def test_insert (self): |
| 164 s = self.make_screen_with_put() |
| 165 s.insert_abs (10,1,'Z') |
| 166 s.insert_abs (1,1,'Z') |
| 167 s.insert_abs (1,1,'Z') |
| 168 s.insert_abs (1,1,'Z') |
| 169 s.insert_abs (1,1,'Z') |
| 170 s.insert_abs (1,1,'Z') |
| 171 s.insert_abs (10,1,'Z') |
| 172 s.insert_abs (1,1,'Z') |
| 173 s.insert_abs (1,1,'Z') |
| 174 s.insert_abs (5,1,'Z') |
| 175 s.insert_abs (6,6,'Z') |
| 176 s.cursor_home (1,1) # Also test relative insert. |
| 177 s.insert ('Z') |
| 178 s.insert ('Z') |
| 179 s.insert ('Z') |
| 180 s.insert ('Z') |
| 181 s.insert_abs (1,8,'X') |
| 182 s.insert_abs (1,2,'X') |
| 183 s.insert_abs (10,9,'Z') |
| 184 s.insert_abs (10,9,'Z') |
| 185 assert str(s) == insert_target |
| 186 |
| 187 def make_screen_with_box_unicode(self, *args, **kwargs): |
| 188 '''Creates a screen containing a box drawn using double-line |
| 189 line drawing characters. The characters are fed in as |
| 190 unicode. ''' |
| 191 s = screen.screen (2,2,*args,**kwargs) |
| 192 s.put_abs (1,1,u'\u2554') |
| 193 s.put_abs (1,2,u'\u2557') |
| 194 s.put_abs (2,1,u'\u255A') |
| 195 s.put_abs (2,2,u'\u255D') |
| 196 return s |
| 197 |
| 198 def make_screen_with_box_cp437(self, *args, **kwargs): |
| 199 '''Creates a screen containing a box drawn using double-line |
| 200 line drawing characters. The characters are fed in as |
| 201 CP437. ''' |
| 202 s = screen.screen (2,2,*args,**kwargs) |
| 203 s.put_abs (1,1,b'\xc9') |
| 204 s.put_abs (1,2,b'\xbb') |
| 205 s.put_abs (2,1,b'\xc8') |
| 206 s.put_abs (2,2,b'\xbc') |
| 207 return s |
| 208 |
| 209 def make_screen_with_box_utf8(self, *args, **kwargs): |
| 210 '''Creates a screen containing a box drawn using double-line |
| 211 line drawing characters. The characters are fed in as |
| 212 UTF-8. ''' |
| 213 s = screen.screen (2,2,*args,**kwargs) |
| 214 s.put_abs (1,1,b'\xe2\x95\x94') |
| 215 s.put_abs (1,2,b'\xe2\x95\x97') |
| 216 s.put_abs (2,1,b'\xe2\x95\x9a') |
| 217 s.put_abs (2,2,b'\xe2\x95\x9d') |
| 218 return s |
| 219 |
| 220 def test_unicode_ascii (self): |
| 221 # With the default encoding set to ASCII, we should still be |
| 222 # able to feed in unicode strings and get them back out: |
| 223 s = self.make_screen_with_box_unicode('ascii') |
| 224 if PY3: |
| 225 assert str(s) == unicode_box_unicode_result |
| 226 else: |
| 227 assert unicode(s) == unicode_box_unicode_result |
| 228 # And we should still get something for Python 2 str(), though |
| 229 # it might not be very useful |
| 230 str(s) |
| 231 |
| 232 assert s.pretty() == unicode_box_pretty_result |
| 233 |
| 234 def test_decoding_errors(self): |
| 235 # With strict error handling, it should reject bytes it can't decode |
| 236 with self.assertRaises(UnicodeDecodeError): |
| 237 self.make_screen_with_box_cp437('ascii', 'strict') |
| 238 |
| 239 # replace should turn them into unicode replacement characters, U+FFFD |
| 240 s = self.make_screen_with_box_cp437('ascii', 'replace') |
| 241 expected = u'\ufffd\ufffd\n\ufffd\ufffd' |
| 242 if PY3: |
| 243 assert str(s) == expected |
| 244 else: |
| 245 assert unicode(s) == expected |
| 246 |
| 247 def test_unicode_cp437 (self): |
| 248 # Verify decoding from and re-encoding to CP437. |
| 249 s = self.make_screen_with_box_cp437('cp437','strict') |
| 250 if PY3: |
| 251 assert str(s) == unicode_box_unicode_result |
| 252 else: |
| 253 assert unicode(s) == unicode_box_unicode_result |
| 254 assert str(s) == unicode_box_cp437_bytes_result |
| 255 assert s.pretty() == unicode_box_pretty_result |
| 256 |
| 257 def test_unicode_utf8 (self): |
| 258 # Verify decoding from and re-encoding to UTF-8. |
| 259 s = self.make_screen_with_box_utf8('utf-8','strict') |
| 260 if PY3: |
| 261 assert str(s) == unicode_box_unicode_result |
| 262 else: |
| 263 assert unicode(s) == unicode_box_unicode_result |
| 264 assert str(s) == unicode_box_utf8_bytes_result |
| 265 assert s.pretty() == unicode_box_pretty_result |
| 266 |
| 267 def test_no_bytes(self): |
| 268 s = screen.screen(2, 2, encoding=None) |
| 269 s.put_abs(1, 1, u'A') |
| 270 s.put_abs(2, 2, u'D') |
| 271 |
| 272 with self.assertRaises(TypeError): |
| 273 s.put_abs(1, 2, b'B') |
| 274 |
| 275 if PY3: |
| 276 assert str(s) == u'A \n D' |
| 277 else: |
| 278 assert unicode(s) == u'A \n D' |
| 279 # This will still work if it's limited to ascii |
| 280 assert str(s) == b'A \n D' |
| 281 |
| 282 if __name__ == '__main__': |
| 283 unittest.main() |
| 284 |
| 285 suite = unittest.makeSuite(screenTestCase,'test') |
| 286 |
| 287 |
OLD | NEW |