1 #include "TestCursor.h"
2 #include "hunks/HunksCursor.h"
3 #include "hunks/File.h"
4 #include "hunks/ChangeSet.h"
6 class MyHunksCursor
: public HunksCursor
{
8 MyHunksCursor(ChangeSet
&changeSet
)
9 : HunksCursor(changeSet
)
24 return m_subHunkIndex
;
27 void setPosition(int file
, int hunk
, int subhunk
)
31 m_subHunkIndex
= subhunk
;
34 int currentIndexOpen()
36 return currentIndex();
40 void TestCursor::testBasis() {
41 // these are all using the same backend to make moving around objects easier.
45 File file4
; // a different one :)
47 QByteArray
fn("testfilename");
48 file1
.setFileName(fn
);
49 QCOMPARE(file1
.fileName(), fn
);
50 QCOMPARE(file2
.fileName(), fn
);
51 QCOMPARE(file3
.fileName(), fn
);
52 QVERIFY(file4
.fileName() != fn
);
61 QCOMPARE(hunk1
.header(), fn
);
62 QCOMPARE(hunk2
.header(), fn
);
63 QCOMPARE(hunk3
.header(), fn
);
66 void TestCursor::testForward1() {
69 file
.setFileName("foo");
70 file
.setOldFileName("bar");
71 file
.setProtection("100755");
72 file
.setOldProtection("100755");
73 changeSet
.addFile(file
);
75 QCOMPARE(changeSet
.count(), 1);
77 MyHunksCursor
cursor(changeSet
);
78 QCOMPARE(cursor
.isValid(), true);
79 QCOMPARE(cursor
.currentText(), QString("move `bar' `foo'\n"));
80 QCOMPARE(cursor
.forward(), 2);
81 QCOMPARE(cursor
.atFile(), 1);
82 QCOMPARE(cursor
.atHunk(), 0);
83 QCOMPARE(cursor
.atSubhunk(), 0);
84 QCOMPARE(cursor
.isValid(), false);
85 QCOMPARE(cursor
.currentText(), QString());
87 QCOMPARE(cursor
.back(), 1);
88 QCOMPARE(cursor
.atFile(), 0);
89 QCOMPARE(cursor
.atHunk(), 0);
90 QCOMPARE(cursor
.atSubhunk(), 0);
91 QCOMPARE(cursor
.isValid(), true);
92 QCOMPARE(cursor
.back(), 1);
93 QCOMPARE(cursor
.isValid(), true);
96 void TestCursor::testForward2() {
99 newFile
.setFileName("foo");
100 newFile
.setProtection("100755");
101 newFile
.setOldProtection("000000");
102 QVERIFY(newFile
.isValid());
103 changeSet
.addFile(newFile
);
106 file
.setFileName("filename");
107 file
.setOldFileName("filename");
108 file
.setProtection("100644");
109 file
.setOldProtection("100644");
111 hunk
.addLine(QByteArray("@@ -1,4 +1,4 @@"));
112 hunk
.addLine(" foo");
113 hunk
.addLine("+bar");
114 hunk
.addLine(" separator");
115 hunk
.addLine("-another subhunk");
116 QCOMPARE(hunk
.subHunkCount(), 2);
117 QCOMPARE(hunk
.lineNumber(), 1);
118 QCOMPARE(hunk
.lineNumber(0), 2);
119 QCOMPARE(hunk
.lineNumber(1), 4);
121 QVERIFY(file
.isValid());
122 changeSet
.addFile(file
);
124 QCOMPARE(changeSet
.count(), 2);
126 MyHunksCursor
cursor(changeSet
);
128 QCOMPARE(cursor
.isValid(), true);
129 QCOMPARE(cursor
.count(), 3);
130 QCOMPARE(cursor
.back(), 1);
131 QCOMPARE(cursor
.atFile(), 0);
132 QCOMPARE(cursor
.atHunk(), 0);
133 QCOMPARE(cursor
.atSubhunk(), 0);
134 QCOMPARE(cursor
.forward(), 2);
135 QCOMPARE(cursor
.atFile(), 1);
136 QCOMPARE(cursor
.atHunk(), 2);
137 QCOMPARE(cursor
.atSubhunk(), 0);
138 QCOMPARE(cursor
.isValid(), true);
139 QCOMPARE(cursor
.forward(), 3);
140 QCOMPARE(cursor
.atFile(), 1);
141 QCOMPARE(cursor
.atHunk(), 2);
142 QCOMPARE(cursor
.atSubhunk(), 1);
143 QCOMPARE(cursor
.isValid(), true);
144 QCOMPARE(cursor
.forward(), 4);
145 QCOMPARE(cursor
.atFile(), 2); // non existing file.
146 QCOMPARE(cursor
.isValid(), false);
148 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
149 QCOMPARE(cursor
.isValid(), true);
150 QCOMPARE(cursor
.forward(InterviewCursor::FullRange
), 4);
151 QCOMPARE(cursor
.isValid(), false);
153 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
154 QCOMPARE(cursor
.forward(InterviewCursor::BlockScope
), 2);
155 QCOMPARE(cursor
.forward(InterviewCursor::BlockScope
), 4);
157 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
158 cursor
.setResponse(true);
159 QCOMPARE(cursor
.forward(), 2);
160 cursor
.setResponse(true);
161 QCOMPARE(cursor
.forward(), 3);
163 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
164 QCOMPARE(cursor
.forward(), 3);
166 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
167 QCOMPARE(cursor
.forward(InterviewCursor::ItemScope
, false), 2);
168 QCOMPARE(cursor
.forward(InterviewCursor::ItemScope
, false), 3);
169 QCOMPARE(cursor
.forward(InterviewCursor::ItemScope
, true), 4);
172 file2
.setFileName("document");
173 file2
.setOldFileName("document");
174 file2
.setProtection("100644");
175 file2
.setOldProtection("100644");
177 hunk
.addLine(QByteArray("@@ -10,4 +10,4 @@"));
178 hunk
.addLine(" foo");
179 hunk
.addLine("+bar");
180 hunk
.addLine(" separator");
181 hunk
.addLine("-another subhunk");
187 MyHunksCursor
myCursor(cs
);
189 QCOMPARE(myCursor
.atFile(), 0);
190 QCOMPARE(myCursor
.atHunk(), 0);
191 QCOMPARE(myCursor
.atSubhunk(), 0);
192 QCOMPARE(myCursor
.back(InterviewCursor::FullRange
), 1);
193 QCOMPARE(myCursor
.atFile(), 0);
194 QCOMPARE(myCursor
.atHunk(), 0);
195 QCOMPARE(myCursor
.atSubhunk(), 0);
196 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 2);
197 QCOMPARE(myCursor
.atFile(), 1);
198 QCOMPARE(myCursor
.atHunk(), 2);
199 QCOMPARE(myCursor
.atSubhunk(), 0);
200 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 3);
201 QCOMPARE(myCursor
.atFile(), 1);
202 QCOMPARE(myCursor
.atHunk(), 2);
203 QCOMPARE(myCursor
.atSubhunk(), 1);
204 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 4);
205 QCOMPARE(myCursor
.atFile(), 2);
206 QCOMPARE(myCursor
.atHunk(), 2);
207 QCOMPARE(myCursor
.atSubhunk(), 0);
208 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 5);
209 QCOMPARE(myCursor
.atFile(), 2);
210 QCOMPARE(myCursor
.atHunk(), 2);
211 QCOMPARE(myCursor
.atSubhunk(), 1);
212 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 6);
213 QCOMPARE(myCursor
.atFile(), 3);
214 QCOMPARE(myCursor
.atHunk(), 0);
215 QCOMPARE(myCursor
.atSubhunk(), 0);
217 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 5);
218 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 4);
219 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 3);
220 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 2);
221 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
222 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
225 void TestCursor::testPartialSelection()
227 ChangeSet changeSet
= createChangeSet();
228 MyHunksCursor
myCursor(changeSet
);
229 QCOMPARE( myCursor
.atFile(), 0);
230 QCOMPARE( myCursor
.atHunk(), 2);
231 QCOMPARE( myCursor
.atSubhunk(), 0);
232 QCOMPARE( myCursor
.currentIndexOpen(), 1);
233 myCursor
.setResponse(false);
234 QCOMPARE( myCursor
.currentIndexOpen(), 1);
235 QCOMPARE( myCursor
.forward(), 2);
236 QCOMPARE( myCursor
.atFile(), 0);
237 QCOMPARE( myCursor
.atHunk(), 2);
238 QCOMPARE( myCursor
.atSubhunk(), 1);
239 QCOMPARE( myCursor
.currentIndexOpen(), 2);
240 myCursor
.setResponse(true, InterviewCursor::FullRange
);
242 File file
= changeSet
.file(0);
245 foreach(Hunk hunk
, file
.hunks()) {
246 for (int sh
= 0; sh
< hunk
.subHunkCount(); ++sh
) {
247 //qDebug() << "acceptance"<< x << sh << "is" << hunk.acceptance(sh);
248 QCOMPARE(hunk
.acceptance(sh
), first
? Vng::Rejected
: Vng::Accepted
);
255 ChangeSet
TestCursor::createChangeSet()
259 file
.setFileName("filename");
260 file
.setOldFileName("filename");
261 file
.setProtection("100644");
262 file
.setOldProtection("100644");
263 QList
<QByteArray
> patch
;
264 patch
.append(" foo\n");
265 patch
.append("+bar\n");
266 patch
.append("+b\n");
267 patch
.append("+c\n");
268 patch
.append(" separator\n");
269 patch
.append("-another subhunk\n");
270 patch
.append("-c\n");
271 patch
.append(" separator\n");
272 patch
.append("+another subhunk\n");
275 hunk
.addLine(QByteArray("@@ -1,4 +1,4 @@"));
276 foreach(QByteArray s
, patch
) hunk
.addLine(s
);
280 hunk2
.addLine(QByteArray("@@ -30,4 +40,4 @@"));
281 foreach(QByteArray s
, patch
) hunk2
.addLine(s
);
283 changeSet
.addFile(file
);
287 void TestCursor::testBackwards()
289 ChangeSet changeSet
= createChangeSet();
290 MyHunksCursor
myCursor(changeSet
);
291 myCursor
.setPosition(0, 3, 2);
292 QCOMPARE(myCursor
.currentIndexOpen(), 6);
293 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 5);
294 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 4);
295 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 3);
296 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 2);
297 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
298 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
301 QTEST_MAIN(TestCursor
)