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
[week1] 1주차 테스트 코드 10개 작성
  • Loading branch information
gardening-y authored Feb 27, 2024
2 parents 58c9d78 + 6295d77 commit 41a3422
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 2 deletions.
15 changes: 15 additions & 0 deletions testcode/.idea/git_toolbox_prj.xml

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

5 changes: 4 additions & 1 deletion testcode/.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 testcode/.idea/jpa-buddy.xml

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

5 changes: 4 additions & 1 deletion testcode/.idea/misc.xml

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

1 change: 1 addition & 0 deletions testcode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repositories {
dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation("org.assertj:assertj-core:3.25.1")
}

test {
Expand Down
65 changes: 65 additions & 0 deletions testcode/src/main/java/org/example/week1/Calculator.java
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");
}
}
}
6 changes: 6 additions & 0 deletions testcode/src/main/java/org/example/week1/CalculatorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.week1;

public enum CalculatorType {
DIGITAL,
ENGINEERING
}
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);
}
}
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);
}
}
14 changes: 14 additions & 0 deletions testcode/src/test/java/SampleTest.java
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 testcode/src/test/java/org/example/week1/CalculatorTest.java
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");
}
}

0 comments on commit 41a3422

Please sign in to comment.