11package org .schabi .newpipe .extractor .localization ;
22
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
36import org .junit .jupiter .api .BeforeAll ;
47import org .junit .jupiter .api .Test ;
58import org .schabi .newpipe .extractor .exceptions .ParsingException ;
69
7- import static org .junit .jupiter .api .Assertions .assertEquals ;
8- import static org .junit .jupiter .api .Assertions .assertThrows ;
10+ import java .time .OffsetDateTime ;
11+ import java .time .ZoneOffset ;
12+ import java .time .temporal .ChronoUnit ;
913
10- class TimeAgoParserTest {
11- private static TimeAgoParser timeAgoParser ;
14+ public class TimeAgoParserTest {
15+ private static TimeAgoParser parser ;
16+ private static OffsetDateTime now ;
1217
1318 @ BeforeAll
14- static void setUp () {
15- timeAgoParser = TimeAgoPatternsManager .getTimeAgoParserFor (Localization .DEFAULT );
19+ public static void setUp () {
20+ parser = TimeAgoPatternsManager .getTimeAgoParserFor (Localization .DEFAULT );
21+ now = OffsetDateTime .now (ZoneOffset .UTC );
1622 }
1723
1824 @ 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" ));
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+ );
2482 }
2583
2684 @ Test
27- void testGetDurationError () {
28- assertThrows (ParsingException .class , () -> timeAgoParser .parseDuration ("abcd" ));
29- assertThrows (ParsingException .class , () -> timeAgoParser .parseDuration ("12 abcd" ));
85+ void parseTimeAgoShort () throws ParsingException {
86+ final TimeAgoParser parser = TimeAgoPatternsManager .getTimeAgoParserFor (Localization .DEFAULT );
87+ final OffsetDateTime now = OffsetDateTime .now (ZoneOffset .UTC );
88+
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 ()
144+ );
145+ }
146+
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\n Actual: %s" , expected , actual ));
30150 }
31- }
151+ }
0 commit comments