Made committer details and time = author details and time
[git-darcs-import.git] / tests / match.pl
blob670b35534398941d0d4f1029973a0b22f34ad74a
1 #!/usr/bin/env perl
3 # Some tests for the '--match' flag
5 use lib 'lib/perl';
6 use Test::More 'no_plan';
7 use Test::Darcs;
8 use Shell::Command;
9 use strict;
10 use warnings;
12 # predeclare some subs so we can use them without parens.
13 sub reset_repo;
14 sub create_entry;
15 sub match_date;
16 sub nomatch_date;
17 sub parse_date;
19 # matching by date
20 reset_repo();
21 create_entry( "1973-02-04 15:08Z" );
23 # parse_date just checks for parsing, while match_date checks for an actual match.
24 # It's better if we can use "match_date", but we have to be able to construct such a date
25 # based on a date dynamically generated by this test script.
27 my $raw_date = get_first_date_from_changes();
28 my ($mon,$mm,$dd,$year,$hhmmss,$tz) = deconstruct_date( $raw_date );
30 # use Data::Dumper;
31 # warn Dumper ($raw_date, $mon,$mm,$dd,$year,$hhmmss,$tz );
33 # alternately, it might be more useful to build a random date string generator
34 # using QuickCheck... for any n random CalendarTimes, have it generate some
35 # possible variants and roundtrip them to see if they match
37 # this block of dates should all refer to the same thing
38 match_date "$year-$mm-$dd";
39 match_date "$year$mm$dd";
40 match_date "$year-$mm";
41 match_date "$year$mm";
42 match_date "$year";
44 # week dates. note that 2007 was selected as it starts on Monday
45 reset_repo();
46 create_entry "2007-01-04 15:00";
47 match_date '2007-W01-4';
48 nomatch_date '2007-W01-1';
49 match_date '2007W014';
50 match_date '2007-W01';
51 nomatch_date '2007-W02-1';
52 create_entry "2007-01-08 15:00";
53 match_date '2007-W02';
54 match_date '2007-W02-1';
55 create_entry "2007-05-20 15:00";
56 match_date '2007-W20';
57 nomatch_date '2007-W21';
58 nomatch_date '2007-W19';
59 # ordinal dates. eh... why not?
60 match_date '2007-004'; # fourth day of 2007
61 match_date '2007004';
62 nomatch_date '2007-005';
64 # midnight and zero
65 reset_repo();
66 create_entry "1992-10-15 00:00";
67 match_date '1992-10-14 24:00';
68 match_date '1992-10-15 00:00';
70 # all the same date/time
71 reset_repo();
72 create_entry "1992-02-12T22:32:11";
73 match_date '1992-02-12T22:32:11';
74 match_date '1992-02-12 22:32:11';
75 match_date '1992-02-12T223211.0000';
77 # english dates - the old hard coded from < darcs 1.0.6
78 reset_repo();
79 create_entry_now();
80 $raw_date = get_first_date_from_changes();
81 ($mon,$mm,$dd,$year,$hhmmss,$tz) = deconstruct_date( $raw_date );
82 reset_repo();
83 create_entry(($year-1)."$mm-$dd");
84 nomatch_date 'today';
85 nomatch_date 'yesterday';
86 nomatch_date 'day before yesterday';
87 nomatch_date 'last week';
88 nomatch_date 'last month';
89 # note: this test might fail if you run it just before midnight
90 reset_repo();
91 create_entry_now();
92 match_date 'today';
93 nomatch_date 'yesterday';
94 nomatch_date 'day before yesterday';
95 match_date 'last week';
96 match_date 'last month';
98 reset_repo();
99 create_entry(($year-1)."-$mm-$dd");
100 # english dates - new possibilities
101 nomatch_date 'yesterday at 14:00:00';
102 match_date 'last 3 years';
103 match_date 'last year';
104 nomatch_date '2 days ago';
105 nomatch_date 'last month 13:00' ;
106 nomatch_date '3 days before last week';
107 reset_repo();
108 create_entry_now();
109 match_date 'day after yesterday';
110 match_date 'week after last week';
111 create_entry("1992-10-02 00:15");
112 match_date '15 minutes after 1992-10-02';
113 reset_repo();
114 create_entry("1992-10-02 00:15+05");
115 # note that earlier dates will always match
116 match_date '15 minutes after 1992-10-02 00:00+05'; # same time
117 match_date '15 minutes after 1992-10-01 23:00+04'; # same time
118 nomatch_date '15 minutes after 1992-10-02 01:00+05'; # 1 hour later
119 nomatch_date '15 minutes after 1992-10-02 00:00+04'; # 1 hour later
120 nomatch_date '1 hour, 15 minutes after 1992-10-02 00:00+05'; # 1 hour later
121 match_date '1 hour, 15 minutes after 1992-10-02 00:00+06'; # same time
122 match_date '1 hour, 15 minutes after 1992-10-01 23:00+05'; # same time
124 reset_repo();
125 create_entry_now();
126 create_entry("1992-10-02 00:15");
127 # english intervals
128 nomatch_date 'between last fortnight and day before yesterday';
129 match_date 'between last fortnight and today';
130 match_date 'in the last 45 seconds';
131 match_date 'after 1992';
134 # iso 8601 intervals
135 parse_date '1992-10-02 00:00Z/1992-10-02 00:16Z';
136 match_date '1992-10-02 00:00/1992-10-02 00:16';
137 match_date 'between 1992-10-02 00:00 and 1992-10-12 00:16';
138 parse_date 'P3YT3M/1992';
139 parse_date '1992/P3Y3M4DT5H3M2S';
140 parse_date '1992/P3Y3M';
142 # stuff from the manual
143 reset_repo();
144 create_entry_now();
145 nomatch_date 'between 2004-03-12 and last week';
146 match_date 'last week';
147 parse_date 'yesterday';
148 parse_date 'today 14:00';
149 nomatch_date '3 days before last year at 17:00';
150 # We can't in general parse the raw date output by darcs. If we change the
151 # format to not include timezone information, this would be possible. But
152 # maybe that's not desireable. For now, we just won't test the raw date.
153 #match_date "$raw_date";
154 parse_date 'after 2005';
155 parse_date 'in the last 3 weeks';
156 parse_date 'P3M/2006-03-17';
157 parse_date '2004-01-02/2006-03-17';
158 parse_date 'P2M6D';
160 # cvs dates
161 parse_date '2006/01/19 21:14:20 UTC';
162 # We can't handle all timezones in the old style dates
163 # so this test will not work everywhere
164 # match_date "$year/$mm/$dd $hhmmss $tz";
166 # -------------------------------------------------------------------
167 # matching on atomic stuff (other than date)
168 # -------------------------------------------------------------------
170 init_tmp_repo();
172 touch 'bar';
173 darcs qw( add bar );
174 darcs qw( record -a -m "first patch" bar -A author1 );
175 `echo foo > bar`;
176 darcs qw( record -a -m "\"second\" \\ patch" bar -A author2 );
177 `echo blop > bar`;
178 darcs qw( record -a -m "second" bar -A author3 );
180 # matching on author really matches on that, and not something else
181 unlike(darcs(qw(changes --match='author "first patch"')), qr(.+));
183 { # normal changes shows both authors and both names
184 my $res = darcs qw( changes );
185 like($res, qr(author1));
186 like($res, qr(author2));
187 like($res, qr(author3));
188 like($res, qr(first patch));
189 like($res, qr("second" \\ patch));
190 like($res, qr(second));
193 { # exact
194 my $res = darcs qw( changes --match='exact second' );
195 unlike ($res, qr(author1), 'does not find unrelated patch');
196 unlike ($res, qr(author2), 'does not find similar patch');
197 like ($res, qr(author3), 'finds the patch');
200 { # name
201 my $res = darcs qw( changes --match='name second' );
202 unlike ($res, qr(author1), 'does not find unrelated patch');
203 like ($res, qr(author2), 'finds one of the patches');
204 like ($res, qr(author3), 'finds the other patch');
207 { # author
208 my $res = darcs qw( changes --match='author author1');
209 like ($res, qr(author1));
210 unlike ($res, qr(author2));
211 unlike ($res, qr(author3));
214 { #hash
215 my $xml = darcs(qw(changes --xml-output --match='exact "\"second\" \ patch"'));
216 if ($xml =~ /hash='(.*?)'/) {
217 my $res = darcs "changes --match='hash $1'";
218 unlike($res, qr(author1));
219 like ($res, qr(author2));
220 unlike($res, qr(author3));
221 } else {
222 ok ( 0 );
226 # -------------------------------------------------------------------
227 # matching on combinations
229 # uses the setup from the atomic patches
230 # -------------------------------------------------------------------
232 { # or
233 my $res = darcs(qw(changes --match='author author1 || author author2'));
234 like ($res, qr(author1));
235 like ($res, qr(author2));
236 unlike($res, qr(author3));
239 { # and
240 my $res = darcs(qw(changes --match='name second && author author2'));
241 unlike($res, qr(author1));
242 like ($res, qr(author2));
243 unlike($res, qr(author3));
246 { # not
247 my $res = darcs(qw(changes --match='not name second'));
248 like ($res, qr(author1));
249 unlike($res, qr(author2));
250 unlike($res, qr(author3));
253 { # grouping
254 my $res = darcs(qw(changes --match='(not name second) || author author3'));
255 like ($res, qr(author1));
256 unlike($res, qr(author2));
257 like ($res, qr(author3));
262 ######
264 # just be happy if it doesn't complain about the date being fancy
265 sub parse_date {
266 my $d = shift;
267 unlike( darcs("changes --match='date $d'"), qr(fancy), "date format $d is recognized" );
270 sub match_date {
271 my $d = shift;
272 like( darcs(qq(changes --match 'date "$d"')), qr/tester/, "date format $d finds a match");
275 sub nomatch_date {
276 my $d = shift;
277 my $out = darcs(qq(changes --match 'date "$d"'));
278 unlike($out, qr/tester/, "date format $d shouldn't find a match");
279 unlike($out, qr/fancy/, "date format $d should be recognized");
282 # Return the date found in the first entry of 'darcs changes';
283 sub get_first_date_from_changes {
284 my $out = darcs 'changes';
285 my ($date) = split " tester", $out;
286 # $date should now look like: Wed Jan 9 20:02:18 EST 2008
287 return $date;
292 =head2 deconstruct_date
294 my ($mon,$mon_num,$day_num,$year,$hhmmss,$tz) = deconstruct_date( $raw_date );
296 # mon => 'Jan'
297 # mon_num => 01
298 # day_num => 09
299 # year => 2008
300 # hhmmss => 01:03:04
302 Given a date as get_first_date_from_changes() returns, deconstruct it into base components.
304 example input: Wed Jan 9 20:02:18 EST 2008
308 =cut
310 sub deconstruct_date {
311 my $raw_date = shift;
313 my %month_to_num = (
314 'Jan' => '01',
315 'Feb' => '02',
316 'Mar' => '03',
317 'Apr' => '04',
318 'May' => '05',
319 'Jun' => '06',
320 'Jul' => '07',
321 'Aug' => '08',
322 'Sep' => '09',
323 'Oct' => '10',
324 'Nov' => '11',
325 'Dec' => '12',
328 # example input: Wed Jan 9 20:02:18 EST 2008
329 my ($mon,$day_num,$hhmmss,$tz,$year) = ($raw_date =~ m{
331 \w+ # dow
333 (\w+) # mon
335 (\d+) # day
337 (\d\d:\d\d:\d\d) # HH::MM:SS
339 (\w+) # Time zone abbreviation
341 (\d\d\d\d) # year
343 }msx);
344 $day_num = '0'.$day_num if (length $day_num == 1);
345 my $mon_num = $month_to_num{$mon};
347 return ($mon,$mon_num,$day_num,$year,$hhmmss,$tz);
350 sub reset_repo {
351 init_tmp_repo();
352 touch 'bar';
353 darcs 'add bar';
356 sub create_entry_now {
357 open BAR,">>bar";
358 print BAR "today\n";
359 close BAR;
360 darcs('record -a -m "" bar');
363 sub create_entry {
364 my ($date) = shift;
365 open BAR,">>bar";
366 print BAR "$date\n";
367 close BAR;
368 echo_to_darcs('record -m "" --pipe bar',"$date","tester","a","","");