Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / PaginationHelperTestCase.cs
blobc52018e3cbc8b462672e6c38636fe2b6fbe71813
1 // Copyright 2004-2008 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.Collections;
18 using System.Collections.Generic;
19 using NUnit.Framework;
21 using Castle.MonoRail.Framework.Helpers;
23 [TestFixture]
24 public class PaginationHelperTestCase
26 [Test]
27 public void EmptyCollection()
29 IList items = new ArrayList();
31 IPaginatedPage page = PaginationHelper.CreatePagination(items, 10, 1);
33 Assert.IsNotNull(page);
35 Assert.AreEqual(0, page.FirstItem);
36 Assert.AreEqual(0, page.LastItem);
37 Assert.AreEqual(0, page.TotalItems);
39 Assert.IsFalse(page.HasFirst);
40 Assert.IsFalse(page.HasLast);
41 Assert.IsFalse(page.HasNext);
42 Assert.IsFalse(page.HasPrevious);
45 [Test]
46 public void LessItemsThanPageSize()
48 IList items = new ArrayList();
50 items.Add("1");
51 items.Add("2");
52 items.Add("3");
53 items.Add("4");
54 items.Add("5");
56 IPaginatedPage page = PaginationHelper.CreatePagination(items, 10, 1);
58 Assert.IsNotNull(page);
60 Assert.AreEqual(1, page.FirstItem);
61 Assert.AreEqual(5, page.LastItem);
62 Assert.AreEqual(5, page.TotalItems);
64 Assert.IsFalse(page.HasFirst);
65 Assert.IsFalse(page.HasLast);
66 Assert.IsFalse(page.HasNext);
67 Assert.IsFalse(page.HasPrevious);
70 [Test]
71 public void JustOneItem()
73 IList items = new ArrayList();
75 items.Add("1");
77 IPaginatedPage page = PaginationHelper.CreatePagination(items, 10, 1);
79 Assert.IsNotNull(page);
81 Assert.AreEqual(1, page.FirstItem);
82 Assert.AreEqual(1, page.LastItem);
83 Assert.AreEqual(1, page.TotalItems);
85 Assert.IsFalse(page.HasFirst);
86 Assert.IsFalse(page.HasLast);
87 Assert.IsFalse(page.HasNext);
88 Assert.IsFalse(page.HasPrevious);
91 [Test]
92 public void ItemsEqualsToPageSize()
94 IList items = new ArrayList();
96 items.Add("1");
97 items.Add("2");
98 items.Add("3");
100 IPaginatedPage page = PaginationHelper.CreatePagination(items, 3, 1);
102 Assert.IsNotNull(page);
104 Assert.AreEqual(1, page.FirstItem);
105 Assert.AreEqual(3, page.LastItem);
106 Assert.AreEqual(3, page.TotalItems);
108 Assert.IsFalse(page.HasFirst);
109 Assert.IsFalse(page.HasLast);
110 Assert.IsFalse(page.HasNext);
111 Assert.IsFalse(page.HasPrevious);
114 [Test]
115 public void ItemsBiggerThanPageSize()
117 IList items = new ArrayList();
119 for(int i=1; i <= 15; i++)
121 items.Add(i);
124 // First page
126 IPaginatedPage page = PaginationHelper.CreatePagination(items, 5, 1);
128 Assert.IsNotNull(page);
130 Assert.AreEqual(1, page.FirstItem);
131 Assert.AreEqual(5, page.LastItem);
132 Assert.AreEqual(15, page.TotalItems);
134 Assert.IsFalse(page.HasFirst);
135 Assert.IsTrue(page.HasLast);
136 Assert.IsTrue(page.HasNext);
137 Assert.IsFalse(page.HasPrevious);
139 // Second page
141 page = PaginationHelper.CreatePagination( items, 5, 2 );
143 Assert.IsNotNull(page);
145 Assert.AreEqual(6, page.FirstItem);
146 Assert.AreEqual(10, page.LastItem);
147 Assert.AreEqual(15, page.TotalItems);
149 Assert.IsTrue(page.HasFirst);
150 Assert.IsTrue(page.HasLast);
151 Assert.IsTrue(page.HasNext);
152 Assert.IsTrue(page.HasPrevious);
154 // Last page
156 page = PaginationHelper.CreatePagination( items, 5, 3 );
158 Assert.IsNotNull(page);
160 Assert.AreEqual(11, page.FirstItem);
161 Assert.AreEqual(15, page.LastItem);
162 Assert.AreEqual(15, page.TotalItems);
164 Assert.IsTrue(page.HasFirst);
165 Assert.IsFalse(page.HasLast);
166 Assert.IsFalse(page.HasNext);
167 Assert.IsTrue(page.HasPrevious);
170 [Test]
171 public void UsageWithGenerics()
173 IList<int> items = new List<int>();
175 for (int i = 1; i <= 15; i++)
177 items.Add(i);
180 // First page
182 IPaginatedPage page = PaginationHelper.CreatePagination(items, 5, 1);
184 Assert.IsNotNull(page);
186 Assert.AreEqual(1, page.FirstItem);
187 Assert.AreEqual(5, page.LastItem);
188 Assert.AreEqual(15, page.TotalItems);
190 Assert.IsFalse(page.HasFirst);
191 Assert.IsTrue(page.HasLast);
192 Assert.IsTrue(page.HasNext);
193 Assert.IsFalse(page.HasPrevious);
195 // Second page
197 page = PaginationHelper.CreatePagination(items, 5, 2);
199 Assert.IsNotNull(page);
201 Assert.AreEqual(6, page.FirstItem);
202 Assert.AreEqual(10, page.LastItem);
203 Assert.AreEqual(15, page.TotalItems);
205 Assert.IsTrue(page.HasFirst);
206 Assert.IsTrue(page.HasLast);
207 Assert.IsTrue(page.HasNext);
208 Assert.IsTrue(page.HasPrevious);
210 // Last page
212 page = PaginationHelper.CreatePagination(items, 5, 3);
214 Assert.IsNotNull(page);
216 Assert.AreEqual(11, page.FirstItem);
217 Assert.AreEqual(15, page.LastItem);
218 Assert.AreEqual(15, page.TotalItems);
220 Assert.IsTrue(page.HasFirst);
221 Assert.IsFalse(page.HasLast);
222 Assert.IsFalse(page.HasNext);
223 Assert.IsTrue(page.HasPrevious);
227 [Test]
228 public void CustomPaginationForObjectCollection()
230 IList items = new ArrayList();
232 /* First page */
233 items.Add("1");
234 items.Add("2");
235 items.Add("3");
237 IPaginatedPage page = PaginationHelper.CreateCustomPage(items, 3, 1, 8);
239 Assert.IsNotNull(page);
241 Assert.AreEqual(1, page.FirstItem);
242 Assert.AreEqual(3, page.LastItem);
243 Assert.AreEqual(8, page.TotalItems);
245 Assert.IsFalse(page.HasFirst);
246 Assert.IsTrue(page.HasLast);
247 Assert.IsTrue(page.HasNext);
248 Assert.IsFalse(page.HasPrevious);
250 /* Second page */
251 items.Clear();
252 items.Add("4");
253 items.Add("5");
254 items.Add("6");
256 page = PaginationHelper.CreateCustomPage(items, 3, 2, 8);
258 Assert.IsNotNull(page);
260 Assert.AreEqual(4, page.FirstItem);
261 Assert.AreEqual(6, page.LastItem);
262 Assert.AreEqual(8, page.TotalItems);
264 Assert.IsTrue(page.HasFirst);
265 Assert.IsTrue(page.HasLast);
266 Assert.IsTrue(page.HasNext);
267 Assert.IsTrue(page.HasPrevious);
269 /* Third page, partial */
270 items.Clear();
271 items.Add("7");
272 items.Add("8");
274 page = PaginationHelper.CreateCustomPage(items, 3, 3, 8);
276 Assert.IsNotNull(page);
278 Assert.AreEqual(7, page.FirstItem);
279 Assert.AreEqual(8, page.LastItem);
280 Assert.AreEqual(8, page.TotalItems);
282 Assert.IsTrue(page.HasFirst);
283 Assert.IsFalse(page.HasLast);
284 Assert.IsFalse(page.HasNext);
285 Assert.IsTrue(page.HasPrevious);
288 [Test]
289 public void CustomPaginationForGenericCollection()
291 IList<string> items = new List<string>();
293 /* First page */
294 items.Add("1");
295 items.Add("2");
296 items.Add("3");
298 IPaginatedPage page = PaginationHelper.CreateCustomPage(items, 3, 1, 8);
300 Assert.IsNotNull(page);
302 Assert.AreEqual(1, page.FirstItem);
303 Assert.AreEqual(3, page.LastItem);
304 Assert.AreEqual(8, page.TotalItems);
306 Assert.IsFalse(page.HasFirst);
307 Assert.IsTrue(page.HasLast);
308 Assert.IsTrue(page.HasNext);
309 Assert.IsFalse(page.HasPrevious);
311 /* Second page */
312 items.Clear();
313 items.Add("4");
314 items.Add("5");
315 items.Add("6");
317 page = PaginationHelper.CreateCustomPage(items, 3, 2, 8);
319 Assert.IsNotNull(page);
321 Assert.AreEqual(4, page.FirstItem);
322 Assert.AreEqual(6, page.LastItem);
323 Assert.AreEqual(8, page.TotalItems);
325 Assert.IsTrue(page.HasFirst);
326 Assert.IsTrue(page.HasLast);
327 Assert.IsTrue(page.HasNext);
328 Assert.IsTrue(page.HasPrevious);
330 /* Third page, partial */
331 items.Clear();
332 items.Add("7");
333 items.Add("8");
335 page = PaginationHelper.CreateCustomPage(items, 3, 3, 8);
337 Assert.IsNotNull(page);
339 Assert.AreEqual(7, page.FirstItem);
340 Assert.AreEqual(8, page.LastItem);
341 Assert.AreEqual(8, page.TotalItems);
343 Assert.IsTrue(page.HasFirst);
344 Assert.IsFalse(page.HasLast);
345 Assert.IsFalse(page.HasNext);
346 Assert.IsTrue(page.HasPrevious);