def test_line_draw(self): ascii_canvas = AsciiCanvas(5, 2) ascii_canvas.add_line(0, 0, 0, 0) canvas_with_lines_str = \ line('o ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_lines_str, 'Incorrect canvas with lines') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_line(-5, -5, 10, 10, Style('****')) ascii_canvas.add_line(4, 0, 0, 4, Style('#')) canvas_with_lines_str = \ line('* #') + \ line(' * # ') + \ line(' # ') + \ line(' # * ') + \ last('# *') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_lines_str, 'Incorrect canvas with lines') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_line(0, 0, 4, 0, style=Style('-')) ascii_canvas.add_line(0, 0, 0, 3, style=Style('|')) canvas_with_lines_str = \ line('|----') + \ line('| ') + \ line('| ') + \ line('| ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_lines_str, 'Incorrect canvas with lines')
def test_point_draw(self): ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_point(0, 0) canvas_with_points_str = \ line('o ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_points_str, 'Incorrect canvas with lines') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_point(-5, -5) canvas_with_points_str = \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_points_str, 'Incorrect canvas with lines') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_point(1, 1, Style('Ooo')) ascii_canvas.add_point(3, 3, Style('*')) ascii_canvas.add_point(0, 4, Style('.')) ascii_canvas.add_point(4, 0, Style('')) ascii_canvas.add_point(4, 1, Style(' ')) canvas_with_lines_str = \ line(' o') + \ line(' O ') + \ line(' ') + \ line(' * ') + \ last('. ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_lines_str, 'Incorrect canvas with lines')
def test_rect_draw(self): ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_rect(0, 0, 5, 5) canvas_with_rect_str = \ line('ooooo') + \ line('o o') + \ line('o o') + \ line('o o') + \ last('ooooo') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with rect') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_rect(0, 0, 5, 5, fill_style=Style('.'), outline_style=Style('#')) canvas_with_rect_str = \ line('#####') + \ line('#...#') + \ line('#...#') + \ line('#...#') + \ last('#####') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with rect') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_rect(4, 4, 1, 1) canvas_with_rect_str = \ line(' ') + \ line(' ') + \ line(' ') + \ line(' ') + \ last(' o') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with rect') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_rect(1, 1, 0, 0) canvas_with_rect_str = \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with rect') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_rect(1, 1, -1, -1) canvas_with_rect_str = \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with rect')
def test_canvas(self): ascii_canvas = AsciiCanvas(10, 10) canvas_str = (' ' * 10 + '\n') * 9 + ' ' * 10 self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str) ascii_canvas = AsciiCanvas(1, 1, Style('#')) canvas_str = '#' self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str) ascii_canvas = AsciiCanvas(2, 1, Style('XYZ')) canvas_str = 'XX' self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str) ascii_canvas.clear() # must be the same as before clear self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str)
def draw(self): # NOTE: coordinates might me negative, so we need to shift # everything to the positive plane before we actually draw it. Xs = [] Ys = [] for v in self.sug.g.sV: # NOTE: moving boxes w/2 to the left Xs.append(v.view.xy[0] - v.view.w / 2.0) Xs.append(v.view.xy[0] + v.view.w / 2.0) Ys.append(v.view.xy[1]) Ys.append(v.view.xy[1] + v.view.h) for e in self.sug.g.sE: for xy in e.view._pts: Xs.append(xy[0]) Ys.append(xy[1]) minx = min(Xs) miny = min(Ys) maxx = max(Xs) maxy = max(Ys) canvas_cols = int(math.ceil(math.ceil(maxx) - math.floor(minx))) + 1 canvas_lines = int(round(maxy - miny)) canvas = AsciiCanvas(canvas_cols, canvas_lines) # NOTE: first draw edges so that node boxes could overwrite them for e in self.sug.g.sE: assert len(e.view._pts) > 1 for index in range(1, len(e.view._pts)): start = e.view._pts[index - 1] end = e.view._pts[index] start_x = int(round(start[0] - minx)) start_y = int(round(start[1] - miny)) end_x = int(round(end[0] - minx)) end_y = int(round(end[1] - miny)) assert start_x >= 0 assert start_y >= 0 assert end_x >= 0 assert end_y >= 0 canvas.add_line(start_x, start_y, end_x, end_y, style=Style('*')) for v in self.sug.g.sV: # NOTE: moving boxes w/2 to the left x = v.view.xy[0] - v.view.w / 2.0 y = v.view.xy[1] canvas.add_nine_patch_rect(int(round(x - minx)), int(round(y - miny)), v.view.w, v.view.h) canvas.add_text( int(round(x - minx)) + 1, int(round(y - miny)) + 1, v.data) canvas.print_out()
def test_ellipse_draw(self): ascii_canvas = AsciiCanvas(3, 3) ascii_canvas.add_ellipse(0, 0, 2, 2) canvas_str = \ line('oo ') + \ line('oo ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(3, 3) ascii_canvas.add_ellipse(0, 0, 1, 1) canvas_str = \ line('o ') + \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(3, 3) ascii_canvas.add_ellipse(0, 0, 0, 0) canvas_str = \ line(' ') + \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(3, 3) ascii_canvas.add_ellipse(3, 3, -2, -2) canvas_str = \ line(' ') + \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(3, 3) ascii_canvas.add_ellipse(0, 2, 3, 1) canvas_str = \ line(' ') + \ line(' ') + \ last('ooo') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_ellipse(1, 1, 3, 3, Style('o'), Style('O')) canvas_str = \ line(' ') + \ line(' O ') + \ line(' OoO ') + \ line(' O ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_ellipse(0, 0, 4, 5) canvas_str = \ line(' oo ') + \ line('o o ') + \ line('o o ') + \ line('o o ') + \ last(' oo ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_ellipse(0, 0, 3, 5) canvas_str = \ line(' o ') + \ line('o o ') + \ line('o o ') + \ line('o o ') + \ last(' o ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_ellipse(0, -1, 2, 7) canvas_str = \ line('oo ') + \ line('oo ') + \ line('oo ') + \ line('oo ') + \ last('oo ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_ellipse(0, 0, 5, 3) canvas_str = \ line(' ooo ') + \ line('o o') + \ line(' ooo ') + \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse') ascii_canvas = AsciiCanvas(15, 5) ascii_canvas.add_ellipse(0, 0, 15, 5, Style('.')) canvas_str = \ line(' ooooooooo ') + \ line(' oo.........oo ') + \ line('o.............o') + \ line(' oo.........oo ') + \ last(' ooooooooo ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_str, 'Incorrect canvas with ellipse')
def test_nine_patch_rect_draw(self): ascii_canvas = AsciiCanvas(5, 5) ascii_canvas.add_nine_patch_rect(0, 0, 5, 5) canvas_with_rect_str = \ line('.---.') + \ line('| |') + \ line('| |') + \ line('| |') + \ last("`---'") self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with 9-patch rect') ascii_canvas = AsciiCanvas(5, 5) nine_patch_style = (Style('1'), Style('2'), Style('3'), Style('4'), Style('5'), Style('6'), Style('7'), Style('8'), Style('9')) ascii_canvas.add_nine_patch_rect(0, 0, 5, 5, nine_patch_style) canvas_with_rect_str = \ line('12223') + \ line('45556') + \ line('45556') + \ line('45556') + \ last('78889') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with 9-patch rect') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_nine_patch_rect(0, 0, 2, 2) canvas_with_rect_str = \ line('..') + \ last("`'") self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with 9-patch rect') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_nine_patch_rect(0, 0, 0, 0) canvas_with_rect_str = \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with 9-patch rect') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_nine_patch_rect(1, 1, 1, 1, nine_patch_style) canvas_with_rect_str = \ line(' ') + \ last(' 1') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with 9-patch rect') ascii_canvas = AsciiCanvas(2, 2) ascii_canvas.add_nine_patch_rect(1, 1, -1, -1) canvas_with_rect_str = \ line(' ') + \ last(' ') self.assertEqual(ascii_canvas.get_canvas_as_str(), canvas_with_rect_str, 'Incorrect canvas with 9-patch rect')