Пример #1
0
class TestSquare:
    _instance_count = 0

    def setup_class(self):
        self.square = Square(5)

    def test_name(self):
        rectangle = Square(1)
        assert rectangle.name == 'Квадрат_2'

    def test_area(self):
        assert self.square.area == 5 * 5

    def test_angles(self):
        assert self.square.angles == 4

    def test_perimeter(self):
        assert self.square.perimeter == 4 * 5

    def test_add_square(self):
        circle = Square(1)
        assert self.square.add_square(circle) == 25 + 1

    def test_add_square_neg(self):
        with pytest.raises(TypeError):
            not_shape = Shape()
            assert self.square.add_square(not_shape) == 25
Пример #2
0
    def testValidateSquare(self):
        s1 = ShapeFactory.build("square", Point(1, 1), Point(3, 1),
                                Point(3, 3), Point(1, 3))
        Square.validateSquare(s1, "Square unexpectedly invalid")

        self.assertRaises(
            ShapeException, Square.validateSquare, "(1, 1, 3, 1, 3, 3, 1, 3)",
            "String \'(1, 1, 3, 1, 3, 3, 3, 3)\' is not a valid square")
        self.assertRaises(ShapeException, Square.validateSquare, Point(1, 1),
                          "Point is not a valid square")
Пример #3
0
 def create_shape(type):
     if type == "Circle":
         return Circle()
     if type == "Square":
         return Square()
     else:
         raise TypeError("Shape {} is not known!".format(type))
Пример #4
0
    def testValidateRectangle(self):
        s1 = Square(1, 1, 3, 1, 3, 3, 1, 3)
        Validator.validateSquare(s1, "Square unexpectedly invalid")

        self.assertRaises(
            ShapeException, Validator.validateSquare,
            "(1, 1, 3, 1, 3, 3, 1, 3)",
            "String \'(1, 1, 3, 1, 3, 3, 3, 3)\' is not a valid square")
        self.assertRaises(ShapeException, Validator.validateSquare,
                          Point(1, 1), "Point is not a valid square")
Пример #5
0
    def testValidConstruction(self):
        p1 = Point(1, 3)
        p2 = Point(5, 3)
        p3 = Point(5, -1)
        p4 = Point(1, -1)
        s1 = Square(p1, p2, p3, p4)

        self.assertEqual(p1, s1.point1)
        self.assertEqual(p2, s1.point2)
        self.assertEqual(p3, s1.point3)
        self.assertEqual(p4, s1.point4)
        self.assertTrue(s1.line1.computeLength() == s1.line2.computeLength() ==
                        s1.line3.computeLength() == s1.line4.computeLength())

        l1 = Line(-3, 1, 1, 1)
        l2 = Line(1, 1, 1, -3)
        l3 = Line(1, -3, -3, -3)
        l4 = Line(-3, -3, -3, 1)
        s2 = Square(l1, l2, l3, l4)

        self.assertEqual(l1, s2.line1)
        self.assertEqual(l2, s2.line2)
        self.assertEqual(l3, s2.line3)
        self.assertEqual(l4, s2.line4)
        self.assertTrue(s2.line1.computeLength() == s2.line2.computeLength() ==
                        s2.line3.computeLength() == s2.line4.computeLength())

        s3 = Square(-.8, .4, 1.2, 2.4, 3.2, .4, 1.2, -1.6)
        self.assertEqual(-.8, s3.point1.x)
        self.assertEqual(.4, s3.point1.y)
        self.assertEqual(1.2, s3.point2.x)
        self.assertEqual(2.4, s3.point2.y)
        self.assertEqual(3.2, s3.point3.x)
        self.assertEqual(.4, s3.point3.y)
        self.assertEqual(1.2, s3.point4.x)
        self.assertEqual(-1.6, s3.point4.y)
        self.assertTrue(s3.line1.computeLength() == s3.line2.computeLength() ==
                        s3.line3.computeLength() == s3.line4.computeLength())
Пример #6
0
    def test_square(self):
        figure = {
            "type": "square",
            "x": 50,
            "y": 10,
            "size": 10,
            "color": "blue"
        }
        square = Square.build(figure)

        self.assertEqual(50, square.x)
        self.assertEqual(10, square.y)
        self.assertEqual("blue", square.color)
        self.assertEqual(10, square.size)

        figure = {"type": "square", "x": 50, "y": 10}
        self.assertRaises(ValueError, Square.build, figure)
Пример #7
0
    def build_figure(figure):
        if figure["type"] == "point":
            return Point.build(figure)

        if figure["type"] == "circle":
            return Circle.build(figure)

        if figure["type"] == "polygon":
            return Polygon.build(figure)

        if figure["type"] == "rectangle":
            return Rectangle.build(figure)

        if figure["type"] == "square":
            return Square.build(figure)

        raise ValueError("incorrect type")
Пример #8
0
def test_square_angles_equals_four():
    rectangle = Square(10, 6)
    assert rectangle.get_angles() == 4
Пример #9
0
 def test_add_square(self):
     circle = Square(1)
     assert self.square.add_square(circle) == 25 + 1
Пример #10
0
def test_name_square():
    rectangle = Square(4, 2)
    assert rectangle.get_name() == "Square"
Пример #11
0
def test_square_is_instance_figure():
    assert isinstance(Square(4, 2), BaseShape)
