-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from DO-SOPT-SERVER/week1
[week1] 1주차 테스트 코드 10개 작성
- Loading branch information
Showing
11 changed files
with
230 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.example.week1; | ||
|
||
import java.util.Objects; | ||
|
||
public class Calculator { | ||
private CalculatorType calculatorType; | ||
|
||
public Calculator(CalculatorType calculatorType) { | ||
this.calculatorType = calculatorType; | ||
} | ||
|
||
public int add(Integer x, Integer y) { | ||
validateNull(x, y); | ||
return validateResult(x+y); | ||
} | ||
|
||
public int subtract(Integer x, Integer y) { | ||
validateNull(x, y); | ||
return validateResult(x-y); | ||
} | ||
|
||
public int multiply(Integer x, Integer y) { | ||
validateNull(x, y); | ||
return validateResult(x*y); | ||
} | ||
|
||
public int divide(Integer x, Integer y) { | ||
validateNull(x, y); | ||
validateDivisor(y); | ||
return validateResult((double) x/y); | ||
} | ||
|
||
protected void validateNull(Integer x, Integer y) { | ||
if(Objects.isNull(x) || Objects.isNull(y)){ | ||
throw new IllegalArgumentException("NULL"); | ||
} | ||
} | ||
|
||
protected int validateResult(double result) { | ||
int resultInteger = validateInteger(result); | ||
validateLimit(resultInteger); | ||
|
||
return resultInteger; | ||
} | ||
|
||
private int validateInteger(double result) { | ||
if (result != (int)result) { | ||
throw new IllegalArgumentException("Invalid integer value"); | ||
} | ||
|
||
return (int)result; | ||
} | ||
|
||
private void validateLimit(int result) { | ||
if (result < 0 || result > 100000) { | ||
throw new IllegalArgumentException("Invalid limit"); | ||
} | ||
} | ||
|
||
private void validateDivisor(int divisor) { | ||
if (divisor == 0) { | ||
throw new IllegalArgumentException("Divisor Zero"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.example.week1; | ||
|
||
public enum CalculatorType { | ||
DIGITAL, | ||
ENGINEERING | ||
} |
7 changes: 7 additions & 0 deletions
7
testcode/src/main/java/org/example/week1/DigitalCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.example.week1; | ||
|
||
public class DigitalCalculator extends Calculator { | ||
public DigitalCalculator() { | ||
super(CalculatorType.DIGITAL); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
testcode/src/main/java/org/example/week1/EngineeringCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.example.week1; | ||
|
||
public class EngineeringCalculator extends Calculator{ | ||
public EngineeringCalculator() { | ||
super(CalculatorType.ENGINEERING); | ||
} | ||
|
||
public int calculateRightTriangleWidth(Integer width, Integer height) { | ||
validateNull(width, height); | ||
return validateResult((double) width*height/2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SampleTest { | ||
@Test | ||
void 성공_일더하기일은2이다() { | ||
Assertions.assertThat(1+1).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
void 실패_일더하기일은3이다() { | ||
Assertions.assertThat(1+1).isEqualTo(3); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
testcode/src/test/java/org/example/week1/CalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.example.week1; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CalculatorTest { | ||
DigitalCalculator digitalCalculator = new DigitalCalculator(); | ||
EngineeringCalculator engineeringCalculator = new EngineeringCalculator(); | ||
|
||
@Test | ||
@DisplayName("두 개의 정수를 입력하면 두 정수의 더한 값을 출력한다.") | ||
void add_check() { | ||
int result = digitalCalculator.add(1, 4); | ||
Assertions.assertThat(result).isEqualTo(5); | ||
} | ||
|
||
@Test | ||
@DisplayName("입력 값에 Null이 존재할 경우 예외가 발생한다.") | ||
void addWithNullInput() { | ||
Assertions.assertThatThrownBy( | ||
() -> digitalCalculator.add(null, 2) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("NULL"); | ||
} | ||
|
||
@Test | ||
@DisplayName("결과 값이 100,000 초과일 경우 예외가 발생한다.") | ||
void addResultOver100000() { | ||
Assertions.assertThatThrownBy( | ||
() -> digitalCalculator.add(100000, 1) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Invalid limit"); | ||
} | ||
|
||
@Test | ||
@DisplayName("결과 값이 100,000 초과일 경우 예외가 발생한다.") | ||
void mulResultOver100000() { | ||
Assertions.assertThatThrownBy( | ||
() -> digitalCalculator.multiply(100001, 1) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Invalid limit"); | ||
} | ||
|
||
@Test | ||
@DisplayName("결과 값이 0 미만일 경우 예외가 발생한다.") | ||
void subResultUnder0() { | ||
Assertions.assertThatThrownBy( | ||
() -> digitalCalculator.subtract(1, 3) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Invalid limit"); | ||
} | ||
|
||
@Test | ||
@DisplayName("결과 값이 정수가 아닐 경우 예외가 발생한다.") | ||
void divResultNotInteger() { | ||
Assertions.assertThatThrownBy( | ||
() -> digitalCalculator.divide(7, 3) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Invalid integer value"); | ||
} | ||
|
||
@Test | ||
@DisplayName("나누는 값이 0일 경우 예외가 발생한다.") | ||
void divDivisorZero() { | ||
Assertions.assertThatThrownBy( | ||
() -> digitalCalculator.divide(7, 0) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Divisor Zero"); | ||
} | ||
|
||
@Test | ||
@DisplayName("직각 삼각형의 밑변과 높이를 입력하면, 직각 삼각형의 넓이를 출력한다.") | ||
void triangle_check() { | ||
int result = engineeringCalculator.calculateRightTriangleWidth(7, 8); | ||
Assertions.assertThat(result).isEqualTo(28); | ||
} | ||
|
||
@Test | ||
@DisplayName("직각 삼각형의 넓이가 정수가 아닐 경우 예외가 발생한다.") | ||
void triangleResultNotInteger() { | ||
Assertions.assertThatThrownBy( | ||
() -> engineeringCalculator.calculateRightTriangleWidth(7, 3) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Invalid integer value"); | ||
} | ||
|
||
@Test | ||
@DisplayName("직각 삼각형의 넓이가 100,000 초과일 경우 예외가 발생한다.") | ||
void triangleResultOver100000() { | ||
Assertions.assertThatThrownBy( | ||
() -> engineeringCalculator.calculateRightTriangleWidth(100001, 2) | ||
).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("Invalid limit"); | ||
} | ||
} |