Skip to content

Commit

Permalink
Merge pull request #1 from DO-SOPT-SERVER/week1
Browse files Browse the repository at this point in the history
1주차 테스트 코드 10개 작성
  • Loading branch information
unanchoi authored Mar 1, 2024
2 parents 3e2f017 + 39fe511 commit 2227af6
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 47 deletions.
1 change: 1 addition & 0 deletions testtest/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions testtest/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 49 additions & 1 deletion testtest/src/main/java/org/unan/week1/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// 계산기 유형이 DIGITAL, ENGINEERING
public abstract class Calculator {
private static final int MAX_VALUE = 100000;
private CalculatorType type;

public Calculator(CalculatorType type) {
Expand All @@ -16,13 +17,60 @@ public int add(Integer x, Integer y) {
throw new IllegalArgumentException("NULL");
}

if (x+y > 100000) {
if (x+y > MAX_VALUE) {
throw new IllegalArgumentException("LIMIT");
}

if (x+y < 0) {
throw new IllegalArgumentException("NEGATIVE");
}
return x+y;
}

public int subtract(Integer x, Integer y) {
if (Objects.isNull(x) || Objects.isNull(y)) {
throw new IllegalArgumentException("NULL");
}

if (x-y > MAX_VALUE) {
throw new IllegalArgumentException("LIMIT");
}

if (x-y < 0) {
throw new IllegalArgumentException("NEGATIVE");
}
return x-y;
}

public int multiply(Integer x, Integer y) {
if (Objects.isNull(x) || Objects.isNull(y)) {
throw new IllegalArgumentException("NULL");
}

if (x * y > MAX_VALUE) {
throw new IllegalArgumentException("LIMIT");
}

if (x * y < 0) {
throw new IllegalArgumentException("NEGATIVE");
}

return x*y;
}

public int divide(Integer x, Integer y) {
if (Objects.isNull(x) || Objects.isNull(y)) {
throw new IllegalArgumentException("NULL");
}

if (y == 0) {
throw new IllegalArgumentException("ZERO");
}

if (x / y < 0) {
throw new IllegalArgumentException("NEGATIVE");
}

return x / y;
}
}
15 changes: 15 additions & 0 deletions testtest/src/main/java/org/unan/week1/EngineeringCalculator.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package org.unan.week1;


import java.util.Objects;

public class EngineeringCalculator extends Calculator{
public EngineeringCalculator() {
super(CalculatorType.ENGINEERING);
}

public int areaOfVerticalTriangle(final Integer base, final Integer height) {
if (Objects.isNull(base) || Objects.isNull(height)) {
throw new IllegalArgumentException("NULL");
}

if (base % 2 != 0 || height % 2 != 0) {
throw new IllegalArgumentException("NOTINT");
}

return (int) base * height / 2;
}

}
162 changes: 141 additions & 21 deletions testtest/src/test/java/org/unan/week1/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.unan.week1;


import com.sun.jdi.IntegerType;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class CalculatorTest {
Expand All @@ -11,33 +13,151 @@ class CalculatorTest {
private static final Integer INVALID_NEGATIVE_NUMBER = -1;
private static final Integer ONE = 1;

@Nested
public class AddTest {
@Test
@DisplayName("입력 값을 넣으면 더한 값을 출력해준다.")
void add() {
DigitalCalculator digitalCalculator = new DigitalCalculator();
EngineeringCalculator engineeringCalculator = new EngineeringCalculator();
int result = digitalCalculator.add(ONE,1);
int result2 = engineeringCalculator.add(ONE,1);

@Test
@DisplayName("입력 값을 넣으면 더한 값을 출력해준다.")
void add() {
DigitalCalculator calculator = new DigitalCalculator();
int action = calculator.add(ONE,1);
Assertions.assertThat(action).isEqualTo(2);
Assertions.assertThat(result).isEqualTo(2);
Assertions.assertThat(result2).isEqualTo(2);
}

@Test
@DisplayName("입력 값 중에 Null이 있으면, 계산이 이루어지지 않는다.")
void addWithNullInput() {
DigitalCalculator calculator = new DigitalCalculator();

Assertions.assertThatThrownBy(
() -> {calculator.add(NULL_NUMBER, ONE);}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("NULL");
}

@Test
@DisplayName("입력 값을 더했을 때 10만이 넘으면 예외가 발생한다.")
void addResultExceed100000() {
DigitalCalculator calculator = new DigitalCalculator();
Assertions.assertThatThrownBy(
() -> {calculator.add(50000, 50001);}
).isInstanceOf(IllegalArgumentException.class);

}
}

@Test
@DisplayName("입력 값 중에 Null이 있으면, 계산이 이루어지지 않는다.")
void addWithNullInput() {
DigitalCalculator calculator = new DigitalCalculator();
@Nested
public class SubtractTest {

@Test
@DisplayName("입력 값을 넣으면 뺀 값을 출력해준다.")
void subtract() {
DigitalCalculator digitalCalculator = new DigitalCalculator();
EngineeringCalculator engineeringCalculator = new EngineeringCalculator();
int result = digitalCalculator.subtract(ONE,1);
int result2 = engineeringCalculator.subtract(ONE,1);

Assertions.assertThat(result).isEqualTo(0);
Assertions.assertThat(result2).isEqualTo(0);
}

@Test
@DisplayName("입력한 값 중 null이 있으면 빼기 연산이 되지 않는다.")
void subtractWithNull() {
// given
Integer x = 1;
Integer y = null;
DigitalCalculator digitalCalculator = new DigitalCalculator();
Assertions.assertThatThrownBy(
() -> {
digitalCalculator.subtract(x, y);
}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("NULL");

}

@Test
@DisplayName("빼기 결과는 최댓값을 넘으면 안된다.")
void subtractWithMaxResult() {
DigitalCalculator digitalCalculator = new DigitalCalculator();
int max = 100000;
Assertions.assertThatThrownBy(
() -> {
digitalCalculator.subtract(-1, -100002);
}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("LIMIT");

}

Assertions.assertThatThrownBy(
() -> {calculator.add(NULL_NUMBER, ONE);}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("NULL");
}

@Test
@DisplayName("입력 값을 더했을 때 10만이 넘으면 예외가 발생한다.")
void addResultExceed100000() {
DigitalCalculator calculator = new DigitalCalculator();
Assertions.assertThatThrownBy(
() -> {calculator.add(50000, 50001);}
).isInstanceOf(IllegalArgumentException.class);
@Nested
public class MultiplyTest {

@Test
@DisplayName("빈 값을 입력시에, 곱하기 연산이 이루어지지 않는다.")
void multiplyWithNullParameter() {
DigitalCalculator digitalCalculator = new DigitalCalculator();
Assertions.assertThatThrownBy(
() -> {
digitalCalculator.multiply(2, null);
}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("NULL");

}

}

@Nested
public class DivideTest {

@Test
@DisplayName("빈 값을 입력시에, 나누기 연산이 이루어지지 않는다.")
void divideWithNullValue() {
DigitalCalculator digitalCalculator = new DigitalCalculator();
Assertions.assertThatThrownBy(
() -> {
digitalCalculator.divide(1, null);
}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("NULL");
}

@Test
@DisplayName("Divisor가 0인 경우 나누기 연산이 이루어지지 않는다.")
void divideWithZeroDivisor() {
DigitalCalculator digitalCalculator = new DigitalCalculator();
Assertions.assertThatThrownBy(
() -> {
digitalCalculator.divide(1 ,0);
}
).isInstanceOf(IllegalArgumentException.class);
}

}

@Nested
public class EngineerCalculatorTest {

@Test
@DisplayName("직각 삼각형의 넓이는 정수여야 한다.")
void areaWithFloatResult() {
final Integer x = 2;
final Integer y = 3;

EngineeringCalculator engineeringCalculator = new EngineeringCalculator();
Assertions.assertThatThrownBy(
() -> {engineeringCalculator.areaOfVerticalTriangle(x,y);}
).isInstanceOf(IllegalArgumentException.class)
.hasMessage("NOTINT");
}


}

Expand Down
25 changes: 0 additions & 25 deletions testtest/src/test/java/org/unan/week1/PetTEst.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.unan.week1.fixture;

public class EngineerCalculatorTest {
}

0 comments on commit 2227af6

Please sign in to comment.