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");
107 Assert.AreEqual (typeof (string), attr.Minimum.GetType (), "#A2-1");
108 Assert.AreEqual (typeof (string), attr.Maximum.GetType (), "#A2-2");
109 Assert.AreEqual ("-10", attr.Minimum, "#A3-1");
110 Assert.AreEqual ("10", attr.Maximum, "#A3-2");
111 Assert.IsNotNull (attr.OperandType, "#A4-1");
112 Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
116 //LAMESPEC: documented to throw
117 public void Constructor_Type_String_String_Null_Type ()
119 var attr = new DA.RangeAttribute (null, "-10", "10");
121 Assert.IsNull (attr.OperandType, "#A1");
125 public void IsValid ()
127 var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
129 Assert.IsTrue (attr.IsValid ("0"), "#A1-1");
130 Assert.IsFalse (attr.IsValid ("12"), "#A1-2");
131 Assert.IsTrue (attr.IsValid (null), "#A1-3");
132 Assert.IsTrue (attr.IsValid (String.Empty), "#A1-4");
133 AssertExtensions.Throws <Exception> (() => {
134 attr.IsValid ("zero");
136 Assert.IsTrue (attr.IsValid (null), "#A1-6");
137 attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
138 AssertExtensions.Throws<Exception> (() => {
141 AssertExtensions.Throws<Exception> (() => {
144 AssertExtensions.Throws<Exception> (() => {
145 attr.IsValid ("zero");
148 attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
149 AssertExtensions.Throws<InvalidOperationException> (() => {
153 AssertExtensions.Throws<InvalidOperationException> (() => {
154 attr.IsValid (String.Empty);
157 AssertExtensions.Throws<InvalidOperationException> (() => {
158 // The type MonoTests.System.ComponentModel.DataAnnotations.RangeAttributeTest must implement System.IComparable.
162 attr = new DA.RangeAttribute (null, "-10", "10");
163 AssertExtensions.Throws<InvalidOperationException> (() => {
164 // The OperandType must be set when strings are used for minimum and maximum values.
168 attr = new DA.RangeAttribute (typeof (int), null, "10");
169 AssertExtensions.Throws<InvalidOperationException> (() => {
170 // The minimum and maximum values must be set.
174 attr = new DA.RangeAttribute (typeof (int), "10", null);
175 AssertExtensions.Throws<InvalidOperationException> (() => {
176 // The minimum and maximum values must be set.
180 attr = new DA.RangeAttribute (typeof (int), "-10", "10");
181 Assert.AreEqual (typeof (string), attr.Minimum.GetType (), "#A6-1");
182 Assert.AreEqual (typeof (string), attr.Maximum.GetType (), "#A6-2");
184 // IsValid appears to reassign Minimum and Maximum with objects of the OperandType type, converted from the original strings
186 Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A7-1");
187 Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A7-2");
191 public void FormatMessageString ()
193 var attr = new DA.RangeAttribute (-10, 10);
195 Assert.AreEqual ("The field MyField must be between -10 and 10.", attr.FormatErrorMessage ("MyField"), "#A1");
197 attr.ErrorMessage = "Param 0: {0}";
198 Assert.AreEqual ("Param 0: MyField", attr.FormatErrorMessage ("MyField"), "#A2-1");
199 attr.ErrorMessage = "Param 0: {0}; Param 1: {1}";
200 Assert.AreEqual ("Param 0: MyField; Param 1: -10", attr.FormatErrorMessage ("MyField"), "#A2-2");
201 attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}";
202 Assert.AreEqual ("Param 0: MyField; Param 1: -10; Param 2: 10", attr.FormatErrorMessage ("MyField"), "#A2-3");
203 attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}; Param 3: {3}";
204 AssertExtensions.Throws<FormatException> (() => {
205 attr.FormatErrorMessage ("MyField");