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
7# are met:
8# 1.  Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10# 2.  Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25# Unit tests of parseDiffHeader().
26
27use strict;
28use warnings;
29
30use Test::More;
31use VCSUtils;
32
33my @diffHashRefKeys = ( # The hash reference keys to check per diff.
34    "copiedFromPath",
35    "indexPath",
36    "sourceRevision",
37    "svnConvertedText",
38);
39
40# New test
41my $testNameStart = "parsePatch(): [SVN: Rename] ";
42my $patch = <<'END',
43Index: Makefile
44===================================================================
45--- Makefile	(revision 53131)
46+++ Makefile	(working copy)
47@@ -1,1 +0,0 @@
48-MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools
49Index: Makefile_new
50===================================================================
51--- Makefile_new	(revision 53131)	(from Makefile:53131)
52+++ Makefile_new	(working copy)
53@@ -0,0 +1,1 @@
54+MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools
55END
56
57my @expectedDiffHashRefs = (
58{
59    svnConvertedText => <<'END',
60Index: Makefile
61===================================================================
62--- Makefile	(revision 53131)
63+++ Makefile	(working copy)
64@@ -1,1 +0,0 @@
65-MODULES = JavaScriptCore JavaScriptGlue WebCore WebKit WebKitTools
66END
67    copiedFromPath => undef,
68    indexPath => "Makefile",
69    sourceRevision => "53131",
70},
71{
72    copiedFromPath => "Makefile",
73    indexPath => "Makefile_new",
74    sourceRevision => "53131",
75},
76);
77
78plan(tests => @expectedDiffHashRefs * @diffHashRefKeys);
79
80my $fileHandle;
81open($fileHandle, "<", \$patch);
82
83my @gotDiffHashRefs = parsePatch($fileHandle);
84
85my $i = 0;
86foreach my $expectedDiffHashRef (@expectedDiffHashRefs) {
87
88    my $gotDiffHashRef = $gotDiffHashRefs[$i++];
89
90    foreach my $diffHashRefKey (@diffHashRefKeys) {
91        my $testName = "${testNameStart}[diff $i] key=\"$diffHashRefKey\"";
92        is($gotDiffHashRef->{$diffHashRefKey}, $expectedDiffHashRef->{$diffHashRefKey}, $testName);
93    }
94}
95