mercredi 18 décembre 2019

How to test below code using Spock & groovy?

I am a fresher i was asked to develop the below code and put test case using spock and groovy.I do no know to invoke the class in groovy for check outputs of each operations like rad,diff X, diff Y etc. kindly help me.

 public static void main(String[] args) {
    RectangularShape rectangularShape
    rectangularShape = new Rectangle2D.Double(10,20,30,50)
    Rectangle2D rectangle = rotateRect(rectangularShape,30)
     println(rectangle)
 }

static Rectangle2D rotateRect (RectangularShape rect, int angle, Point2D pivot = null ) {

    if (pivot == null)
    {
        pivot = new Point2D.Double(rect.x + (int) Math.round(rect.width / 2d), rect.y + (int) Math.round(rect.height / 2d))


    }

    double rad = angle * Math.PI / 180;

    double diffX = rect.x - pivot.x

    double diffY = rect.y - pivot.y

    int x = (int) Math.round(rect.x - diffX + (diffX * Math.cos(rad)) + (diffY * Math.sin(rad)))

    int y = (int) Math.round(rect.y - diffY + (diffY * Math.cos(rad)) - (diffX * Math.sin(rad)))

    return new Rectangle2D.Double(x, y, rect.width, rect.height)
}

Aucun commentaire:

Enregistrer un commentaire