Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / Helpers / Validations / FormValidationTestCase.cs
blobbe5747fe9d6baa273114740d6a2630851c8e9e25
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.Validations
17 using System.Globalization;
18 using System.Threading;
19 using Castle.Components.Validator;
20 using Castle.MonoRail.Framework.Helpers;
21 using Castle.MonoRail.Framework.Tests.Controllers;
22 using NUnit.Framework;
24 [TestFixture]
25 public class FormValidationTestCase
27 private FormHelper helper;
28 private ModelWithValidation model;
30 [SetUp]
31 public void Init()
33 CultureInfo en = CultureInfo.CreateSpecificCulture("en");
35 Thread.CurrentThread.CurrentCulture = en;
36 Thread.CurrentThread.CurrentUICulture = en;
38 helper = new FormHelper();
39 model = new ModelWithValidation();
41 HomeController controller = new HomeController();
42 ControllerContext controllerContext = new ControllerContext();
44 controllerContext.PropertyBag.Add("model", model);
46 helper.SetController(controller, controllerContext);
49 [Test]
50 public void ValidationIsGeneratedForModel()
52 helper.FormTag(DictHelper.Create("noaction=true"));
54 Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
55 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
56 "title=\"This is a required field.\" />", helper.TextField("model.nonemptyfield"));
58 Assert.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
59 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
60 "title=\"Email doesnt look right.\" />", helper.TextField("model.emailfield"));
62 // Attribute order cannot be guaranted, so this test may fail ocasionally
63 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
64 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
65 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("model.nonemptyemailfield"));
67 helper.EndFormTag();
70 [Test]
71 public void UsingScopes()
73 helper.FormTag(DictHelper.Create("noaction=true"));
74 helper.Push("model");
76 Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyfield\" " +
77 "name=\"model.nonemptyfield\" value=\"\" class=\"required\" " +
78 "title=\"This is a required field.\" />", helper.TextField("nonemptyfield"));
80 Assert.AreEqual("<input type=\"text\" id=\"model_emailfield\" " +
81 "name=\"model.emailfield\" value=\"\" class=\"validate-email\" " +
82 "title=\"Email doesnt look right.\" />", helper.TextField("emailfield"));
84 // Attribute order cannot be guaranted, so this test may fail ocasionally
85 // Assert.AreEqual("<input type=\"text\" id=\"model_nonemptyemailfield\" " +
86 // "name=\"model.nonemptyemailfield\" value=\"\" class=\"validate-email required\" " +
87 // "title=\"Please enter a valid email address. For example fred@domain.com, This is a required field\" />", helper.TextField("nonemptyemailfield"));
89 helper.Pop();
90 helper.EndFormTag();
93 [Test]
94 public void ValidationForSelects()
96 helper.FormTag(DictHelper.Create("noaction=true"));
98 Assert.AreEqual("<select id=\"model_city\" " +
99 "name=\"model.city\" class=\"validate-selection\" " +
100 "title=\"This is a required field.\" >\r\n" +
101 "<option value=\"0\">---</option>\r\n" +
102 "<option value=\"Sao Paulo\">Sao Paulo</option>\r\n" +
103 "<option value=\"Sao Carlos\">Sao Carlos</option>\r\n" +
104 "</select>",
105 helper.Select("model.city",
106 new string[] { "Sao Paulo", "Sao Carlos" }, DictHelper.Create("firstoption=---")));
108 helper.EndFormTag();
111 [Test]
112 public void ValidationAreInherited()
114 helper.FormTag(DictHelper.Create("noaction=true"));
116 Assert.AreEqual("<select id=\"model_city_id\" " +
117 "name=\"model.city.id\" class=\"validate-selection\" " +
118 "title=\"This is a required field.\" >\r\n" +
119 "<option value=\"0\">---</option>\r\n" +
120 "<option value=\"1\">1</option>\r\n" +
121 "<option value=\"2\">2</option>\r\n" +
122 "</select>",
123 helper.Select("model.city.id",
124 new string[] { "1", "2" }, DictHelper.Create("firstoption=---")));
126 helper.EndFormTag();
130 public class ModelWithValidation
132 private string nonEmptyField;
133 private string emailField;
134 private string nonEmptyEmailField;
135 private string city;
136 private Country country;
138 [ValidateNonEmpty]
139 public Country Country
141 get { return country; }
142 set { country = value; }
145 [ValidateNonEmpty]
146 public string City
148 get { return city; }
149 set { city = value; }
152 [ValidateNonEmpty]
153 public string NonEmptyField
155 get { return nonEmptyField; }
156 set { nonEmptyField = value; }
159 [ValidateEmail("Email doesnt look right")]
160 public string EmailField
162 get { return emailField; }
163 set { emailField = value; }
166 [ValidateNonEmpty, ValidateEmail]
167 public string NonEmptyEmailField
169 get { return nonEmptyEmailField; }
170 set { nonEmptyEmailField = value; }
174 public class Country
176 private int id;
178 public int Id
180 get { return id; }
181 set { id = value; }