Fix capitalization
[vng.git] / tests / cursor / TestCursor.cpp
blobacf4b4f9a51b68285c76f15d23025d2087f55ac7
1 #include "TestCursor.h"
2 #include "hunks/HunksCursor.h"
3 #include "hunks/File.h"
4 #include "hunks/ChangeSet.h"
6 class MyHunksCursor : public HunksCursor {
7 public:
8 MyHunksCursor(ChangeSet &changeSet)
9 : HunksCursor(changeSet)
13 int atFile() const
15 return m_fileIndex;
17 int atHunk() const
19 return m_hunkIndex;
22 int atSubhunk() const
24 return m_subHunkIndex;
27 void setPosition(int file, int hunk, int subhunk)
29 m_fileIndex = file;
30 m_hunkIndex = hunk;
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.
42 File file1;
43 File file2 = file1;
44 File file3(file1);
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);
54 Hunk hunk1;
55 Hunk hunk2 = hunk1;
56 Hunk hunk3(hunk1);
57 Hunk hunk4;
58 hunk1.addLine(fn);
59 hunk1.addLine(fn);
61 QCOMPARE(hunk1.header(), fn);
62 QCOMPARE(hunk2.header(), fn);
63 QCOMPARE(hunk3.header(), fn);
66 void TestCursor::testForward1() {
67 ChangeSet changeSet;
68 File file;
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() {
97 ChangeSet changeSet;
98 File newFile;
99 newFile.setFileName("foo");
100 newFile.setProtection("100755");
101 newFile.setOldProtection("000000");
102 QVERIFY(newFile.isValid());
103 changeSet.addFile(newFile);
105 File file;
106 file.setFileName("filename");
107 file.setOldFileName("filename");
108 file.setProtection("100644");
109 file.setOldProtection("100644");
110 Hunk hunk;
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);
120 file.addHunk(hunk);
121 QVERIFY(file.isValid());
122 changeSet.addFile(file);
124 QCOMPARE(changeSet.count(), 2);
126 MyHunksCursor cursor(changeSet);
127 cursor.forceCount();
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);
171 File file2;
172 file2.setFileName("document");
173 file2.setOldFileName("document");
174 file2.setProtection("100644");
175 file2.setOldProtection("100644");
176 hunk = Hunk();
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");
182 file2.addHunk(hunk);
183 ChangeSet cs;
184 cs.addFile(newFile);
185 cs.addFile(file);
186 cs.addFile(file2);
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);
243 bool first = true;
244 int x = 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);
249 first = false;
251 ++x;
255 ChangeSet TestCursor::createChangeSet()
257 ChangeSet changeSet;
258 File file;
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");
274 Hunk hunk;
275 hunk.addLine(QByteArray("@@ -1,4 +1,4 @@"));
276 foreach(QByteArray s, patch) hunk.addLine(s);
277 file.addHunk(hunk);
279 Hunk hunk2;
280 hunk2.addLine(QByteArray("@@ -30,4 +40,4 @@"));
281 foreach(QByteArray s, patch) hunk2.addLine(s);
282 file.addHunk(hunk2);
283 changeSet.addFile(file);
284 return changeSet;
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)