|
1 | 1 | package org.schabi.newpipe.extractor.localization; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertAll; |
3 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.schabi.newpipe.extractor.localization.TimeAgoParserTest.ParseTimeAgoTestData.greaterThanDay; |
| 6 | +import static org.schabi.newpipe.extractor.localization.TimeAgoParserTest.ParseTimeAgoTestData.lessThanDay; |
5 | 7 |
|
6 | | -import org.junit.jupiter.api.BeforeAll; |
7 | | -import org.junit.jupiter.api.Test; |
8 | | -import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
9 | 11 |
|
| 12 | +import java.time.Duration; |
| 13 | +import java.time.LocalDateTime; |
10 | 14 | import java.time.OffsetDateTime; |
11 | 15 | import java.time.ZoneOffset; |
12 | 16 | import java.time.temporal.ChronoUnit; |
| 17 | +import java.util.Objects; |
| 18 | +import java.util.function.Function; |
| 19 | +import java.util.stream.Stream; |
13 | 20 |
|
14 | | -public class TimeAgoParserTest { |
15 | | - private static TimeAgoParser parser; |
16 | | - private static OffsetDateTime now; |
17 | | - |
18 | | - @BeforeAll |
19 | | - public static void setUp() { |
20 | | - parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); |
21 | | - now = OffsetDateTime.now(ZoneOffset.UTC); |
| 21 | +class TimeAgoParserTest { |
| 22 | + public static Stream<Arguments> parseTimeAgo() { |
| 23 | + return Stream.of( |
| 24 | + lessThanDay(Duration.ofSeconds(1), "1 second", "1 sec"), |
| 25 | + lessThanDay(Duration.ofSeconds(12), "12 second", "12 sec"), |
| 26 | + lessThanDay(Duration.ofMinutes(1), "1 minute", "1 min"), |
| 27 | + lessThanDay(Duration.ofMinutes(23), "23 minutes", "23 min"), |
| 28 | + lessThanDay(Duration.ofHours(1), "1 hour", "1 hr"), |
| 29 | + lessThanDay(Duration.ofHours(8), "8 hour", "8 hr"), |
| 30 | + greaterThanDay(d -> d.minusDays(1), "1 day", "1 day"), |
| 31 | + greaterThanDay(d -> d.minusDays(3), "3 days", "3 day"), |
| 32 | + greaterThanDay(d -> d.minusWeeks(1), "1 week", "1 wk"), |
| 33 | + greaterThanDay(d -> d.minusWeeks(3), "3 weeks", "3 wk"), |
| 34 | + greaterThanDay(d -> d.minusMonths(1), "1 month", "1 mo"), |
| 35 | + greaterThanDay(d -> d.minusMonths(3), "3 months", "3 mo"), |
| 36 | + greaterThanDay(d -> d.minusYears(1).minusDays(1), "1 year", "1 yr"), |
| 37 | + greaterThanDay(d -> d.minusYears(3).minusDays(1), "3 years", "3 yr") |
| 38 | + ).map(Arguments::of); |
22 | 39 | } |
23 | 40 |
|
24 | | - @Test |
25 | | - void parseTimeAgo() throws ParsingException { |
26 | | - assertTimeWithin1s( |
27 | | - now.minusSeconds(1), |
28 | | - parser.parse("1 second ago").offsetDateTime() |
29 | | - ); |
30 | | - assertTimeWithin1s( |
31 | | - now.minusSeconds(12), |
32 | | - parser.parse("12 second ago").offsetDateTime() |
33 | | - ); |
34 | | - assertTimeWithin1s( |
35 | | - now.minusMinutes(1), |
36 | | - parser.parse("1 minute ago").offsetDateTime() |
37 | | - ); |
38 | | - assertTimeWithin1s( |
39 | | - now.minusMinutes(23), |
40 | | - parser.parse("23 minutes ago").offsetDateTime() |
41 | | - ); |
42 | | - assertTimeWithin1s( |
43 | | - now.minusHours(1), |
44 | | - parser.parse("1 hour ago").offsetDateTime() |
45 | | - ); |
46 | | - assertTimeWithin1s( |
47 | | - now.minusHours(8), |
48 | | - parser.parse("8 hours ago").offsetDateTime() |
49 | | - ); |
50 | | - assertEquals( |
51 | | - now.minusDays(1).truncatedTo(ChronoUnit.HOURS), |
52 | | - parser.parse("1 day ago").offsetDateTime() |
53 | | - ); |
54 | | - assertEquals( |
55 | | - now.minusDays(3).truncatedTo(ChronoUnit.HOURS), |
56 | | - parser.parse("3 days ago").offsetDateTime() |
57 | | - ); |
58 | | - assertEquals( |
59 | | - now.minusWeeks(1).truncatedTo(ChronoUnit.HOURS), |
60 | | - parser.parse("1 week ago").offsetDateTime() |
61 | | - ); |
62 | | - assertEquals( |
63 | | - now.minusWeeks(3).truncatedTo(ChronoUnit.HOURS), |
64 | | - parser.parse("3 weeks ago").offsetDateTime() |
65 | | - ); |
66 | | - assertEquals( |
67 | | - now.minusMonths(1).truncatedTo(ChronoUnit.HOURS), |
68 | | - parser.parse("1 month ago").offsetDateTime() |
69 | | - ); |
70 | | - assertEquals( |
71 | | - now.minusMonths(3).truncatedTo(ChronoUnit.HOURS), |
72 | | - parser.parse("3 months ago").offsetDateTime() |
73 | | - ); |
74 | | - assertEquals( |
75 | | - now.minusYears(1).minusDays(1).truncatedTo(ChronoUnit.HOURS), |
76 | | - parser.parse("1 year ago").offsetDateTime() |
77 | | - ); |
78 | | - assertEquals( |
79 | | - now.minusYears(3).minusDays(1).truncatedTo(ChronoUnit.HOURS), |
80 | | - parser.parse("3 years ago").offsetDateTime() |
81 | | - ); |
82 | | - } |
| 41 | + @ParameterizedTest |
| 42 | + @MethodSource |
| 43 | + void parseTimeAgo(final ParseTimeAgoTestData testData) { |
| 44 | + final OffsetDateTime now = OffsetDateTime.of( |
| 45 | + LocalDateTime.of(2020, 1, 1, 1, 1, 1), |
| 46 | + ZoneOffset.UTC); |
| 47 | + final TimeAgoParser parser = Objects.requireNonNull( |
| 48 | + TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT, now)); |
83 | 49 |
|
84 | | - @Test |
85 | | - void parseTimeAgoShort() throws ParsingException { |
86 | | - final TimeAgoParser parser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.DEFAULT); |
87 | | - final OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC); |
| 50 | + final OffsetDateTime expected = testData.getExpectedApplyToNow().apply(now); |
88 | 51 |
|
89 | | - assertTimeWithin1s( |
90 | | - now.minusSeconds(1), |
91 | | - parser.parse("1 sec ago").offsetDateTime() |
92 | | - ); |
93 | | - assertTimeWithin1s( |
94 | | - now.minusSeconds(12), |
95 | | - parser.parse("12 sec ago").offsetDateTime() |
96 | | - ); |
97 | | - assertTimeWithin1s( |
98 | | - now.minusMinutes(1), |
99 | | - parser.parse("1 min ago").offsetDateTime() |
100 | | - ); |
101 | | - assertTimeWithin1s( |
102 | | - now.minusMinutes(23), |
103 | | - parser.parse("23 min ago").offsetDateTime() |
104 | | - ); |
105 | | - assertTimeWithin1s( |
106 | | - now.minusHours(1), |
107 | | - parser.parse("1 hr ago").offsetDateTime() |
108 | | - ); |
109 | | - assertTimeWithin1s( |
110 | | - now.minusHours(8), |
111 | | - parser.parse("8 hr ago").offsetDateTime() |
112 | | - ); |
113 | | - assertEquals( |
114 | | - now.minusDays(1).truncatedTo(ChronoUnit.HOURS), |
115 | | - parser.parse("1 day ago").offsetDateTime() |
116 | | - ); |
117 | | - assertEquals( |
118 | | - now.minusDays(3).truncatedTo(ChronoUnit.HOURS), |
119 | | - parser.parse("3 days ago").offsetDateTime() |
120 | | - ); |
121 | | - assertEquals( |
122 | | - now.minusWeeks(1).truncatedTo(ChronoUnit.HOURS), |
123 | | - parser.parse("1 wk ago").offsetDateTime() |
124 | | - ); |
125 | | - assertEquals( |
126 | | - now.minusWeeks(3).truncatedTo(ChronoUnit.HOURS), |
127 | | - parser.parse("3 wk ago").offsetDateTime() |
128 | | - ); |
129 | | - assertEquals( |
130 | | - now.minusMonths(1).truncatedTo(ChronoUnit.HOURS), |
131 | | - parser.parse("1 mo ago").offsetDateTime() |
132 | | - ); |
133 | | - assertEquals( |
134 | | - now.minusMonths(3).truncatedTo(ChronoUnit.HOURS), |
135 | | - parser.parse("3 mo ago").offsetDateTime() |
136 | | - ); |
137 | | - assertEquals( |
138 | | - now.minusYears(1).minusDays(1).truncatedTo(ChronoUnit.HOURS), |
139 | | - parser.parse("1 yr ago").offsetDateTime() |
140 | | - ); |
141 | | - assertEquals( |
142 | | - now.minusYears(3).minusDays(1).truncatedTo(ChronoUnit.HOURS), |
143 | | - parser.parse("3 yr ago").offsetDateTime() |
| 52 | + assertAll( |
| 53 | + Stream.of( |
| 54 | + testData.getTextualDateLong(), |
| 55 | + testData.getTextualDateShort()) |
| 56 | + .map(textualDate -> () -> assertEquals( |
| 57 | + expected, |
| 58 | + parser.parse(textualDate).offsetDateTime(), |
| 59 | + "Expected " + expected + " for " + textualDate |
| 60 | + )) |
144 | 61 | ); |
145 | 62 | } |
146 | 63 |
|
147 | | - void assertTimeWithin1s(final OffsetDateTime expected, final OffsetDateTime actual) { |
148 | | - final long delta = Math.abs(expected.toEpochSecond() - actual.toEpochSecond()); |
149 | | - assertTrue(delta <= 1, String.format("Expected: %s\nActual: %s", expected, actual)); |
| 64 | + static class ParseTimeAgoTestData { |
| 65 | + public static final String AGO_SUFFIX = " ago"; |
| 66 | + private final Function<OffsetDateTime, OffsetDateTime> expectedApplyToNow; |
| 67 | + private final String textualDateLong; |
| 68 | + private final String textualDateShort; |
| 69 | + |
| 70 | + ParseTimeAgoTestData( |
| 71 | + final Function<OffsetDateTime, OffsetDateTime> expectedApplyToNow, |
| 72 | + final String textualDateLong, |
| 73 | + final String textualDateShort |
| 74 | + ) { |
| 75 | + this.expectedApplyToNow = expectedApplyToNow; |
| 76 | + this.textualDateLong = textualDateLong; |
| 77 | + this.textualDateShort = textualDateShort; |
| 78 | + } |
| 79 | + |
| 80 | + public static ParseTimeAgoTestData lessThanDay( |
| 81 | + final Duration duration, |
| 82 | + final String textualDateLong, |
| 83 | + final String textualDateShort |
| 84 | + ) { |
| 85 | + return new ParseTimeAgoTestData( |
| 86 | + d -> d.minus(duration), |
| 87 | + textualDateLong + AGO_SUFFIX, |
| 88 | + textualDateShort + AGO_SUFFIX); |
| 89 | + } |
| 90 | + |
| 91 | + public static ParseTimeAgoTestData greaterThanDay( |
| 92 | + final Function<OffsetDateTime, OffsetDateTime> expectedApplyToNow, |
| 93 | + final String textualDateLong, |
| 94 | + final String textualDateShort |
| 95 | + ) { |
| 96 | + return new ParseTimeAgoTestData( |
| 97 | + d -> expectedApplyToNow.apply(d).truncatedTo(ChronoUnit.HOURS), |
| 98 | + textualDateLong + AGO_SUFFIX, |
| 99 | + textualDateShort + AGO_SUFFIX); |
| 100 | + } |
| 101 | + |
| 102 | + public Function<OffsetDateTime, OffsetDateTime> getExpectedApplyToNow() { |
| 103 | + return expectedApplyToNow; |
| 104 | + } |
| 105 | + |
| 106 | + public String getTextualDateLong() { |
| 107 | + return textualDateLong; |
| 108 | + } |
| 109 | + |
| 110 | + public String getTextualDateShort() { |
| 111 | + return textualDateShort; |
| 112 | + } |
150 | 113 | } |
151 | 114 | } |
0 commit comments