1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
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
;
30 public class DictHelperTestCase
32 private DictHelper helper
;
37 helper
= new DictHelper();
38 Thread
.CurrentThread
.CurrentCulture
= CultureInfo
.InvariantCulture
;
42 public void EmptyDict()
44 IDictionary dict
= helper
.CreateDict();
46 Assert
.IsNotNull(dict
);
47 Assert
.AreEqual(0, dict
.Count
);
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"]);
70 Assert
.Fail("unexpected key? " + key
);
76 /// Pulled out of [Test] method, so it can be call in timeing time
77 /// w/o the overhead of the Asserts
79 /// <returns></returns>
80 public IDictionary
ComplexDict_woChk()
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(),
95 public void ComplexDict()
97 IDictionary dict
= ComplexDict_woChk();
102 /// Pulled out of [Test] method, so it can be call in timing test
103 /// w/o the overhead of the Asserts
105 /// <returns></returns>
106 public IDictionary
ComplexDict2_woChk()
111 IDictionary dict
= helper
.N("name", "value")
112 .N("other", "somethingelse")
124 public void ComplexDict2()
126 IDictionary dict
= ComplexDict2_woChk();
127 ComplexAsserts(dict
);
130 public IDictionary
ComplexDictStatic_woChk()
135 return DictHelper
.CreateN("name", "value")
136 .N("other", "somethingelse")
146 public void ComplexDictStatic()
148 IDictionary dict
= ComplexDictStatic_woChk();
149 ComplexAsserts(dict
);
152 public IDictionary
ComplexDictMixed_woChk()
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
);
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"]);
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");
202 nvc
.Add("CCC", "DDD");
203 nvc
.Add("EEE", "FFF=GGG");
205 nvc
.Add("iii", "3.1415");
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]);
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"]);
251 Assert
.Fail("unexpected key? " + key
);
256 #region Testing test (set to Ignore presently)
258 private const int NumReps
= 200000;
261 public void RepeatTest()
263 for (int i
= 0; i
< NumReps
; i
++)
270 public void RepeatTest2()
272 for (int i
= 0; i
< NumReps
; i
++)
274 ComplexDict2_woChk();
279 public void RepeatTest3()
281 for (int i
= 0; i
< NumReps
; i
++)