Пример #12
0
def test_squares_equals_area_after_add_area():
    square_1 = Square(4, 2)
    triangle_2 = Triangle(6, 2)
    assert square_1.add_area(triangle_2) == triangle_2.add_area(square_1)
Пример #13
0
def test_error_if_not_figure():
    rectangle = Square(4, 2)
    with pytest.raises(Exception):
        rectangle.add_area(object())
Пример #14
0
 def setup_class(self):
     self.square = Square(5)
Пример #15
0
    def run_command(self, input_list):
        """
    Checks the arguments of the input list are within the command list and run the associated command.\n
    Parameters: (1) user input parameters
    """
        # Create clear console function
        clear_console = lambda: os.system('cls')

        # Create command list
        command_list = [
            "rectangle", "square", "circle", "add", "shift", "move", "scale",
            "menu", "display", "exit"
        ]

        # Create parameters list
        parameters = list(input_list)
        parameters.pop(0)  # remove first element
        parameters = [int(i) for i in parameters]

        # Check if command is in command list
        if input_list[0] not in command_list:
            print(
                "First argument is invalid, check correct spelling provided!")

        # Create Rectangle
        elif input_list[0] == "rectangle" and len(input_list) == 5:

            # Set Rectangle object, store shape and return output
            r = Rectangle(parameters[0], parameters[1], parameters[2],
                          parameters[3])
            self._shapes[self._idx] = r
            r.display_stats()

            # Update shape index
            self._idx += 1

            print()
            print("Input the command 'menu' for the list of commands.")

        # Create Square
        elif input_list[0] == "square" and len(input_list) == 4:

            # Set Square object, store shape and return output
            s = Square(parameters[0], parameters[1], parameters[2])
            self._shapes[self._idx] = s
            s.display_stats()

            # Update shape index
            self._idx += 1

            print()
            print("Input the command 'menu' for the list of commands.")

        # Create Circle
        elif input_list[0] == "circle" and len(input_list) == 4:

            # Set Circle object, store shape and return output
            c = Circle(parameters[0], parameters[1], parameters[2])
            self._shapes[self._idx] = c
            c.display_stats()

            # Update shape index
            self._idx += 1

            print()
            print("Input the command 'menu' for the list of commands.")

        # Move or scale a shape
        elif (input_list[0] == "move"
              or input_list[0] == "scale") and len(input_list) == 4:

            # Check shape parameter is correct
            if parameters[0] not in self._shapes:
                print("Shape doesn't exist! Try a different index.")
            else:
                print("---------------------------------")
                print(f"Shape Number: {parameters[0]} updated")
                print("---------------------------------")

                # Move the selected shape
                if input_list[0] == "move":
                    self._shapes[parameters[0]].move(parameters[1],
                                                     parameters[2])

                # Scale the selected shape
                elif input_list[0] == "scale":
                    self._shapes[parameters[0]].scale(parameters[1],
                                                      parameters[2])

                # Display shape statistics
                self._shapes[parameters[0]].display_stats()

                print()
                print("Input the command 'menu' for the list of commands.")

        # Display add menu
        elif input_list[0] == "add":
            clear_console()
            self.add_menu()

        # Display shift menu or shapes
        elif input_list[0] == "shift" or input_list[0] == "display":

            # Check shape size
            if len(self._shapes) == 0:
                print("No shapes have been created! Create a shape first.")
            else:
                clear_console()

                # Show shift menu
                if input_list[0] == "shift":
                    self.shift_menu()

                # Display shape list
                for key, shape in self._shapes.items():
                    print("------------------------------")
                    print(f"Shape Number: {key}")
                    print("------------------------------")
                    shape.display_stats()
                    print()

                # Show additional print
                if input_list[0] == "display":
                    print("Input the command 'menu' for the list of commands.")

        # Display menu
        elif input_list[0] == "menu":
            clear_console()
            self.main_menu()

        # Exit the program
        elif input_list[0] == "exit":
            self.exit_program()

        # Check arguments length
        else:
            print(
                f"Incorrect argument length. Length provided: {len(input_list)}"
            )

        # Create extra space
        print()
Пример #16
0
def square():
    return Square(0)  # provide the fixture value
Пример #17
0
 def test_name(self):
     rectangle = Square(1)
     assert rectangle.name == 'Квадрат_2'
Пример #18
0
from shapes.circle import Circle
from shapes.rectangle import Rectangle
from shapes.square import Square
from shape_list.shape_list import ShapeList
from color.color import Color

shape_list = ShapeList()

try:
    shape_list.append(1)
except TypeError:
    print('Only shape append support')

shape_list.append(Circle(5, Color.BLUE))
shape_list.append(Rectangle(2, 3, Color.RED))
shape_list.append(Square(5, Color.BLUE))
shape_list.append(Ellipse(2, 3, Color.GREEN))

print(shape_list)
print(shape_list.sorted_shapes_areas(Color.BLUE))
print(Ellipse(4, 5, Color.BLUE).__hash__())
print(Ellipse(4, 5, Color.RED).__hash__())
print(Ellipse(5, 4, Color.BLUE).__hash__())
a = Circle(4, Color.BLUE)
d = Circle(4, Color.BLUE)
b = Circle(4, Color.RED)
c = Ellipse(4, 5, Color.BLUE)
print(a == c)  #False
print(a == b)  #False
print(a == d)  #True