Skip to content

Commit

Permalink
๐Ÿงช [Unit Test] usertextcolorcheckservice ํ…Œ์ŠคํŠธ ์ฝ”๋“œ (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
middlefitting authored Apr 3, 2024
1 parent 6c42a3d commit 99ff2b7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@
@Service
@RequiredArgsConstructor
public class UserTextColorCheckService {

/**
* textColor ์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ
* @param textColor
* @return boolean
*/
public static boolean check(String textColor) {
if (textColor == null) {
return false;
}
if (textColor.length() != 7) {
return false;
}
if (textColor.charAt(0) != '#') {
return false;
}
for (int i = 1; i < 7; i++) {
char charTestColor = textColor.charAt(i);
if (!((charTestColor >= '0' && charTestColor <= '9') || (charTestColor >= 'a' && charTestColor <= 'f') || (
charTestColor >= 'A' && charTestColor <= 'F'))) {
return false;
}
}
return true;
return textColor != null && textColor.matches("#[0-9a-fA-F]{6}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package gg.pingpong.api.user.user.service;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import gg.utils.annotation.UnitTest;

@UnitTest
class UserTextColorCheckServiceUnitTest {

UserTextColorCheckService userTextColorCheckService = new UserTextColorCheckService();

@Nested
@DisplayName("check")
class Check {
@Test
@DisplayName("textColor๊ฐ€ null์ผ ๊ฒฝ์šฐ false ๋ฐ˜ํ™˜")
void nullCheck() {
//Arrange
String textColor = null;

//Act
boolean check = userTextColorCheckService.check(textColor);

//Assert
Assertions.assertThat(check).isFalse();
}

@ParameterizedTest
@DisplayName("7๊ธ€์ž๊ฐ€ ์•„๋‹๊ฒฝ์šฐ false ๋ฐ˜ํ™˜")
@ValueSource(strings = {"", "#", "#1", "#12", "#123", "#12345", "#1234567"})
void lengthCheck(String textColor) {
//Arrange
//Act
boolean check = userTextColorCheckService.check(textColor);

//Assert
Assertions.assertThat(check).isFalse();
}
}

@ParameterizedTest
@DisplayName("์ฒซ ๊ธ€์ž # ์•„๋‹๊ฒฝ์šฐ false ๋ฐ˜ํ™˜")
@ValueSource(strings = {"#,", "a#12345", "ใ„ฑ#12345", "1#12345", "Z#12345", " #12345"})
void startCheck(String textColor) {
//Arrange
//Act
boolean check = userTextColorCheckService.check(textColor);

//Assert
Assertions.assertThat(check).isFalse();
}

@ParameterizedTest
@DisplayName("์ •๊ทœ์‹ ์‹คํŒจํ• ๊ฒฝ์šฐ false ๋ฐ˜ํ™˜")
@ValueSource(strings = {"#12345G", "#gabcde", "#zABCDE", "#Ab3DEx"})
void regexFail(String textColor) {
//Arrange
//Act
boolean check = userTextColorCheckService.check(textColor);

//Assert
Assertions.assertThat(check).isFalse();
}

@ParameterizedTest
@DisplayName("๋ชจ๋“  ์กฐ๊ฑด์„ ํ†ต๊ณผํ• ๊ฒฝ์šฐ true ๋ฐ˜ํ™˜")
@ValueSource(strings = {"#023589", "#abcdef", "#ABCDEF", "#Ab3DeE"})
void checkSuccess(String textColor) {
//Arrange
//Act
boolean check = userTextColorCheckService.check(textColor);

//Assert
Assertions.assertThat(check).isTrue();
}
}

0 comments on commit 99ff2b7

Please sign in to comment.