2 // RequiredAttributeTest.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
33 using NUnit.Framework;
34 using MonoTests.Common;
36 using DA = global::System.ComponentModel.DataAnnotations;
38 namespace MonoTests.System.ComponentModel.DataAnnotations
40 class RangeAttributePoker : DA.RangeAttribute
42 public RangeAttributePoker (double min, double max)
46 public string GetErrorMessageString ()
48 return ErrorMessageString;
53 public class RangeAttributeTest
56 public void ErrorMessage ()
58 var attr = new RangeAttributePoker (-10.123D, 10.123D);
60 Assert.IsNull (attr.ErrorMessage, "#A1-1");
61 Assert.AreEqual ("The field {0} must be between {1} and {2}.", attr.GetErrorMessageString (), "#A1-2");
65 public void Constructor_Double_Double ()
67 var attr = new DA.RangeAttribute (-10.123D, 10.123D);
69 Assert.IsNotNull (attr.Minimum, "#A1-1");
70 Assert.IsNotNull (attr.Maximum, "#A1-2");
72 Assert.AreEqual (typeof (double), attr.Minimum.GetType (), "#A2-1");
73 Assert.AreEqual (typeof (double), attr.Maximum.GetType (), "#A2-2");
75 Assert.AreEqual (-10.123D, attr.Minimum, "#A3-1");
76 Assert.AreEqual (10.123D, attr.Maximum, "#A3-2");
78 Assert.IsNotNull (attr.OperandType, "#A4-1");
79 Assert.AreEqual (typeof (double), attr.OperandType, "#A4-2");
83 public void Constructor_Int_Int ()
85 var attr = new DA.RangeAttribute (-10, 10);
87 Assert.IsNotNull (attr.Minimum, "#A1-1");
88 Assert.IsNotNull (attr.Maximum, "#A1-2");
90 Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A2-1");
91 Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A2-2");
93 Assert.AreEqual (-10, attr.Minimum, "#A3-1");
94 Assert.AreEqual (10, attr.Maximum, "#A3-2");
96 Assert.IsNotNull (attr.OperandType, "#A4-1");
97 Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
101 public void Constructor_Type_String_String ()
103 var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
105 Assert.IsNotNull (attr.Minimum, "#A1-1");
106 Assert.IsNotNull (attr.Maximum, "#A1-2");
108 Assert.AreEqual (typeof (string), attr.Minimum.GetType (), "#A2-1");
109 Assert.AreEqual (typeof (string), attr.Maximum.GetType (), "#A2-2");
110 Assert.AreEqual ("-10", attr.Minimum, "#A3-1");
111 Assert.AreEqual ("10", attr.Maximum, "#A3-2");
113 Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A2-1");
114 Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A2-2");
115 Assert.AreEqual (-10, attr.Minimum, "#A3-1");
116 Assert.AreEqual (10, attr.Maximum, "#A3-2");
118 Assert.IsNotNull (attr.OperandType, "#A4-1");
119 Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
123 //LAMESPEC: documented to throw
125 [ExpectedException (typeof (ArgumentNullException))]
127 public void Constructor_Type_String_String_Null_Type ()
129 var attr = new DA.RangeAttribute (null, "-10", "10");
131 Assert.IsNull (attr.OperandType, "#A1");
135 public void IsValid ()
137 var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
139 Assert.IsTrue (attr.IsValid ("0"), "#A1-1");
140 Assert.IsFalse (attr.IsValid ("12"), "#A1-2");
141 Assert.IsTrue (attr.IsValid (null), "#A1-3");
142 Assert.IsTrue (attr.IsValid (String.Empty), "#A1-4");
143 AssertExtensions.Throws <Exception> (() => {
144 attr.IsValid ("zero");
146 Assert.IsTrue (attr.IsValid (null), "#A1-6");
148 attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
149 AssertExtensions.Throws<Exception> (() => {
152 AssertExtensions.Throws<Exception> (() => {
155 AssertExtensions.Throws<Exception> (() => {
156 attr.IsValid ("zero");
159 attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
160 AssertExtensions.Throws<InvalidOperationException> (() => {
164 AssertExtensions.Throws<InvalidOperationException> (() => {
165 attr.IsValid (String.Empty);
168 AssertExtensions.Throws<InvalidOperationException> (() => {
169 // The type MonoTests.System.ComponentModel.DataAnnotations.RangeAttributeTest must implement System.IComparable.
173 attr = new DA.RangeAttribute (null, "-10", "10");
174 AssertExtensions.Throws<InvalidOperationException> (() => {
175 // The OperandType must be set when strings are used for minimum and maximum values.
179 attr = new DA.RangeAttribute (typeof (int), null, "10");
180 AssertExtensions.Throws<InvalidOperationException> (() => {
181 // The minimum and maximum values must be set.
185 attr = new DA.RangeAttribute (typeof (int), "10", null);
186 AssertExtensions.Throws<InvalidOperationException> (() => {
187 // The minimum and maximum values must be set.
191 attr = new DA.RangeAttribute (typeof (int), "-10", "10");
192 Assert.AreEqual (typeof (string), attr.Minimum.GetType (), "#A6-1");
193 Assert.AreEqual (typeof (string), attr.Maximum.GetType (), "#A6-2");
195 // IsValid appears to reassign Minimum and Maximum with objects of the OperandType type, converted from the original strings
197 Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A7-1");
198 Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A7-2");
200 AssertExtensions.Throws<Exception> (() => {
201 attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
204 AssertExtensions.Throws<ArgumentException> (() => {
205 attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
208 AssertExtensions.Throws<ArgumentNullException> (() => {
209 attr = new DA.RangeAttribute (null, "-10", "10");
212 AssertExtensions.Throws<NotSupportedException> (() => {
213 attr = new DA.RangeAttribute (typeof (int), null, "10");
216 AssertExtensions.Throws<NotSupportedException> (() => {
217 attr = new DA.RangeAttribute (typeof (int), "10", null);
220 attr = new DA.RangeAttribute (typeof (int), "-10", "10");
221 Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A6-1");
222 Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A6-2");
227 public void FormatMessageString ()
229 var attr = new DA.RangeAttribute (-10, 10);
231 Assert.AreEqual ("The field MyField must be between -10 and 10.", attr.FormatErrorMessage ("MyField"), "#A1");
233 attr.ErrorMessage = "Param 0: {0}";
234 Assert.AreEqual ("Param 0: MyField", attr.FormatErrorMessage ("MyField"), "#A2-1");
236 attr = new DA.RangeAttribute (-10, 10);
238 attr.ErrorMessage = "Param 0: {0}; Param 1: {1}";
239 Assert.AreEqual ("Param 0: MyField; Param 1: -10", attr.FormatErrorMessage ("MyField"), "#A2-2");
241 attr = new DA.RangeAttribute (-10, 10);
243 attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}";
244 Assert.AreEqual ("Param 0: MyField; Param 1: -10; Param 2: 10", attr.FormatErrorMessage ("MyField"), "#A2-3");
246 attr = new DA.RangeAttribute (-10, 10);
248 attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}; Param 3: {3}";
249 AssertExtensions.Throws<FormatException> (() => {
250 attr.FormatErrorMessage ("MyField");