-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functions to replace {times} to actual amount of times (#7871)
* add functions to replace {times} to actual amount of times * tests * issue
- Loading branch information
Showing
4 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
service/src/main/java/greencity/utils/NotificationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package greencity.utils; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class NotificationUtils { | ||
public static String resolveTimesInEnglish(final int number) { | ||
return switch (number) { | ||
case 1 -> ""; | ||
case 2 -> "twice"; | ||
default -> number + " times"; | ||
}; | ||
} | ||
|
||
public static String resolveTimesInUkrainian(int number) { | ||
number = Math.abs(number); | ||
final int lastTwoDigits = number % 100; | ||
final int lastDigit = number % 10; | ||
|
||
if (number == 1) { | ||
return ""; | ||
} | ||
|
||
if (lastTwoDigits >= 11 && lastTwoDigits <= 19) { | ||
return number + " разів"; | ||
} | ||
|
||
return switch (lastDigit) { | ||
case 1 -> number + " раз"; | ||
case 2, 3, 4 -> number + " рази"; | ||
default -> number + " разів"; | ||
}; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
service/src/test/java/greencity/utils/NotificationUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package greencity.utils; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import java.util.stream.Stream; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class NotificationUtilsTest { | ||
@ParameterizedTest(name = "Test resolveTimeInUkrainian with input {0}") | ||
@MethodSource("provideUkrainianTestCases") | ||
void testResolveTimeInUkrainian(int input, String expected) { | ||
String result = NotificationUtils.resolveTimesInUkrainian(input); | ||
assertEquals(expected, result); | ||
} | ||
|
||
private static Stream<Arguments> provideUkrainianTestCases() { | ||
return Stream.of( | ||
Arguments.of(1, ""), | ||
Arguments.of(2, "2 рази"), | ||
Arguments.of(4, "4 рази"), | ||
Arguments.of(5, "5 разів"), | ||
Arguments.of(11, "11 разів"), | ||
Arguments.of(19, "19 разів"), | ||
Arguments.of(21, "21 раз"), | ||
Arguments.of(22, "22 рази"), | ||
Arguments.of(25, "25 разів"), | ||
Arguments.of(100, "100 разів"), | ||
Arguments.of(101, "101 раз"), | ||
Arguments.of(102, "102 рази"), | ||
Arguments.of(112, "112 разів"), | ||
Arguments.of(123, "123 рази")); | ||
} | ||
|
||
@ParameterizedTest(name = "Test resolveTimesInEnglish with input {0}") | ||
@MethodSource("provideEnglishTestCases") | ||
void testResolveTimesInEnglish(int input, String expected) { | ||
String result = NotificationUtils.resolveTimesInEnglish(input); | ||
assertEquals(expected, result); | ||
} | ||
|
||
private static Stream<Arguments> provideEnglishTestCases() { | ||
return Stream.of( | ||
Arguments.of(1, ""), | ||
Arguments.of(2, "twice"), | ||
Arguments.of(3, "3 times"), | ||
Arguments.of(5, "5 times"), | ||
Arguments.of(11, "11 times"), | ||
Arguments.of(21, "21 times")); | ||
} | ||
} |