PageRenderTime 64ms CodeModel.GetById 41ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Manos.Tests/Manos.Http/RequestDataDictionaryTest.cs

http://github.com/jacksonh/manos
C# | 400 lines | 284 code | 93 blank | 23 comment | 0 complexity | f487701ca83f9ab97cca3caff6ffe47a MD5 | raw file
  1//
  2// Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3//
  4// Permission is hereby granted, free of charge, to any person obtaining
  5// a copy of this software and associated documentation files (the
  6// "Software"), to deal in the Software without restriction, including
  7// without limitation the rights to use, copy, modify, merge, publish,
  8// distribute, sublicense, and/or sell copies of the Software, and to
  9// permit persons to whom the Software is furnished to do so, subject to
 10// the following conditions:
 11// 
 12// The above copyright notice and this permission notice shall be
 13// included in all copies or substantial portions of the Software.
 14// 
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 22//
 23//
 24
 25using System;
 26using System.Collections;
 27using System.Collections.Generic;
 28using System.Collections.Specialized;
 29
 30using NUnit.Framework;
 31using Manos.Collections;
 32
 33namespace Manos.Server.Tests
 34{
 35	[TestFixture()]
 36	public class RequestDataDictionaryTest
 37	{
 38		[Test()]
 39		public void Count_NoItems_ReturnsZero ()
 40		{
 41			var rdd = new RequestDataDictionary ();
 42			
 43			Assert.AreEqual (0, rdd.Count);
 44		}
 45		
 46		[Test]
 47		public void Count_SingleItem_ReturnsOne ()
 48		{
 49			var rdd = new RequestDataDictionary ();
 50			
 51			rdd.Add ("foobar", "baz");
 52			
 53			Assert.AreEqual (1, rdd.Count);
 54		}
 55		
 56		[Test]
 57		public void Count_SingleItemInCollection_ReturnsOne ()
 58		{
 59			var nvc = new DataDictionary ();
 60			var rdd = new RequestDataDictionary ();
 61			
 62			nvc.Add ("foobar", "baz");
 63			rdd.AddCollection (nvc);
 64			
 65			Assert.AreEqual (1, rdd.Count);
 66		}
 67		
 68		[Test]
 69		public void Count_ItemInCollectionAndItemAdded_ReturnsTwo ()
 70		{
 71			var nvc = new DataDictionary ();
 72			var rdd = new RequestDataDictionary ();
 73			
 74			nvc.Add ("foobar", "baz");
 75			rdd.AddCollection (nvc);
 76			rdd.Add ("foobar", "baz");
 77			
 78			Assert.AreEqual (2, rdd.Count);
 79		}
 80		
 81		[Test]
 82		public void Clear_SingleItemAdded_SetsCountZero ()
 83		{
 84			var rdd = new RequestDataDictionary ();
 85
 86			rdd.Add ("foobar", "baz");
 87			rdd.Clear ();
 88			
 89			Assert.AreEqual (0, rdd.Count);
 90		}
 91		
 92		[Test]
 93		public void Clear_ItemInCollection_SetsCountZero ()
 94		{
 95			var nvc = new DataDictionary ();
 96			var rdd = new RequestDataDictionary ();
 97			
 98			nvc.Add ("foobar", "baz");
 99			rdd.AddCollection (nvc);
100			
101			rdd.Clear ();
102			Assert.AreEqual (0, rdd.Count);
103		}
104		
105		[Test]
106		public void Clear_ItemInCollectionAndItemAdded_SetsCountZero ()
107		{
108			var nvc = new DataDictionary ();
109			var rdd = new RequestDataDictionary ();
110			
111			nvc.Add ("foobar", "baz");
112			rdd.AddCollection (nvc);
113			rdd.Add ("foobar", "baz");
114			
115			rdd.Clear ();
116			
117			Assert.AreEqual (0, rdd.Count);
118		}
119		
120		[Test]
121		public void Clear_SingleItemAdded_RemovesItem ()
122		{
123			var rdd = new RequestDataDictionary ();
124
125			rdd.Add ("foobar", "baz");
126			rdd.Clear ();
127			
128			string dummy;
129			bool has_item = rdd.ContainsKey ("foobar");
130			
131			Assert.IsFalse (has_item);
132		}
133		
134		[Test]
135		public void Clear_ItemInCollection_RemovesItem ()
136		{
137			var nvc = new DataDictionary ();
138			var rdd = new RequestDataDictionary ();
139			
140			nvc.Add ("foobar", "baz");
141			rdd.AddCollection (nvc);
142			
143			rdd.Clear ();
144			
145			bool has_item = rdd.ContainsKey ("foobar");
146			
147			Assert.IsFalse (has_item);
148		}
149		
150		[Test]
151		public void Clear_ItemInCollectionAndItemAdded_RemovesItem ()
152		{
153			var nvc = new DataDictionary ();
154			var rdd = new RequestDataDictionary ();
155			
156			nvc.Add ("foobar", "baz");
157			rdd.AddCollection (nvc);
158			rdd.Add ("foobar", "baz");
159			
160			rdd.Clear ();
161			
162			bool has_item = rdd.ContainsKey ("foobar");
163			
164			Assert.IsFalse (has_item);
165		}
166		
167		[Test]
168		public void ContainsKey_NoItems_ReturnsFalse ()
169		{
170			var rdd = new RequestDataDictionary ();
171
172			bool has_item = rdd.ContainsKey ("foobar");
173			Assert.IsFalse (has_item);
174		}
175		
176		[Test]
177		public void ContainsKey_ItemAdded_ReturnsTrue ()
178		{
179			var rdd = new RequestDataDictionary ();
180			
181			rdd.Add ("foobar", "baz");
182			
183			bool has_item = rdd.ContainsKey ("foobar");
184			Assert.IsTrue (has_item);
185		}
186		
187		[Test]
188		public void ContainsKey_ItemInCollection_ReturnsTrue ()
189		{
190			var nvc = new DataDictionary ();
191			var rdd = new RequestDataDictionary ();
192			
193			nvc.Add ("foobar", "baz");
194			rdd.AddCollection (nvc);
195			
196			bool has_item = rdd.ContainsKey ("foobar");
197			Assert.IsTrue (has_item);
198		}
199		
200		[Test]
201		public void Contains_NoItemsAdded_ReturnsFalse ()
202		{
203			var rdd = new RequestDataDictionary ();
204			var pair = new KeyValuePair<string, string> ("foobar", "baz");
205			
206			bool has_item = rdd.Contains (pair);
207			Assert.IsFalse (has_item);
208		}
209		
210		[Test]
211		public void Contains_ItemAddedKeyMatchesValueDoesnt_ReturnsFalse ()
212		{
213			var rdd = new RequestDataDictionary ();
214			var pair = new KeyValuePair<string, string> ("foobar", "baz");
215			
216			rdd.Add ("foobar", "nope");
217			
218			bool has_item = rdd.Contains (pair);
219			Assert.IsFalse (has_item);
220		}
221		
222		[Test]
223		public void Contains_ItemAddedKeyDoesNotMatchValueDoes_ReturnsFalse ()
224		{
225			var rdd = new RequestDataDictionary ();
226			var pair = new KeyValuePair<string, string> ("foobar", "baz");
227			
228			rdd.Add ("nope", "baz");
229			
230			bool has_item = rdd.Contains (pair);
231			Assert.IsFalse (has_item);
232		}
233		
234		[Test]
235		public void Contains_ItemAddedKeyAndValueMatch_ReturnsTrue ()
236		{
237			var rdd = new RequestDataDictionary ();
238			var pair = new KeyValuePair<string, string> ("foobar", "baz");
239			
240			rdd.Add ("foobar", "baz");
241			
242			bool has_item = rdd.Contains (pair);
243			Assert.IsTrue (has_item);
244		}
245		
246		[Test]
247		public void Contains_ItemInCollectionKeyAndValuesMatch_ReturnsTrue ()
248		{
249			var nvc = new DataDictionary ();
250			var rdd = new RequestDataDictionary ();
251			var pair = new KeyValuePair<string,string> ("foobar", "baz");
252			
253			nvc.Add ("foobar", "baz");
254			rdd.AddCollection (nvc);
255			
256			bool has_item = rdd.Contains (pair);
257			Assert.IsTrue (has_item);
258		}
259		
260		[Test]
261		public void Contains_MultipleItemsInCollectionFirstKeyAndValuesMatch_ReturnsTrue ()
262		{
263			var nvc = new DataDictionary ();
264			var rdd = new RequestDataDictionary ();
265			var pair = new KeyValuePair<string,string> ("foobar", "baz");
266			
267			nvc.Add ("foobar", "baz");
268			nvc.Add ("foobar", "blah");
269			rdd.AddCollection (nvc);
270			
271			bool has_item = rdd.Contains (pair);
272			Assert.IsTrue (has_item);
273		}
274		
275		[Test]
276		public void Contains_MultipleItemsInCollectionLastKeyAndValuesMatch_ReturnsTrue ()
277		{
278			var nvc = new DataDictionary ();
279			var rdd = new RequestDataDictionary ();
280			var pair = new KeyValuePair<string,string> ("foobar", "baz");
281			
282			nvc.Add ("foobar", "blah");
283			nvc.Add ("foobar", "baz");
284			rdd.AddCollection (nvc);
285			
286			bool has_item = rdd.Contains (pair);
287			Assert.IsTrue (has_item);
288		}
289		
290		[Test]
291		public void Contains_MultipleItemsInCollectionMiddleKeyAndValuesMatch_ReturnsTrue ()
292		{
293			var nvc = new DataDictionary ();
294			var rdd = new RequestDataDictionary ();
295			var pair = new KeyValuePair<string,string> ("foobar", "baz");
296			
297			nvc.Add ("foobar", "blah");
298			nvc.Add ("foobar", "baz");
299			nvc.Add ("foobar", "burrah");
300			rdd.AddCollection (nvc);
301			
302			bool has_item = rdd.Contains (pair);
303			Assert.IsTrue (has_item);
304		}
305		
306		[Test]
307		public void Contains_MultipleItemsWithPartialStartMatchesInCollection_ReturnsFalse ()
308		{
309			var nvc = new DataDictionary ();
310			var rdd = new RequestDataDictionary ();
311			var pair = new KeyValuePair<string,string> ("foobar", "baz");
312			
313			nvc.Add ("foobar", "baza");
314			nvc.Add ("foobar", "baza");
315			nvc.Add ("foobar", "baza");
316			rdd.AddCollection (nvc);
317			
318			bool has_item = rdd.Contains (pair);
319			Assert.IsFalse (has_item);
320		}
321		
322		[Test]
323		public void Contains_MultipleItemsWithPartialEndMatchesInCollection_ReturnsFalse ()
324		{
325			var nvc = new DataDictionary ();
326			var rdd = new RequestDataDictionary ();
327			var pair = new KeyValuePair<string,string> ("foobar", "baz");
328			
329			nvc.Add ("foobar", "abaz");
330			nvc.Add ("foobar", "abaz");
331			nvc.Add ("foobar", "abaz");
332			rdd.AddCollection (nvc);
333			
334			bool has_item = rdd.Contains (pair);
335			Assert.IsFalse (has_item);
336		}
337		
338		[Test]
339		public void TryGetValue_NoValuesAdded_ReturnsFalse ()
340		{
341			var rdd = new RequestDataDictionary ();
342			
343			string dummy;
344			bool has_value = rdd.TryGetValue ("foobar", out dummy);
345			
346			Assert.IsFalse (has_value);
347		}
348		
349		[Test]
350		public void TryGetValue_NoValuesAdded_SetsValueNull ()
351		{
352			var rdd = new RequestDataDictionary ();
353			
354			string the_data;
355			rdd.TryGetValue ("foobar", out the_data);
356			
357			Assert.IsNull (the_data);
358		}
359		
360		[Test]
361		public void TryGetValue_NonMatchingValueAdded_ReturnsFalse ()
362		{
363			var rdd = new RequestDataDictionary ();
364			
365			rdd.Add ("blizbar", "baz");
366			
367			string dummy;
368			bool has_value = rdd.TryGetValue ("foobar", out dummy);
369			
370			Assert.IsFalse (has_value);
371		}
372		
373		[Test]
374		public void TryGetValue_SingleAddedValueMatches_ReturnsTrue ()
375		{
376			var rdd = new RequestDataDictionary ();
377			
378			rdd.Add ("foobar", "baz");
379			
380			string dummy;
381			bool has_value = rdd.TryGetValue ("foobar", out dummy);
382			
383			Assert.IsTrue (has_value);
384		}
385		
386		[Test]
387		public void TryGetValue_SingleAddedValueMatches_SetsData ()
388		{
389			var rdd = new RequestDataDictionary ();
390			
391			rdd.Add ("foobar", "baz");
392			
393			string the_data;
394			rdd.TryGetValue ("foobar", out the_data);
395			
396			Assert.AreEqual ("baz", the_data);
397		}
398	}
399}
400