class SplitTileTestCase(TestCase): def test_init(self): tile = tiles.SplitTile(1, 2, 3) self.assertEqual([1, 2, 3], tile.tiles) @data_provider(lambda: [ [[ DumbTile(subject=TileSubject(''), shouldRender=False), DumbTile(subject=TileSubject(''), shouldRender=False), DumbTile(subject=TileSubject(''), shouldRender=True) ], True], [[DumbTile(subject=TileSubject(''), shouldRender=True)], True], [[DumbTile(subject=TileSubject(''), shouldRender=False)], False], ]) def test_shouldRender(self, dumbTiles: List[Tile], shouldRender: bool): tile = tiles.SplitTile(*dumbTiles) self.assertEqual(shouldRender, tile.shouldRender()) def test_render(self): terminal = blessed.Terminal width = 80 height = 40 tile1 = Mock() tile2 = Mock() tile3 = Mock() tile1.refresh = Mock() tile2.refresh = Mock() tile3.refresh = Mock() tile1.lines = ['line1'] tile2.lines = ['line2'] tile3.lines = ['line3'] expectedWidth = math.floor(width / 3) expectedHeight = height expectedOutput = [ ( tile1.lines[0] + tile2.lines[0] + tile3.lines[0] + (' '*(width - len(tile1.lines[0]) - len(tile2.lines[0]) - len(tile3.lines[0]))) ), *[' '*width for _ in range(expectedHeight - 1)] ] tile = tiles.SplitTile(tile1, tile2, tile3) output = tile.render(terminal=terminal, width=width, height=height) self.assertEqual(expectedOutput, output) for t in [tile1, tile2, tile3]: self.assertEqual(1, t.refresh.call_count, "Each tile's refresh method should be called once") tile1.refresh.assert_called_with( terminal=terminal, width=expectedWidth, height=expectedHeight)
def test_init(self): tile = DumbTile(subject=TileSubject('test'), title='title', withBorders=True) self.assertEqual('title', tile.title) self.assertEqual('test', tile.subject) self.assertEqual([], tile.lines)
def test_update(self): tile = DumbTile(subject=TileSubject('foo')) tile.update('foo') self.assertFalse(tile.shouldRender()) self.assertEqual('foo', tile.subject) tile.update('bar') self.assertTrue(tile.shouldRender()) self.assertEqual('bar', tile.subject)
def test_refresh_without_borders(self): terminal = Terminal() tile = DumbTile(subject=TileSubject('foo'), title='bar', withBorders=False) tile.refresh(terminal=terminal, width=19, height=5) self.assertEqual([ ' bar ', 'foo ', ' ', ' ', ' ', ], tile.lines)
def test_refresh_with_borders(self): terminal = Terminal() tile = DumbTile(subject=TileSubject('foo'), title='bar', withBorders=True) tile.refresh(terminal=terminal, width=19, height=5) self.assertEqual([ '┌──── bar ────┐', '│foo │', '│ │', '│ │', '└─────────────────┘', ], tile.lines)
def test_render(self): terminal = Terminal() subject = TileSubject( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.') tile = tiles.TextTile(subject=subject, title='Title', withBorders=False) result = tile.render(terminal=terminal, width=28, height=5) self.assertEqual([ 'Lorem ipsum dolor sit amet, ', 'consectetur adipiscing elit.', ], result)
def test_tilesubject_subscribe_unsubscribe(self): subject = TileSubject('') observer = Mock() subject.subscribe(observer) subject('change 1') subject.unsubscribe(observer) subject('change 2') observer.update.assert_called_with('change 1') self.assertEqual(1, observer.update.call_count, 'The observer should be called only when subscribed')
from blessedui import BlessedUI, TileSubject, tiles from time import sleep import lorem textSubject1 = TileSubject(lorem.sentence()) textSubject2 = TileSubject(lorem.sentence()) textSubject3 = TileSubject(lorem.sentence()) textSubject4 = TileSubject(lorem.sentence()) textSubject5 = TileSubject(lorem.sentence()) ui = BlessedUI( tiles.TextTile(subject=textSubject1, withBorders=False, title='Whenever'), tiles.SplitTile( tiles.TextTile(subject=textSubject2, withBorders=True, title='Split 1'), tiles.TextTile(subject=textSubject3, withBorders=True, title='Split 2'), tiles.TextTile(subject=textSubject4, withBorders=True, title='Split 3'), tiles.TextTile(subject=textSubject5, withBorders=True, title='Split 4'), ), ) ui.run() while True: sleep(3) textSubject1(lorem.paragraph()) textSubject2(lorem.paragraph()) textSubject3(lorem.paragraph())
class BlessedUITestCase(TestCase): @data_provider(lambda: [ [[], { 'terminal': None, 'maxRefreshRate': 1 }, 0, 1.00], [[DumbTile(TileSubject('')), DumbTile(TileSubject(''))], { 'terminal': None, 'maxRefreshRate': 30 }, 2, 0.0333333], ]) def test_init(self, tiles: List[Tile], kwargs: dict, expectedTilesLen, expectedFrameDuration): ui = BlessedUI(*tiles, **kwargs) self.assertIsInstance(ui.terminal, blessed.Terminal) self.assertLen(expectedTilesLen, ui.tiles, f'There should be {expectedTilesLen} files') self.assertAlmostEqual( expectedFrameDuration, ui.frameDuration, msg=f'Frame duration should be {expectedFrameDuration}') def test_start_stop(self): # Return True once, then False def shouldRender(mockObj: Mock): def inner(): mockObj.shouldRender.side_effect = lambda: False return True return inner tile1 = Mock() tile1.shouldRender = Mock(side_effect=shouldRender(tile1)) tile1.lines = [''] tile2 = Mock() tile2.shouldRender = Mock(side_effect=shouldRender(tile2)) tile2.lines = [''] ui = BlessedUI(tile1, tile2) self.assertIsNone( ui._thread, msg="Before starting it, the UI's thread should not exist") ui.run() self.assertTrue( ui._thread.is_alive(), msg="After starting it, the UI's thread should be running") ui.run() self.assertTrue( ui._thread.is_alive(), msg="After re-starting it, the UI's thread should still be running" ) ui.stop() # Await the async process to stop ui.join() self.assertFalse( ui._thread.is_alive(), msg="After stopping it, the UI's thread should not be running") self.assertEqual( 1, tile1.refresh.call_count, "After running multiple times, the tile1's refresh method should have been called once" ) # noqa E501 self.assertEqual( 1, tile2.refresh.call_count, "After running multiple times, the tile2's refresh method should have been called once" ) # noqa E501