initial import
[vuplus_webkit] / Tools / Scripts / webkitperl / VCSUtils_unittest / parseChunkRange.pl
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@gmail.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 VCSUtils::parseChunkRange().
33
34 use strict;
35 use warnings;
36
37 use Test::More;
38 use VCSUtils;
39
40 my @testCaseHashRefs = (
41 ###
42 # Invalid and malformed chunk range
43 ##
44 # FIXME: We should make this set of tests more comprehensive.
45 {   # New test
46     testName => "[invalid] Empty string",
47     inputText => "",
48     expectedReturn => []
49 },
50 {   # New test
51     testName => "[invalid] Bogus chunk range",
52     inputText => "@@ this is not valid @@",
53     expectedReturn => []
54 },
55 {   # New test
56     testName => "[invalid] Chunk range missing -/+ prefix",
57     inputText => "@@ 0,0 1,4 @@",
58     expectedReturn => []
59 },
60 {   # New test
61     testName => "[invalid] Chunk range missing commas",
62     inputText => "@@ -0 0 +1 4 @@",
63     expectedReturn => []
64 },
65 {   # New test
66     testName => "[invalid] Chunk range with swapped old and rew ranges",
67     inputText => "@@ +0,0 -1,4 @@",
68     expectedReturn => []
69 },
70 {   # New test
71     testName => "[invalid] Chunk range with leading junk",
72     inputText => "leading junk @@ -0,0 +1,4 @@",
73     expectedReturn => []
74 },
75 ###
76 #  Simple test cases
77 ##
78 {   # New test
79     testName => "Line count is 0",
80     inputText => "@@ -0,0 +1,4 @@",
81     expectedReturn => [
82 {
83     startingLine => 0,
84     lineCount => 0,
85     newStartingLine => 1,
86     newLineCount => 4,
87 }
88 ]
89 },
90 {   # New test
91     testName => "Line count is 1",
92     inputText => "@@ -1 +1,4 @@",
93     expectedReturn => [
94 {
95     startingLine => 1,
96     lineCount => 1,
97     newStartingLine => 1,
98     newLineCount => 4,
99 }
100 ]
101 },
102 {   # New test
103     testName => "Both original and new line count is 1",
104     inputText => "@@ -1 +1 @@",
105     expectedReturn => [
106 {
107     startingLine => 1,
108     lineCount => 1,
109     newStartingLine => 1,
110     newLineCount => 1,
111 }
112 ]
113 },
114 {   # New test
115     testName => "Line count and new line count > 1",
116     inputText => "@@ -1,2 +1,4 @@",
117     expectedReturn => [
118 {
119     startingLine => 1,
120     lineCount => 2,
121     newStartingLine => 1,
122     newLineCount => 4,
123 }
124 ]
125 },
126 {   # New test
127     testName => "New line count is 0",
128     inputText => "@@ -1,4 +0,0 @@",
129     expectedReturn => [
130 {
131     startingLine => 1,
132     lineCount => 4,
133     newStartingLine => 0,
134     newLineCount => 0,
135 }
136 ]
137 },
138 {   # New test
139     testName => "New line count is 1",
140     inputText => "@@ -1,4 +1 @@",
141     expectedReturn => [
142 {
143     startingLine => 1,
144     lineCount => 4,
145     newStartingLine => 1,
146     newLineCount => 1,
147 }
148 ]
149 },
150 ###
151 #  Chunk range followed by ending junk
152 ##
153 {   # New test
154     testName => "Line count is 0 and chunk range has ending junk",
155     inputText => "@@ -0,0 +1,4 @@ foo()",
156     expectedReturn => [
157 {
158     startingLine => 0,
159     lineCount => 0,
160     newStartingLine => 1,
161     newLineCount => 4,
162 }
163 ]
164 },
165 {   # New test
166     testName => "Line count is 1 and chunk range has ending junk",
167     inputText => "@@ -1 +1,4 @@ foo()",
168     expectedReturn => [
169 {
170     startingLine => 1,
171     lineCount => 1,
172     newStartingLine => 1,
173     newLineCount => 4,
174 }
175 ]
176 },
177 {   # New test
178     testName => "Both original and new line count is 1 and chunk range has ending junk",
179     inputText => "@@ -1 +1 @@ foo()",
180     expectedReturn => [
181 {
182     startingLine => 1,
183     lineCount => 1,
184     newStartingLine => 1,
185     newLineCount => 1,
186 }
187 ]
188 },
189 {   # New test
190     testName => "Line count and new line count > 1 and chunk range has ending junk",
191     inputText => "@@ -1,2 +1,4 @@ foo()",
192     expectedReturn => [
193 {
194     startingLine => 1,
195     lineCount => 2,
196     newStartingLine => 1,
197     newLineCount => 4,
198 }
199 ]
200 },
201 {   # New test
202     testName => "New line count is 0 and chunk range has ending junk",
203     inputText => "@@ -1,4 +0,0 @@ foo()",
204     expectedReturn => [
205 {
206     startingLine => 1,
207     lineCount => 4,
208     newStartingLine => 0,
209     newLineCount => 0,
210 }
211 ]
212 },
213 {   # New test
214     testName => "New line count is 1 and chunk range has ending junk",
215     inputText => "@@ -1,4 +1 @@ foo()",
216     expectedReturn => [
217 {
218     startingLine => 1,
219     lineCount => 4,
220     newStartingLine => 1,
221     newLineCount => 1,
222 }
223 ]
224 },
225 );
226
227 my $testCasesCount = @testCaseHashRefs;
228 plan(tests => $testCasesCount);
229
230 foreach my $testCase (@testCaseHashRefs) {
231     my $testNameStart = "parseChunkRange(): $testCase->{testName}: comparing";
232
233     my @got = VCSUtils::parseChunkRange($testCase->{inputText});
234     my $expectedReturn = $testCase->{expectedReturn};
235
236     is_deeply(\@got, $expectedReturn, "$testNameStart return value.");
237 }