1#!/usr/bin/perl -w
2#
3# Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16# its contributors may be used to endorse or promote products derived
17# from this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31# Unit tests of parseSvnDiffHeader().
32
33use strict;
34use warnings;
35
36use Test::More;
37use VCSUtils;
38
39# The array of test cases.
40my @testCaseHashRefs = (
41{
42    # New test
43    diffName => "simple diff",
44    inputText => <<'END',
45Index: WebKitTools/Scripts/VCSUtils.pm
46===================================================================
47--- WebKitTools/Scripts/VCSUtils.pm	(revision 53004)
48+++ WebKitTools/Scripts/VCSUtils.pm	(working copy)
49@@ -32,6 +32,7 @@ use strict;
50 use warnings;
51END
52    expectedReturn => [
53{
54    svnConvertedText => <<'END',
55Index: WebKitTools/Scripts/VCSUtils.pm
56===================================================================
57--- WebKitTools/Scripts/VCSUtils.pm	(revision 53004)
58+++ WebKitTools/Scripts/VCSUtils.pm	(working copy)
59END
60    indexPath => "WebKitTools/Scripts/VCSUtils.pm",
61    sourceRevision => "53004",
62},
63"@@ -32,6 +32,7 @@ use strict;\n"],
64    expectedNextLine => " use warnings;\n",
65},
66{
67    # New test
68    diffName => "new file",
69    inputText => <<'END',
70Index: WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl
71===================================================================
72--- WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl	(revision 0)
73+++ WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl	(revision 0)
74@@ -0,0 +1,262 @@
75+#!/usr/bin/perl -w
76END
77    expectedReturn => [
78{
79    svnConvertedText => <<'END',
80Index: WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl
81===================================================================
82--- WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl	(revision 0)
83+++ WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl	(revision 0)
84END
85    indexPath => "WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl",
86    isNew => 1,
87},
88"@@ -0,0 +1,262 @@\n"],
89    expectedNextLine => "+#!/usr/bin/perl -w\n",
90},
91{
92    # New test
93    diffName => "copied file",
94    inputText => <<'END',
95Index: index_path.py
96===================================================================
97--- index_path.py	(revision 53048)	(from copied_from_path.py:53048)
98+++ index_path.py	(working copy)
99@@ -0,0 +1,7 @@
100+# Python file...
101END
102    expectedReturn => [
103{
104    svnConvertedText => <<'END',
105Index: index_path.py
106===================================================================
107--- index_path.py	(revision 53048)	(from copied_from_path.py:53048)
108+++ index_path.py	(working copy)
109END
110    copiedFromPath => "copied_from_path.py",
111    indexPath => "index_path.py",
112    sourceRevision => 53048,
113},
114"@@ -0,0 +1,7 @@\n"],
115    expectedNextLine => "+# Python file...\n",
116},
117{
118    # New test
119    diffName => "contains \\r\\n lines",
120    inputText => <<END, # No single quotes to allow interpolation of "\r"
121Index: index_path.py\r
122===================================================================\r
123--- index_path.py	(revision 53048)\r
124+++ index_path.py	(working copy)\r
125@@ -0,0 +1,7 @@\r
126+# Python file...\r
127END
128    expectedReturn => [
129{
130    svnConvertedText => <<END, # No single quotes to allow interpolation of "\r"
131Index: index_path.py\r
132===================================================================\r
133--- index_path.py	(revision 53048)\r
134+++ index_path.py	(working copy)\r
135END
136    indexPath => "index_path.py",
137    sourceRevision => 53048,
138},
139"@@ -0,0 +1,7 @@\r\n"],
140    expectedNextLine => "+# Python file...\r\n",
141},
142{
143    # New test
144    diffName => "contains path corrections",
145    inputText => <<'END',
146Index: index_path.py
147===================================================================
148--- bad_path	(revision 53048)	(from copied_from_path.py:53048)
149+++ bad_path	(working copy)
150@@ -0,0 +1,7 @@
151+# Python file...
152END
153    expectedReturn => [
154{
155    svnConvertedText => <<'END',
156Index: index_path.py
157===================================================================
158--- index_path.py	(revision 53048)	(from copied_from_path.py:53048)
159+++ index_path.py	(working copy)
160END
161    copiedFromPath => "copied_from_path.py",
162    indexPath => "index_path.py",
163    sourceRevision => 53048,
164},
165"@@ -0,0 +1,7 @@\n"],
166    expectedNextLine => "+# Python file...\n",
167},
168####
169#    Binary test cases
170##
171{
172    # New test
173    diffName => "binary file",
174    inputText => <<'END',
175Index: test_file.swf
176===================================================================
177Cannot display: file marked as a binary type.
178svn:mime-type = application/octet-stream
179
180Property changes on: test_file.swf
181___________________________________________________________________
182Name: svn:mime-type
183   + application/octet-stream
184
185
186Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA==
187END
188    expectedReturn => [
189{
190    svnConvertedText => <<'END',
191Index: test_file.swf
192===================================================================
193Cannot display: file marked as a binary type.
194END
195    indexPath => "test_file.swf",
196    isBinary => 1,
197},
198"svn:mime-type = application/octet-stream\n"],
199    expectedNextLine => "\n",
200},
201);
202
203my $testCasesCount = @testCaseHashRefs;
204plan(tests => 2 * $testCasesCount); # Total number of assertions.
205
206foreach my $testCase (@testCaseHashRefs) {
207    my $testNameStart = "parseSvnDiffHeader(): $testCase->{diffName}: comparing";
208
209    my $fileHandle;
210    open($fileHandle, "<", \$testCase->{inputText});
211    my $line = <$fileHandle>;
212
213    my @got = VCSUtils::parseSvnDiffHeader($fileHandle, $line);
214    my $expectedReturn = $testCase->{expectedReturn};
215
216    is_deeply(\@got, $expectedReturn, "$testNameStart return value.");
217
218    my $gotNextLine = <$fileHandle>;
219    is($gotNextLine, $testCase->{expectedNextLine},  "$testNameStart next read line.");
220}
221