Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / DictHelperTestCase.cs
blobe390fbee881adc0a241936d92a37c884a7e2d118
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;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.Collections.Generic;
21 using System.Globalization;
22 using System.Threading;
24 using Castle.MonoRail.Framework.Helpers;
26 using NUnit.Framework;
29 [TestFixture]
30 public class DictHelperTestCase
32 private DictHelper helper;
34 [SetUp]
35 public void Init()
37 helper = new DictHelper();
38 Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
41 [Test]
42 public void EmptyDict()
44 IDictionary dict = helper.CreateDict();
46 Assert.IsNotNull(dict);
47 Assert.AreEqual(0, dict.Count);
50 [Test]
51 public void SimpleDict()
53 IDictionary dict = helper.CreateDict("name=value", "other=somethingelse");
55 Assert.IsNotNull(dict);
56 Assert.AreEqual(2, dict.Count);
58 foreach (String key in dict.Keys)
60 if (key.Equals("name"))
62 Assert.AreEqual("value", dict["name"]);
64 else if (key.Equals("other"))
66 Assert.AreEqual("somethingelse", dict["other"]);
68 else
70 Assert.Fail("unexpected key? " + key);
75 /// <summary>
76 /// Pulled out of [Test] method, so it can be call in timeing time
77 /// w/o the overhead of the Asserts
78 /// </summary>
79 /// <returns></returns>
80 public IDictionary ComplexDict_woChk()
82 int h = 1;
83 double i = 3.1415;
84 int j = 5;
85 IDictionary dict = helper.CreateDict("name=value", "other=somethingelse", "A=B",
86 "CCC=DDD", "EEE=FFF=GGG",
87 "hhh=" + h.ToString(),
88 "iii=" + i.ToString(),
89 "jjj=" + j.ToString()
91 return dict;
94 [Test]
95 public void ComplexDict()
97 IDictionary dict = ComplexDict_woChk();
98 ComplexAsserts(dict);
101 /// <summary>
102 /// Pulled out of [Test] method, so it can be call in timing test
103 /// w/o the overhead of the Asserts
104 /// </summary>
105 /// <returns></returns>
106 public IDictionary ComplexDict2_woChk()
108 int h = 1;
109 double i = 3.1415;
110 int j = 5;
111 IDictionary dict = helper.N("name", "value")
112 .N("other", "somethingelse")
113 .N("A", "B")
114 .N("CCC", "DDD")
115 .N("EEE", "FFF=GGG")
116 .N("hhh", h)
117 .N("iii", i)
118 .N("jjj", j);
120 return dict;
123 [Test]
124 public void ComplexDict2()
126 IDictionary dict = ComplexDict2_woChk();
127 ComplexAsserts(dict);
130 public IDictionary ComplexDictStatic_woChk()
132 int h = 1;
133 double i = 3.1415;
134 int j = 5;
135 return DictHelper.CreateN("name", "value")
136 .N("other", "somethingelse")
137 .N("A", "B")
138 .N("CCC", "DDD")
139 .N("EEE", "FFF=GGG")
140 .N("hhh", h)
141 .N("iii", i)
142 .N("jjj", j);
145 [Test]
146 public void ComplexDictStatic()
148 IDictionary dict = ComplexDictStatic_woChk();
149 ComplexAsserts(dict);
152 public IDictionary ComplexDictMixed_woChk()
154 int h = 1;
155 double i = 3.1415;
156 int j = 5;
157 return helper.CreateDict("name=value", "other=somethingelse", "A=B")
158 .N("CCC", "DDD").N("EEE", "FFF=GGG").N("hhh", h).N("iii", i).N("jjj", j);
161 [Test]
162 public void ComplexDictMixed()
164 IDictionary dict = ComplexDictMixed_woChk();
165 ComplexAsserts(dict);
168 private void ComplexAsserts(IDictionary dict)
170 Assert.IsNotNull(dict);
171 Assert.AreEqual(8, dict.Count);
173 ArrayList keys = new ArrayList(dict.Keys);
175 Assert.Contains("name", keys);
176 Assert.Contains("other", keys);
177 Assert.Contains("A", keys);
178 Assert.Contains("CCC", keys);
179 Assert.Contains("EEE", keys);
180 Assert.Contains("hhh", keys);
181 Assert.Contains("iii", keys);
182 Assert.Contains("jjj", keys);
184 Assert.AreEqual("value", dict["name"]);
185 Assert.AreEqual("somethingelse", dict["other"]);
186 Assert.AreEqual("B", dict["A"]);
187 Assert.AreEqual("DDD", dict["CCC"]);
188 Assert.AreEqual("FFF=GGG", dict["EEE"]);
189 Assert.AreEqual("1", dict["hhh"]);
190 Assert.AreEqual("3.1415", dict["iii"]);
191 Assert.AreEqual("5", dict["jjj"]);
194 [Test]
195 public void NameValueDict()
197 NameValueCollection nvc = new NameValueCollection(8);
198 nvc.Add("name", "value");
199 nvc.Add("name", "value2");
200 nvc.Add("other", "somethingelse");
201 nvc.Add("A", "B");
202 nvc.Add("CCC", "DDD");
203 nvc.Add("EEE", "FFF=GGG");
204 nvc.Add("hhh", "1");
205 nvc.Add("iii", "3.1415");
206 nvc.Add("jjj", "5");
207 IDictionary dict = helper.FromNameValueCollection(nvc);
209 Assert.IsNotNull(dict);
210 Assert.AreEqual(8, dict.Count);
212 ArrayList keys = new ArrayList(dict.Keys);
214 Assert.Contains("name", keys);
215 Assert.Contains("other", keys);
216 Assert.Contains("A", keys);
217 Assert.Contains("CCC", keys);
218 Assert.Contains("EEE", keys);
219 Assert.Contains("hhh", keys);
220 Assert.Contains("iii", keys);
221 Assert.Contains("jjj", keys);
223 Assert.AreEqual("value", ((string[]) dict["name"])[0]);
224 Assert.AreEqual("value2", ((string[]) dict["name"])[1]);
225 Assert.AreEqual("somethingelse", ((string[]) dict["other"])[0]);
226 Assert.AreEqual("B", ((string[]) dict["A"])[0]);
227 Assert.AreEqual("DDD", ((string[]) dict["CCC"])[0]);
228 Assert.AreEqual("FFF=GGG", ((string[]) dict["EEE"])[0]);
229 Assert.AreEqual("1", ((string[]) dict["hhh"])[0]);
230 Assert.AreEqual("3.1415", ((string[]) dict["iii"])[0]);
231 Assert.AreEqual("5", ((string[]) dict["jjj"])[0]);
235 [Test]
236 public void PushingParser()
238 IDictionary dict = helper.CreateDict("name=value=aa");
240 Assert.IsNotNull(dict);
241 Assert.AreEqual(1, dict.Count);
243 foreach (String key in dict.Keys)
245 if (key.Equals("name"))
247 Assert.AreEqual("value=aa", dict["name"]);
249 else
251 Assert.Fail("unexpected key? " + key);
256 #region Testing test (set to Ignore presently)
258 private const int NumReps = 200000;
260 [Test, Ignore]
261 public void RepeatTest()
263 for (int i = 0; i < NumReps; i++)
265 ComplexDict_woChk();
269 [Test, Ignore]
270 public void RepeatTest2()
272 for (int i = 0; i < NumReps; i++)
274 ComplexDict2_woChk();
278 [Test, Ignore]
279 public void RepeatTest3()
281 for (int i = 0; i < NumReps; i++)
283 ComplexDictStatic();
287 #endregion