-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1212 from Isira-Seneviratne/TimeAgoParser-unused
Remove unused method in TimeAgoParser
- Loading branch information
Showing
5 changed files
with
141 additions
and
233 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
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
125 changes: 104 additions & 21 deletions
125
extractor/src/test/java/org/schabi/newpipe/extractor/localization/TimeAgoParserTest.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 |
---|---|---|
@@ -1,31 +1,114 @@ | ||
package org.schabi.newpipe.extractor.localization; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.schabi.newpipe.extractor.exceptions.ParsingException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.schabi.newpipe.extractor.localization.TimeAgoParserTest.ParseTimeAgoTestData.greaterThanDay; | ||
import static org.schabi.newpipe.extractor.localization.TimeAgoParserTest.ParseTimeAgoTestData.lessThanDay; | ||
|
||
class TimeAgoParserTest { | ||
private static TimeAgoParser timeAgoParser; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
timeAgoParser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); | ||
class TimeAgoParserTest { | ||
public static Stream<Arguments> parseTimeAgo() { | ||
return Stream.of( | ||
lessThanDay(Duration.ofSeconds(1), "1 second", "1 sec"), | ||
lessThanDay(Duration.ofSeconds(12), "12 second", "12 sec"), | ||
lessThanDay(Duration.ofMinutes(1), "1 minute", "1 min"), | ||
lessThanDay(Duration.ofMinutes(23), "23 minutes", "23 min"), | ||
lessThanDay(Duration.ofHours(1), "1 hour", "1 hr"), | ||
lessThanDay(Duration.ofHours(8), "8 hour", "8 hr"), | ||
greaterThanDay(d -> d.minusDays(1), "1 day", "1 day"), | ||
greaterThanDay(d -> d.minusDays(3), "3 days", "3 day"), | ||
greaterThanDay(d -> d.minusWeeks(1), "1 week", "1 wk"), | ||
greaterThanDay(d -> d.minusWeeks(3), "3 weeks", "3 wk"), | ||
greaterThanDay(d -> d.minusMonths(1), "1 month", "1 mo"), | ||
greaterThanDay(d -> d.minusMonths(3), "3 months", "3 mo"), | ||
greaterThanDay(d -> d.minusYears(1).minusDays(1), "1 year", "1 yr"), | ||
greaterThanDay(d -> d.minusYears(3).minusDays(1), "3 years", "3 yr") | ||
).map(Arguments::of); | ||
} | ||
|
||
@Test | ||
void testGetDuration() throws ParsingException { | ||
assertEquals(1, timeAgoParser.parseDuration("one second")); | ||
assertEquals(1, timeAgoParser.parseDuration("second")); | ||
assertEquals(49, timeAgoParser.parseDuration("49 seconds")); | ||
assertEquals(61, timeAgoParser.parseDuration("1 minute, 1 second")); | ||
@ParameterizedTest | ||
@MethodSource | ||
void parseTimeAgo(final ParseTimeAgoTestData testData) { | ||
final OffsetDateTime now = OffsetDateTime.of( | ||
LocalDateTime.of(2020, 1, 1, 1, 1, 1), | ||
ZoneOffset.UTC); | ||
final TimeAgoParser parser = Objects.requireNonNull( | ||
TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT, now)); | ||
|
||
final OffsetDateTime expected = testData.getExpectedApplyToNow().apply(now); | ||
|
||
assertAll( | ||
Stream.of( | ||
testData.getTextualDateLong(), | ||
testData.getTextualDateShort()) | ||
.map(textualDate -> () -> assertEquals( | ||
expected, | ||
parser.parse(textualDate).offsetDateTime(), | ||
"Expected " + expected + " for " + textualDate | ||
)) | ||
); | ||
} | ||
|
||
@Test | ||
void testGetDurationError() { | ||
assertThrows(ParsingException.class, () -> timeAgoParser.parseDuration("abcd")); | ||
assertThrows(ParsingException.class, () -> timeAgoParser.parseDuration("12 abcd")); | ||
static class ParseTimeAgoTestData { | ||
public static final String AGO_SUFFIX = " ago"; | ||
private final Function<OffsetDateTime, OffsetDateTime> expectedApplyToNow; | ||
private final String textualDateLong; | ||
private final String textualDateShort; | ||
|
||
ParseTimeAgoTestData( | ||
final Function<OffsetDateTime, OffsetDateTime> expectedApplyToNow, | ||
final String textualDateLong, | ||
final String textualDateShort | ||
) { | ||
this.expectedApplyToNow = expectedApplyToNow; | ||
this.textualDateLong = textualDateLong; | ||
this.textualDateShort = textualDateShort; | ||
} | ||
|
||
public static ParseTimeAgoTestData lessThanDay( | ||
final Duration duration, | ||
final String textualDateLong, | ||
final String textualDateShort | ||
) { | ||
return new ParseTimeAgoTestData( | ||
d -> d.minus(duration), | ||
textualDateLong + AGO_SUFFIX, | ||
textualDateShort + AGO_SUFFIX); | ||
} | ||
|
||
public static ParseTimeAgoTestData greaterThanDay( | ||
final Function<OffsetDateTime, OffsetDateTime> expectedApplyToNow, | ||
final String textualDateLong, | ||
final String textualDateShort | ||
) { | ||
return new ParseTimeAgoTestData( | ||
d -> expectedApplyToNow.apply(d).truncatedTo(ChronoUnit.HOURS), | ||
textualDateLong + AGO_SUFFIX, | ||
textualDateShort + AGO_SUFFIX); | ||
} | ||
|
||
public Function<OffsetDateTime, OffsetDateTime> getExpectedApplyToNow() { | ||
return expectedApplyToNow; | ||
} | ||
|
||
public String getTextualDateLong() { | ||
return textualDateLong; | ||
} | ||
|
||
public String getTextualDateShort() { | ||
return textualDateShort; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.