From c06b8731f1f648d6dbd01b1c40d31efee1ccf4ec Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 19 Aug 2024 11:38:07 +0530 Subject: [PATCH] Remove unused method in TimeAgoParser --- .../extractor/localization/TimeAgoParser.java | 57 +------ .../localization/TimeAgoParserTest.java | 150 +++++++++++++++-- .../newpipe/extractor/utils/TimeagoTest.java | 154 ------------------ 3 files changed, 138 insertions(+), 223 deletions(-) delete mode 100644 extractor/src/test/java/org/schabi/newpipe/extractor/utils/TimeagoTest.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java index a1ef801c6..edf03e370 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java @@ -7,21 +7,14 @@ import org.schabi.newpipe.extractor.utils.Parser; import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.regex.Pattern; -import java.util.regex.Matcher; -import java.util.regex.MatchResult; /** * A helper class that is meant to be used by services that need to parse durations such as * {@code 23 seconds} and/or upload dates in the format {@code 2 days ago} or similar. */ public class TimeAgoParser { - - private static final Pattern DURATION_PATTERN = Pattern.compile("(?:(\\d+) )?([A-z]+)"); - private final PatternsHolder patternsHolder; private final OffsetDateTime now; @@ -50,13 +43,11 @@ public class TimeAgoParser { * @throws ParsingException if the time unit could not be recognized */ public DateWrapper parse(final String textualDate) throws ParsingException { - for (final Map.Entry> caseUnitEntry - : patternsHolder.specialCases().entrySet()) { + for (final var caseUnitEntry : patternsHolder.specialCases().entrySet()) { final ChronoUnit chronoUnit = caseUnitEntry.getKey(); - for (final Map.Entry caseMapToAmountEntry - : caseUnitEntry.getValue().entrySet()) { + for (final var caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) { final String caseText = caseMapToAmountEntry.getKey(); - final Integer caseAmount = caseMapToAmountEntry.getValue(); + final int caseAmount = caseMapToAmountEntry.getValue(); if (textualDateMatches(textualDate, caseText)) { return getResultFor(caseAmount, chronoUnit); @@ -67,48 +58,6 @@ public class TimeAgoParser { return getResultFor(parseTimeAgoAmount(textualDate), parseChronoUnit(textualDate)); } - /** - * Parses a textual duration into a duration computer number. - * - * @param textualDuration the textual duration to parse - * @return the textual duration parsed, as a primitive {@code long} - * @throws ParsingException if the textual duration could not be parsed - */ - public long parseDuration(final String textualDuration) throws ParsingException { - // We can't use Matcher.results, as it is only available on Android 14 and above - final Matcher matcher = DURATION_PATTERN.matcher(textualDuration); - final List results = new ArrayList<>(); - while (matcher.find()) { - results.add(matcher.toMatchResult()); - } - - return results.stream() - .map(match -> { - final String digits = match.group(1); - final String word = match.group(2); - - int amount; - try { - amount = Integer.parseInt(digits); - } catch (final NumberFormatException ignored) { - amount = 1; - } - - final ChronoUnit unit; - try { - unit = parseChronoUnit(word); - } catch (final ParsingException ignored) { - return 0L; - } - - return amount * unit.getDuration().getSeconds(); - }) - .filter(n -> n > 0) - .reduce(Long::sum) - .orElseThrow(() -> new ParsingException( - "Could not parse duration \"" + textualDuration + "\"")); - } - private int parseTimeAgoAmount(final String textualDate) { try { return Integer.parseInt(textualDate.replaceAll("\\D+", "")); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/localization/TimeAgoParserTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/localization/TimeAgoParserTest.java index 4d12b3da9..a5d3e9cbb 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/localization/TimeAgoParserTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/localization/TimeAgoParserTest.java @@ -1,31 +1,151 @@ package org.schabi.newpipe.extractor.localization; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + 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.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.temporal.ChronoUnit; -class TimeAgoParserTest { - private static TimeAgoParser timeAgoParser; +public class TimeAgoParserTest { + private static TimeAgoParser parser; + private static OffsetDateTime now; @BeforeAll - static void setUp() { - timeAgoParser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); + public static void setUp() { + parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); + now = OffsetDateTime.now(ZoneOffset.UTC); } @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")); + void parseTimeAgo() throws ParsingException { + assertTimeWithin1s( + now.minusSeconds(1), + parser.parse("1 second ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusSeconds(12), + parser.parse("12 second ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusMinutes(1), + parser.parse("1 minute ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusMinutes(23), + parser.parse("23 minutes ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusHours(1), + parser.parse("1 hour ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusHours(8), + parser.parse("8 hours ago").offsetDateTime() + ); + assertEquals( + now.minusDays(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 day ago").offsetDateTime() + ); + assertEquals( + now.minusDays(3).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 days ago").offsetDateTime() + ); + assertEquals( + now.minusWeeks(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 week ago").offsetDateTime() + ); + assertEquals( + now.minusWeeks(3).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 weeks ago").offsetDateTime() + ); + assertEquals( + now.minusMonths(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 month ago").offsetDateTime() + ); + assertEquals( + now.minusMonths(3).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 months ago").offsetDateTime() + ); + assertEquals( + now.minusYears(1).minusDays(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 year ago").offsetDateTime() + ); + assertEquals( + now.minusYears(3).minusDays(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 years ago").offsetDateTime() + ); } @Test - void testGetDurationError() { - assertThrows(ParsingException.class, () -> timeAgoParser.parseDuration("abcd")); - assertThrows(ParsingException.class, () -> timeAgoParser.parseDuration("12 abcd")); + void parseTimeAgoShort() throws ParsingException { + final TimeAgoParser parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); + final OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC); + + assertTimeWithin1s( + now.minusSeconds(1), + parser.parse("1 sec ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusSeconds(12), + parser.parse("12 sec ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusMinutes(1), + parser.parse("1 min ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusMinutes(23), + parser.parse("23 min ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusHours(1), + parser.parse("1 hr ago").offsetDateTime() + ); + assertTimeWithin1s( + now.minusHours(8), + parser.parse("8 hr ago").offsetDateTime() + ); + assertEquals( + now.minusDays(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 day ago").offsetDateTime() + ); + assertEquals( + now.minusDays(3).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 days ago").offsetDateTime() + ); + assertEquals( + now.minusWeeks(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 wk ago").offsetDateTime() + ); + assertEquals( + now.minusWeeks(3).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 wk ago").offsetDateTime() + ); + assertEquals( + now.minusMonths(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 mo ago").offsetDateTime() + ); + assertEquals( + now.minusMonths(3).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 mo ago").offsetDateTime() + ); + assertEquals( + now.minusYears(1).minusDays(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("1 yr ago").offsetDateTime() + ); + assertEquals( + now.minusYears(3).minusDays(1).truncatedTo(ChronoUnit.HOURS), + parser.parse("3 yr ago").offsetDateTime() + ); } -} \ No newline at end of file + + void assertTimeWithin1s(final OffsetDateTime expected, final OffsetDateTime actual) { + final long delta = Math.abs(expected.toEpochSecond() - actual.toEpochSecond()); + assertTrue(delta <= 1, String.format("Expected: %s\nActual: %s", expected, actual)); + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/TimeagoTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/TimeagoTest.java deleted file mode 100644 index 0b4eecb50..000000000 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/TimeagoTest.java +++ /dev/null @@ -1,154 +0,0 @@ -package org.schabi.newpipe.extractor.utils; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.schabi.newpipe.extractor.exceptions.ParsingException; -import org.schabi.newpipe.extractor.localization.Localization; -import org.schabi.newpipe.extractor.localization.TimeAgoParser; -import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager; - -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.time.temporal.ChronoUnit; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class TimeagoTest { - private static TimeAgoParser parser; - private static OffsetDateTime now; - - @BeforeAll - public static void setUp() { - parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); - now = OffsetDateTime.now(ZoneOffset.UTC); - } - - @Test - void parseTimeago() throws ParsingException { - assertTimeWithin1s( - now.minus(1, ChronoUnit.SECONDS), - parser.parse("1 second ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(12, ChronoUnit.SECONDS), - parser.parse("12 second ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(1, ChronoUnit.MINUTES), - parser.parse("1 minute ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(23, ChronoUnit.MINUTES), - parser.parse("23 minutes ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(1, ChronoUnit.HOURS), - parser.parse("1 hour ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(8, ChronoUnit.HOURS), - parser.parse("8 hours ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 day ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 days ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 week ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 weeks ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 month ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 months ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 year ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 years ago").offsetDateTime() - ); - } - - @Test - void parseTimeagoShort() throws ParsingException { - final TimeAgoParser parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); - final OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC); - - assertTimeWithin1s( - now.minus(1, ChronoUnit.SECONDS), - parser.parse("1 sec ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(12, ChronoUnit.SECONDS), - parser.parse("12 sec ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(1, ChronoUnit.MINUTES), - parser.parse("1 min ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(23, ChronoUnit.MINUTES), - parser.parse("23 min ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(1, ChronoUnit.HOURS), - parser.parse("1 hr ago").offsetDateTime() - ); - assertTimeWithin1s( - now.minus(8, ChronoUnit.HOURS), - parser.parse("8 hr ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 day ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.DAYS).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 days ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 wk ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.WEEKS).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 wk ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 mo ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.MONTHS).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 mo ago").offsetDateTime() - ); - assertEquals( - now.minus(1, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS), - parser.parse("1 yr ago").offsetDateTime() - ); - assertEquals( - now.minus(3, ChronoUnit.YEARS).minusDays(1).truncatedTo(ChronoUnit.HOURS), - parser.parse("3 yr ago").offsetDateTime() - ); - } - - void assertTimeWithin1s(final OffsetDateTime expected, final OffsetDateTime actual) { - final long delta = Math.abs(expected.toEpochSecond() - actual.toEpochSecond()); - assertTrue(delta <= 1, String.format("Expected: %s\nActual: %s", expected, actual)); - } -}