Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / PaginationHelperTestCase.cs
blob3c8b414d269dab5ddd8aab90e32b3df82d929bd3
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Tests.Helpers
17 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using NUnit.Framework;
22 using Castle.MonoRail.Framework.Helpers;
24 [TestFixture]
25 public class PaginationHelperTestCase
27 private PaginationHelper helper;
29 [SetUp]
30 public void Init()
32 helper = new PaginationHelper();
35 [Test]
36 public void EmptyCollection()
38 IList items = new ArrayList();
40 IPaginatedPage page = PaginationHelper.CreatePagination(items, 10, 1);
42 Assert.IsNotNull(page);
44 Assert.AreEqual(0, page.FirstItem);
45 Assert.AreEqual(0, page.LastItem);
46 Assert.AreEqual(0, page.TotalItems);
48 Assert.IsFalse(page.HasFirst);
49 Assert.IsFalse(page.HasLast);
50 Assert.IsFalse(page.HasNext);
51 Assert.IsFalse(page.HasPrevious);
54 [Test]
55 public void LessItemsThanPageSize()
57 IList items = new ArrayList();
59 items.Add("1");
60 items.Add("2");
61 items.Add("3");
62 items.Add("4");
63 items.Add("5");
65 IPaginatedPage page = PaginationHelper.CreatePagination(items, 10, 1);
67 Assert.IsNotNull(page);
69 Assert.AreEqual(1, page.FirstItem);
70 Assert.AreEqual(5, page.LastItem);
71 Assert.AreEqual(5, page.TotalItems);
73 Assert.IsFalse(page.HasFirst);
74 Assert.IsFalse(page.HasLast);
75 Assert.IsFalse(page.HasNext);
76 Assert.IsFalse(page.HasPrevious);
79 [Test]
80 public void JustOneItem()
82 IList items = new ArrayList();
84 items.Add("1");
86 IPaginatedPage page = PaginationHelper.CreatePagination(items, 10, 1);
88 Assert.IsNotNull(page);
90 Assert.AreEqual(1, page.FirstItem);
91 Assert.AreEqual(1, page.LastItem);
92 Assert.AreEqual(1, page.TotalItems);
94 Assert.IsFalse(page.HasFirst);
95 Assert.IsFalse(page.HasLast);
96 Assert.IsFalse(page.HasNext);
97 Assert.IsFalse(page.HasPrevious);
100 [Test]
101 public void ItemsEqualsToPageSize()
103 IList items = new ArrayList();
105 items.Add("1");
106 items.Add("2");
107 items.Add("3");
109 IPaginatedPage page = PaginationHelper.CreatePagination(items, 3, 1);
111 Assert.IsNotNull(page);
113 Assert.AreEqual(1, page.FirstItem);
114 Assert.AreEqual(3, page.LastItem);
115 Assert.AreEqual(3, page.TotalItems);
117 Assert.IsFalse(page.HasFirst);
118 Assert.IsFalse(page.HasLast);
119 Assert.IsFalse(page.HasNext);
120 Assert.IsFalse(page.HasPrevious);
123 [Test]
124 public void ItemsBiggerThanPageSize()
126 IList items = new ArrayList();
128 for(int i=1; i <= 15; i++)
130 items.Add(i);
133 // First page
135 IPaginatedPage page = PaginationHelper.CreatePagination(items, 5, 1);
137 Assert.IsNotNull(page);
139 Assert.AreEqual(1, page.FirstItem);
140 Assert.AreEqual(5, page.LastItem);
141 Assert.AreEqual(15, page.TotalItems);
143 Assert.IsFalse(page.HasFirst);
144 Assert.IsTrue(page.HasLast);
145 Assert.IsTrue(page.HasNext);
146 Assert.IsFalse(page.HasPrevious);
148 // Second page
150 page = PaginationHelper.CreatePagination( items, 5, 2 );
152 Assert.IsNotNull(page);
154 Assert.AreEqual(6, page.FirstItem);
155 Assert.AreEqual(10, page.LastItem);
156 Assert.AreEqual(15, page.TotalItems);
158 Assert.IsTrue(page.HasFirst);
159 Assert.IsTrue(page.HasLast);
160 Assert.IsTrue(page.HasNext);
161 Assert.IsTrue(page.HasPrevious);
163 // Last page
165 page = PaginationHelper.CreatePagination( items, 5, 3 );
167 Assert.IsNotNull(page);
169 Assert.AreEqual(11, page.FirstItem);
170 Assert.AreEqual(15, page.LastItem);
171 Assert.AreEqual(15, page.TotalItems);
173 Assert.IsTrue(page.HasFirst);
174 Assert.IsFalse(page.HasLast);
175 Assert.IsFalse(page.HasNext);
176 Assert.IsTrue(page.HasPrevious);
179 [Test]
180 public void UsageWithGenerics()
182 IList<int> items = new List<int>();
184 for (int i = 1; i <= 15; i++)
186 items.Add(i);
189 // First page
191 IPaginatedPage page = PaginationHelper.CreatePagination(items, 5, 1);
193 Assert.IsNotNull(page);
195 Assert.AreEqual(1, page.FirstItem);
196 Assert.AreEqual(5, page.LastItem);
197 Assert.AreEqual(15, page.TotalItems);
199 Assert.IsFalse(page.HasFirst);
200 Assert.IsTrue(page.HasLast);
201 Assert.IsTrue(page.HasNext);
202 Assert.IsFalse(page.HasPrevious);
204 // Second page
206 page = PaginationHelper.CreatePagination(items, 5, 2);
208 Assert.IsNotNull(page);
210 Assert.AreEqual(6, page.FirstItem);
211 Assert.AreEqual(10, page.LastItem);
212 Assert.AreEqual(15, page.TotalItems);
214 Assert.IsTrue(page.HasFirst);
215 Assert.IsTrue(page.HasLast);
216 Assert.IsTrue(page.HasNext);
217 Assert.IsTrue(page.HasPrevious);
219 // Last page
221 page = PaginationHelper.CreatePagination(items, 5, 3);
223 Assert.IsNotNull(page);
225 Assert.AreEqual(11, page.FirstItem);
226 Assert.AreEqual(15, page.LastItem);
227 Assert.AreEqual(15, page.TotalItems);
229 Assert.IsTrue(page.HasFirst);
230 Assert.IsFalse(page.HasLast);
231 Assert.IsFalse(page.HasNext);
232 Assert.IsTrue(page.HasPrevious);
236 [Test]
237 public void CustomPaginationForObjectCollection()
239 IList items = new ArrayList();
241 /* First page */
242 items.Add("1");
243 items.Add("2");
244 items.Add("3");
246 IPaginatedPage page = PaginationHelper.CreateCustomPage(items, 3, 1, 8);
248 Assert.IsNotNull(page);
250 Assert.AreEqual(1, page.FirstItem);
251 Assert.AreEqual(3, page.LastItem);
252 Assert.AreEqual(8, page.TotalItems);
254 Assert.IsFalse(page.HasFirst);
255 Assert.IsTrue(page.HasLast);
256 Assert.IsTrue(page.HasNext);
257 Assert.IsFalse(page.HasPrevious);
259 /* Second page */
260 items.Clear();
261 items.Add("4");
262 items.Add("5");
263 items.Add("6");
265 page = PaginationHelper.CreateCustomPage(items, 3, 2, 8);
267 Assert.IsNotNull(page);
269 Assert.AreEqual(4, page.FirstItem);
270 Assert.AreEqual(6, page.LastItem);
271 Assert.AreEqual(8, page.TotalItems);
273 Assert.IsTrue(page.HasFirst);
274 Assert.IsTrue(page.HasLast);
275 Assert.IsTrue(page.HasNext);
276 Assert.IsTrue(page.HasPrevious);
278 /* Third page, partial */
279 items.Clear();
280 items.Add("7");
281 items.Add("8");
283 page = PaginationHelper.CreateCustomPage(items, 3, 3, 8);
285 Assert.IsNotNull(page);
287 Assert.AreEqual(7, page.FirstItem);
288 Assert.AreEqual(8, page.LastItem);
289 Assert.AreEqual(8, page.TotalItems);
291 Assert.IsTrue(page.HasFirst);
292 Assert.IsFalse(page.HasLast);
293 Assert.IsFalse(page.HasNext);
294 Assert.IsTrue(page.HasPrevious);
297 [Test]
298 public void CustomPaginationForGenericCollection()
300 IList<string> items = new List<string>();
302 /* First page */
303 items.Add("1");
304 items.Add("2");
305 items.Add("3");
307 IPaginatedPage page = PaginationHelper.CreateCustomPage(items, 3, 1, 8);
309 Assert.IsNotNull(page);
311 Assert.AreEqual(1, page.FirstItem);
312 Assert.AreEqual(3, page.LastItem);
313 Assert.AreEqual(8, page.TotalItems);
315 Assert.IsFalse(page.HasFirst);
316 Assert.IsTrue(page.HasLast);
317 Assert.IsTrue(page.HasNext);
318 Assert.IsFalse(page.HasPrevious);
320 /* Second page */
321 items.Clear();
322 items.Add("4");
323 items.Add("5");
324 items.Add("6");
326 page = PaginationHelper.CreateCustomPage(items, 3, 2, 8);
328 Assert.IsNotNull(page);
330 Assert.AreEqual(4, page.FirstItem);
331 Assert.AreEqual(6, page.LastItem);
332 Assert.AreEqual(8, page.TotalItems);
334 Assert.IsTrue(page.HasFirst);
335 Assert.IsTrue(page.HasLast);
336 Assert.IsTrue(page.HasNext);
337 Assert.IsTrue(page.HasPrevious);
339 /* Third page, partial */
340 items.Clear();
341 items.Add("7");
342 items.Add("8");
344 page = PaginationHelper.CreateCustomPage(items, 3, 3, 8);
346 Assert.IsNotNull(page);
348 Assert.AreEqual(7, page.FirstItem);
349 Assert.AreEqual(8, page.LastItem);
350 Assert.AreEqual(8, page.TotalItems);
352 Assert.IsTrue(page.HasFirst);
353 Assert.IsFalse(page.HasLast);
354 Assert.IsFalse(page.HasNext);
355 Assert.IsTrue(page.HasPrevious);