1 #!/usr/bin/perl -w 2 # 3 # Copyright (C) 2011 Research In Motion Limited. All rights reserved. 4 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek (at] gmail.com) 5 # Copyright (C) 2012 Daniel Bates (dbates (at] intudata.com) 6 # 7 # Redistribution and use in source and binary forms, with or without 8 # modification, are permitted provided that the following conditions are 9 # met: 10 # 11 # * Redistributions of source code must retain the above copyright 12 # notice, this list of conditions and the following disclaimer. 13 # * Redistributions in binary form must reproduce the above 14 # copyright notice, this list of conditions and the following disclaimer 15 # in the documentation and/or other materials provided with the 16 # distribution. 17 # * Neither the name of Apple Computer, Inc. ("Apple") nor the names of 18 # its contributors may be used to endorse or promote products derived 19 # from this software without specific prior written permission. 20 # 21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33 # Unit tests of VCSUtils::parseChunkRange(). 34 35 use strict; 36 use warnings; 37 38 use Test::More; 39 use VCSUtils; 40 41 my @testCaseHashRefs = ( 42 ### 43 # Invalid and malformed chunk range 44 ## 45 # FIXME: We should make this set of tests more comprehensive. 46 { # New test 47 testName => "[invalid] Empty string", 48 inputText => "", 49 expectedReturn => [] 50 }, 51 { # New test 52 testName => "[invalid] Bogus chunk range", 53 inputText => "@@ this is not valid @@", 54 expectedReturn => [] 55 }, 56 { # New test 57 testName => "[invalid] Chunk range missing -/+ prefix", 58 inputText => "@@ 0,0 1,4 @@", 59 expectedReturn => [] 60 }, 61 { # New test 62 testName => "[invalid] Chunk range missing commas", 63 inputText => "@@ -0 0 +1 4 @@", 64 expectedReturn => [] 65 }, 66 { # New test 67 testName => "[invalid] Chunk range with swapped old and rew ranges", 68 inputText => "@@ +0,0 -1,4 @@", 69 expectedReturn => [] 70 }, 71 { # New test 72 testName => "[invalid] Chunk range with leading junk", 73 inputText => "leading junk @@ -0,0 +1,4 @@", 74 expectedReturn => [] 75 }, 76 ### 77 # Simple test cases 78 ## 79 { # New test 80 testName => "Line count is 0", 81 inputText => "@@ -0,0 +1,4 @@", 82 expectedReturn => [ 83 { 84 startingLine => 0, 85 lineCount => 0, 86 newStartingLine => 1, 87 newLineCount => 4, 88 } 89 ] 90 }, 91 { # New test 92 testName => "Line count is 1", 93 inputText => "@@ -1 +1,4 @@", 94 expectedReturn => [ 95 { 96 startingLine => 1, 97 lineCount => 1, 98 newStartingLine => 1, 99 newLineCount => 4, 100 } 101 ] 102 }, 103 { # New test 104 testName => "Both original and new line count is 1", 105 inputText => "@@ -1 +1 @@", 106 expectedReturn => [ 107 { 108 startingLine => 1, 109 lineCount => 1, 110 newStartingLine => 1, 111 newLineCount => 1, 112 } 113 ] 114 }, 115 { # New test 116 testName => "Line count and new line count > 1", 117 inputText => "@@ -1,2 +1,4 @@", 118 expectedReturn => [ 119 { 120 startingLine => 1, 121 lineCount => 2, 122 newStartingLine => 1, 123 newLineCount => 4, 124 } 125 ] 126 }, 127 { # New test 128 testName => "New line count is 0", 129 inputText => "@@ -1,4 +0,0 @@", 130 expectedReturn => [ 131 { 132 startingLine => 1, 133 lineCount => 4, 134 newStartingLine => 0, 135 newLineCount => 0, 136 } 137 ] 138 }, 139 { # New test 140 testName => "New line count is 1", 141 inputText => "@@ -1,4 +1 @@", 142 expectedReturn => [ 143 { 144 startingLine => 1, 145 lineCount => 4, 146 newStartingLine => 1, 147 newLineCount => 1, 148 } 149 ] 150 }, 151 ### 152 # Simple SVN 1.7 property diff chunk range tests 153 ## 154 { # New test 155 testName => "Line count is 0", 156 inputText => "## -0,0 +1,4 ##", 157 chunkSentinel => "##", 158 expectedReturn => [ 159 { 160 startingLine => 0, 161 lineCount => 0, 162 newStartingLine => 1, 163 newLineCount => 4, 164 } 165 ] 166 }, 167 { # New test 168 testName => "New line count is 1", 169 inputText => "## -0,0 +1 ##", 170 chunkSentinel => "##", 171 expectedReturn => [ 172 { 173 startingLine => 0, 174 lineCount => 0, 175 newStartingLine => 1, 176 newLineCount => 1, 177 } 178 ] 179 }, 180 ### 181 # Chunk range followed by ending junk 182 ## 183 { # New test 184 testName => "Line count is 0 and chunk range has ending junk", 185 inputText => "@@ -0,0 +1,4 @@ foo()", 186 expectedReturn => [ 187 { 188 startingLine => 0, 189 lineCount => 0, 190 newStartingLine => 1, 191 newLineCount => 4, 192 } 193 ] 194 }, 195 { # New test 196 testName => "Line count is 1 and chunk range has ending junk", 197 inputText => "@@ -1 +1,4 @@ foo()", 198 expectedReturn => [ 199 { 200 startingLine => 1, 201 lineCount => 1, 202 newStartingLine => 1, 203 newLineCount => 4, 204 } 205 ] 206 }, 207 { # New test 208 testName => "Both original and new line count is 1 and chunk range has ending junk", 209 inputText => "@@ -1 +1 @@ foo()", 210 expectedReturn => [ 211 { 212 startingLine => 1, 213 lineCount => 1, 214 newStartingLine => 1, 215 newLineCount => 1, 216 } 217 ] 218 }, 219 { # New test 220 testName => "Line count and new line count > 1 and chunk range has ending junk", 221 inputText => "@@ -1,2 +1,4 @@ foo()", 222 expectedReturn => [ 223 { 224 startingLine => 1, 225 lineCount => 2, 226 newStartingLine => 1, 227 newLineCount => 4, 228 } 229 ] 230 }, 231 { # New test 232 testName => "New line count is 0 and chunk range has ending junk", 233 inputText => "@@ -1,4 +0,0 @@ foo()", 234 expectedReturn => [ 235 { 236 startingLine => 1, 237 lineCount => 4, 238 newStartingLine => 0, 239 newLineCount => 0, 240 } 241 ] 242 }, 243 { # New test 244 testName => "New line count is 1 and chunk range has ending junk", 245 inputText => "@@ -1,4 +1 @@ foo()", 246 expectedReturn => [ 247 { 248 startingLine => 1, 249 lineCount => 4, 250 newStartingLine => 1, 251 newLineCount => 1, 252 } 253 ] 254 }, 255 ); 256 257 my $testCasesCount = @testCaseHashRefs; 258 plan(tests => $testCasesCount); 259 260 foreach my $testCase (@testCaseHashRefs) { 261 my $testNameStart = "parseChunkRange(): $testCase->{testName}: comparing"; 262 263 my @got = VCSUtils::parseChunkRange($testCase->{inputText}, $testCase->{chunkSentinel}); 264 my $expectedReturn = $testCase->{expectedReturn}; 265 266 is_deeply(\@got, $expectedReturn, "$testNameStart return value."); 267 } 268