def test_draw_open_graph_shadow(self): self.__backend.set_canvas_size(50, 150) self.__backend.create_canvas() graph = nx.Graph() open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph_shadow(open_graph) self.__backend.export_to_file(TestUtils.OPEN_GRAPH_SHADOW_EMPTY_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal(TestUtils.OPEN_GRAPH_SHADOW_EMPTY_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_SHADOW_EMPTY_EXPECTED_IMAGE) edge_list = [((10, 10), (40, 10)), ((40, 10), (60, 10)), ((40, 10), (50, 40)), ((50, 40), (20, 30)), ((50, 40), (50, 60))] edge_list2 = [((10, 10), (40, 10)), ((40, 10), (50, 40)), ((50, 40), (20, 30)),((20, 30), (10, 10))] for edge in edge_list: graph.add_edge(Node(*edge[0]), Node(*edge[1])) graph2 = nx.Graph() for edge in edge_list2: graph2.add_edge(Node(*edge[0], style = 'curve'), Node(*edge[1], style = 'curve')) open_graph = OpenGraph(graph) open_graph2 = OpenGraph(graph2) self.__backend.push_surface() self.__backend.draw_open_graph_shadow(open_graph) self.__backend.translate(0, 60) self.__backend.draw_open_graph_shadow(open_graph2) self.__backend.export_to_file(TestUtils.OPEN_GRAPH_SHADOW_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal(TestUtils.OPEN_GRAPH_SHADOW_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_SHADOW_EXPECTED_IMAGE)
def test_draw_objects(self): polygon1 = TestUtils.generate_test_polygon(seed=0, points=12, radius_range=(1, 10)) polygon2 = copy.deepcopy(polygon1) polygon2.style().set_shadow('off') opengraph1 = TestUtils.generate_test_opengraph(seed=0, points=12, radius_range=(1, 10)) opengraph2 = copy.deepcopy(opengraph1) opengraph2.style().set_shadow('off') text = Text() arrow1 = RightArrow() arrow2 = RightArrow() arrow2.style().set_shadow('off') objects = [ polygon1, polygon2, opengraph1, opengraph2, text, arrow2, arrow1 ] self.__backend.push_surface = MagicMock() self.__backend.pop_surface = MagicMock() self.__backend.draw_polygon_shadow = MagicMock() self.__backend.draw_polygon = MagicMock() self.__backend.draw_open_graph_shadow = MagicMock() self.__backend.draw_open_graph = MagicMock() self.__backend.draw_text = MagicMock() self.__backend.draw_text_shadow = MagicMock() self.__backend.translate = MagicMock() self.__backend.blur_surface = MagicMock() self.__backend.draw_objects(objects) assert self.__backend.push_surface.call_count == self.__backend.pop_surface.call_count assert self.__backend.translate.call_count == 2 assert self.__backend.draw_polygon.call_count == 4 self.__backend.draw_polygon.assert_any_call(polygon1) self.__backend.draw_polygon.assert_any_call(polygon2) self.__backend.draw_polygon.assert_any_call(arrow1) self.__backend.draw_polygon.assert_any_call(arrow2) assert self.__backend.draw_polygon_shadow.call_count == 2 self.__backend.draw_polygon_shadow.assert_any_call(polygon1) self.__backend.draw_polygon_shadow.assert_any_call(arrow1) assert self.__backend.draw_open_graph.call_count == 4 self.__backend.draw_open_graph.assert_any_call(polygon1.frame()) self.__backend.draw_open_graph.assert_any_call(polygon2.frame()) self.__backend.draw_open_graph.assert_any_call(opengraph1) self.__backend.draw_open_graph.assert_any_call(opengraph2) self.__backend.draw_open_graph_shadow.assert_called_once_with( opengraph1) self.__backend.draw_text.assert_called_once_with(text) assert self.__backend.blur_surface.call_count == 2
def test_run(self): background = Background((7, 3)) polygon = TestUtils.generate_test_polygon(seed = 0, points = 12, radius_range = (1, 10)) polygon_copy = copy.deepcopy(polygon) objects = [background, polygon, Translatable()] self.__backend.draw_objects = MagicMock() self.__backend.create_canvas = MagicMock() self.__backend.export_to_file = MagicMock() self.__backend.run(objects, "testname") self.__backend.draw_objects.assert_called(objects) self.__backend.create_canvas.assert_called(objects) self.__backend.export_to_file.assert_called_with("testname") assert TestUtils.unordered_lists_equal(self.__backend.draw_objects.call_args[0][0], objects)
def test_draw_text(self): text = Text("abcdef123456!=_/>") text.scale((10,20)) self.__backend.set_canvas_size(160, 30) self.__backend.create_canvas() self.__backend.push_surface() self.__backend.draw_text(text) self.__backend.export_to_file(TestUtils.TEXT_GENERATED_IMAGE) self.__backend.pop_surface() self.__backend.push_surface() assert TestUtils.images_equal(TestUtils.TEXT_GENERATED_IMAGE, TestUtils.TEXT_EXPECTED_IMAGE) text.style().font().set_name("123") self.__backend.draw_text(text) assert TestUtils.images_equal(TestUtils.TEXT_GENERATED_IMAGE, TestUtils.TEXT_EXPECTED_IMAGE)
def test_draw_text(self): text = Text("abcdef123456!=_/>") text.scale((10, 20)) self.__backend.set_canvas_size(160, 30) self.__backend.create_canvas() self.__backend.push_surface() self.__backend.draw_text(text) self.__backend.export_to_file(TestUtils.TEXT_GENERATED_IMAGE) self.__backend.pop_surface() self.__backend.push_surface() assert TestUtils.images_equal(TestUtils.TEXT_GENERATED_IMAGE, TestUtils.TEXT_EXPECTED_IMAGE) text.style().font().set_name("123") self.__backend.draw_text(text) assert TestUtils.images_equal(TestUtils.TEXT_GENERATED_IMAGE, TestUtils.TEXT_EXPECTED_IMAGE)
def test_run(self): background = Background((7, 3)) polygon = TestUtils.generate_test_polygon(seed=0, points=12, radius_range=(1, 10)) polygon_copy = copy.deepcopy(polygon) objects = [background, polygon, Translatable()] self.__backend.draw_objects = MagicMock() self.__backend.create_canvas = MagicMock() self.__backend.export_to_file = MagicMock() self.__backend.run(objects, "testname") self.__backend.draw_objects.assert_called(objects) self.__backend.create_canvas.assert_called(objects) self.__backend.export_to_file.assert_called_with("testname") assert TestUtils.unordered_lists_equal( self.__backend.draw_objects.call_args[0][0], objects)
def test_frame(self): polygon = Polygon([Node(-1, 5), Node(4, 1)]) assert len(polygon.frame().paths()) == 1 assert len(polygon.frame().paths()[0]) == 2 assert TestUtils.unordered_lists_equal( [Node(-1, 5), Node(4, 1)], polygon.frame().paths()[0])
def test_draw_objects(self): polygon1 = TestUtils.generate_test_polygon(seed = 0, points = 12, radius_range = (1, 10)) polygon2 = copy.deepcopy(polygon1) polygon2.style().set_shadow('off') opengraph1 = TestUtils.generate_test_opengraph(seed = 0, points = 12, radius_range = (1, 10)) opengraph2 = copy.deepcopy(opengraph1) opengraph2.style().set_shadow('off') text = Text() arrow1 = RightArrow() arrow2 = RightArrow() arrow2.style().set_shadow('off') objects = [polygon1, polygon2, opengraph1, opengraph2, text, arrow2, arrow1] self.__backend.push_surface = MagicMock() self.__backend.pop_surface = MagicMock() self.__backend.draw_polygon_shadow = MagicMock() self.__backend.draw_polygon = MagicMock() self.__backend.draw_open_graph_shadow = MagicMock() self.__backend.draw_open_graph = MagicMock() self.__backend.draw_text = MagicMock() self.__backend.draw_text_shadow = MagicMock() self.__backend.translate = MagicMock() self.__backend.blur_surface = MagicMock() self.__backend.draw_objects(objects) assert self.__backend.push_surface.call_count == self.__backend.pop_surface.call_count assert self.__backend.translate.call_count == 2 assert self.__backend.draw_polygon.call_count == 4 self.__backend.draw_polygon.assert_any_call(polygon1) self.__backend.draw_polygon.assert_any_call(polygon2) self.__backend.draw_polygon.assert_any_call(arrow1) self.__backend.draw_polygon.assert_any_call(arrow2) assert self.__backend.draw_polygon_shadow.call_count == 2 self.__backend.draw_polygon_shadow.assert_any_call(polygon1) self.__backend.draw_polygon_shadow.assert_any_call(arrow1) assert self.__backend.draw_open_graph.call_count == 4 self.__backend.draw_open_graph.assert_any_call(polygon1.frame()) self.__backend.draw_open_graph.assert_any_call(polygon2.frame()) self.__backend.draw_open_graph.assert_any_call(opengraph1) self.__backend.draw_open_graph.assert_any_call(opengraph2) self.__backend.draw_open_graph_shadow.assert_called_once_with(opengraph1) self.__backend.draw_text.assert_called_once_with(text) assert self.__backend.blur_surface.call_count == 2
def test_create_canvas(self): self.__backend.create_canvas() self.__backend.export_to_file(TestUtils.EMPTY_CANVAS_GENERATED_IMAGE) assert TestUtils.images_equal(TestUtils.EMPTY_CANVAS_GENERATED_IMAGE, TestUtils.EMPTY_CANVAS_EXPECTED_IMAGE) with patch('os.makedirs', side_effect=OSError(errno.EPERM)): assert_raises(OSError, self.__backend.export_to_file, TestUtils.EMPTY_CANVAS_GENERATED_IMAGE)
def test_draw_open_graph(self): self.__backend.set_canvas_size(80, 80) self.__backend.create_canvas() graph = nx.Graph() open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph(open_graph) self.__backend.export_to_file(TestUtils.OPEN_GRAPH_EMPTY_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal( TestUtils.OPEN_GRAPH_EMPTY_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_EMPTY_EXPECTED_IMAGE ) graph.add_edge(Node(10, 10), Node(20, 10)) graph.add_edge(Node(20, 10), Node(20, 20)) graph.add_edge(Node(20, 20), Node(10, 20)) graph.add_edge(Node(10, 20), Node(10, 10)) open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph(open_graph) self.__backend.export_to_file(TestUtils.OPEN_GRAPH_CLOSED_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal( TestUtils.OPEN_GRAPH_CLOSED_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_CLOSED_EXPECTED_IMAGE ) graph = nx.Graph() edge_list = [ ((10, 10), (40, 10)), ((40, 10), (60, 10)), ((40, 10), (50, 40)), ((50, 40), (20, 30)), ((50, 40), (50, 60)), ] for edge in edge_list: graph.add_edge(Node(*edge[0]), Node(*edge[1])) graph.add_edge(Node(50, 60, "curve"), Node(20, 80, "curve")) graph.add_edge(Node(20, 80, "curve"), Node(40, 90, "curve")) open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph(open_graph) self.__backend.export_to_file(TestUtils.OPEN_GRAPH_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal(TestUtils.OPEN_GRAPH_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_EXPECTED_IMAGE)
def test_contains(self): polygon = TestUtils.generate_test_polygon(seed = 0, points = 12, radius_range = (5, 10)) assert polygon.contains((2,3)) == True assert polygon.contains((11,3)) == False assert polygon.contains((3,11)) == False polygon = Polygon([Node(0, 0), Node(4, 0), Node(17, 4), Node(0, 4), Node(0, 0)]) assert polygon.contains((8,1)) == False
def test_scale(self): original_g = TestUtils.generate_test_opengraph(seed = 0, points = 12, radius_range = (5, 5)) g = copy.deepcopy(original_g) scale = (2, 3) g.scale(scale) assert len(g.graph().nodes()) == len(original_g.graph().nodes()) for node in original_g.graph().nodes(): assert Node(node[0] * scale[0], node[1] * scale[1]) in g.graph().nodes()
def test_draw_open_graph(self): self.__backend.set_canvas_size(80, 80) self.__backend.create_canvas() graph = nx.Graph() open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph(open_graph) self.__backend.export_to_file( TestUtils.OPEN_GRAPH_EMPTY_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal( TestUtils.OPEN_GRAPH_EMPTY_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_EMPTY_EXPECTED_IMAGE) graph.add_edge(Node(10, 10), Node(20, 10)) graph.add_edge(Node(20, 10), Node(20, 20)) graph.add_edge(Node(20, 20), Node(10, 20)) graph.add_edge(Node(10, 20), Node(10, 10)) open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph(open_graph) self.__backend.export_to_file( TestUtils.OPEN_GRAPH_CLOSED_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal( TestUtils.OPEN_GRAPH_CLOSED_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_CLOSED_EXPECTED_IMAGE) graph = nx.Graph() edge_list = [((10, 10), (40, 10)), ((40, 10), (60, 10)), ((40, 10), (50, 40)), ((50, 40), (20, 30)), ((50, 40), (50, 60))] for edge in edge_list: graph.add_edge(Node(*edge[0]), Node(*edge[1])) graph.add_edge(Node(50, 60, 'curve'), Node(20, 80, 'curve')) graph.add_edge(Node(20, 80, 'curve'), Node(40, 90, 'curve')) open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph(open_graph) self.__backend.export_to_file(TestUtils.OPEN_GRAPH_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal(TestUtils.OPEN_GRAPH_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_EXPECTED_IMAGE)
def test_run(self): parser = NameParser() polygon = TestUtils.generate_test_polygon(seed = 0, points = 10, radius_range = (9, 10)) text1 = Text("abc") text2 = Text("def", (20, 0)) raw_data = '123' objects = [polygon, text1, text2] parser.run(raw_data, objects) assert polygon.names() == Set(['', 'abc'])
def test_blur_surface(self): test_img_surface = cairo.ImageSurface.create_from_png(TestUtils.BLUR_INPUT) self.__backend.set_image_size(test_img_surface.get_width(), test_img_surface.get_height()) self.__backend.set_margin(0, 0, 0, 0) self.__backend.push_surface() self.__backend.ctx().set_source_surface(test_img_surface) self.__backend.ctx().paint() self.__backend.blur_surface() self.__backend.export_to_file(TestUtils.BLUR_GENERATED_IMAGE) assert TestUtils.images_equal(TestUtils.BLUR_GENERATED_IMAGE, TestUtils.BLUR_EXPECTED_IMAGE), TestUtils.BLUR_GENERATED_IMAGE + " != " + TestUtils.BLUR_EXPECTED_IMAGE
def test_run(self): parser = NameParser() polygon = TestUtils.generate_test_polygon(seed=0, points=10, radius_range=(9, 10)) text1 = Text("abc") text2 = Text("def", (20, 0)) raw_data = '123' objects = [polygon, text1, text2] parser.run(raw_data, objects) assert polygon.names() == Set(['', 'abc'])
def test_draw_open_graph_shadow(self): self.__backend.set_canvas_size(50, 150) self.__backend.create_canvas() graph = nx.Graph() open_graph = OpenGraph(graph) self.__backend.push_surface() self.__backend.draw_open_graph_shadow(open_graph) self.__backend.export_to_file( TestUtils.OPEN_GRAPH_SHADOW_EMPTY_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal( TestUtils.OPEN_GRAPH_SHADOW_EMPTY_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_SHADOW_EMPTY_EXPECTED_IMAGE) edge_list = [((10, 10), (40, 10)), ((40, 10), (60, 10)), ((40, 10), (50, 40)), ((50, 40), (20, 30)), ((50, 40), (50, 60))] edge_list2 = [((10, 10), (40, 10)), ((40, 10), (50, 40)), ((50, 40), (20, 30)), ((20, 30), (10, 10))] for edge in edge_list: graph.add_edge(Node(*edge[0]), Node(*edge[1])) graph2 = nx.Graph() for edge in edge_list2: graph2.add_edge(Node(*edge[0], style='curve'), Node(*edge[1], style='curve')) open_graph = OpenGraph(graph) open_graph2 = OpenGraph(graph2) self.__backend.push_surface() self.__backend.draw_open_graph_shadow(open_graph) self.__backend.translate(0, 60) self.__backend.draw_open_graph_shadow(open_graph2) self.__backend.export_to_file( TestUtils.OPEN_GRAPH_SHADOW_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal( TestUtils.OPEN_GRAPH_SHADOW_GENERATED_IMAGE, TestUtils.OPEN_GRAPH_SHADOW_EXPECTED_IMAGE)
def test_blur_surface(self): test_img_surface = cairo.ImageSurface.create_from_png( TestUtils.BLUR_INPUT) self.__backend.set_image_size(test_img_surface.get_width(), test_img_surface.get_height()) self.__backend.set_margin(0, 0, 0, 0) self.__backend.push_surface() self.__backend.ctx().set_source_surface(test_img_surface) self.__backend.ctx().paint() self.__backend.blur_surface() self.__backend.export_to_file(TestUtils.BLUR_GENERATED_IMAGE) assert TestUtils.images_equal( TestUtils.BLUR_GENERATED_IMAGE, TestUtils.BLUR_EXPECTED_IMAGE ), TestUtils.BLUR_GENERATED_IMAGE + " != " + TestUtils.BLUR_EXPECTED_IMAGE
def test_contains(self): polygon = TestUtils.generate_test_polygon(seed=0, points=12, radius_range=(5, 10)) assert polygon.contains((2, 3)) == True assert polygon.contains((11, 3)) == False assert polygon.contains((3, 11)) == False polygon = Polygon( [Node(0, 0), Node(4, 0), Node(17, 4), Node(0, 4), Node(0, 0)]) assert polygon.contains((8, 1)) == False
def test_draw_polygon_shadow(self): point_list = [(10, 10), (40, 10), (30, 20), (40, 40), (20, 30), (10, 10)] node_list = [Node(*p) for p in point_list] polygon1 = Polygon(node_list) polygon2 = Polygon(node_list[:-1]) self.__backend.set_canvas_size(50, 100) self.__backend.create_canvas() self.__backend.push_surface() self.__backend.draw_polygon_shadow(polygon1) self.__backend.translate(0, 50) self.__backend.draw_polygon_shadow(polygon2) self.__backend.export_to_file(TestUtils.POLYGON_SHADOW_GENERATED_IMAGE) self.__backend.pop_surface() assert TestUtils.images_equal(TestUtils.POLYGON_SHADOW_GENERATED_IMAGE, TestUtils.POLYGON_SHADOW_EXPECTED_IMAGE)
def test_input(self): files = [ f for f in os.listdir(TestAllInput.INPUT_PATH) if os.path.isfile(os.path.join(TestAllInput.INPUT_PATH, f)) and os.path.splitext(f)[1] == '.shaape' ] results = [] for f in files: f_in = TestAllInput.INPUT_PATH + f f_out = TestAllInput.GENERATED_IMAGES_PATH + f + '.png' f_expected = TestAllInput.EXPECTED_IMAGES_PATH + f + '.png' shaape = Shaape(f_in, f_out) shaape.run() if False == TestUtils.images_equal(f_out, f_expected): results.append([f_out, f_expected]) if results: for result in results: print("cp " + result[0] + " " + result[1]) assert False, "Not all test input images equal their expected images, see image list."
def test_frame(self): polygon = Polygon([Node(-1, 5), Node(4, 1)]) assert len(polygon.frame().paths()) == 1 assert len(polygon.frame().paths()[0]) == 2 assert TestUtils.unordered_lists_equal([Node(-1, 5), Node(4, 1)], polygon.frame().paths()[0])