PageRenderTime 201ms CodeModel.GetById 191ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Manos.Tests/Manos.Routing/RegexMatchOperationTest.cs

http://github.com/jacksonh/manos
C# | 131 lines | 84 code | 24 blank | 23 comment | 0 complexity | 89402a78c0099fe9898ac3f5dc91e70d 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
 25
 26using System;
 27using System.Text.RegularExpressions;
 28
 29
 30using NUnit.Framework;
 31
 32using Manos.Routing;
 33using System.Collections.Specialized;
 34using Manos.ShouldExt;
 35
 36using Manos.Collections;
 37
 38namespace Manos.Routing.Tests
 39{
 40
 41
 42	[TestFixture()]
 43	public class RegexMatchOperationTest
 44	{
 45
 46		[Test]
 47		public void CtorTest ()
 48		{
 49			Should.Throw<ArgumentNullException> (() => new RegexMatchOperation (null), "a1");
 50			
 51			var r = new Regex (".*");
 52			var op = new RegexMatchOperation (r);
 53			Assert.AreEqual (r, op.Regex, "a2");
 54		}
 55		
 56		[Test]
 57		public void SetRegexTest ()
 58		{
 59			var r = new Regex (".*");
 60			var op = new RegexMatchOperation (r);
 61			Assert.AreEqual (r, op.Regex, "a1");
 62			
 63			Should.Throw<ArgumentNullException> (() => op.Regex = null, "a2");
 64			
 65			r = new Regex ("foo");
 66			op.Regex = r;
 67			Assert.AreEqual (r, op.Regex, "a3");
 68		}
 69		
 70		[Test()]
 71		public void EmptyRegexTest ()
 72		{
 73			var r = new Regex ("");
 74			var op = new RegexMatchOperation (r);
 75			var col = new DataDictionary ();
 76			int end;
 77
 78			bool m = op.IsMatch ("", 0, out col, out end);
 79			
 80			Assert.IsTrue (m, "a1");
 81			Assert.AreEqual (0, end, "a2");
 82		}
 83		
 84		[Test]
 85		public void SimpleRegexTest ()
 86		{
 87			var r = new Regex (".og");
 88			var op = new RegexMatchOperation (r);
 89			var col = new DataDictionary ();
 90			int end;
 91			bool m;
 92			
 93			m = op.IsMatch ("dog", 0, out col, out end);
 94			Assert.IsTrue (m, "a1");
 95			Assert.AreEqual (0, col.Count, "a2");
 96			Assert.AreEqual (3, end, "a3");
 97			
 98			m = op.IsMatch ("log", 0, out col, out end);
 99			Assert.IsTrue (m, "a4");
100			Assert.AreEqual (0, col.Count, "a5");
101			Assert.AreEqual (3, end, "a6");
102			
103			m = op.IsMatch ("fox", 0, out col, out end);
104			Assert.IsFalse (m, "a7");
105			Assert.IsNull (col, "a8");
106			Assert.AreEqual (0, end, "a9");
107		}
108		
109		[Test]
110		public void SimpleGroupTest ()
111		{
112			var r = new Regex ("-(?<foo>.*?)-");
113			var op = new RegexMatchOperation (r);
114			var col = new DataDictionary ();
115			int end;
116			bool m;
117			
118			m = op.IsMatch ("-manos-", 0, out col, out end);
119			Assert.IsTrue (m, "a1");
120			Assert.AreEqual (1, col.Count, "a2");
121			Assert.AreEqual ("manos", col ["foo"], "a3");
122			Assert.AreEqual (7, end, "a4");
123			
124			col = new DataDictionary ();
125			m = op.IsMatch ("manos-", 0, out col, out end);
126			Assert.IsFalse (m, "a5");
127			Assert.IsNull (col, "a6");
128			Assert.AreEqual (0, end, "a7");
129		}
130	}
131}