1#!/usr/bin/perl -w 2# 3# Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com) 4# Copyright (C) 2012 Daniel Bates (dbates@intudata.com) 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 Apple Computer, Inc. ("Apple") nor the names of 17# its contributors may be used to endorse or promote products derived 18# from 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 parseSvnDiffHeader(). 33 34use strict; 35use warnings; 36 37use Test::More; 38use VCSUtils; 39 40# The array of test cases. 41my @testCaseHashRefs = ( 42{ 43 # New test 44 diffName => "simple diff", 45 inputText => <<'END', 46Index: WebKitTools/Scripts/VCSUtils.pm 47=================================================================== 48--- WebKitTools/Scripts/VCSUtils.pm (revision 53004) 49+++ WebKitTools/Scripts/VCSUtils.pm (working copy) 50@@ -32,6 +32,7 @@ use strict; 51 use warnings; 52END 53 expectedReturn => [ 54{ 55 svnConvertedText => <<'END', 56Index: WebKitTools/Scripts/VCSUtils.pm 57=================================================================== 58--- WebKitTools/Scripts/VCSUtils.pm (revision 53004) 59+++ WebKitTools/Scripts/VCSUtils.pm (working copy) 60END 61 indexPath => "WebKitTools/Scripts/VCSUtils.pm", 62 sourceRevision => "53004", 63}, 64"@@ -32,6 +32,7 @@ use strict;\n"], 65 expectedNextLine => " use warnings;\n", 66}, 67{ 68 # New test 69 diffName => "new file", 70 inputText => <<'END', 71Index: WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl 72=================================================================== 73--- WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl (revision 0) 74+++ WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl (revision 0) 75@@ -0,0 +1,262 @@ 76+#!/usr/bin/perl -w 77END 78 expectedReturn => [ 79{ 80 svnConvertedText => <<'END', 81Index: WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl 82=================================================================== 83--- WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl (revision 0) 84+++ WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl (revision 0) 85END 86 indexPath => "WebKitTools/Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl", 87 isNew => 1, 88}, 89"@@ -0,0 +1,262 @@\n"], 90 expectedNextLine => "+#!/usr/bin/perl -w\n", 91}, 92{ 93 # New test 94 diffName => "new file with spaces in its name", 95 inputText => <<'END', 96Index: WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme 97=================================================================== 98--- WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme (revision 0) 99+++ WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme (revision 0) 100@@ -0,0 +1,8 @@ 101+<?xml version="1.0" encoding="UTF-8"?> 102END 103 expectedReturn => [ 104{ 105 svnConvertedText => <<'END', 106Index: WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme 107=================================================================== 108--- WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme (revision 0) 109+++ WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme (revision 0) 110END 111 indexPath => "WebKit.xcworkspace/xcshareddata/xcschemes/All Source (target WebProcess).xcscheme", 112 isNew => 1, 113}, 114"@@ -0,0 +1,8 @@\n"], 115 expectedNextLine => "+<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", 116}, 117{ 118 # New test 119 diffName => "copied file", 120 inputText => <<'END', 121Index: index_path.py 122=================================================================== 123--- index_path.py (revision 53048) (from copied_from_path.py:53048) 124+++ index_path.py (working copy) 125@@ -0,0 +1,7 @@ 126+# Python file... 127END 128 expectedReturn => [ 129{ 130 svnConvertedText => <<'END', 131Index: index_path.py 132=================================================================== 133--- index_path.py (revision 53048) (from copied_from_path.py:53048) 134+++ index_path.py (working copy) 135END 136 copiedFromPath => "copied_from_path.py", 137 indexPath => "index_path.py", 138 sourceRevision => 53048, 139}, 140"@@ -0,0 +1,7 @@\n"], 141 expectedNextLine => "+# Python file...\n", 142}, 143{ 144 # New test 145 diffName => "contains \\r\\n lines", 146 inputText => <<END, # No single quotes to allow interpolation of "\r" 147Index: index_path.py\r 148===================================================================\r 149--- index_path.py (revision 53048)\r 150+++ index_path.py (working copy)\r 151@@ -0,0 +1,7 @@\r 152+# Python file...\r 153END 154 expectedReturn => [ 155{ 156 svnConvertedText => <<END, # No single quotes to allow interpolation of "\r" 157Index: index_path.py\r 158===================================================================\r 159--- index_path.py (revision 53048)\r 160+++ index_path.py (working copy)\r 161END 162 indexPath => "index_path.py", 163 sourceRevision => 53048, 164}, 165"@@ -0,0 +1,7 @@\r\n"], 166 expectedNextLine => "+# Python file...\r\n", 167}, 168{ 169 # New test 170 diffName => "contains path corrections", 171 inputText => <<'END', 172Index: index_path.py 173=================================================================== 174--- bad_path (revision 53048) (from copied_from_path.py:53048) 175+++ bad_path (working copy) 176@@ -0,0 +1,7 @@ 177+# Python file... 178END 179 expectedReturn => [ 180{ 181 svnConvertedText => <<'END', 182Index: index_path.py 183=================================================================== 184--- index_path.py (revision 53048) (from copied_from_path.py:53048) 185+++ index_path.py (working copy) 186END 187 copiedFromPath => "copied_from_path.py", 188 indexPath => "index_path.py", 189 sourceRevision => 53048, 190}, 191"@@ -0,0 +1,7 @@\n"], 192 expectedNextLine => "+# Python file...\n", 193}, 194#### 195# Binary test cases 196## 197{ 198 # New test 199 diffName => "binary file", 200 inputText => <<'END', 201Index: test_file.swf 202=================================================================== 203Cannot display: file marked as a binary type. 204svn:mime-type = application/octet-stream 205 206Property changes on: test_file.swf 207___________________________________________________________________ 208Name: svn:mime-type 209 + application/octet-stream 210 211 212Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA== 213END 214 expectedReturn => [ 215{ 216 svnConvertedText => <<'END', 217Index: test_file.swf 218=================================================================== 219Cannot display: file marked as a binary type. 220svn:mime-type = application/octet-stream 221 222END 223 indexPath => "test_file.swf", 224 isBinary => 1, 225}, 226"Property changes on: test_file.swf\n"], 227 expectedNextLine => "___________________________________________________________________\n", 228}, 229{ 230 # New test 231 diffName => "binary file using SVN 1.7 syntax", 232 inputText => <<'END', 233Index: test_file.swf 234=================================================================== 235Cannot display: file marked as a binary type. 236svn:mime-type = application/octet-stream 237Index: test_file.swf 238=================================================================== 239--- test_file.swf 240+++ test_file.swf 241 242Property changes on: test_file.swf 243___________________________________________________________________ 244Added: svn:mime-type 245## -0,0 +1 ## 246+application/octet-stream 247\ No newline at end of property 248 249Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA== 250END 251 expectedReturn => [ 252{ 253 svnConvertedText => <<'END', 254Index: test_file.swf 255=================================================================== 256Cannot display: file marked as a binary type. 257svn:mime-type = application/octet-stream 258Index: test_file.swf 259=================================================================== 260--- test_file.swf 261+++ test_file.swf 262END 263 indexPath => "test_file.swf", 264 isBinary => 1, 265}, 266"\n"], 267 expectedNextLine => "Property changes on: test_file.swf\n", 268}, 269); 270 271my $testCasesCount = @testCaseHashRefs; 272plan(tests => 2 * $testCasesCount); # Total number of assertions. 273 274foreach my $testCase (@testCaseHashRefs) { 275 my $testNameStart = "parseSvnDiffHeader(): $testCase->{diffName}: comparing"; 276 277 my $fileHandle; 278 open($fileHandle, "<", \$testCase->{inputText}); 279 my $line = <$fileHandle>; 280 281 my @got = VCSUtils::parseSvnDiffHeader($fileHandle, $line); 282 my $expectedReturn = $testCase->{expectedReturn}; 283 284 is_deeply(\@got, $expectedReturn, "$testNameStart return value."); 285 286 my $gotNextLine = <$fileHandle>; 287 is($gotNextLine, $testCase->{expectedNextLine}, "$testNameStart next read line."); 288} 289