1 // Copyright 2004-2007 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
.Routing
17 using System
.Collections
;
18 using Castle
.MonoRail
.Framework
.Routing
;
19 using NUnit
.Framework
;
22 public class RegexRuleTestCase
25 public void ShouldMatchAndGenerateAnInformativeResult()
27 RegexRule rule
= RegexRule
.Build("ProductById", "product/(?<id>\\d+)", typeof(ProductController
), "View");
29 RouteMatch match
= new RouteMatch(typeof(ProductController
), "name", "view");
31 Assert
.IsFalse(rule
.Matches("localhost", "", "product/", new RouteMatch(typeof(ProductController
), "name", "view")));
32 Assert
.IsFalse(rule
.Matches("localhost", "", "product/iPod", new RouteMatch(typeof(ProductController
), "name", "view")));
33 Assert
.IsTrue(rule
.Matches("localhost", "", "product/1", match
));
35 Assert
.AreEqual(0, match
.Literals
.Count
);
36 Assert
.AreEqual(2, match
.Parameters
.Count
);
37 Assert
.AreEqual("1", match
.Parameters
["id"]);
41 public void ShouldMatchNamedGroups()
43 RegexRule rule
= RegexRule
.Build("ProductById", "product/(?<id>\\d+)/(?<page>\\d+)", typeof(ProductController
), "View");
45 RouteMatch match
= new RouteMatch(typeof(ProductController
), "name", "view");
47 Assert
.IsFalse(rule
.Matches("localhost", "", "product/", new RouteMatch(typeof(ProductController
), "name", "view")));
48 Assert
.IsFalse(rule
.Matches("localhost", "", "product/iPod", new RouteMatch(typeof(ProductController
), "name", "view")));
49 Assert
.IsFalse(rule
.Matches("localhost", "", "product/1", new RouteMatch(typeof(ProductController
), "name", "view")));
50 Assert
.IsTrue(rule
.Matches("localhost", "", "product/12/10", match
));
52 Assert
.AreEqual(0, match
.Literals
.Count
);
53 Assert
.AreEqual(3, match
.Parameters
.Count
);
54 Assert
.AreEqual("12", match
.Parameters
["id"]);
55 Assert
.AreEqual("10", match
.Parameters
["page"]);
58 public class ProductController
: Controller { }