Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / RangeAttributeTest.cs
1 //
2 // RequiredAttributeTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2010 Novell, Inc. (http://novell.com/)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.ComponentModel.DataAnnotations;
31 using System.Text;
32
33 using NUnit.Framework;
34 using MonoTests.Common;
35
36 using DA = global::System.ComponentModel.DataAnnotations;
37
38 namespace MonoTests.System.ComponentModel.DataAnnotations
39 {
40         class RangeAttributePoker : DA.RangeAttribute
41         {
42                 public RangeAttributePoker (double min, double max)
43                         : base (min, max)
44                 { }
45
46                 public string GetErrorMessageString ()
47                 {
48                         return ErrorMessageString;
49                 }
50         }
51
52         [TestFixture]
53         public class RangeAttributeTest
54         {
55                 [Test]
56                 public void ErrorMessage ()
57                 {
58                         var attr = new RangeAttributePoker (-10.123D, 10.123D);
59
60                         Assert.IsNull (attr.ErrorMessage, "#A1-1");
61                         Assert.AreEqual ("The field {0} must be between {1} and {2}.", attr.GetErrorMessageString (), "#A1-2");
62                 }
63
64                 [Test]
65                 public void Constructor_Double_Double ()
66                 {
67                         var attr = new DA.RangeAttribute (-10.123D, 10.123D);
68
69                         Assert.IsNotNull (attr.Minimum, "#A1-1");
70                         Assert.IsNotNull (attr.Maximum, "#A1-2");
71
72                         Assert.AreEqual (typeof (double), attr.Minimum.GetType (), "#A2-1");
73                         Assert.AreEqual (typeof (double), attr.Maximum.GetType (), "#A2-2");
74
75                         Assert.AreEqual (-10.123D, attr.Minimum, "#A3-1");
76                         Assert.AreEqual (10.123D, attr.Maximum, "#A3-2");
77
78                         Assert.IsNotNull (attr.OperandType, "#A4-1");
79                         Assert.AreEqual (typeof (double), attr.OperandType, "#A4-2");
80                 }
81
82                 [Test]
83                 public void Constructor_Int_Int ()
84                 {
85                         var attr = new DA.RangeAttribute (-10, 10);
86
87                         Assert.IsNotNull (attr.Minimum, "#A1-1");
88                         Assert.IsNotNull (attr.Maximum, "#A1-2");
89
90                         Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A2-1");
91                         Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A2-2");
92
93                         Assert.AreEqual (-10, attr.Minimum, "#A3-1");
94                         Assert.AreEqual (10, attr.Maximum, "#A3-2");
95
96                         Assert.IsNotNull (attr.OperandType, "#A4-1");
97                         Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
98                 }
99
100                 [Test]
101                 public void Constructor_Type_String_String ()
102                 {
103                         var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
104
105                         Assert.IsNotNull (attr.Minimum, "#A1-1");
106                         Assert.IsNotNull (attr.Maximum, "#A1-2");
107 #if NET_4_0
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");
112 #else
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");
117 #endif
118                         Assert.IsNotNull (attr.OperandType, "#A4-1");
119                         Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
120                 }
121
122                 [Test]
123                 //LAMESPEC: documented to throw
124 #if !NET_4_0
125                 [ExpectedException (typeof (ArgumentNullException))]
126 #endif
127                 public void Constructor_Type_String_String_Null_Type ()
128                 {
129                         var attr = new DA.RangeAttribute (null, "-10", "10");
130
131                         Assert.IsNull (attr.OperandType, "#A1");
132                 }
133
134                 [Test]
135                 public void IsValid ()
136                 {
137                         var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
138
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");
145                         }, "#A1-5");
146                         Assert.IsTrue (attr.IsValid (null), "#A1-6");
147 #if NET_4_0
148                         attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
149                         AssertExtensions.Throws<Exception> (() => {
150                                 attr.IsValid ("0");
151                         }, "#A2-1");
152                         AssertExtensions.Throws<Exception> (() => {
153                                 attr.IsValid ("12");
154                         }, "#A2-2");
155                         AssertExtensions.Throws<Exception> (() => {
156                                 attr.IsValid ("zero");
157                         }, "#A2-3");
158
159                         attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
160                         AssertExtensions.Throws<InvalidOperationException> (() => {
161                                 attr.IsValid (null);
162                         }, "#A3-1");
163
164                         AssertExtensions.Throws<InvalidOperationException> (() => {
165                                 attr.IsValid (String.Empty);
166                         }, "#A3-2");
167
168                         AssertExtensions.Throws<InvalidOperationException> (() => {
169                                 // The type MonoTests.System.ComponentModel.DataAnnotations.RangeAttributeTest must implement System.IComparable.
170                                 attr.IsValid ("10");
171                         }, "#A3-3");
172
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.
176                                 attr.IsValid ("10");
177                         }, "#A4");
178
179                         attr = new DA.RangeAttribute (typeof (int), null, "10");
180                         AssertExtensions.Throws<InvalidOperationException> (() => {
181                                 // The minimum and maximum values must be set.
182                                 attr.IsValid (10);
183                         }, "#A5-1");
184
185                         attr = new DA.RangeAttribute (typeof (int), "10", null);
186                         AssertExtensions.Throws<InvalidOperationException> (() => {
187                                 // The minimum and maximum values must be set.
188                                 attr.IsValid (10);
189                         }, "#A5-2");
190
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");
194
195                         // IsValid appears to reassign Minimum and Maximum with objects of the OperandType type, converted from the original strings
196                         attr.IsValid (12);
197                         Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A7-1");
198                         Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A7-2");
199 #else
200                         AssertExtensions.Throws<Exception> (() => {
201                                 attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
202                         }, "#A2");
203
204                         AssertExtensions.Throws<ArgumentException> (() => {
205                                 attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
206                         }, "#A3");
207
208                         AssertExtensions.Throws<ArgumentNullException> (() => {
209                                 attr = new DA.RangeAttribute (null, "-10", "10");
210                         }, "#A4");
211
212                         AssertExtensions.Throws<NotSupportedException> (() => {
213                                 attr = new DA.RangeAttribute (typeof (int), null, "10");
214                         }, "#A5-1");
215
216                         AssertExtensions.Throws<NotSupportedException> (() => {
217                                 attr = new DA.RangeAttribute (typeof (int), "10", null);
218                         }, "#A5-2");
219
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");
223 #endif
224                 }
225
226                 [Test]
227                 public void FormatMessageString ()
228                 {
229                         var attr = new DA.RangeAttribute (-10, 10);
230
231                         Assert.AreEqual ("The field MyField must be between -10 and 10.", attr.FormatErrorMessage ("MyField"), "#A1");
232
233                         attr.ErrorMessage = "Param 0: {0}";
234                         Assert.AreEqual ("Param 0: MyField", attr.FormatErrorMessage ("MyField"), "#A2-1");
235 #if !NET_4_0
236                         attr = new DA.RangeAttribute (-10, 10);
237 #endif
238                         attr.ErrorMessage = "Param 0: {0}; Param 1: {1}";
239                         Assert.AreEqual ("Param 0: MyField; Param 1: -10", attr.FormatErrorMessage ("MyField"), "#A2-2");
240 #if !NET_4_0
241                         attr = new DA.RangeAttribute (-10, 10);
242 #endif
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");
245 #if !NET_4_0
246                         attr = new DA.RangeAttribute (-10, 10);
247 #endif
248                         attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}; Param 3: {3}";
249                         AssertExtensions.Throws<FormatException> (() => {
250                                 attr.FormatErrorMessage ("MyField");
251                         }, "#A2-1");
252                 }
253         }
254 }