1 #include "TestCursor.h"
2 #include "../HunksCursor.h"
4 #include "../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
);
67 void TestCursor::testForward1() {
70 file
.setFileName("foo");
71 file
.setOldFileName("bar");
72 file
.setProtection("100755");
73 file
.setOldProtection("100755");
74 changeSet
.addFile(file
);
76 QCOMPARE(changeSet
.count(), 1);
78 MyHunksCursor
cursor(changeSet
);
79 QCOMPARE(cursor
.isValid(), true);
80 QCOMPARE(cursor
.currentText(), QString("move `bar' `foo'\n"));
81 QCOMPARE(cursor
.forward(), 2);
82 QCOMPARE(cursor
.atFile(), 1);
83 QCOMPARE(cursor
.atHunk(), 0);
84 QCOMPARE(cursor
.atSubhunk(), 0);
85 QCOMPARE(cursor
.isValid(), false);
86 QCOMPARE(cursor
.currentText(), QString());
88 QCOMPARE(cursor
.back(), 1);
89 QCOMPARE(cursor
.atFile(), 0);
90 QCOMPARE(cursor
.atHunk(), 0);
91 QCOMPARE(cursor
.atSubhunk(), 0);
92 QCOMPARE(cursor
.isValid(), true);
93 QCOMPARE(cursor
.back(), 1);
94 QCOMPARE(cursor
.isValid(), true);
97 void TestCursor::testForward2() {
100 newFile
.setFileName("foo");
101 newFile
.setProtection("100755");
102 newFile
.setOldProtection("000000");
103 QVERIFY(newFile
.isValid());
104 changeSet
.addFile(newFile
);
107 file
.setFileName("filename");
108 file
.setOldFileName("filename");
109 file
.setProtection("100644");
110 file
.setOldProtection("100644");
112 hunk
.addLine(QByteArray("@@ -1,4 +1,4 @@"));
113 hunk
.addLine(" foo");
114 hunk
.addLine("+bar");
115 hunk
.addLine(" separator");
116 hunk
.addLine("-another subhunk");
117 QCOMPARE(hunk
.subHunkCount(), 2);
118 QCOMPARE(hunk
.lineNumber(), 1);
119 QCOMPARE(hunk
.lineNumber(0), 2);
120 QCOMPARE(hunk
.lineNumber(1), 4);
122 QVERIFY(file
.isValid());
123 changeSet
.addFile(file
);
125 QCOMPARE(changeSet
.count(), 2);
127 MyHunksCursor
cursor(changeSet
);
129 QCOMPARE(cursor
.isValid(), true);
130 QCOMPARE(cursor
.count(), 3);
131 QCOMPARE(cursor
.back(), 1);
132 QCOMPARE(cursor
.atFile(), 0);
133 QCOMPARE(cursor
.atHunk(), 0);
134 QCOMPARE(cursor
.atSubhunk(), 0);
135 QCOMPARE(cursor
.forward(), 2);
136 QCOMPARE(cursor
.atFile(), 1);
137 QCOMPARE(cursor
.atHunk(), 2);
138 QCOMPARE(cursor
.atSubhunk(), 0);
139 QCOMPARE(cursor
.isValid(), true);
140 QCOMPARE(cursor
.forward(), 3);
141 QCOMPARE(cursor
.atFile(), 1);
142 QCOMPARE(cursor
.atHunk(), 2);
143 QCOMPARE(cursor
.atSubhunk(), 1);
144 QCOMPARE(cursor
.isValid(), true);
145 QCOMPARE(cursor
.forward(), 4);
146 QCOMPARE(cursor
.atFile(), 2); // non existing file.
147 QCOMPARE(cursor
.isValid(), false);
149 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
150 QCOMPARE(cursor
.isValid(), true);
151 QCOMPARE(cursor
.forward(InterviewCursor::FullRange
), 4);
152 QCOMPARE(cursor
.isValid(), false);
154 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
155 QCOMPARE(cursor
.forward(InterviewCursor::BlockScope
), 2);
156 QCOMPARE(cursor
.forward(InterviewCursor::BlockScope
), 4);
158 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
159 cursor
.setResponse(true);
160 QCOMPARE(cursor
.forward(), 2);
161 cursor
.setResponse(true);
162 QCOMPARE(cursor
.forward(), 3);
164 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
165 QCOMPARE(cursor
.forward(), 3);
167 QCOMPARE(cursor
.back(InterviewCursor::FullRange
), 1);
168 QCOMPARE(cursor
.forward(InterviewCursor::ItemScope
, false), 2);
169 QCOMPARE(cursor
.forward(InterviewCursor::ItemScope
, false), 3);
170 QCOMPARE(cursor
.forward(InterviewCursor::ItemScope
, true), 4);
173 file2
.setFileName("document");
174 file2
.setOldFileName("document");
175 file2
.setProtection("100644");
176 file2
.setOldProtection("100644");
178 hunk
.addLine(QByteArray("@@ -10,4 +10,4 @@"));
179 hunk
.addLine(" foo");
180 hunk
.addLine("+bar");
181 hunk
.addLine(" separator");
182 hunk
.addLine("-another subhunk");
188 MyHunksCursor
myCursor(cs
);
190 QCOMPARE(myCursor
.atFile(), 0);
191 QCOMPARE(myCursor
.atHunk(), 0);
192 QCOMPARE(myCursor
.atSubhunk(), 0);
193 QCOMPARE(myCursor
.back(InterviewCursor::FullRange
), 1);
194 QCOMPARE(myCursor
.atFile(), 0);
195 QCOMPARE(myCursor
.atHunk(), 0);
196 QCOMPARE(myCursor
.atSubhunk(), 0);
197 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 2);
198 QCOMPARE(myCursor
.atFile(), 1);
199 QCOMPARE(myCursor
.atHunk(), 2);
200 QCOMPARE(myCursor
.atSubhunk(), 0);
201 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 3);
202 QCOMPARE(myCursor
.atFile(), 1);
203 QCOMPARE(myCursor
.atHunk(), 2);
204 QCOMPARE(myCursor
.atSubhunk(), 1);
205 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 4);
206 QCOMPARE(myCursor
.atFile(), 2);
207 QCOMPARE(myCursor
.atHunk(), 2);
208 QCOMPARE(myCursor
.atSubhunk(), 0);
209 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 5);
210 QCOMPARE(myCursor
.atFile(), 2);
211 QCOMPARE(myCursor
.atHunk(), 2);
212 QCOMPARE(myCursor
.atSubhunk(), 1);
213 QCOMPARE(myCursor
.forward(InterviewCursor::ItemScope
, false), 6);
214 QCOMPARE(myCursor
.atFile(), 3);
215 QCOMPARE(myCursor
.atHunk(), 0);
216 QCOMPARE(myCursor
.atSubhunk(), 0);
218 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 5);
219 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 4);
220 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 3);
221 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 2);
222 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
223 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
226 void TestCursor::testBackwards()
230 file
.setFileName("filename");
231 file
.setOldFileName("filename");
232 file
.setProtection("100644");
233 file
.setOldProtection("100644");
234 QList
<QByteArray
> patch
;
235 patch
.append(" foo\n");
236 patch
.append("+bar\n");
237 patch
.append("+b\n");
238 patch
.append("+c\n");
239 patch
.append(" separator\n");
240 patch
.append("-another subhunk\n");
241 patch
.append("-c\n");
242 patch
.append(" separator\n");
243 patch
.append("+another subhunk\n");
246 hunk
.addLine(QByteArray("@@ -1,4 +1,4 @@"));
247 foreach(QByteArray s
, patch
) hunk
.addLine(s
);
251 hunk2
.addLine(QByteArray("@@ -30,4 +40,4 @@"));
252 foreach(QByteArray s
, patch
) hunk2
.addLine(s
);
254 changeSet
.addFile(file
);
256 MyHunksCursor
myCursor(changeSet
);
257 myCursor
.setPosition(0, 3, 2);
258 QCOMPARE(myCursor
.currentIndexOpen(), 6);
259 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 5);
260 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 4);
261 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 3);
262 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 2);
263 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
264 QCOMPARE(myCursor
.back(InterviewCursor::ItemScope
), 1);
267 QTEST_MAIN(TestCursor
)