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_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_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 generate_test_polygon(seed=0, points=12, radius_range=(10, 50)): random.seed(seed) point_list = [] for i in range(0, points): angle = math.radians(i * 360 / points - 180) radius = random.randint(*radius_range) point = (math.sin(angle) * radius, math.cos(angle) * radius) point_list.append(point) node_list = [Node(*p) for p in point_list] polygon = Polygon(node_list) return polygon
def test_scale(self): polygon = Polygon([Node(-1, 5), Node(4, 1)]) polygon.scale((2, 3)) assert polygon.nodes() == [Node(-2, 15), Node(8, 3)]
def test_min_max(self): polygon = Polygon([Node(-1, 5), Node(4, 1)]) assert polygon.min() == (-1, 1) assert polygon.max() == (4, 5)
def test_nodes(self): polygon = Polygon([Node(0, 0), Node(4, 0)]) assert polygon.nodes() == [Node(0, 0), Node(4, 0)]
def test_run(self): parser = StyleParser() arrow_style = Style('_arrow_', 'fill', ['red'], 1) fill_style = Style('.*', 'fill', ['blue'], 0) line_style = Style('_line_', 'fill', ['green'], 2) frame_style = Style('.*', 'frame', [[0.1, 0.2, 0.3]], 0) custom_fill_style = Style('(abc)|(def)', 'fill', [[0.3, 0.2, 0.3]], 3) custom_line_style = Style('line', 'fill', [[0.4, 0.3, 0.3]], 3) custom_frame_style = Style('(abc)|(def)', 'frame', [[0.5, 0.2, 0.3]], 3) polygon1 = Polygon([Node(0, 0)]) opengraph1 = OpenGraph() opengraph2 = OpenGraph() opengraph2.add_name('line') arrow = Arrow() background = Background() polygon2 = Polygon([Node(0, 0)]) polygon2.add_name('abc') polygon3 = Polygon([Node(0, 0)]) polygon3.add_name('def') objects = [ arrow_style, fill_style, line_style, frame_style, custom_fill_style, custom_line_style, custom_frame_style, polygon1, polygon2, polygon3, opengraph1, opengraph2, arrow, background ] parser.run([], objects) assert arrow.style().color() == arrow_style.color(), str( arrow.style().color()) + " != " + str(arrow_style.color()) assert polygon1.style().color() == fill_style.color(), str( polygon1.style().color()) + " != " + str(fill_style.color()) assert polygon1.frame().style().color() == frame_style.color() assert opengraph1.style().color() == line_style.color() assert opengraph2.style().color() == custom_line_style.color() assert polygon2.style().color() == custom_fill_style.color() assert polygon3.style().color() == custom_fill_style.color() assert polygon2.frame().style().color() == custom_frame_style.color() assert polygon3.frame().style().color() == custom_frame_style.color()
def test_apply_fill(self): drawable = Polygon([Node(0,0)]) self.__backend.create_canvas() drawable.style().add_color((0.1, 0.2, 0.3)) drawable.style().set_type('solid') self.__backend.apply_fill(drawable) assert type(self.__backend.ctx().get_source()) == cairo.SolidPattern assert self.__backend.ctx().get_source().get_rgba() == (0.1, 0.2, 0.3, 1.0) drawable = Polygon([Node(0,0)]) drawable.style().add_color((0.1, 0.2, 0.3, 0.4)) self.__backend.apply_fill(drawable) assert self.__backend.ctx().get_source().get_rgba() == (0.1, 0.2, 0.3, 0.4) drawable = Polygon([Node(0,0)]) drawable.style().add_color((0.1, 0.2, 0.3)) drawable.style().add_color((0.1, 0.2, 0.3)) self.__backend.apply_fill(drawable) assert type(self.__backend.ctx().get_source()) == cairo.LinearGradient drawable = Polygon([Node(0,0)]) drawable.style().add_color((0.1, 0.2, 0.3, 0.4)) drawable.style().add_color((0.1, 0.2, 0.3, 0.4)) self.__backend.apply_fill(drawable) assert type(self.__backend.ctx().get_source()) == cairo.LinearGradient
def test_apply_fill(self): drawable = Polygon([Node(0, 0)]) self.__backend.create_canvas() drawable.style().add_color((0.1, 0.2, 0.3)) drawable.style().set_type('solid') self.__backend.apply_fill(drawable) assert type(self.__backend.ctx().get_source()) == cairo.SolidPattern assert self.__backend.ctx().get_source().get_rgba() == (0.1, 0.2, 0.3, 1.0) drawable = Polygon([Node(0, 0)]) drawable.style().add_color((0.1, 0.2, 0.3, 0.4)) self.__backend.apply_fill(drawable) assert self.__backend.ctx().get_source().get_rgba() == (0.1, 0.2, 0.3, 0.4) drawable = Polygon([Node(0, 0)]) drawable.style().add_color((0.1, 0.2, 0.3)) drawable.style().add_color((0.1, 0.2, 0.3)) self.__backend.apply_fill(drawable) assert type(self.__backend.ctx().get_source()) == cairo.LinearGradient drawable = Polygon([Node(0, 0)]) drawable.style().add_color((0.1, 0.2, 0.3, 0.4)) drawable.style().add_color((0.1, 0.2, 0.3, 0.4)) self.__backend.apply_fill(drawable) assert type(self.__backend.ctx().get_source()) == cairo.LinearGradient
def test_run(self): parser = StyleParser() arrow_style = Style('_arrow_', 'fill', ['red'], 1) fill_style = Style('.*', 'fill', ['blue'], 0) line_style = Style('_line_', 'fill', ['green'], 2) frame_style = Style('.*', 'frame', [[0.1, 0.2, 0.3]], 0) custom_fill_style = Style('(abc)|(def)', 'fill', [[0.3, 0.2, 0.3]], 3) custom_line_style = Style('line', 'fill', [[0.4, 0.3, 0.3]], 3) custom_frame_style = Style('(abc)|(def)', 'frame', [[0.5, 0.2, 0.3]], 3) polygon1 = Polygon([Node(0, 0)]) opengraph1 = OpenGraph() opengraph2 = OpenGraph() opengraph2.add_name('line') arrow = Arrow() background = Background() polygon2 = Polygon([Node(0, 0)]) polygon2.add_name('abc') polygon3 = Polygon([Node(0, 0)]) polygon3.add_name('def') objects = [arrow_style, fill_style, line_style, frame_style, custom_fill_style, custom_line_style, custom_frame_style, polygon1, polygon2, polygon3, opengraph1, opengraph2, arrow, background] parser.run([], objects) assert arrow.style().color() == arrow_style.color(), str(arrow.style().color()) + " != " + str(arrow_style.color()) assert polygon1.style().color() == fill_style.color(), str(polygon1.style().color()) + " != " + str(fill_style.color()) assert polygon1.frame().style().color() == frame_style.color() assert opengraph1.style().color() == line_style.color() assert opengraph2.style().color() == custom_line_style.color() assert polygon2.style().color() == custom_fill_style.color() assert polygon3.style().color() == custom_fill_style.color() assert polygon2.frame().style().color() == custom_frame_style.color() assert polygon3.frame().style().color() == custom_frame_style.color()
def test_init(self): polygon = Polygon([Node(0, 0)]) assert polygon != None
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])