def test_add(self): """ Controls should be added to each other, Colrs, or strs. """ types = { 'Control': Control().move_down(1), 'Colr': Colr('Test', 'red'), 'str': 'testing', } for othername, other in types.items(): ctl = Control().move_column(3) try: newctl = ctl + other except TypeError: self.fail( 'Control + {} should not raise a TypeError.'.format( othername )) else: self.assertIsInstance( newctl, Control, msg=( 'Adding {} to a Control did not return a Control.' ).format(othername) ) ctl_str_result = ''.join((str(ctl), str(other))) s = str(newctl) self.assertEqual( ctl_str_result, s, msg='str(Control()) did not match.' )
def test_repeat_all(self): """ Control.repeat() should be like str(Control()) * count. """ count = 3 s = ''.join(( str(move.up()), str(move.down()), str(move.down()), str(move.down()), )) * count ctl = Control().move_up().move_down().repeat(3) self.assertEqual( s, str(ctl.repeat_all(count)), msg='Control.repeat_all produced an unexpected result.', )
def test_delay(self): """ delay() should be callable. """ c = Control().delay(0.005) self.assertIsInstance( c, Control, msg='Control.delay() returned a non-Control instance. Impossible.' )
def test_repeat(self): """ Control.repeat() should repeat the last code. """ s = ''.join(( str(move.up()), str(move.down()), str(move.down()), str(move.down()), )) ctl = Control().move_up().move_down().repeat(3) self.assertEqual( s, str(ctl), msg='Control.repeat produced an unexpected result.', ) # Non-integer count should raise. with self.assertRaises(TypeError): Control().repeat(count='BAD')
def run_scroll(delay=0.05): """ This is a rough test of the scroll_* functions. """ linedelay = delay * 6 height = 6 height_dbl = height * 2 start_colr = 255 - height_dbl for i in range(height): Control(C('Scrolled to {} lines.'.format((i * 2) + 1), start_colr + i)).scroll_up(2).move_column(1).write() sleep(linedelay) sleep(0.5) Control().scroll_down(1).repeat(height_dbl).write() for i in range(height_dbl): Control( C('Scrolled down, overwriting {}.'.format(i + 1), start_colr + i)).write(end='\n') sleep(linedelay) print('\nFinished with scroll functions.\n')
def test_repeat_all(self): """ Control.repeat() should be like str(Control()) * count. """ count = 3 s = ''.join(( str(move.up()), str(move.down()), str(move.down()), str(move.down()), )) * count ctl = Control().move_up().move_down().repeat(3) self.assertEqual( s, str(ctl.repeat_all(count)), msg='Control.repeat_all produced an unexpected result.', ) # Non-integer count should raise. with self.assertRaises(TypeError): Control().repeat_all(count='BAD')
def run_print(delay=0.05): """ This is a test of the print_* functions. """ Control( C('This is a test of colr.control.print_* functions.\n').rainbow() ).write() sleep(0.5) print_overwrite( '...but you get to see some text animations.', delay=delay, ) Control(C('\nI\'m just gonna put this: ', 'cyan')).write(delay=delay) for val in (C('here', 'red'), C('there', 'green'), C('nowhere', 'blue')): sleep(delay) print_inplace(val, delay=delay) Control().move_column(1).write() for col in range(len('I\'m just gonna put this: nowhere')): Control(C('X', random.randint(0, 255))).write().delay(delay) print(Control().erase_line()) print('\nFinished with print functions.\n')
def test_hash(self): """ hash(Control()) should return a unique hash for self.data. """ a, b = hash(Control().move_down()), hash(Control().move_down()) self.assertCallEqual( a, b, func=hash, args=[a], otherargs=[b], msg='Mismatched hash values.', ) b = hash(Control().move_up()) self.assertCallNotEqual( a, b, func=hash, args=[a], otherargs=[b], msg='Hash values should not match.', )
def test_repeat(self): """ Control.repeat() should repeat the last code. """ s = ''.join(( str(move.up()), str(move.down()), str(move.down()), str(move.down()), )) ctl = Control().move_up().move_down().repeat(3) self.assertEqual( s, str(ctl), msg='Control.repeat produced an unexpected result.', )
def test_escape_code_methods(self): """ methods appending escape codes should be callable without error. """ c = ( Control() .cursor_hide() .cursor_show() .erase_display() .erase_line() .move_back() .move_column() .move_down() .move_forward() .move_next() .move_pos() .move_prev() .move_return() .move_up() .pos_restore() .pos_save() .scroll_down() .scroll_up() ) codes = ( cursor.hide(), cursor.show(), erase.display(), erase.line(), move.back(), move.column(), move.down(), move.forward(), move.next(), move.pos(), move.prev(), move.carriage_return(), move.up(), position.restore(), position.save(), scroll.down(), scroll.up(), ) self.assertEqual( str(c), ''.join(str(x) for x in codes), msg='Failed to create escape codes from methods.' )
def test_last_code(self): """ last_code() should return the last escape code appended. """ c = Control('datadata').move_up() self.assertEqual( c.last_code(), str(move.up()), msg='Failed to return last escape code.' ) blank = Control() self.assertEqual( blank.last_code(), '', msg='Empty Control should not have a last code.' )
def run_move(delay=0.025): """ This is a rough test of the move_* functions. """ width = 48 width_half = width // 2 height = 24 # Draw a block of Xs. Control(C('\n'.join( ('X' * width) for _ in range(height))).rainbow()).move_up(height - 1).write() # Draw a line down the middle. Control().move_column(width_half).write() for _ in range(height): Control(C('|', 'blue')).move_back().move_down().write().delay(delay) # Draw a line at the top left half. Control().move_up(height - 1).move_column().write() for _ in range(width_half): Control(C('-', 'blue')).write().delay(delay) # Draw a line at the bottom right half. Control().move_down(height - 1).move_back().write() for _ in range(width_half + 1): Control(C('-', 'blue')).write().delay(delay) # Erase the right half. Control().move_column(width_half + 1).erase_line(EraseMethod.END).write() for _ in range(height - 1): Control().move_up().erase_line(EraseMethod.END).write().delay(delay) # Erase the left half. (Control().move_column(width_half - 1).erase_line( EraseMethod.START).write()) for _ in range(height - 1): (Control().move_down().erase_line( EraseMethod.START).write().delay(delay)) # Widen the "stick". start_colr = 255 - width_half for colrval in range(start_colr + width_half, start_colr, -1): (Control().move_up(height - 1).move_column(width + (start_colr - colrval)).text( C('-', colrval)).write()) for _ in range(height - 2): Control().move_down().move_back().text(C('|', colrval)).write() (Control().move_down().move_back().text(C( '-', colrval)).write().delay(delay)) # Shrink the "sticks". Control().move_up(height - 1).write() chardelay = delay / 3 for _ in range(height // 2): Control().move_column(width).write() for _ in range(width_half + 2): (Control().erase_line( EraseMethod.END).move_back().write().delay(chardelay)) Control().move_down().move_column(width_half).write() for _ in range(width_half): Control(' ').write().delay(chardelay) sleep(chardelay) Control().move_down().write() # Move to the end. Control().move_down(height + 1).move_column(width).write() print('\nFinished with move functions.\n')
def test_bytes(self): """ bytes(Control()) should encode self.data. """ s = 'test' a = s.encode() b = bytes(Control(s)) self.assertEqual(a, b, msg='Encoded Control is not the same.')