11package org .schabi .newpipe .extractor .localization ;
22
3- import org .junit .jupiter .api .BeforeAll ;
4- import org .junit .jupiter .api .Test ;
5- import org .schabi .newpipe .extractor .exceptions .ParsingException ;
6-
3+ import static org .junit .jupiter .api .Assertions .assertAll ;
74import static org .junit .jupiter .api .Assertions .assertEquals ;
8- import static org .junit .jupiter .api .Assertions .assertThrows ;
5+ import static org .schabi .newpipe .extractor .localization .TimeAgoParserTest .ParseTimeAgoTestData .greaterThanDay ;
6+ import static org .schabi .newpipe .extractor .localization .TimeAgoParserTest .ParseTimeAgoTestData .lessThanDay ;
97
10- class TimeAgoParserTest {
11- private static TimeAgoParser timeAgoParser ;
8+ import org .junit .jupiter .params .ParameterizedTest ;
9+ import org .junit .jupiter .params .provider .Arguments ;
10+ import org .junit .jupiter .params .provider .MethodSource ;
11+
12+ import java .time .Duration ;
13+ import java .time .LocalDateTime ;
14+ import java .time .OffsetDateTime ;
15+ import java .time .ZoneOffset ;
16+ import java .time .temporal .ChronoUnit ;
17+ import java .util .Objects ;
18+ import java .util .function .Function ;
19+ import java .util .stream .Stream ;
1220
13- @ BeforeAll
14- static void setUp () {
15- timeAgoParser = TimeAgoPatternsManager .getTimeAgoParserFor (Localization .DEFAULT );
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 );
1639 }
1740
18- @ Test
19- void testGetDuration () throws ParsingException {
20- assertEquals (1 , timeAgoParser .parseDuration ("one second" ));
21- assertEquals (1 , timeAgoParser .parseDuration ("second" ));
22- assertEquals (49 , timeAgoParser .parseDuration ("49 seconds" ));
23- assertEquals (61 , timeAgoParser .parseDuration ("1 minute, 1 second" ));
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 ));
49+
50+ final OffsetDateTime expected = testData .getExpectedApplyToNow ().apply (now );
51+
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+ ))
61+ );
2462 }
2563
26- @ Test
27- void testGetDurationError () {
28- assertThrows (ParsingException .class , () -> timeAgoParser .parseDuration ("abcd" ));
29- assertThrows (ParsingException .class , () -> timeAgoParser .parseDuration ("12 abcd" ));
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+ }
30113 }
31- }
114+ }
0 commit comments