1#!/usr/bin/env bash
2
3. lib
4
5rm -rf temp1
6# Some tests for the '--match' flag, specifically the date-matching
7
8fmt_offset() {
9    if date -d "now${1}days" >& /dev/null; then
10        date -d "now${1}days" +"%Y%m%d"
11    elif date -v ${1}d >& /dev/null; then
12        date -v ${1}d +"%Y%m%d"
13    else
14        echo "Can't do date arithmetic on this system :(" >&2
15        return 1
16    fi
17}
18
19reset_repo () {
20  cd ..
21  rm -rf temp1
22  mkdir temp1
23  cd temp1
24  darcs init
25  touch bar
26  darcs add bar
27}
28
29create_entry () {
30  echo $1 >> bar
31  echo "$1/tester/a///" | tr / \\012 | darcs record -m "does not matter" --pipe bar
32}
33
34create_entry_now () {
35  echo today >> bar
36  darcs record -m "does not matter" bar -a -A tester
37}
38
39# parse_date just checks for parsing, while match_date checks for an actual match.
40# It's better if we can use "match_date", but we have to be able to construct such a date
41# based on a date dynamically generated by this test script.
42
43# alternately, it might be more useful to build a random date string generator
44# using QuickCheck... for any n random CalendarTimes, have it generate some
45# possible variants and roundtrip them to see if they match
46
47parse_date () {
48  darcs changes --match "date \"$1\"" > log
49  not grep fancy log
50}
51
52match_date () {
53  darcs changes --match "date \"$1\"" > log
54  grep tester log
55  not grep fancy log
56}
57
58nomatch_date () {
59  darcs changes --match "date \"$1\"" > log
60  not grep tester log
61  not grep fancy  log
62}
63
64mkdir temp1
65cd temp1
66reset_repo
67
68# this block of dates should all refer to the same thing
69year=1973
70mm=02
71dd=04
72hhmmss="15:08"
73create_entry "$year-$mm-$dd $hhmmss"
74match_date "$year-$mm-$dd"
75match_date "$year$mm$dd"
76match_date "$year-$mm"
77match_date "$year$mm"
78match_date "$year"
79
80# week dates. note that 2007 was selected as it starts on Monday
81reset_repo
82create_entry "2007-01-04 15:00"
83match_date '2007-W01-4'
84nomatch_date '2007-W01-1'
85match_date '2007W014'
86match_date '2007-W01'
87nomatch_date '2007-W02-1'
88create_entry "2007-01-08 15:00"
89match_date '2007-W02'
90match_date '2007-W02-1'
91create_entry "2007-05-20 15:00"
92match_date '2007-W20'
93nomatch_date '2007-W21'
94nomatch_date '2007-W19'
95# ordinal dates. eh... why not?
96match_date '2007-004'; # fourth day of 2007
97match_date '2007004'
98nomatch_date '2007-005'
99
100# midnight and zero
101reset_repo
102create_entry "1992-10-15 00:00"
103match_date '1992-10-14 24:00'
104match_date '1992-10-15 00:00'
105
106# all the same date/time
107reset_repo
108create_entry "1992-02-12T22:32:11"
109match_date '1992-02-12T22:32:11'
110match_date '1992-02-12 22:32:11'
111match_date '1992-02-12T223211.0000'
112
113# english dates - the old hard coded from < darcs 1.0.6
114reset_repo
115year=`date +%Y`
116mm=`date +%m`
117dd=`date +%d`
118hhmmss=`date +%k:%M:%S`
119tz=`date +%z`
120create_entry "$(($year-1))-$mm-$dd"
121nomatch_date 'today'
122nomatch_date 'yesterday'
123nomatch_date 'day before yesterday'
124nomatch_date 'last week'
125nomatch_date 'last month'
126# note: this test might fail if you run it just before midnight
127reset_repo
128create_entry_now
129match_date 'today'
130nomatch_date 'yesterday'
131nomatch_date 'day before yesterday'
132match_date 'last week'
133match_date 'last month'
134
135fmt_offset -1 || exit 200
136
137reset_repo
138create_entry "$(fmt_offset -350)"
139# english dates - new possibilities
140nomatch_date 'yesterday at 14:00:00'
141match_date 'last 3 years'
142match_date 'last year'
143nomatch_date '2 days ago'
144nomatch_date 'last month 13:00'
145nomatch_date '3 days before last week'
146reset_repo
147create_entry_now
148match_date 'day after yesterday'
149match_date 'day since yesterday'
150match_date 'week after last week'
151match_date 'week since last week'
152
153create_entry "1992-10-02 00:15"
154match_date '15 minutes after 1992-10-02'
155match_date '15 minutes since 1992-10-02'
156
157reset_repo
158create_entry "1992-10-02 00:15+05"
159# note that earlier dates will always match
160match_date '15 minutes after 1992-10-02 00:00+05';   # same time
161match_date '15 minutes after 1992-10-01 23:00+04';   # same time
162match_date '15 minutes since 1992-10-02 00:00+05';   # same time
163match_date '15 minutes since 1992-10-01 23:00+04';   # same time
164nomatch_date '15 minutes after 1992-10-02 01:00+05'; # 1 hour later
165nomatch_date '15 minutes after 1992-10-02 00:00+04'; # 1 hour later
166nomatch_date '1 hour, 15 minutes after 1992-10-02 00:00+05'; # 1 hour later
167nomatch_date '15 minutes since 1992-10-02 01:00+05'; # 1 hour later
168nomatch_date '15 minutes since 1992-10-02 00:00+04'; # 1 hour later
169nomatch_date '1 hour, 15 minutes since 1992-10-02 00:00+05'; # 1 hour later
170match_date '1 hour, 15 minutes after 1992-10-02 00:00+06'; # same time
171match_date '1 hour, 15 minutes after 1992-10-01 23:00+05'; # same time
172match_date '1 hour, 15 minutes since 1992-10-02 00:00+06'; # same time
173match_date '1 hour, 15 minutes since 1992-10-01 23:00+05'; # same time
174
175
176reset_repo
177create_entry_now
178create_entry 1992-10-02 00:15
179# english intervals
180nomatch_date 'between last fortnight and day before yesterday'
181match_date 'between last fortnight and today'
182match_date 'in the last 45 seconds'
183match_date 'after 1992'
184match_date 'since 1992'
185
186# iso 8601 intervals
187parse_date '1992-10-02 00:00Z/1992-10-02 00:16Z'
188match_date '1992-10-02 00:00/1992-10-02 00:16'
189match_date 'between 1992-10-02 00:00 and 1992-10-12 00:16'
190parse_date 'P3YT3M/1992'
191parse_date '1992/P3Y3M4DT5H3M2S'
192parse_date '1992/P3Y3M'
193
194# stuff from the manual
195reset_repo
196create_entry_now
197nomatch_date 'between 2004-03-12 and last week'
198match_date 'last week'
199parse_date 'yesterday'
200parse_date 'today 14:00'
201nomatch_date '3 days before last year at 17:00'
202# We can't in general parse the raw date output by darcs.  If we change the
203# format to not include timezone information, this would be possible.  But
204# maybe that's not desireable.  For now, we just won't test the raw date.
205#match_date "$raw_date"
206parse_date 'after 2005'
207parse_date 'since 2005'
208parse_date 'in the last 3 weeks'
209parse_date 'P3M/2006-03-17'
210parse_date '2004-01-02/2006-03-17'
211parse_date 'P2M6D'
212
213# cvs dates
214parse_date '2006/01/19 21:14:20 UTC'
215# We can't handle all timezones in the old style dates
216# so this test will not work everywhere
217# match_date "$year/$mm/$dd $hhmmss $tz"
218
219reset_repo
220create_entry '2038-01-01'
221match_date 'after 2037'
222match_date 'since 2037'
223rm -rf temp1 temp2
224