-
-
Notifications
You must be signed in to change notification settings - Fork 0
GH-40 Add simple duration util #40
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.eternalcode.commons.time; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
public class SimpleDurationUtil { | ||
|
||
private static final Duration ONE_SECOND = Duration.ofSeconds(1); | ||
private static final String ZERO_SECONDS = "0s"; | ||
|
||
private static final TemporalAmountParser<Duration> WITHOUT_MILLIS_FORMAT = new DurationParser() | ||
.withUnit("s", ChronoUnit.SECONDS) | ||
.withUnit("m", ChronoUnit.MINUTES) | ||
.withUnit("h", ChronoUnit.HOURS) | ||
.withUnit("d", ChronoUnit.DAYS) | ||
.withUnit("w", ChronoUnit.WEEKS) | ||
.withUnit("mo", ChronoUnit.MONTHS) | ||
.withUnit("y", ChronoUnit.YEARS) | ||
.roundOff(ChronoUnit.MILLIS); | ||
|
||
private static final TemporalAmountParser<Duration> STANDARD_FORMAT = new DurationParser() | ||
.withUnit("ms", ChronoUnit.MILLIS) | ||
.withUnit("s", ChronoUnit.SECONDS) | ||
.withUnit("m", ChronoUnit.MINUTES) | ||
.withUnit("h", ChronoUnit.HOURS) | ||
.withUnit("d", ChronoUnit.DAYS) | ||
.withUnit("w", ChronoUnit.WEEKS) | ||
.withUnit("mo", ChronoUnit.MONTHS) | ||
.withUnit("y", ChronoUnit.YEARS); | ||
|
||
public static String format(Duration duration, boolean removeMillis) { | ||
if (removeMillis) { | ||
if (duration.toMillis() < ONE_SECOND.toMillis()) { | ||
return "0s"; | ||
} | ||
|
||
return WITHOUT_MILLIS_FORMAT.format(duration); | ||
} | ||
|
||
return STANDARD_FORMAT.format(duration); | ||
} | ||
|
||
public static String format(Duration duration) { | ||
return format(duration, false); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.eternalcode.commons.time; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import java.time.Duration; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class SimpleDurationUtilTest { | ||
|
||
@Test | ||
public void testFormatWithoutMillis() { | ||
Duration duration = Duration.ofMillis(500); | ||
String result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("0s", result); | ||
|
||
duration = Duration.ofSeconds(30); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("30s", result); | ||
|
||
duration = Duration.ofMinutes(5); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("5m", result); | ||
|
||
duration = Duration.ofHours(2); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("2h", result); | ||
|
||
duration = Duration.ofDays(1); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("1d", result); | ||
|
||
duration = Duration.ofDays(14); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("2w", result); | ||
|
||
duration = Duration.ofDays(60); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("2mo", result); | ||
|
||
duration = Duration.ofDays(365 * 3); | ||
result = SimpleDurationUtil.format(duration, true); | ||
assertEquals("3y", result); | ||
} | ||
Comment on lines
+9
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve test method clarity and add assertion messages The test method
Consider the following improvements:
- assertEquals("0s", result);
+ assertEquals("0s", result, "Duration of 500ms should be formatted as '0s' without milliseconds"); Apply this pattern to all assertions in the method.
|
||
|
||
@Test | ||
public void testFormatWithMillis() { | ||
Duration duration = Duration.ofMillis(500); | ||
String result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("500ms", result); | ||
|
||
duration = Duration.ofSeconds(30); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("30s", result); | ||
|
||
duration = Duration.ofMinutes(5); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("5m", result); | ||
|
||
duration = Duration.ofHours(2); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("2h", result); | ||
|
||
duration = Duration.ofDays(1); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("1d", result); | ||
|
||
duration = Duration.ofDays(14); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("2w", result); | ||
|
||
duration = Duration.ofDays(60); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("2mo", result); | ||
|
||
duration = Duration.ofDays(365 * 3); | ||
result = SimpleDurationUtil.format(duration, false); | ||
assertEquals("3y", result); | ||
} | ||
Comment on lines
+44
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve test method clarity and add assertion messages The test method
Consider the following improvements:
- assertEquals("500ms", result);
+ assertEquals("500ms", result, "Duration of 500ms should be formatted as '500ms' with milliseconds"); Apply this pattern to all assertions in the method.
|
||
|
||
@Test | ||
public void testFormatDefault() { | ||
Duration duration = Duration.ofSeconds(610); | ||
String result = SimpleDurationUtil.format(duration); | ||
assertEquals("10m10s", result); | ||
|
||
duration = Duration.ofMillis(120); | ||
result = SimpleDurationUtil.format(duration); | ||
assertEquals("120ms", result); | ||
} | ||
} | ||
Comment on lines
+1
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance overall test class structure and coverage While the test class covers the main functionality of
Consider the following improvements:
/**
* Test class for SimpleDurationUtil.
* This class contains unit tests to verify the correct functioning
* of the SimpleDurationUtil.format method under various scenarios.
*/
public class SimpleDurationUtilTest {
// ... existing code ...
}
@Test
public void testFormatZeroDuration() {
Duration duration = Duration.ZERO;
String result = SimpleDurationUtil.format(duration);
assertEquals("0s", result, "Zero duration should be formatted as '0s'");
}
@Test
public void testFormatNegativeDuration() {
Duration duration = Duration.ofSeconds(-60);
String result = SimpleDurationUtil.format(duration);
assertEquals("-1m", result, "Negative duration should be formatted with a minus sign");
}
@Test
public void testFormatMaxDuration() {
Duration duration = Duration.ofSeconds(Long.MAX_VALUE);
String result = SimpleDurationUtil.format(duration);
// Assert the expected result for maximum duration
}
class SimpleDurationUtilTest {
@Nested
class FormatWithoutMillis {
@Test
void shouldFormatSecondsWithoutMillis() {
// ... test case for seconds ...
}
@Test
void shouldFormatMinutesWithoutMillis() {
// ... test case for minutes ...
}
// ... other test methods ...
}
@Nested
class FormatWithMillis {
// ... similar structure for with-millis tests ...
}
@Nested
class FormatDefault {
// ... default format test methods ...
}
@Nested
class EdgeCases {
// ... edge case test methods ...
}
} These improvements will enhance the overall structure, readability, and coverage of the test class. |
Uh oh!
There was an error while loading. Please reload this page.