Merge pull request #1870 from saper/langinfo_h
[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                         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");
113                 }
114
115                 [Test]
116                 //LAMESPEC: documented to throw
117                 public void Constructor_Type_String_String_Null_Type ()
118                 {
119                         var attr = new DA.RangeAttribute (null, "-10", "10");
120
121                         Assert.IsNull (attr.OperandType, "#A1");
122                 }
123
124                 [Test]
125                 public void IsValid ()
126                 {
127                         var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
128
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");
135                         }, "#A1-5");
136                         Assert.IsTrue (attr.IsValid (null), "#A1-6");
137                         attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
138                         AssertExtensions.Throws<Exception> (() => {
139                                 attr.IsValid ("0");
140                         }, "#A2-1");
141                         AssertExtensions.Throws<Exception> (() => {
142                                 attr.IsValid ("12");
143                         }, "#A2-2");
144                         AssertExtensions.Throws<Exception> (() => {
145                                 attr.IsValid ("zero");
146                         }, "#A2-3");
147
148                         attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
149                         AssertExtensions.Throws<InvalidOperationException> (() => {
150                                 attr.IsValid (null);
151                         }, "#A3-1");
152
153                         AssertExtensions.Throws<InvalidOperationException> (() => {
154                                 attr.IsValid (String.Empty);
155                         }, "#A3-2");
156
157                         AssertExtensions.Throws<InvalidOperationException> (() => {
158                                 // The type MonoTests.System.ComponentModel.DataAnnotations.RangeAttributeTest must implement System.IComparable.
159                                 attr.IsValid ("10");
160                         }, "#A3-3");
161
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.
165                                 attr.IsValid ("10");
166                         }, "#A4");
167
168                         attr = new DA.RangeAttribute (typeof (int), null, "10");
169                         AssertExtensions.Throws<InvalidOperationException> (() => {
170                                 // The minimum and maximum values must be set.
171                                 attr.IsValid (10);
172                         }, "#A5-1");
173
174                         attr = new DA.RangeAttribute (typeof (int), "10", null);
175                         AssertExtensions.Throws<InvalidOperationException> (() => {
176                                 // The minimum and maximum values must be set.
177                                 attr.IsValid (10);
178                         }, "#A5-2");
179
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");
183
184                         // IsValid appears to reassign Minimum and Maximum with objects of the OperandType type, converted from the original strings
185                         attr.IsValid (12);
186                         Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A7-1");
187                         Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A7-2");
188                 }
189
190                 [Test]
191                 public void FormatMessageString ()
192                 {
193                         var attr = new DA.RangeAttribute (-10, 10);
194
195                         Assert.AreEqual ("The field MyField must be between -10 and 10.", attr.FormatErrorMessage ("MyField"), "#A1");
196
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");
206                         }, "#A2-1");
207                 }
208         }
209 }