Skip to content

Commit

Permalink
[FEAT] : 1차 세미나 과제 선택 구현 - 이모지 포함 글자수 계산 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sansan20535 committed Oct 9, 2024
1 parent 1e98bfc commit 2ece0f0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
6 changes: 1 addition & 5 deletions .idea/workspace.xml

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

2 changes: 1 addition & 1 deletion src/main/java/org/sopt/week1/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final List<Diary> getList() {
}

final void post(final String body) {
if (body.length() > 30) {
if (TextUtils.getLengthOfBody(body) > 30) {
throw new IllegalArgumentException("30자 이하로 적어주세요");
}
diaryService.writeDiary(body);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/sopt/week1/TextUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.sopt.week1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtils {

private static final Pattern graphemePattern = Pattern.compile("\\X");
private static final Matcher graphemeMatcher = graphemePattern.matcher("");

public static int getLengthOfBody(final String body) {
if (body == null) {
return 0;
}

graphemeMatcher.reset(body);

int count = 0;
while (graphemeMatcher.find()) {
count++;
}
return count;
}
}
25 changes: 25 additions & 0 deletions src/test/java/TextUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import org.junit.jupiter.api.*;
import org.sopt.week1.TextUtils;

@DisplayName("일기 글자 수 세기 테스트")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class TextUtilsTest {

@Test
void 이모지는_한_개의_문자로_인식한다() {
//given
final String test1 = "😃행복한 하루였어요👍";
final String test2 = "🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻🤞🏻";
final int expected1 = 11;
final int expected2 = 31;

//when
final int actual1 = TextUtils.getLengthOfBody(test1);
final int actual2 = TextUtils.getLengthOfBody(test2);

//then
Assertions.assertEquals(actual1, expected1);
Assertions.assertEquals(actual2, expected2);

}
}

0 comments on commit 2ece0f0

Please sign in to comment.