-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ 2๋จ๊ณ - ๋ฌธ์์ด ๋ง์ ๊ณ์ฐ๊ธฐ #5916
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์กฐ๊ธ ๋ ๊ฐ์ ํด๋ณด๋ฉด ์ข์ ๋ถ๋ถ์ ๋ฆฌ๋ทฐ๋ฅผ ๋จ๊ฒจ๋์์ผ๋ ํ์ธํด์ฃผ์ธ์~!
๋ฐ๋์ ๋ชจ๋ ์ฌํญ์ ๋ฐ์ํ ํ์๋ ์์ง๋ง ํ๋ฒ์ฏค ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์๊ฒ ๊ฐ์ต๋๋ค!
public class StringAddCalculatorTest { | ||
|
||
@Test | ||
public void splitAndSum_null() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ชจ๋ ํ ์คํธ์ ์ด๋ฆ์ ๋ถ์ฌํด์ฃผ์ จ๊ตฐ์ ๐
ํ ์คํธ ์ฝ๋๋ ํ๋์ ๋ฌธ์๋ก ๋ณผ ์ ์๋๋ฐ์~!
์กฐ๊ธ ๋ ํ ์คํธ๊ฐ ๋ํ๋ด๊ณ ์ ํ๋ ๋ฐ๋ฅผ ๋๋ฌ๋ผ ์ ์๋๋ก ์ด๋ฆ์ ์์ฑํด๋ณด๋ ๊ฒ์ ์ด๋จ๊น์?
๊ฐ๋ น ํ ์คํธ์์ ํ๋ ํ์์ ๊ทธ ํ์๋ก ์ธํด ์ด๋ ํ ๊ฒฐ๊ณผ๊ฐ ๋ฐ์ํ๋ค๋ ์์ผ๋ก ์ด๋ฆ์ ์ง์ด๋ณผ ์ ์์๊ฒ ๊ฐ์ต๋๋ค.
"how to write test case name"๋ผ๊ณ ๊ฒ์์ ํด๋ณด์๋ฉด ์ด์ ๋ํด ๋ค๋ฃจ๋ ๊ธ์ด ๋ง์ผ๋ ํ๋ฒ ๊ฒ์ํด๋ณด์๋ ๊ฒ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค :)
๋ํ @DisplayName
์ ์ด์ฉํ์ฌ ํ๊ธ๋ก ๋ ํ
์คํธ ์ด๋ฆ์ ์์ฑํ๊ธฐ๋ ํ๋ ์ฐธ๊ณ ํด์ฃผ์ธ์~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Baeldung :: Best Practices for Unit Testing in Java ๋งํฌ๋ฅผ ์ฐธ๊ณ ํด ๋ณด๊ฒ ์ต๋๋ค!
@DisplayName
๋ ํ์ฉํด๋ณด๊ฒ ์ต๋๋ค!
@Test | ||
public void splitAndSum_negative() { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3")) | ||
.isInstanceOf(RuntimeException.class); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_delimCustomAndNegative() { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("//-\n-1-2-3")) | ||
.isInstanceOf(RuntimeException.class); | ||
} | ||
|
||
@Test | ||
public void splitAndSum_blank() { | ||
assertThatThrownBy(() -> StringAddCalculator.splitAndSum(" ")) | ||
.isInstanceOf(RuntimeException.class); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ชจ๋ RuntimeException์ ๋ํด์๋ง ๊ฒ์ฌ๋ฅผ ํ๊ณ ์๋ค์!
๊ทธ๋ ๋ค๋ฉด ๋ชจ๋ ์์ํ์ ๋ถ๋ถ์์ exception์ด throw ๋์๋ค๊ณ ํ์ ํ ์ ์์๊น์?
์กฐ๊ธ ๋ ์ ๋ขฐ์ฑ ์๋ ๊ฒ์ฌ๋ฅผ ํด๋ณด๋๊ฑด ์ด๋จ๊น์?
์๋ ๊ธ์ ์ฐธ๊ณ ํด๋ณด์ ๋ ์ข์ต๋๋ค~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- ๊ณต์ ํด์ฃผ์ Blog :: Exception์ ํ
์คํธ ํ ๋ ๊ฑฐ์ง ์์ฑ์ ์ฃผ์ํ์ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํด ๋ณด๊ฒ ์ต๋๋ค!
- "exception์ ํน์ ํ ์ ์๋ ์ ๋ณด๋ฅผ ํจ๊ป ํ ์คํธ์ ํฌํจ์์ผ ๊ฒ์ฆํ๋๋ก ํ์."
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class StringAddCalculatorTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ์ฒด์ ์ผ๋ก ํ ์คํธ๋ฅผ ์ผ์ด์ค๋ณ๋ก ๊ผผ๊ผผํ๊ฒ ์์ฑํด์ฃผ์ ๊ฒ ๊ฐ์ต๋๋ค ๐
๊ทธ๋ฐ๋ฐ ์กฐ๊ธ ๋ ๊ฐ์ ํด๋ณผ๋งํ ๋ถ๋ถ์ด ์์๊ฒ ๊ฐ์ต๋๋ค.
splitAndSum์ด๋ผ๋ ์ด๋ฆ์์ ์ ์ ์๋ฏ
split๊ณผ sum ๊ธฐ๋ฅ์ด ํ๊ณณ์ ๊ตฌํ์ด ๋์ด์๋ค๋ณด๋
๊ตฌ๋ถ์๊ฐ ๋์ํ๋๊ฒ๋ sum์ ๋์ํ๋ ๊ฒ๋ ํ๋์ ๋ฉ์๋๋ฅผ ํตํด ์ผ๋ จ์ ๊ฒฐ๊ณผ๋ก ํ์ธ์ ํ ์ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
SRP๋ฅผ ์ค์ํ๋ ค๋ฉด "๊ตฌ๋ถ์๋ฅผ ํตํ ๋ฌธ์์ด ๋ถ๋ฆฌ"์ ๋ํ ๋ณ๊ฒฝ๊ณผ "๋ถ๋ฆฌ๋ ๋ฌธ์์ด๋ค์ ํฉ"์ ๋ํ ๋ณ๊ฒฝ์ ์๋ก ์ํฅ์ ๋ฏธ์น์ง ์์์ผ ํ์ง ์์๊น์?
๋ฌธ์์ด์ ๊ตฌ๋ถํ๋ ๋ฅ๋ ฅ์ ๊ฐ์ง ํด๋์ค์ ๋ฌธ์์ด๋ค์ ์งํฉ์ ์ด์ฉํด ๊ณ์ฐ์ ํ๋ ํด๋์ค๋ฅผ ์ค๊ณํ๋ค ๋ ํด๋์ค๊ฐ ์๋ก ๋ฉ์ธ์ง๋ฅผ ์ฃผ๊ณ ๋ฐ์ผ๋ฉฐ ํ๋ ฅํ๋ ๊ทธ๋ฆผ์ด ๊ทธ๋ ค์ง๋๋ก ์ฝ๋๋ฅผ ์์ฑํด๋ณด๋ฉด ์ข์๊ฒ ๊ฐ์ต๋๋ค. :)
๊ทธ๋ ๊ฒ ํ๋ค๋ฉด ๊ฐ๋ณ์ ์ธ ํ ์คํธ๊ฐ ๊ฐ๋ฅํด์ง ๊ฒ ๊ฐ์ต๋๋ค~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- SPR(Single Responsibility Principle) ์์ ์์์ต๋๋ค.
splitAndSum
-->split
+sum
์ผ๋ก ๋ถ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค!
if (!isValid(input)) { | ||
return 0; | ||
} | ||
int[] tokens = tokenize(input); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๋ฌ๊ฐ์ token์ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ์ผ๋ก ๋ฐฐ์ด์ ์ ํํ์ ๊ฒ ๊ฐ์ต๋๋ค.
๋ค๋ฅธ collection(ex. List)๋ค๋ ์ด๋ฃจ๊ณ ์ ํ๋ ๋ฐ๋ฅผ ๋ฌ์ฑํ๊ธฐ์ ์ข์ ์ ํ์ง๊ฐ ๋ ์ ์์๊ฒ ๊ฐ์๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์ค๊น์? ๋ฐฐ์ด์ ์ ํํ์ ์ด์ ๊ฐ ๊ถ๊ธํ๋ค์ :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- ์ ๊ฐ ์๋ฐ๋ฅผ ์ ๋ชฐ๋ผ์... (
Python3
๊ฐ ์ฃผ๋ ฅ์ด๊ณ ์ ๋งJava
๋ฅผ ์ ๋ชจ๋ฆ ๋๋ค) - ๋ต๋ต,
Collection
์ผ๋ก ์ ํํด๋ณด๊ฒ ์ต๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ผญ ์ ํ์ ๋ฐ๋ ๋ฆฌ๋ทฐ๋ ์๋์์ต๋๋ค :)
์ ์ ๋๋๋ ๋ชจ๋ ๋ฆฌ๋ทฐ๋ ์๊ฐ์ ๋๋๋ ๊ฒ์ด๋ผ ์๊ฐํ๊ณ ๋ชจ๋ ๋ถ๋ถ์ ๊ผญ ๊ณ ์ณ์ผํ๋ ์ฌํญ์ผ๋ก ๋ฐ์๋ค์ด๊ธฐ ๋ณด๋ค
์ด๋ ํ ์์์ ํ๋ฆ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํด๋ณด์๋์ง, ์๊ฒฌ์ ๊ต๋ฅํ๋ ๊ฒ์ด๋ผ ์๊ฐํด์ฃผ์ธ์~!
|
||
public class StringAddCalculator { | ||
public static int splitAndSum(String input) { | ||
if (!isValid(input)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!๋ฅผ ์ด์ฉํด ๋ถ์ ํํ์ ํ๋ ๊ฒ๋ ์ข์ง๋ง ์ธ์ง๋ถํ๋ฅผ ๋๋ฆฌ๋ ์์๋ก ์์ฉํ ์ ์์๊ฒ ๊ฐ์ต๋๋ค.
๊ฐ๋ฅํ๋ฉด ๋ฉ์๋์ ์ด๋ฆ์์ ๋ถ์ ํํ์ด ๋๋ฌ๋๋๋ก ํด๋ณด๋๊ฑด ์ด๋จ๊น์?
ex. isNotValid, isInvalid
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- ๋ต, ๋ช ์์ ์ผ๋ก ๋๋ฌ๋๋๋ก ์์ ํด๋ณด๊ฒ ์ต๋๋ค!
if (Pattern.matches("(//\\D\\n)*(\\d+\\D)*\\d+", input)) { | ||
return true; | ||
} | ||
throw new RuntimeException(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exception์ ๋ฐ์์ํฌ๋๋ ๊ทธ ์ด์ ์ ๋ํด ํจ๊ป ๋ฉ์ธ์ง๋ก ์ ๋ฌํด์ฃผ๋ฉด ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- ๋ต, ๋ฉ์์ง๊น์ง ๋ฃ์ด์ ์ข ๋ ํน์ ํ ์ ์๋ Exception ์ ๋ฐ์์ํค๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค!
String[] delimiters = new String[]{",", ":"}; | ||
Matcher m = Pattern.compile("//(\\D)\n(.*)").matcher(input); | ||
if (m.find()) { | ||
delimiters = new String[]{delimiters[0], delimiters[1], m.group(1)}; | ||
input = m.group(2); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๊ธฐ์ ๋ ๋ค๋ฅธ ํจํด์ ๊ตฌ๋ถ์๋ฅผ ์ง์ํ๋ ๋ฐฉ์์ด ์๊ธด๋ค๋ฉด ์ด๋ป๊ฒ ๋์ฒํด์ผํ ๊น์?
๊ณ์ํด์ if์ ์ด ์ถ๊ฐ๋๊ณ ๊ทธ์๋ฐ๋ฅธ delimiters๋ฅผ ํ ๋นํด์ฃผ์ด์ผํ ๊ฒ ๊ฐ์๋ฐ์.
์ ๋ตํจํด์ ์ด์ฉํด์ ํ์ฅ์ ์ฉ์ดํ ๊ตฌ์กฐ๋ฅผ ์ทจํด๋ณด๋๊ฑด ์ด๋จ๊น์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- ํจํด์ ์ถ๊ฐํ ์ ์๋ ๋ฐฉํฅ์ผ๋ก ์ฝ๋๋ฅผ ๊ฐ์ ํด๋ณด๊ฒ ์ต๋๋ค!
delimiters = new String[]{delimiters[0], delimiters[1], m.group(1)}; | ||
input = m.group(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ธฐ์กด์ ์ฌ์ฉ์ค์ด๋ ๊ฐ์ ๊ณ์ํด์ ํ ๋นํ๋ฉฐ ๋ค๋ฅธ ์ฉ๋๋ก ์ฌ์ฉํ๋ค๋ฉด ์ด๋ ์ฝ๋๋ฅผ ์ฝ๋๋ฐ ๋ฐฉํด๊ฐ ๋๋ ์์๋ก ์์ฉํ ์ ์์ต๋๋ค. ๊ณ์ํด์ ๋ณ๊ฒฝ๋๋ ์๋ฏธ๋ฅผ ํ์ ํด์ผํ๋๊น์~
๊ฐ๋ฅํ ๋ถ๋ณํ ์ํ๋ฅผ ์ ์งํ๋๋ก ๋ณ์๋ฅผ ์ฌ์ฉํด๋ณด๋ฉด ์ข์๊ฒ ๊ฐ์ต๋๋ค.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- ๋ต, ๋ถ๋ณํ ์ํ๋ฅผ ์ ์งํ๋๋ก ๋ณ์๋ฅผ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค!
์ถ๊ฐ๋ก PR ๋ณธ๋ฌธ์ ๋ฆฌ๋ทฐ๋ฅผ ์์ฒญ์ฃผ์ค๋ ์ฌ๋ฌ๊ฐ์ง ์๊ตฌ์ฌํญ์ ์ ์ด์ฃผ์๋ฉด ์ ๊ฐ ๋ฆฌ๋ทฐํ๋๋ฐ ํฐ ๋์์ด ๋ ๊ฒ ๊ฐ์ต๋๋ค. ์๋ ๊ธ์ ์ฐธ๊ณ ํด๋ณด์ ๋ ์ข์๊ฒ ๊ฐ์์~ |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ค์ ๋จ๊ณ๋ฅผ ์งํํ๋ฉฐ ์ด๋ฒ ๋จ๊ณ์ ๋จ๊ฒจ๋๋ฆฐ ๋ฆฌ๋ทฐ๋ฅผ ํ๋ฒ ๊ณ ๋ฏผํด๋ณด์๋ฉด ์ข์๊ฒ ๊ฐ์ต๋๋ค. :)
mergeํ๊ฒ ์ต๋๋ค. ๐
public class StringAddCalculatorToken { | ||
|
||
private final int value; | ||
|
||
public StringAddCalculatorToken(String value) { | ||
if (value.isEmpty()) { | ||
this.value = 0; | ||
return; | ||
} | ||
this.value = Integer.parseInt(value); | ||
validate(); | ||
} | ||
|
||
public StringAddCalculatorToken(int number) { | ||
this.value = number; | ||
validate(); | ||
} | ||
|
||
private void validate() { | ||
if (this.value < 0) { | ||
throw new StringAddCalculatorTokenException("Don't allow negative number"); | ||
} | ||
} | ||
|
||
public int toInteger() { | ||
return this.value; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ ๐ ๐
๊ทธ๋ฐ๋ฐ Exception๊ณผ ๊ฐ์ ํ์ผ์ ์ ์ธํด๋๋ฉด ํด๋์ค๋ฅผ ์ฐพ๊ธฐ ์ด๋ ค์ฐ๋ ๋ ๋ฆฝ์ ์ผ๋ก ์ ์ธํด์ฃผ์๋ฉด ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค. :)
public class StringAddCalculatorTokenCalculator { | ||
|
||
public static int sum(StringAddCalculatorToken[] tokens) { | ||
int result = 0; | ||
for (StringAddCalculatorToken stringAddCalculatorToken : tokens) { | ||
result += stringAddCalculatorToken.toInteger(); | ||
} | ||
return result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค sum์ ํ ์ ์๋ ๋ณ๋์ ์ญํ ์ ํํํด์ฃผ์ จ๋ค์~!
์ด์ ๊ด๋ จํ์ฌ ์ผ๊ธ์ปฌ๋ ์ ์ด๋ผ๋ ๊ฐ๋ ์ ํ๋ฒ ์ฐพ์๋ณด์๊ณ ๋ค์ ๋ฏธ์ ๋ถํฐ ์ฐธ๊ณ ํ์ฌ ๋ฐ์ํด๋ณด์๋ฉด ์ข์๊ฒ ๊ฐ์ต๋๋ค :)
for (String customDelimiterRegex : defaultCustomDelimiterRegexes) { | ||
Matcher matcher = Pattern.compile(customDelimiterRegex).matcher(input); | ||
if (matcher.find()) { | ||
String customDelimiter = matcher.group(1); | ||
delimiters.add(customDelimiter); | ||
input = matcher.group(2); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ์ผ๋ จ์ ๊ณผ์ ๋ ์ด ์ ํ๋ฆฌ์ผ์ด์ ์์ ์์ฃผ ์ค์ํ ํญ๋ชฉ์ผ๋ก ๋ณด์ด๊ธฐ ๋๋ฌธ์ ๊ต์ฅํ ํ ์คํธ๋ฅผ ํ๋ฉด ์ข์ ๋ถ๋ถ์ผ๋ก ๋ณด์ด๋ค์!
์ด๋ ๊ฒ ํจํด์ ์ฐพ๊ณ ๋ฌธ์์ด๊ณผ ๊ตฌ๋ถ์๋ฅผ ๋ถ๋ฆฌํด๋ด๋ ๊ฒ๋ ํ๋์ ํด๋์ค๊ฐ ๋ด๋นํ ์ ์๊ฒ ๋ง๋ค์ด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค~
๊ทธ๋ฆฌ๊ณ ๊ฑฐ๊ธฐ์ ๋ ๋์๊ฐ๋ค๋ฉด ํ์ฅ์ ์ฉ์ดํ๋๋ก ๋์์ธ ํจํด(์ ๋ต ํจํด)์ ์ ์ฉํด๋ณผ ์ ์์๊ฒ ๊ฐ๋ค์!
์ง๊ธํ๊ธฐ์๋ ๋๋ฌด ๋ง์ ๋ณ๊ฒฝ์ฌํญ์ด๋ ๊ด๋ จํ์ฌ ์ฌ~์ฉ ์ฐพ์๋ง ๋ณด์ ๋ ์ข์๊ฒ ๊ฐ์ต๋๋ค. :)
String customDelimiter = matcher.group(1); | ||
delimiters.add(customDelimiter); | ||
input = matcher.group(2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1๊ณผ 2๋ ์ด๋ ํ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ ์ซ์์ผ๊น์? ๐ค
๋ค๋ฅธ์ฌ๋์ด ๋จ๋ฒ์ ์์์ฐจ๋ฆด ์ ์๋๋ก ์ด๋ฆ์ ๋ถ์ฌํด๋ณด๋๊ฑด ์ด๋จ๊น์?
public class StringAddCalculatorTest { | ||
|
||
@Test | ||
@DisplayName("input: null --> output: 0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ ๋ณดํต ํ ์คํธ ์ด๋ฆ์ ์์ ์ ์ผ๋ก ํํํ๋ ํธ์ธ ๊ฒ ๊ฐ์ต๋๋ค~
"์ ๋ ฅ์ด null์ด๋ฉด ๊ธฐ๋ณธ๊ฐ์ ๋ฐํํ๋ค." ์ ๊ฐ์ด ์ง์ ์ ์์๊ฒ ๊ฐ๋ค์ :)
No description provided.