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 parseDiffHeader().
32
33use strict;
34use warnings;
35
36use Test::More;
37use VCSUtils;
38
39# The unit tests for parseGitDiffHeader() and parseSvnDiffHeader()
40# already thoroughly test parsing each format.
41#
42# For parseDiffHeader(), it should suffice to verify that -- (1) for each
43# format, the method can return non-trivial values back for each key
44# supported by that format (e.g. "sourceRevision" for SVN), (2) the method
45# correctly sets default values when specific key-values are not set
46# (e.g. undef for "sourceRevision" for Git), and (3) key-values unique to
47# this method are set correctly (e.g. "scmFormat").
48my @testCaseHashRefs = (
49####
50#    SVN test cases
51##
52{   # New test
53    diffName => "SVN: non-trivial copiedFromPath and sourceRevision values",
54    inputText => <<'END',
55Index: index_path.py
56===================================================================
57--- index_path.py	(revision 53048)	(from copied_from_path.py:53048)
58+++ index_path.py	(working copy)
59@@ -0,0 +1,7 @@
60+# Python file...
61END
62    expectedReturn => [
63{
64    svnConvertedText => <<'END',
65Index: index_path.py
66===================================================================
67--- index_path.py	(revision 53048)	(from copied_from_path.py:53048)
68+++ index_path.py	(working copy)
69END
70    copiedFromPath => "copied_from_path.py",
71    indexPath => "index_path.py",
72    isSvn => 1,
73    sourceRevision => 53048,
74},
75"@@ -0,0 +1,7 @@\n"],
76    expectedNextLine => "+# Python file...\n",
77},
78####
79#    Git test cases
80##
81{   # New test case
82    diffName => "Git: Non-zero executable bit",
83    inputText => <<'END',
84diff --git a/foo.exe b/foo.exe
85old mode 100644
86new mode 100755
87END
88    expectedReturn => [
89{
90    svnConvertedText => <<'END',
91Index: foo.exe
92old mode 100644
93new mode 100755
94END
95    executableBitDelta => 1,
96    indexPath => "foo.exe",
97    isGit => 1,
98},
99undef],
100    expectedNextLine => undef,
101},
102);
103
104my $testCasesCount = @testCaseHashRefs;
105plan(tests => 2 * $testCasesCount); # Total number of assertions.
106
107foreach my $testCase (@testCaseHashRefs) {
108    my $testNameStart = "parseDiffHeader(): $testCase->{diffName}: comparing";
109
110    my $fileHandle;
111    open($fileHandle, "<", \$testCase->{inputText});
112    my $line = <$fileHandle>;
113
114    my @got = VCSUtils::parseDiffHeader($fileHandle, $line);
115    my $expectedReturn = $testCase->{expectedReturn};
116
117    is_deeply(\@got, $expectedReturn, "$testNameStart return value.");
118
119    my $gotNextLine = <$fileHandle>;
120    is($gotNextLine, $testCase->{expectedNextLine},  "$testNameStart next read line.");
121}
122