Home | History | Annotate | Download | only in VCSUtils_unittest
      1 #!/usr/bin/perl -w
      2 #
      3 # Copyright (C) Research in Motion Limited 2010. 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 parseSvnDiffProperties().
     34 
     35 use strict;
     36 use warnings;
     37 
     38 use Test::More;
     39 use VCSUtils;
     40 
     41 my @testCaseHashRefs = (
     42 ####
     43 # Simple test cases
     44 ##
     45 {
     46     # New test
     47     diffName => "simple: add svn:executable",
     48     inputText => <<'END',
     49 Property changes on: FileA
     50 ___________________________________________________________________
     51 Added: svn:executable
     52    + *
     53 END
     54     expectedReturn => [
     55 {
     56     propertyPath => "FileA",
     57     executableBitDelta => 1,
     58 },
     59 undef],
     60     expectedNextLine => undef,
     61 },
     62 {
     63     # New test
     64     diffName => "simple: add svn:mergeinfo",
     65     inputText => <<'END',
     66 Property changes on: Makefile
     67 ___________________________________________________________________
     68 Added: svn:mergeinfo
     69    Merged /trunk/Makefile:r33020
     70 END
     71     expectedReturn => [
     72 {
     73     propertyPath => "Makefile",
     74 },
     75 undef],
     76     expectedNextLine => undef,
     77 },
     78 {
     79     # New test
     80     diffName => "simple: delete svn:mergeinfo",
     81     inputText => <<'END',
     82 Property changes on: Makefile
     83 ___________________________________________________________________
     84 Deleted: svn:mergeinfo
     85    Reverse-merged /trunk/Makefile:r33020
     86 END
     87     expectedReturn => [
     88 {
     89     propertyPath => "Makefile",
     90 },
     91 undef],
     92     expectedNextLine => undef,
     93 },
     94 {
     95     # New test
     96     diffName => "simple: modified svn:mergeinfo",
     97     inputText => <<'END',
     98 Property changes on: Makefile
     99 ___________________________________________________________________
    100 Modified: svn:mergeinfo
    101    Reverse-merged /trunk/Makefile:r33020
    102    Merged /trunk/Makefile:r41697
    103 END
    104     expectedReturn => [
    105 {
    106     propertyPath => "Makefile",
    107 },
    108 undef],
    109     expectedNextLine => undef,
    110 },
    111 {
    112     # New test
    113     diffName => "simple: delete svn:executable",
    114     inputText => <<'END',
    115 Property changes on: FileA
    116 ___________________________________________________________________
    117 Deleted: svn:executable
    118    - *
    119 END
    120     expectedReturn => [
    121 {
    122     propertyPath => "FileA",
    123     executableBitDelta => -1,
    124 },
    125 undef],
    126     expectedNextLine => undef,
    127 },
    128 {
    129     # New test
    130     diffName => "simple: delete svn:executable using SVN 1.4 syntax",
    131     inputText => <<'END',
    132 Property changes on: FileA
    133 ___________________________________________________________________
    134 Name: svn:executable
    135    - *
    136 END
    137     expectedReturn => [
    138 {
    139     propertyPath => "FileA",
    140     executableBitDelta => -1,
    141 },
    142 undef],
    143     expectedNextLine => undef,
    144 },
    145 ####
    146 # Property value followed by empty line and start of next diff
    147 ##
    148 {
    149     # New test
    150     diffName => "add svn:executable, followed by empty line and start of next diff",
    151     inputText => <<'END',
    152 Property changes on: FileA
    153 ___________________________________________________________________
    154 Added: svn:executable
    155    + *
    156 
    157 Index: Makefile.shared
    158 END
    159     expectedReturn => [
    160 {
    161     propertyPath => "FileA",
    162     executableBitDelta => 1,
    163 },
    164 "\n"],
    165     expectedNextLine => "Index: Makefile.shared\n",
    166 },
    167 {
    168     # New test
    169     diffName => "add svn:executable, followed by empty line and start of next property diff",
    170     inputText => <<'END',
    171 Property changes on: FileA
    172 ___________________________________________________________________
    173 Added: svn:executable
    174    + *
    175 
    176 Property changes on: Makefile.shared
    177 END
    178     expectedReturn => [
    179 {
    180     propertyPath => "FileA",
    181     executableBitDelta => 1,
    182 },
    183 "\n"],
    184     expectedNextLine => "Property changes on: Makefile.shared\n",
    185 },
    186 ####
    187 # Property value followed by empty line and start of the binary contents
    188 ##
    189 {
    190     # New test
    191     diffName => "add svn:executable, followed by empty line and start of binary contents",
    192     inputText => <<'END',
    193 Property changes on: FileA
    194 ___________________________________________________________________
    195 Added: svn:executable
    196    + *
    197 
    198 Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA==
    199 END
    200     expectedReturn => [
    201 {
    202     propertyPath => "FileA",
    203     executableBitDelta => 1,
    204 },
    205 "\n"],
    206     expectedNextLine => "Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA==\n",
    207 },
    208 {
    209     # New test
    210     diffName => "custom property followed by svn:executable, empty line and start of binary contents",
    211     inputText => <<'END',
    212 Property changes on: FileA
    213 ___________________________________________________________________
    214 Added: documentation
    215    + This is an example sentence.
    216 Added: svn:executable
    217    + *
    218 
    219 Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA==
    220 END
    221     expectedReturn => [
    222 {
    223     propertyPath => "FileA",
    224     executableBitDelta => 1,
    225 },
    226 "\n"],
    227     expectedNextLine => "Q1dTBx0AAAB42itg4GlgYJjGwMDDyODMxMDw34GBgQEAJPQDJA==\n",
    228 },
    229 ####
    230 # Successive properties
    231 ##
    232 {
    233     # New test
    234     diffName => "svn:executable followed by custom property",
    235     inputText => <<'END',
    236 Property changes on: FileA
    237 ___________________________________________________________________
    238 Added: svn:executable
    239    + *
    240 Added: documentation
    241    + This is an example sentence.
    242 END
    243     expectedReturn => [
    244 {
    245     propertyPath => "FileA",
    246     executableBitDelta => 1,
    247 },
    248 undef],
    249     expectedNextLine => undef,
    250 },
    251 {
    252     # New test
    253     diffName => "svn:executable followed by custom property using SVN 1.7 syntax",
    254     inputText => <<'END',
    255 Property changes on: FileA
    256 ___________________________________________________________________
    257 Added: svn:executable
    258 ## -0,0 +1 ##
    259 +*
    260 \ No newline at end of property
    261 Added: documentation
    262 ## -0,0 +1 ##
    263 +This is an example sentence.
    264 END
    265     expectedReturn => [
    266 {
    267     propertyPath => "FileA",
    268     executableBitDelta => 1,
    269 },
    270 undef],
    271     expectedNextLine => undef,
    272 },
    273 {
    274     # New test
    275     diffName => "svn:executable followed by custom property without newline using SVN 1.7 syntax",
    276     inputText => <<'END',
    277 Property changes on: FileA
    278 ___________________________________________________________________
    279 Added: svn:executable
    280 ## -0,0 +1 ##
    281 +*
    282 \ No newline at end of property
    283 Added: documentation
    284 ## -0,0 +1 ##
    285 +This is an example sentence.
    286 \ No newline at end of property
    287 END
    288     expectedReturn => [
    289 {
    290     propertyPath => "FileA",
    291     executableBitDelta => 1,
    292 },
    293 undef],
    294     expectedNextLine => undef,
    295 },
    296 {
    297     # New test
    298     diffName => "custom property followed by svn:executable",
    299     inputText => <<'END',
    300 Property changes on: FileA
    301 ___________________________________________________________________
    302 Added: documentation
    303    + This is an example sentence.
    304 Added: svn:executable
    305    + *
    306 END
    307     expectedReturn => [
    308 {
    309     propertyPath => "FileA",
    310     executableBitDelta => 1,
    311 },
    312 undef],
    313     expectedNextLine => undef,
    314 },
    315 ####
    316 # Successive properties followed by empty line and start of next diff
    317 ##
    318 {
    319     # New test
    320     diffName => "custom property followed by svn:executable, empty line and start of next property diff",
    321     inputText => <<'END',
    322 Property changes on: FileA
    323 ___________________________________________________________________
    324 Added: documentation
    325    + This is an example sentence.
    326 Added: svn:executable
    327    + *
    328 
    329 Property changes on: Makefile.shared
    330 END
    331     expectedReturn => [
    332 {
    333     propertyPath => "FileA",
    334     executableBitDelta => 1,
    335 },
    336 "\n"],
    337     expectedNextLine => "Property changes on: Makefile.shared\n",
    338 },
    339 {
    340     # New test
    341     diffName => "custom property followed by svn:executable, empty line and start of next index diff",
    342     inputText => <<'END',
    343 Property changes on: FileA
    344 ___________________________________________________________________
    345 Added: documentation
    346    + This is an example sentence.
    347 Added: svn:executable
    348    + *
    349 
    350 Index: Makefile.shared
    351 END
    352     expectedReturn => [
    353 {
    354     propertyPath => "FileA",
    355     executableBitDelta => 1,
    356 },
    357 "\n"],
    358     expectedNextLine => "Index: Makefile.shared\n",
    359 },
    360 ####
    361 # Custom properties
    362 ##
    363 # FIXME: We do not support anything other than the svn:executable property.
    364 #        We should add support for handling other properties.
    365 {
    366     # New test
    367     diffName => "simple: custom property",
    368     inputText => <<'END',
    369 Property changes on: FileA
    370 ___________________________________________________________________
    371 Name: documentation
    372    + This is an example sentence.
    373 END
    374     expectedReturn => [
    375 {
    376     propertyPath => "FileA",
    377 },
    378 undef],
    379     expectedNextLine => undef,
    380 },
    381 {
    382     # New test
    383     diffName => "custom property followed by custom property",
    384     inputText => <<'END',
    385 Property changes on: FileA
    386 ___________________________________________________________________
    387 Added: copyright
    388    + Copyright (C) Research in Motion Limited 2010. All Rights Reserved.
    389 Added: documentation
    390    + This is an example sentence.
    391 END
    392     expectedReturn => [
    393 {
    394     propertyPath => "FileA",
    395 },
    396 undef],
    397     expectedNextLine => undef,
    398 },
    399 ####
    400 # Malformed property diffs
    401 ##
    402 # We shouldn't encounter such diffs in practice.
    403 {
    404     # New test
    405     diffName => "svn:executable followed by custom property and svn:executable",
    406     inputText => <<'END',
    407 Property changes on: FileA
    408 ___________________________________________________________________
    409 Added: svn:executable
    410    + *
    411 Added: documentation
    412    + This is an example sentence.
    413 Deleted: svn:executable
    414    - *
    415 END
    416     expectedReturn => [
    417 {
    418     propertyPath => "FileA",
    419     executableBitDelta => -1,
    420 },
    421 undef],
    422     expectedNextLine => undef,
    423 },
    424 );
    425 
    426 my $testCasesCount = @testCaseHashRefs;
    427 plan(tests => 2 * $testCasesCount); # Total number of assertions.
    428 
    429 foreach my $testCase (@testCaseHashRefs) {
    430     my $testNameStart = "parseSvnDiffProperties(): $testCase->{diffName}: comparing";
    431 
    432     my $fileHandle;
    433     open($fileHandle, "<", \$testCase->{inputText});
    434     my $line = <$fileHandle>;
    435 
    436     my @got = VCSUtils::parseSvnDiffProperties($fileHandle, $line);
    437     my $expectedReturn = $testCase->{expectedReturn};
    438 
    439     is_deeply(\@got, $expectedReturn, "$testNameStart return value.");
    440 
    441     my $gotNextLine = <$fileHandle>;
    442     is($gotNextLine, $testCase->{expectedNextLine},  "$testNameStart next read line.");
    443 }
    444