Skip to content

Commit

Permalink
[enhancement] Added a getRotation() method to a YaegerEntity (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
meronbrouwer committed Oct 3, 2023
1 parent 3617f4a commit 2c5e9ed
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 22 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/github/hanyaeger/api/entities/Rotatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ default void setRotate(final double degrees) {
getNode().ifPresentOrElse(node -> node.setRotate(-degrees), () -> getInitializationBuffer().setRotation(degrees));
}

/**
* Return the rotation of the {@link YaegerEntity}. This will be a value in degrees and will range
* between 0 and 360.
*
* @return the rotation in degrees as a {@code double}
*/
default double getRotation(){
if (getNode().isPresent()){
return absoluteAndModulo360(getNode().get().getRotate());
} else {
return absoluteAndModulo360(getInitializationBuffer().getRotation());
}
}

private double absoluteAndModulo360(final double value){
return Math.abs(value % 360);
}

/**
* Return an instance of {@link InitializationBuffer} to be used whenever a {@link javafx.scene.Node} is unavailable
* to apply the rotation.
Expand Down
143 changes: 121 additions & 22 deletions src/test/java/com/github/hanyaeger/api/entities/RotatableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,141 @@

import com.github.hanyaeger.core.entities.motion.InitializationBuffer;
import javafx.scene.Node;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class RotatableTest {

public static final int DEGREES = 37;
private RotatableImpl sut;

@Test
void setRotateDelegatesToNode() {
// Arrange
var sut = new RotatableImpl();
var node = mock(Node.class, withSettings().withoutAnnotations());
sut.setNode(node);
@BeforeEach
void setup() {
sut = new RotatableImpl();
}

@Nested
class RotatableWithoutNodeSet {

private InitializationBuffer rotationBuffer;

@BeforeEach
void setup() {

rotationBuffer = mock(InitializationBuffer.class);
sut.setRotationBuffer(rotationBuffer);
}

@Test
void setRotateDelegatesToRotationBufferIfNodeNotAvailable() {
// Arrange

// Act
sut.setRotate(DEGREES);

// Assert
verify(rotationBuffer).setRotation(DEGREES);
}

@Test
void getRotationDelegatesToRotationBufferIfNodeNotAvailable() {
// Arrange

// Act
sut.getRotation();

// Assert
verify(rotationBuffer).getRotation();
}

@Test
void getRotationFromRotationBufferIsAValueBetween0And360IfValueExceeds360() {
// Arrange
when(rotationBuffer.getRotation()).thenReturn(397D);

// Act
var actual = sut.getRotation();

// Assert
assertEquals(37D, actual);
}

@Test
void getRotationFromRotationBufferIsAbsoluteValue() {
// Arrange
when(rotationBuffer.getRotation()).thenReturn(-397D);

// Act
sut.setRotate(DEGREES);
// Act
var actual = sut.getRotation();

// Assert
verify(node).setRotate(-DEGREES);
// Assert
assertEquals(37D, actual);
}
}

@Test
void setRotateDelegatesToRotationBufferIfNodeNotAvailable() {
// Arrange
var sut = new RotatableImpl();
var rotationBuffer = mock(InitializationBuffer.class);
sut.setRotationBuffer(rotationBuffer);
@Nested
class RotatableWithNodeSet {

private Node node;

@BeforeEach
void setup() {
node = mock(Node.class, withSettings().withoutAnnotations());
sut.setNode(node);
}

@Test
void setRotateDelegatesToNode() {
// Arrange

// Act
sut.setRotate(DEGREES);

// Assert
verify(node).setRotate(-DEGREES);
}

@Test
void getRotationDelegatesToNode() {
// Arrange

// Act
sut.setRotate(DEGREES);
// Act
sut.getRotation();

// Assert
verify(rotationBuffer).setRotation(DEGREES);
// Assert
verify(node).getRotate();
}

@Test
void getRotationFromRotationBufferIsAValueBetween0And360IfValueExceeds360() {
// Arrange
when(node.getRotate()).thenReturn(397D);

// Act
var actual = sut.getRotation();

// Assert
assertEquals(37D, actual);
}

@Test
void getRotationFromRotationBufferIsAbsoluteValue() {
// Arrange
when(node.getRotate()).thenReturn(-397D);

// Act
var actual = sut.getRotation();

// Assert
assertEquals(37D, actual);
}
}

private static class RotatableImpl implements Rotatable {
Expand All @@ -54,7 +153,7 @@ public Optional<? extends Node> getNode() {
}
}

public void setNode(Node node) {
public void setNode(final Node node) {
this.node = node;
}

Expand All @@ -63,7 +162,7 @@ public InitializationBuffer getInitializationBuffer() {
return initializationBuffer;
}

public void setRotationBuffer(InitializationBuffer initializationBuffer) {
public void setRotationBuffer(final InitializationBuffer initializationBuffer) {
this.initializationBuffer = initializationBuffer;
}
}
Expand Down

0 comments on commit 2c5e9ed

Please sign in to comment.