From b2d4bc1a4f1a8d2763c5568a23997dfc0d84b2f4 Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Wed, 5 Feb 2020 13:13:11 +0900 Subject: [PATCH 01/10] =?UTF-8?q?String=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=95=99=EC=8A=B5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20(=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=201,?= =?UTF-8?q?2,3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 ++++ src/test/java/empty.txt | 0 src/test/java/study/StringTest.java | 37 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) delete mode 100644 src/test/java/empty.txt create mode 100644 src/test/java/study/StringTest.java diff --git a/build.gradle b/build.gradle index 1b85b74c..e8c022a1 100644 --- a/build.gradle +++ b/build.gradle @@ -11,4 +11,8 @@ repositories { dependencies { testCompile('org.junit.jupiter:junit-jupiter:5.4.2') testCompile('org.assertj:assertj-core:3.11.1') +} + +test { + useJUnitPlatform() } \ No newline at end of file diff --git a/src/test/java/empty.txt b/src/test/java/empty.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java new file mode 100644 index 00000000..60f18c8e --- /dev/null +++ b/src/test/java/study/StringTest.java @@ -0,0 +1,37 @@ +package study; + +import org.assertj.core.api.Assertions; +import org.assertj.core.api.Assertions.*; +import org.junit.jupiter.api.Test; + +public class StringTest { + @Test + void split() { + String value1 = "1,2"; + String value2 = "1"; + + String[] result1 = value1.split(","); + Assertions.assertThat(result1).contains("1"); + Assertions.assertThat(result1).contains("2"); + + String[] result2 = value2.split(","); + Assertions.assertThat(result2).containsExactly("1"); + } + + @Test + void substring() { + String value = "(1,2)"; + String result = value.substring(1, 4); + + Assertions.assertThat(result).isEqualTo("1,2"); + } + + @Test + void charAt() { + Assertions.assertThatThrownBy(()-> { + String value = "abc"; + char value2 = value.charAt(4); + }).isInstanceOf(IndexOutOfBoundsException.class).hasMessageContaining("String index out of range: 4"); + + } +} From 3589f36538925125e96154bb8a12148cff4f0741 Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Thu, 6 Feb 2020 20:33:37 +0900 Subject: [PATCH 02/10] =?UTF-8?q?String=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=ED=95=99=EC=8A=B5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20(=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=203)?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/StringTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index 60f18c8e..4a326ca0 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -1,7 +1,8 @@ package study; import org.assertj.core.api.Assertions; -import org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; public class StringTest { @@ -27,11 +28,17 @@ void substring() { } @Test + @DisplayName("String 범위 확인") void charAt() { Assertions.assertThatThrownBy(()-> { String value = "abc"; char value2 = value.charAt(4); - }).isInstanceOf(IndexOutOfBoundsException.class).hasMessageContaining("String index out of range: 4"); + }).isInstanceOf(IndexOutOfBoundsException.class).hasMessageContaining("index out of range: 4"); + Assertions.assertThatExceptionOfType(IndexOutOfBoundsException.class) + .isThrownBy(() -> { + String value = "abc"; + char value2 = value.charAt(2); + }).withMessageMatching("String index out of range: \\d+"); } } From 019938009721a503744d7ce2459156eaa855de30 Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Thu, 6 Feb 2020 20:34:36 +0900 Subject: [PATCH 03/10] =?UTF-8?q?Set=20Collection=20=ED=95=99=EC=8A=B5=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20(=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=201,=202,=203)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/study/SetTest.java | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/test/java/study/SetTest.java diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java new file mode 100644 index 00000000..28028a3a --- /dev/null +++ b/src/test/java/study/SetTest.java @@ -0,0 +1,45 @@ +package study; + + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.HashSet; +import java.util.Set; + +public class SetTest { + private Set numbers; + + @BeforeEach + void setUp() { + numbers = new HashSet<>(); + numbers.add(1); + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + // Test Case 구현 + @Test + void size(){ + int size = numbers.size(); + Assertions.assertThat(size).isEqualTo(3); + } + + @ParameterizedTest + @ValueSource(ints = {6,2,3}) + void contains(int number) { + Assertions.assertThat(numbers.contains(number)).isTrue(); + } + + @ParameterizedTest + @CsvSource({"9,false", "2,false", "3,true"}) + void contains2(int input, boolean expected) { + boolean result = numbers.contains(input); + Assertions.assertThat(result).isEqualTo(expected); + } +} From 62946238c11f76eeefb04076b96109e65d7a170b Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Fri, 7 Feb 2020 09:59:42 +0900 Subject: [PATCH 04/10] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 34 ++++++++++++++++++++++++++ src/main/java/Main.java | 7 ++++++ src/main/java/Operator.java | 46 +++++++++++++++++++++++++++++++++++ src/main/java/Validator.java | 31 +++++++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/Operator.java create mode 100644 src/main/java/Validator.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 00000000..e09e61a3 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,34 @@ + +import java.util.Scanner; + +public class Calculator { + String value; + String[] values; + double result = 0; + + public Calculator() { + Scanner scanner = new Scanner(System.in); + value = scanner.nextLine(); + values = value.split(" "); + Validator.validateInput(values); + } + + void calculate() { + result = Double.parseDouble(values[0]); + + int length = values.length; + Operator nowOperator = null; + for (int i = 1; i < length; i++) { + if (i % 2 == 1) { // 연산자일 때 + nowOperator = Operator.getOperatorByString(values[i]); + } + else { // 숫자가 나올 때 + result = nowOperator.operate(result, Double.parseDouble(values[i])); + } + } + } + + void printResult() { + System.out.print("결과: "+result); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000..7a77fded --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,7 @@ +public class Main { + public static void main(String[] args) { + Calculator calculator = new Calculator(); + calculator.calculate(); + calculator.printResult(); + } +} diff --git a/src/main/java/Operator.java b/src/main/java/Operator.java new file mode 100644 index 00000000..15235bef --- /dev/null +++ b/src/main/java/Operator.java @@ -0,0 +1,46 @@ +public enum Operator { + PLUS("+") { + double operate(double num1, double num2) { + return num1 + num2; + } + }, + MINUS("-") { + double operate(double num1, double num2) { + return num1 - num2; + } + }, + MUL("*") { + double operate(double num1, double num2) { + return num1 * num2; + } + }, + DIV("/") { + double operate(double num1, double num2) throws ArithmeticException { + try { + return num1 / num2; + } catch (ArithmeticException ae) { + + } + throw new IllegalArgumentException("0으로 나눌 수 없습니다.."); + } + }; + + private String stringOperator; + + Operator(String stringOperator) { + this.stringOperator = stringOperator; + } + + static Operator getOperatorByString(String stringOperator) { + Operator[] operators = values(); + + for (Operator o : operators) { + if (o.stringOperator.equals(stringOperator)) { + return o; + } + } + throw new IllegalArgumentException("연산자가 잘못되었습니다."); + } + + abstract double operate(double num1, double num2) ; +} diff --git a/src/main/java/Validator.java b/src/main/java/Validator.java new file mode 100644 index 00000000..9f371d78 --- /dev/null +++ b/src/main/java/Validator.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class Validator { + public static void validateInput(String[] values) { + int length = values.length; + + if (length % 2 == 0) + throw new IllegalArgumentException("연산자와 숫자가 맞지 않습니다."); + + for (int i = 0; i < length; i++) { + if (i % 2 == 0) { + validateDouble(values[i]); + } + else { + validateOperator(values[i]); + } + } + } + + static void validateDouble(String input) { + try { + Double.parseDouble(input); + } catch (Exception e) { + throw new IllegalArgumentException("피연산자가 잘못되었습니다."); + } + } + + static void validateOperator(String input) { + Operator.getOperatorByString(input); + } +} From 1abbf3084f4ecea6480a75c7d3a0a696814b0c7c Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Fri, 7 Feb 2020 09:59:52 +0900 Subject: [PATCH 05/10] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/CalculatorTest.java | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/CalculatorTest.java diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java new file mode 100644 index 00000000..0f7d81f7 --- /dev/null +++ b/src/test/java/CalculatorTest.java @@ -0,0 +1,58 @@ +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +public class CalculatorTest { + @ParameterizedTest + @DisplayName("실수형 검증") + @CsvSource(value = {"3:true", "1.9:true", "문자 입력:false", + "+:false", "-1:true"}, delimiter = ':') + void validateDoubleTest(String input, boolean expected) { + boolean actualResult = true; + try { + Validator.validateDouble(input); + } catch (Exception e) { + actualResult = false; + } + Assertions.assertThat(actualResult).isEqualTo(expected); + } + + @ParameterizedTest + @DisplayName("연산자 검증") + @CsvSource(value = {"3:false", "+:true", "/:true", + "문자입력:false", "):false"}, delimiter = ':') + void validateOperatorTest(String input, boolean expected) { + boolean actualResult = true; + try { + Validator.validateOperator(input); + } catch (Exception e) { + actualResult = false; + } + Assertions.assertThat(actualResult).isEqualTo(expected); + } + + @ParameterizedTest + @DisplayName("연산자 enum 검증") + @CsvSource(value = {"3:false", "+:true", "/:true", + "문자입력:false", "):false"}, delimiter = ':') + void getOperatorByStringTest(String input, boolean expected){ + boolean actualResult = true; + try { + Operator.getOperatorByString(input); + } catch (Exception e) { + actualResult = false; + } + Assertions.assertThat(actualResult).isEqualTo(expected); + } + + @ParameterizedTest + @DisplayName("연산 결과 검증") + @CsvSource(value = {"+:1:2:3", "-:2:1:1", "/:1:0:0", "*:2:3:6"}, delimiter =':') + void operateTest(String stringOperator, double num1, double num2, double expected){ + Operator operator = Operator.getOperatorByString(stringOperator); + double actualResult = operator.operate(num1, num2); + + Assertions.assertThat(actualResult).isEqualTo(expected); + } +} From c4c6cd5d5c737cc766785f4786703d8a71997a1a Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Fri, 7 Feb 2020 16:51:55 +0900 Subject: [PATCH 06/10] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20=EA=B8=B0=EB=8A=A5=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 34 +++++++------------- src/main/java/InputValues.java | 58 ++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 2 ++ src/main/java/Operator.java | 12 +++---- src/main/java/Validator.java | 31 ------------------ src/main/java/empty.txt | 0 6 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 src/main/java/InputValues.java delete mode 100644 src/main/java/Validator.java delete mode 100644 src/main/java/empty.txt diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index e09e61a3..339566b2 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -1,34 +1,22 @@ - -import java.util.Scanner; - public class Calculator { - String value; - String[] values; - double result = 0; - - public Calculator() { - Scanner scanner = new Scanner(System.in); - value = scanner.nextLine(); - values = value.split(" "); - Validator.validateInput(values); - } + private final int FIRST = 0; + private double result = 0; void calculate() { - result = Double.parseDouble(values[0]); + int length = InputValues.getValuesLength(); + double nextNumber = 0; - int length = values.length; + result = Double.parseDouble(InputValues.getValueByIndex(FIRST)); Operator nowOperator = null; - for (int i = 1; i < length; i++) { - if (i % 2 == 1) { // 연산자일 때 - nowOperator = Operator.getOperatorByString(values[i]); - } - else { // 숫자가 나올 때 - result = nowOperator.operate(result, Double.parseDouble(values[i])); - } + for (int i = 1; i < length; i += 2) { + nowOperator = Operator.getOperatorByString(InputValues.getValueByIndex(i)); + nextNumber = Double.parseDouble(InputValues.getValueByIndex(i + 1)); + result = nowOperator.operate(result, nextNumber); } } + void printResult() { - System.out.print("결과: "+result); + System.out.print("결과: " + result); } } diff --git a/src/main/java/InputValues.java b/src/main/java/InputValues.java new file mode 100644 index 00000000..caa81796 --- /dev/null +++ b/src/main/java/InputValues.java @@ -0,0 +1,58 @@ +import java.util.Scanner; + +public class InputValues { + private static String value; + private static String[] values; + + public InputValues() { + System.out.print("문자열 수식을 입력해주세요: "); + Scanner scanner = new Scanner(System.in); + + value = scanner.nextLine(); + values = value.split(" "); + validateDouble(); + validateOperator(); + validateEndWithOperator(); + } + + public InputValues(String value){ + this.value = value; + values = value.split(" "); + } + + void validateDouble() { + int length = values.length; + + for (int i = 0; i < length; i += 2) { + try { + Double.parseDouble(values[i]); + } catch (Exception e) { + throw new IllegalArgumentException("피연산자가 잘못되었습니다."); + } + } + + } + + void validateOperator() { + int length = values.length; + + for (int i = 1; i < length; i += 2) { + Operator.getOperatorByString(values[i]); + } + } + + void validateEndWithOperator() { + int length = values.length; + + if (length % 2 == 0) + throw new IllegalArgumentException("연산자와 숫자가 맞지 않습니다."); + } + + public static String getValueByIndex(int index) { + return values[index]; + } + + public static int getValuesLength() { + return values.length; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7a77fded..18af6665 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,8 @@ public class Main { public static void main(String[] args) { + InputValues inputValues = new InputValues(); Calculator calculator = new Calculator(); + calculator.calculate(); calculator.printResult(); } diff --git a/src/main/java/Operator.java b/src/main/java/Operator.java index 15235bef..077269c4 100644 --- a/src/main/java/Operator.java +++ b/src/main/java/Operator.java @@ -15,13 +15,13 @@ public enum Operator { } }, DIV("/") { - double operate(double num1, double num2) throws ArithmeticException { - try { - return num1 / num2; - } catch (ArithmeticException ae) { + double operate(double num1, double num2) { + double result = num1 / num2; + if (Double.isInfinite(result) || Double.isNaN(result)) { + throw new IllegalArgumentException("0으로 나눌 수 없습니다."); } - throw new IllegalArgumentException("0으로 나눌 수 없습니다.."); + return result; } }; @@ -42,5 +42,5 @@ static Operator getOperatorByString(String stringOperator) { throw new IllegalArgumentException("연산자가 잘못되었습니다."); } - abstract double operate(double num1, double num2) ; + abstract double operate(double num1, double num2); } diff --git a/src/main/java/Validator.java b/src/main/java/Validator.java deleted file mode 100644 index 9f371d78..00000000 --- a/src/main/java/Validator.java +++ /dev/null @@ -1,31 +0,0 @@ -import java.util.Scanner; - -public class Validator { - public static void validateInput(String[] values) { - int length = values.length; - - if (length % 2 == 0) - throw new IllegalArgumentException("연산자와 숫자가 맞지 않습니다."); - - for (int i = 0; i < length; i++) { - if (i % 2 == 0) { - validateDouble(values[i]); - } - else { - validateOperator(values[i]); - } - } - } - - static void validateDouble(String input) { - try { - Double.parseDouble(input); - } catch (Exception e) { - throw new IllegalArgumentException("피연산자가 잘못되었습니다."); - } - } - - static void validateOperator(String input) { - Operator.getOperatorByString(input); - } -} diff --git a/src/main/java/empty.txt b/src/main/java/empty.txt deleted file mode 100644 index e69de29b..00000000 From 11ab12a8eef43db53f288fbd6cfb684459601fdd Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Fri, 7 Feb 2020 16:52:31 +0900 Subject: [PATCH 07/10] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 프로덕션 코드를 리팩토링하면서 테스트 코드도 수정 --- src/test/java/CalculatorTest.java | 42 +++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java index 0f7d81f7..508714bb 100644 --- a/src/test/java/CalculatorTest.java +++ b/src/test/java/CalculatorTest.java @@ -5,13 +5,14 @@ public class CalculatorTest { @ParameterizedTest - @DisplayName("실수형 검증") - @CsvSource(value = {"3:true", "1.9:true", "문자 입력:false", - "+:false", "-1:true"}, delimiter = ':') + @DisplayName("올바른 숫자를 입력하였는지 검증") + @CsvSource(value = {"3 + ! + 4:false", "1.9 * 6.2:true", "+ + 4:false", + "5 * 6 * 7 * 9:true", "0 / 0:true"}, delimiter = ':') void validateDoubleTest(String input, boolean expected) { boolean actualResult = true; try { - Validator.validateDouble(input); + InputValues inputValues = new InputValues(input); + inputValues.validateDouble(); } catch (Exception e) { actualResult = false; } @@ -19,13 +20,28 @@ void validateDoubleTest(String input, boolean expected) { } @ParameterizedTest - @DisplayName("연산자 검증") - @CsvSource(value = {"3:false", "+:true", "/:true", - "문자입력:false", "):false"}, delimiter = ':') + @DisplayName("올바른 연산자를 입력하였는지 검증") + @CsvSource(value = {"2 a 3:false", "2 * 3 + 4 +:true", "2 ! 3:false", + "2 2 2:false", ") ):false"}, delimiter = ':') void validateOperatorTest(String input, boolean expected) { boolean actualResult = true; try { - Validator.validateOperator(input); + InputValues inputValues = new InputValues(input); + inputValues.validateOperator(); + } catch (Exception e) { + actualResult = false; + } + Assertions.assertThat(actualResult).isEqualTo(expected); + } + + @ParameterizedTest + @DisplayName("연산자로 수식이 끝나는 경우 검증") + @CsvSource(value = {"2 + 3 * 5 / 2:true", "2 +:false", "2 + 3 *:false"}, delimiter = ':') + void validateEndWithOperatorTest(String input, boolean expected) { + boolean actualResult = true; + try { + InputValues inputValues = new InputValues(input); + inputValues.validateEndWithOperator(); } catch (Exception e) { actualResult = false; } @@ -33,10 +49,10 @@ void validateOperatorTest(String input, boolean expected) { } @ParameterizedTest - @DisplayName("연산자 enum 검증") + @DisplayName("연산자 enum이 올바른지 검증") @CsvSource(value = {"3:false", "+:true", "/:true", "문자입력:false", "):false"}, delimiter = ':') - void getOperatorByStringTest(String input, boolean expected){ + void getOperatorByStringTest(String input, boolean expected) { boolean actualResult = true; try { Operator.getOperatorByString(input); @@ -47,9 +63,9 @@ void getOperatorByStringTest(String input, boolean expected){ } @ParameterizedTest - @DisplayName("연산 결과 검증") - @CsvSource(value = {"+:1:2:3", "-:2:1:1", "/:1:0:0", "*:2:3:6"}, delimiter =':') - void operateTest(String stringOperator, double num1, double num2, double expected){ + @DisplayName("연산 결과가 올바른지 검증") + @CsvSource(value = {"+:1:2:3", "-:2:1:1", "*:7:4:28", "*:2:3:6"}, delimiter = ':') + void operateTest(String stringOperator, double num1, double num2, double expected) { Operator operator = Operator.getOperatorByString(stringOperator); double actualResult = operator.operate(num1, num2); From 3b70148eb338be4436df0a74e41f08f602775241 Mon Sep 17 00:00:00 2001 From: moonyoungCHAE Date: Fri, 7 Feb 2020 16:52:48 +0900 Subject: [PATCH 08/10] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B3=84?= =?UTF-8?q?=EC=82=B0=EA=B8=B0=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/CalculatorTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java index 508714bb..06cee386 100644 --- a/src/test/java/CalculatorTest.java +++ b/src/test/java/CalculatorTest.java @@ -71,4 +71,15 @@ void operateTest(String stringOperator, double num1, double num2, double expecte Assertions.assertThat(actualResult).isEqualTo(expected); } + + @ParameterizedTest + @DisplayName("0으로 나눈 경우 검증") + @CsvSource(value = {"0:0", "1:0", "2:0"}, delimiter = ':') + void divideByZeroTest(double num1, double num2) { + Assertions.assertThatThrownBy(() -> { + Operator operator = Operator.getOperatorByString("/"); + double actualResult = operator.operate(num1, num2); + }).isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("0으로 나눌 수 없습니다."); + } } From 66f9d1d5a59bb62b1aa1951828e28ac4af7da936 Mon Sep 17 00:00:00 2001 From: taeheon Date: Sat, 8 Feb 2020 21:40:29 -0800 Subject: [PATCH 09/10] =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=82=B4=EC=9A=A9?= =?UTF-8?q?=20=EB=B0=98=EC=98=81=20InputValues=20=EC=9D=98=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=EB=93=A4=EA=B3=BC=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=A5=BC=20static=20=EC=97=90=EC=84=9C=20=EC=9D=B8=EC=8A=A4?= =?UTF-8?q?=ED=84=B4=EC=8A=A4=20=ED=95=84=EB=93=9C=EC=99=80=20=EB=A9=94?= =?UTF-8?q?=EC=86=8C=EB=93=9C=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20OutputView?= =?UTF-8?q?=20=EC=9E=91=EC=84=B1=EC=9D=84=20=ED=86=B5=ED=95=B4=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20=ED=81=B4=EB=9E=98=EC=8A=A4?= =?UTF-8?q?=EB=A5=BC=20=EB=A7=8C=EB=93=AC=20Operator=20Enum=20=EC=9D=98=20?= =?UTF-8?q?operate=20=EB=A9=94=EC=86=8C=EB=93=9C=EB=A5=BC=20=EB=9E=8C?= =?UTF-8?q?=EB=8B=A4=EC=8B=9D=EC=9D=98=20BinaryOperator=20=EB=A5=BC=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=B4=EC=84=9C=20=EC=9E=AC=EC=9E=91?= =?UTF-8?q?=EC=84=B1=20InputValues=20=EC=9D=98=20Validation=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD=20(=EC=88=AB=EC=9E=90=EC=99=80?= =?UTF-8?q?=20=EC=97=B0=EC=82=B0=EC=9E=90=EB=A5=BC=20=ED=95=A8=EA=BB=98=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=A7=8C?= =?UTF-8?q?=EB=93=AC)=20Calculator=20Test=20=EC=9D=98=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD(=EC=98=AC?= =?UTF-8?q?=EB=B0=94=EB=A5=B8=20=EC=9E=85=EB=A0=A5=EA=B0=92=EC=9D=B4=20?= =?UTF-8?q?=EB=93=A4=EC=96=B4=EC=98=A8=20=EA=B2=BD=EC=9A=B0=EC=99=80=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20=EA=B2=BD=EC=9A=B0=EB=A5=BC=20=EB=82=98?= =?UTF-8?q?=EB=88=A0=EC=84=9C=20=EA=B2=80=EC=A6=9D)=20CalculatorTest?= =?UTF-8?q?=EA=B0=80=20=EC=98=88=EC=83=81=EB=90=9C=20Exception=20=EC=9D=84?= =?UTF-8?q?=20=EC=A0=95=ED=99=95=ED=95=98=EA=B2=8C=20catch=20=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 25 +++++----- src/main/java/InputValues.java | 55 ++++++++++------------ src/main/java/Main.java | 6 +-- src/main/java/Operator.java | 22 ++++++--- src/main/java/OutputView.java | 5 ++ src/test/java/CalculatorTest.java | 78 ++++++++++++++++++++++--------- 6 files changed, 116 insertions(+), 75 deletions(-) create mode 100644 src/main/java/OutputView.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 339566b2..2a76db7a 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -1,22 +1,23 @@ public class Calculator { - private final int FIRST = 0; + private static final int FIRST = 0; + private static final int TWO = 2; private double result = 0; - void calculate() { - int length = InputValues.getValuesLength(); - double nextNumber = 0; + void calculate(InputValues inputValues) { + int length = inputValues.getValuesLength(); + double nextNumber; - result = Double.parseDouble(InputValues.getValueByIndex(FIRST)); - Operator nowOperator = null; - for (int i = 1; i < length; i += 2) { - nowOperator = Operator.getOperatorByString(InputValues.getValueByIndex(i)); - nextNumber = Double.parseDouble(InputValues.getValueByIndex(i + 1)); + result = Double.parseDouble(inputValues.getValueByIndex(FIRST)); + Operator nowOperator; + for (int i = 1; i < length; i += TWO) { + nowOperator = + Operator.getOperatorByString(inputValues.getValueByIndex(i)); + nextNumber = Double.parseDouble(inputValues.getValueByIndex(i + 1)); result = nowOperator.operate(result, nextNumber); } } - - void printResult() { - System.out.print("결과: " + result); + double getResult(){ + return result; } } diff --git a/src/main/java/InputValues.java b/src/main/java/InputValues.java index caa81796..329f5fce 100644 --- a/src/main/java/InputValues.java +++ b/src/main/java/InputValues.java @@ -1,58 +1,53 @@ import java.util.Scanner; public class InputValues { - private static String value; - private static String[] values; + private static final int ZERO = 0; - public InputValues() { - System.out.print("문자열 수식을 입력해주세요: "); - Scanner scanner = new Scanner(System.in); + private String value; + private String[] values; + public InputValues() throws IllegalArgumentException { + Scanner scanner = new Scanner(System.in); + System.out.print("문자열 수식을 입력해주세요: "); value = scanner.nextLine(); values = value.split(" "); - validateDouble(); - validateOperator(); - validateEndWithOperator(); + int valueLength = values.length; + + validateEndWithOperator(valueLength); + for (int i = 0; i < valueLength; i++) { + validateValues(i); + } + } - public InputValues(String value){ + public InputValues(String value) { this.value = value; values = value.split(" "); } - void validateDouble() { - int length = values.length; - - for (int i = 0; i < length; i += 2) { + void validateValues(int index) throws IllegalArgumentException{ + if (index % 2 == ZERO) { try { - Double.parseDouble(values[i]); - } catch (Exception e) { + Double.parseDouble(values[index]); + } catch (NumberFormatException ne) { throw new IllegalArgumentException("피연산자가 잘못되었습니다."); } + } else{ + Operator.getOperatorByString(values[index]); } - } - void validateOperator() { - int length = values.length; - - for (int i = 1; i < length; i += 2) { - Operator.getOperatorByString(values[i]); - } - } - - void validateEndWithOperator() { - int length = values.length; - - if (length % 2 == 0) + void validateEndWithOperator(int valueLength) throws IllegalArgumentException{ + if (valueLength % 2 == ZERO) { throw new IllegalArgumentException("연산자와 숫자가 맞지 않습니다."); + } } - public static String getValueByIndex(int index) { + public String getValueByIndex(int index) { return values[index]; } - public static int getValuesLength() { + public int getValuesLength() { return values.length; } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 18af6665..8363059f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,9 @@ public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws IllegalArgumentException{ InputValues inputValues = new InputValues(); Calculator calculator = new Calculator(); - calculator.calculate(); - calculator.printResult(); + calculator.calculate(inputValues); + OutputView.printResult(calculator); } } diff --git a/src/main/java/Operator.java b/src/main/java/Operator.java index 077269c4..bb8ca2e7 100644 --- a/src/main/java/Operator.java +++ b/src/main/java/Operator.java @@ -1,17 +1,19 @@ +import java.util.function.BinaryOperator; + public enum Operator { PLUS("+") { double operate(double num1, double num2) { - return num1 + num2; + return operate2((s, t) -> s + t, num1, num2); } }, MINUS("-") { double operate(double num1, double num2) { - return num1 - num2; + return operate2((s, t) -> s - t, num1, num2); } }, MUL("*") { double operate(double num1, double num2) { - return num1 * num2; + return operate2((s, t) -> s * t, num1, num2); } }, DIV("/") { @@ -21,7 +23,8 @@ public enum Operator { if (Double.isInfinite(result) || Double.isNaN(result)) { throw new IllegalArgumentException("0으로 나눌 수 없습니다."); } - return result; + + return operate2((s, t) -> s / t, num1, num2); } }; @@ -31,7 +34,14 @@ public enum Operator { this.stringOperator = stringOperator; } - static Operator getOperatorByString(String stringOperator) { + abstract double operate(double num1, double num2); + + double operate2(BinaryOperator binaryOperator, double num1, + double num2) { + return binaryOperator.apply(num1, num2); + } + + static Operator getOperatorByString(String stringOperator) throws IllegalArgumentException { Operator[] operators = values(); for (Operator o : operators) { @@ -41,6 +51,4 @@ static Operator getOperatorByString(String stringOperator) { } throw new IllegalArgumentException("연산자가 잘못되었습니다."); } - - abstract double operate(double num1, double num2); } diff --git a/src/main/java/OutputView.java b/src/main/java/OutputView.java new file mode 100644 index 00000000..49aaab8d --- /dev/null +++ b/src/main/java/OutputView.java @@ -0,0 +1,5 @@ +public class OutputView { + public static void printResult(Calculator calculator){ + System.out.println("결과 : " + calculator.getResult()); + } +} diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java index 06cee386..ce9ed1c0 100644 --- a/src/test/java/CalculatorTest.java +++ b/src/test/java/CalculatorTest.java @@ -2,36 +2,55 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.ValueSource; public class CalculatorTest { @ParameterizedTest - @DisplayName("올바른 숫자를 입력하였는지 검증") - @CsvSource(value = {"3 + ! + 4:false", "1.9 * 6.2:true", "+ + 4:false", - "5 * 6 * 7 * 9:true", "0 / 0:true"}, delimiter = ':') - void validateDoubleTest(String input, boolean expected) { + @DisplayName("입력이 올바른지 검증") + @CsvSource(value = {"1.9 * 6.2:true", "5 * 6 * 7 * 9:true", "0 / 0:true"} + , delimiter = ':') + void validateValuesTest(String input, boolean expected) { boolean actualResult = true; try { + int length = input.split(" ").length; InputValues inputValues = new InputValues(input); - inputValues.validateDouble(); - } catch (Exception e) { + for (int i = 0; i < length; i++) { + inputValues.validateValues(i); + } + } catch (NumberFormatException ne) { + actualResult = false; + } catch (IllegalArgumentException ie) { actualResult = false; } Assertions.assertThat(actualResult).isEqualTo(expected); } @ParameterizedTest - @DisplayName("올바른 연산자를 입력하였는지 검증") - @CsvSource(value = {"2 a 3:false", "2 * 3 + 4 +:true", "2 ! 3:false", - "2 2 2:false", ") ):false"}, delimiter = ':') - void validateOperatorTest(String input, boolean expected) { - boolean actualResult = true; - try { + @DisplayName("올바르지 않은 연산자가 오는 경우 에러메세지 검증") + @ValueSource(strings = {"3 ! 3 * 3", "3 3 3", "5 ) 9"}) + void validateValuesTest2(String input) { + Assertions.assertThatThrownBy(() -> { + int length = input.split(" ").length; InputValues inputValues = new InputValues(input); - inputValues.validateOperator(); - } catch (Exception e) { - actualResult = false; - } - Assertions.assertThat(actualResult).isEqualTo(expected); + for (int i = 0; i < length; i++) { + inputValues.validateValues(i); + } + }).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( + "연산자가 잘못되었습니다."); + } + + @ParameterizedTest + @DisplayName("올바르지 않은 피연산자가 오는 경우 에러메세지 검증") + @ValueSource(strings = {"! * 3 * 3", "a / 3", "5 * * 9"}) + void validateValuesTest3(String input) { + Assertions.assertThatThrownBy(() -> { + int length = input.split(" ").length; + InputValues inputValues = new InputValues(input); + for (int i = 0; i < length; i++) { + inputValues.validateValues(i); + } + }).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( + "피연산자가 잘못되었습니다."); } @ParameterizedTest @@ -40,23 +59,36 @@ void validateOperatorTest(String input, boolean expected) { void validateEndWithOperatorTest(String input, boolean expected) { boolean actualResult = true; try { + int length = input.split(" ").length; InputValues inputValues = new InputValues(input); - inputValues.validateEndWithOperator(); - } catch (Exception e) { + inputValues.validateEndWithOperator(length); + } catch (IllegalArgumentException ie) { actualResult = false; } Assertions.assertThat(actualResult).isEqualTo(expected); } @ParameterizedTest - @DisplayName("연산자 enum이 올바른지 검증") + @DisplayName("연산자로 수식이 끝나는 경우 에러메세지 검증") + @ValueSource(strings = {"2 + 3 *", "2 +", "2 + 3 -"}) + void validateEndWithOperatorTest2(String input) { + Assertions.assertThatThrownBy(() -> { + int length = input.split(" ").length; + InputValues inputValues = new InputValues(input); + inputValues.validateEndWithOperator(length); + }).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( + "연산자와 숫자가 맞지 않습니다."); + } + + @ParameterizedTest + @DisplayName("연산자 enum 이 올바른지 검증") @CsvSource(value = {"3:false", "+:true", "/:true", "문자입력:false", "):false"}, delimiter = ':') void getOperatorByStringTest(String input, boolean expected) { boolean actualResult = true; try { Operator.getOperatorByString(input); - } catch (Exception e) { + } catch (IllegalArgumentException e) { actualResult = false; } Assertions.assertThat(actualResult).isEqualTo(expected); @@ -73,12 +105,12 @@ void operateTest(String stringOperator, double num1, double num2, double expecte } @ParameterizedTest - @DisplayName("0으로 나눈 경우 검증") + @DisplayName("0으로 나눈 경우 에러메세지 검증") @CsvSource(value = {"0:0", "1:0", "2:0"}, delimiter = ':') void divideByZeroTest(double num1, double num2) { Assertions.assertThatThrownBy(() -> { Operator operator = Operator.getOperatorByString("/"); - double actualResult = operator.operate(num1, num2); + operator.operate(num1, num2); }).isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("0으로 나눌 수 없습니다."); } From 00be48b012f3d62145282ba3e7c3eec6da379a55 Mon Sep 17 00:00:00 2001 From: taeheon Date: Sun, 9 Feb 2020 09:55:44 -0800 Subject: [PATCH 10/10] =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=82=B4=EC=9A=A9?= =?UTF-8?q?=20=EB=B0=98=EC=98=81(=EC=B6=94=EA=B0=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Input 클래스 추가 - InputValue 의 필드를 포장 Input 클래스 추가로 인한 나머지 클래스 리팩토링 --- src/main/java/Calculator.java | 9 +++--- src/main/java/InputValues.java | 50 ++++++++++++++++++++----------- src/main/java/Main.java | 3 +- src/test/java/CalculatorTest.java | 36 ++++++++++++++-------- 4 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 2a76db7a..b89ff009 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -4,15 +4,16 @@ public class Calculator { private double result = 0; void calculate(InputValues inputValues) { - int length = inputValues.getValuesLength(); + Input input = inputValues.getInput(); + int length = input.getValuesLength(); double nextNumber; - result = Double.parseDouble(inputValues.getValueByIndex(FIRST)); + result = Double.parseDouble(input.getValueByIndex(FIRST)); Operator nowOperator; for (int i = 1; i < length; i += TWO) { nowOperator = - Operator.getOperatorByString(inputValues.getValueByIndex(i)); - nextNumber = Double.parseDouble(inputValues.getValueByIndex(i + 1)); + Operator.getOperatorByString(input.getValueByIndex(i)); + nextNumber = Double.parseDouble(input.getValueByIndex(i + 1)); result = nowOperator.operate(result, nextNumber); } } diff --git a/src/main/java/InputValues.java b/src/main/java/InputValues.java index 329f5fce..90de2070 100644 --- a/src/main/java/InputValues.java +++ b/src/main/java/InputValues.java @@ -1,17 +1,36 @@ import java.util.Scanner; +class Input{ + String value; + String[] values; + public Input(){ + Scanner scanner = new Scanner(System.in); + System.out.println("문자열 수식을 입력해주세요 : "); + value = scanner.nextLine(); + values = value.split(" "); + } + + public Input(String input){ + this.value = input; + this.values = value.split(" "); + } + + public int getValuesLength(){ + return values.length; + } + + public String getValueByIndex(int index){ + return values[index]; + } +} public class InputValues { private static final int ZERO = 0; - private String value; - private String[] values; + Input input; - public InputValues() throws IllegalArgumentException { - Scanner scanner = new Scanner(System.in); - System.out.print("문자열 수식을 입력해주세요: "); - value = scanner.nextLine(); - values = value.split(" "); - int valueLength = values.length; + public InputValues(Input input) throws IllegalArgumentException { + this.input = input; + int valueLength = input.getValuesLength(); validateEndWithOperator(valueLength); for (int i = 0; i < valueLength; i++) { @@ -21,19 +40,18 @@ public InputValues() throws IllegalArgumentException { } public InputValues(String value) { - this.value = value; - values = value.split(" "); + Input input = new Input(value); } void validateValues(int index) throws IllegalArgumentException{ if (index % 2 == ZERO) { try { - Double.parseDouble(values[index]); + Double.parseDouble(input.getValueByIndex(index)); } catch (NumberFormatException ne) { throw new IllegalArgumentException("피연산자가 잘못되었습니다."); } } else{ - Operator.getOperatorByString(values[index]); + Operator.getOperatorByString(input.getValueByIndex(index)); } } @@ -43,11 +61,7 @@ void validateEndWithOperator(int valueLength) throws IllegalArgumentException{ } } - public String getValueByIndex(int index) { - return values[index]; - } - - public int getValuesLength() { - return values.length; + Input getInput(){ + return input; } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 8363059f..6147b164 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,7 @@ public class Main { public static void main(String[] args) throws IllegalArgumentException{ - InputValues inputValues = new InputValues(); + Input input = new Input(); + InputValues inputValues = new InputValues(input); Calculator calculator = new Calculator(); calculator.calculate(inputValues); diff --git a/src/test/java/CalculatorTest.java b/src/test/java/CalculatorTest.java index ce9ed1c0..42194ce7 100644 --- a/src/test/java/CalculatorTest.java +++ b/src/test/java/CalculatorTest.java @@ -9,11 +9,13 @@ public class CalculatorTest { @DisplayName("입력이 올바른지 검증") @CsvSource(value = {"1.9 * 6.2:true", "5 * 6 * 7 * 9:true", "0 / 0:true"} , delimiter = ':') - void validateValuesTest(String input, boolean expected) { + void validateValuesTest(String value, boolean expected) { boolean actualResult = true; try { - int length = input.split(" ").length; + Input input = new Input(value); InputValues inputValues = new InputValues(input); + int length = input.getValuesLength(); + for (int i = 0; i < length; i++) { inputValues.validateValues(i); } @@ -28,10 +30,12 @@ void validateValuesTest(String input, boolean expected) { @ParameterizedTest @DisplayName("올바르지 않은 연산자가 오는 경우 에러메세지 검증") @ValueSource(strings = {"3 ! 3 * 3", "3 3 3", "5 ) 9"}) - void validateValuesTest2(String input) { + void validateValuesTest2(String value) { Assertions.assertThatThrownBy(() -> { - int length = input.split(" ").length; + Input input = new Input(value); InputValues inputValues = new InputValues(input); + int length = input.getValuesLength(); + for (int i = 0; i < length; i++) { inputValues.validateValues(i); } @@ -41,11 +45,13 @@ void validateValuesTest2(String input) { @ParameterizedTest @DisplayName("올바르지 않은 피연산자가 오는 경우 에러메세지 검증") - @ValueSource(strings = {"! * 3 * 3", "a / 3", "5 * * 9"}) - void validateValuesTest3(String input) { + @ValueSource(strings = {"! * 3 * 3", "a / 3", "5 * * + 6"}) + void validateValuesTest3(String value) { Assertions.assertThatThrownBy(() -> { - int length = input.split(" ").length; + Input input = new Input(value); InputValues inputValues = new InputValues(input); + int length = input.getValuesLength(); + for (int i = 0; i < length; i++) { inputValues.validateValues(i); } @@ -56,11 +62,13 @@ void validateValuesTest3(String input) { @ParameterizedTest @DisplayName("연산자로 수식이 끝나는 경우 검증") @CsvSource(value = {"2 + 3 * 5 / 2:true", "2 +:false", "2 + 3 *:false"}, delimiter = ':') - void validateEndWithOperatorTest(String input, boolean expected) { + void validateEndWithOperatorTest(String value, boolean expected) { boolean actualResult = true; try { - int length = input.split(" ").length; + Input input = new Input(value); InputValues inputValues = new InputValues(input); + int length = input.getValuesLength(); + inputValues.validateEndWithOperator(length); } catch (IllegalArgumentException ie) { actualResult = false; @@ -71,10 +79,12 @@ void validateEndWithOperatorTest(String input, boolean expected) { @ParameterizedTest @DisplayName("연산자로 수식이 끝나는 경우 에러메세지 검증") @ValueSource(strings = {"2 + 3 *", "2 +", "2 + 3 -"}) - void validateEndWithOperatorTest2(String input) { + void validateEndWithOperatorTest2(String value) { Assertions.assertThatThrownBy(() -> { - int length = input.split(" ").length; + Input input = new Input(value); InputValues inputValues = new InputValues(input); + int length = input.getValuesLength(); + inputValues.validateEndWithOperator(length); }).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( "연산자와 숫자가 맞지 않습니다."); @@ -84,10 +94,10 @@ void validateEndWithOperatorTest2(String input) { @DisplayName("연산자 enum 이 올바른지 검증") @CsvSource(value = {"3:false", "+:true", "/:true", "문자입력:false", "):false"}, delimiter = ':') - void getOperatorByStringTest(String input, boolean expected) { + void getOperatorByStringTest(String value, boolean expected) { boolean actualResult = true; try { - Operator.getOperatorByString(input); + Operator.getOperatorByString(value); } catch (IllegalArgumentException e) { actualResult = false; }