1#!/usr/bin/perl
2#
3# Copyright (C) 2009, 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
4# Copyright (C) Research In Motion 2010. All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10#     * Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12#     * Redistributions in binary form must reproduce the above
13# copyright notice, this list of conditions and the following disclaimer
14# in the documentation and/or other materials provided with the
15# distribution.
16#     * Neither the name of Google Inc. nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32# Unit tests of VCSUtils::fixChangeLogPatch().
33
34use strict;
35use warnings;
36
37use Test::More;
38use VCSUtils;
39
40# The source ChangeLog for these tests is the following:
41#
42# 2009-12-22  Alice  <alice@email.address>
43#
44#         Reviewed by Ray.
45#
46#         Changed some code on 2009-12-22.
47#
48#         * File:
49#         * File2:
50#
51# 2009-12-21  Alice  <alice@email.address>
52#
53#         Reviewed by Ray.
54#
55#         Changed some code on 2009-12-21.
56#
57#         * File:
58#         * File2:
59
60my @testCaseHashRefs = (
61{ # New test
62    diffName => "fixChangeLogPatch: [no change] In-place change.",
63    inputText => <<'END',
64--- ChangeLog
65+++ ChangeLog
66@@ -1,5 +1,5 @@
67 2010-12-22  Bob  <bob@email.address>
68
69-        Reviewed by Sue.
70+        Reviewed by Ray.
71
72         Changed some code on 2010-12-22.
73END
74    expectedReturn => {
75    patch => <<'END',
76--- ChangeLog
77+++ ChangeLog
78@@ -1,5 +1,5 @@
79 2010-12-22  Bob  <bob@email.address>
80
81-        Reviewed by Sue.
82+        Reviewed by Ray.
83
84         Changed some code on 2010-12-22.
85END
86    }
87},
88{ # New test
89    diffName => "fixChangeLogPatch: [no change] Remove first entry.",
90    inputText => <<'END',
91--- ChangeLog
92+++ ChangeLog
93@@ -1,11 +1,3 @@
94-2010-12-22  Bob  <bob@email.address>
95-
96-        Reviewed by Ray.
97-
98-        Changed some code on 2010-12-22.
99-
100-        * File:
101-
102 2010-12-22  Alice  <alice@email.address>
103
104         Reviewed by Ray.
105END
106    expectedReturn => {
107    patch => <<'END',
108--- ChangeLog
109+++ ChangeLog
110@@ -1,11 +1,3 @@
111-2010-12-22  Bob  <bob@email.address>
112-
113-        Reviewed by Ray.
114-
115-        Changed some code on 2010-12-22.
116-
117-        * File:
118-
119 2010-12-22  Alice  <alice@email.address>
120
121         Reviewed by Ray.
122END
123    }
124},
125{ # New test
126    diffName => "fixChangeLogPatch: [no change] Remove entry in the middle.",
127    inputText => <<'END',
128--- ChangeLog
129+++ ChangeLog
130@@@ -7,10 +7,6 @@
131
132         * File:
133
134-2010-12-22  Bob  <bob@email.address>
135-
136-        Changed some code on 2010-12-22.
137-
138 2010-12-22  Alice  <alice@email.address>
139
140         Reviewed by Ray.
141END
142    expectedReturn => {
143    patch => <<'END',
144--- ChangeLog
145+++ ChangeLog
146@@@ -7,10 +7,6 @@
147
148         * File:
149
150-2010-12-22  Bob  <bob@email.address>
151-
152-        Changed some code on 2010-12-22.
153-
154 2010-12-22  Alice  <alice@email.address>
155
156         Reviewed by Ray.
157END
158    }
159},
160{ # New test
161    diffName => "fixChangeLogPatch: [no change] Far apart changes (i.e. more than one chunk).",
162    inputText => <<'END',
163--- ChangeLog
164+++ ChangeLog
165@@ -7,7 +7,7 @@
166
167         * File:
168
169-2010-12-22  Bob  <bob@email.address>
170+2010-12-22  Bobby <bob@email.address>
171
172         Changed some code on 2010-12-22.
173
174@@ -21,7 +21,7 @@
175
176         * File2:
177
178-2010-12-21  Bob  <bob@email.address>
179+2010-12-21  Bobby <bob@email.address>
180
181         Changed some code on 2010-12-21.
182END
183    expectedReturn => {
184    patch => <<'END',
185--- ChangeLog
186+++ ChangeLog
187@@ -7,7 +7,7 @@
188
189         * File:
190
191-2010-12-22  Bob  <bob@email.address>
192+2010-12-22  Bobby <bob@email.address>
193
194         Changed some code on 2010-12-22.
195
196@@ -21,7 +21,7 @@
197
198         * File2:
199
200-2010-12-21  Bob  <bob@email.address>
201+2010-12-21  Bobby <bob@email.address>
202
203         Changed some code on 2010-12-21.
204END
205    }
206},
207{ # New test
208    diffName => "fixChangeLogPatch: [no change] First line is new line.",
209    inputText => <<'END',
210--- ChangeLog
211+++ ChangeLog
212@@ -1,3 +1,11 @@
213+2009-12-22  Bob  <bob@email.address>
214+
215+        Reviewed by Ray.
216+
217+        Changed some more code on 2009-12-22.
218+
219+        * File:
220+
221 2009-12-22  Alice  <alice@email.address>
222
223         Reviewed by Ray.
224END
225    expectedReturn => {
226    patch => <<'END',
227--- ChangeLog
228+++ ChangeLog
229@@ -1,3 +1,11 @@
230+2009-12-22  Bob  <bob@email.address>
231+
232+        Reviewed by Ray.
233+
234+        Changed some more code on 2009-12-22.
235+
236+        * File:
237+
238 2009-12-22  Alice  <alice@email.address>
239
240         Reviewed by Ray.
241END
242    }
243},
244{ # New test
245    diffName => "fixChangeLogPatch: [no change] No date string.",
246    inputText => <<'END',
247--- ChangeLog
248+++ ChangeLog
249@@ -6,6 +6,7 @@
250
251         * File:
252         * File2:
253+        * File3:
254
255 2009-12-21  Alice  <alice@email.address>
256
257END
258    expectedReturn => {
259    patch => <<'END',
260--- ChangeLog
261+++ ChangeLog
262@@ -6,6 +6,7 @@
263
264         * File:
265         * File2:
266+        * File3:
267
268 2009-12-21  Alice  <alice@email.address>
269
270END
271    }
272},
273{ # New test
274    diffName => "fixChangeLogPatch: New entry inserted in middle.",
275    inputText => <<'END',
276--- ChangeLog
277+++ ChangeLog
278@@ -11,6 +11,14 @@
279
280         Reviewed by Ray.
281
282+        Changed some more code on 2009-12-21.
283+
284+        * File:
285+
286+2009-12-21  Alice  <alice@email.address>
287+
288+        Reviewed by Ray.
289+
290         Changed some code on 2009-12-21.
291
292         * File:
293END
294    expectedReturn => {
295    patch => <<'END',
296--- ChangeLog
297+++ ChangeLog
298@@ -1,3 +1,11 @@
299+2009-12-21  Alice  <alice@email.address>
300+
301+        Reviewed by Ray.
302+
303+        Changed some more code on 2009-12-21.
304+
305+        * File:
306+
307 2009-12-21  Alice  <alice@email.address>
308
309         Reviewed by Ray.
310END
311    }
312},
313{ # New test
314    diffName => "fixChangeLogPatch: New entry inserted earlier in the file, but after an entry with the same author and date.",
315    inputText => <<'END',
316--- ChangeLog
317+++ ChangeLog
318@@ -70,6 +70,14 @@
319
320 2009-12-22  Alice  <alice@email.address>
321
322+        Reviewed by Sue.
323+
324+        Changed some more code on 2009-12-22.
325+
326+        * File:
327+
328+2009-12-22  Alice  <alice@email.address>
329+
330         Reviewed by Ray.
331
332         Changed some code on 2009-12-22.
333END
334    expectedReturn => {
335    patch => <<'END',
336--- ChangeLog
337+++ ChangeLog
338@@ -1,3 +1,11 @@
339+2009-12-22  Alice  <alice@email.address>
340+
341+        Reviewed by Sue.
342+
343+        Changed some more code on 2009-12-22.
344+
345+        * File:
346+
347 2009-12-22  Alice  <alice@email.address>
348
349         Reviewed by Ray.
350END
351    }
352},
353{ # New test
354    diffName => "fixChangeLogPatch: Leading context includes first line.",
355    inputText => <<'END',
356--- ChangeLog
357+++ ChangeLog
358@@ -1,5 +1,13 @@
359 2009-12-22  Alice  <alice@email.address>
360
361+        Reviewed by Sue.
362+
363+        Changed some more code on 2009-12-22.
364+
365+        * File:
366+
367+2009-12-22  Alice  <alice@email.address>
368+
369         Reviewed by Ray.
370
371         Changed some code on 2009-12-22.
372END
373    expectedReturn => {
374    patch => <<'END',
375--- ChangeLog
376+++ ChangeLog
377@@ -1,3 +1,11 @@
378+2009-12-22  Alice  <alice@email.address>
379+
380+        Reviewed by Sue.
381+
382+        Changed some more code on 2009-12-22.
383+
384+        * File:
385+
386 2009-12-22  Alice  <alice@email.address>
387
388         Reviewed by Ray.
389END
390    }
391},
392{ # New test
393    diffName => "fixChangeLogPatch: Leading context does not include first line.",
394    inputText => <<'END',
395@@ -2,6 +2,14 @@
396
397         Reviewed by Ray.
398
399+        Changed some more code on 2009-12-22.
400+
401+        * File:
402+
403+2009-12-22  Alice  <alice@email.address>
404+
405+        Reviewed by Ray.
406+
407         Changed some code on 2009-12-22.
408
409         * File:
410END
411    expectedReturn => {
412    patch => <<'END',
413@@ -1,3 +1,11 @@
414+2009-12-22  Alice  <alice@email.address>
415+
416+        Reviewed by Ray.
417+
418+        Changed some more code on 2009-12-22.
419+
420+        * File:
421+
422 2009-12-22  Alice  <alice@email.address>
423
424         Reviewed by Ray.
425END
426    }
427},
428{ # New test
429    diffName => "fixChangeLogPatch: Non-consecutive line additions.",
430
431# This can occur, for example, if the new ChangeLog entry includes
432# trailing white space in the first blank line but not the second.
433# A diff command can then match the second blank line of the new
434# ChangeLog entry with the first blank line of the old.
435# The svn diff command with the default --diff-cmd has done this.
436    inputText => <<'END',
437@@ -1,5 +1,11 @@
438 2009-12-22  Alice  <alice@email.address>
439+ <pretend-whitespace>
440+        Reviewed by Ray.
441
442+        Changed some more code on 2009-12-22.
443+
444+2009-12-22  Alice  <alice@email.address>
445+
446         Reviewed by Ray.
447
448         Changed some code on 2009-12-22.
449END
450    expectedReturn => {
451    patch => <<'END',
452@@ -1,3 +1,9 @@
453+2009-12-22  Alice  <alice@email.address>
454+ <pretend-whitespace>
455+        Reviewed by Ray.
456+
457+        Changed some more code on 2009-12-22.
458+
459 2009-12-22  Alice  <alice@email.address>
460
461         Reviewed by Ray.
462END
463    }
464},
465{ # New test
466    diffName => "fixChangeLogPatch: Additional edits after new entry.",
467    inputText => <<'END',
468@@ -2,10 +2,17 @@
469
470         Reviewed by Ray.
471
472+        Changed some more code on 2009-12-22.
473+
474+        * File:
475+
476+2009-12-22  Alice  <alice@email.address>
477+
478+        Reviewed by Ray.
479+
480         Changed some code on 2009-12-22.
481
482         * File:
483-        * File2:
484
485 2009-12-21  Alice  <alice@email.address>
486
487END
488    expectedReturn => {
489    patch => <<'END',
490@@ -1,11 +1,18 @@
491+2009-12-22  Alice  <alice@email.address>
492+
493+        Reviewed by Ray.
494+
495+        Changed some more code on 2009-12-22.
496+
497+        * File:
498+
499 2009-12-22  Alice  <alice@email.address>
500
501         Reviewed by Ray.
502
503         Changed some code on 2009-12-22.
504
505         * File:
506-        * File2:
507
508 2009-12-21  Alice  <alice@email.address>
509
510END
511    }
512},
513);
514
515my $testCasesCount = @testCaseHashRefs;
516plan(tests => $testCasesCount); # Total number of assertions.
517
518foreach my $testCase (@testCaseHashRefs) {
519    my $testNameStart = "fixChangeLogPatch(): $testCase->{diffName}: comparing";
520
521    my $got = VCSUtils::fixChangeLogPatch($testCase->{inputText});
522    my $expectedReturn = $testCase->{expectedReturn};
523
524    is_deeply($got, $expectedReturn, "$testNameStart return value.");
525}
526