[SRE] Improved token fixups processing.
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / StringLengthAttributeTest.cs
1 //
2 // StringLengthAttributeTest.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 namespace MonoTests.System.ComponentModel.DataAnnotations
37 {
38         [TestFixture]
39         public class StringLengthAttributeTest
40         {
41                 class StringLengthAttributePoker : StringLengthAttribute
42                 {
43                         public StringLengthAttributePoker (int maximumLength)
44                                 : base (maximumLength)
45                         { }
46
47                         public string GetErrorMessageString ()
48                         {
49                                 return ErrorMessageString;
50                         }
51                 }
52
53                 [Test]
54                 public void Constructor ()
55                 {
56                         var sla = new StringLengthAttributePoker (10);
57
58                         Assert.AreEqual (10, sla.MaximumLength, "#A1-1");
59                         Assert.AreEqual (null, sla.ErrorMessage, "#A1-2");
60                         Assert.AreEqual ("The field {0} must be a string with a maximum length of {1}.", sla.GetErrorMessageString (), "#A1-3");
61 #if NET_4_0
62                         Assert.AreEqual (0, sla.MinimumLength, "#A1-4");
63
64                         sla = new StringLengthAttributePoker (-10);
65                         Assert.AreEqual (-10, sla.MaximumLength, "#B1");
66 #else
67                         AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
68                                 sla = new StringLengthAttributePoker (-10);
69                         }, "#B1");
70 #endif
71                         sla = new StringLengthAttributePoker (0);
72                         Assert.AreEqual (0, sla.MaximumLength, "#C1");
73                 }
74
75                 [Test]
76                 public void FormatMessageString ()
77                 {
78                         var sla = new StringLengthAttributePoker (10);
79
80                         Assert.AreEqual ("The field MyField must be a string with a maximum length of 10.", sla.FormatErrorMessage ("MyField"), "#A1-1");
81 #if !NET_4_0
82                         sla = new StringLengthAttributePoker (10);
83 #endif
84                         sla.ErrorMessage = "Param 0: {0}";
85                         Assert.AreEqual ("Param 0: MyField", sla.FormatErrorMessage ("MyField"), "#A1-2");
86 #if !NET_4_0
87                         sla = new StringLengthAttributePoker (10);
88 #endif
89                         sla.ErrorMessage = "Param 0: {0}; Param 1: {1}";
90                         Assert.AreEqual ("Param 0: MyField; Param 1: 10", sla.FormatErrorMessage ("MyField"), "#A1-2");
91                         Assert.AreEqual ("Param 0: ; Param 1: 10", sla.FormatErrorMessage (null), "#A1-3");
92                 }
93
94                 [Test]
95                 public void IsValid ()
96                 {
97                         var sla = new StringLengthAttributePoker (10);
98
99                         Assert.IsTrue (sla.IsValid (null), "#A1-1");
100                         Assert.IsTrue (sla.IsValid (String.Empty), "#A1-2");
101                         Assert.IsTrue (sla.IsValid ("string"), "#A1-3");
102                         Assert.IsTrue (sla.IsValid ("0123456789"), "#A1-4");
103                         Assert.IsFalse (sla.IsValid ("0123456789A"), "#A1-5");
104                         AssertExtensions.Throws<InvalidCastException> (() => {
105                                 sla.IsValid (123);
106                         }, "#A1-6");
107                         AssertExtensions.Throws<InvalidCastException> (() => {
108                                 sla.IsValid (DateTime.Now);
109                         }, "#A1-7");
110
111                         sla = new StringLengthAttributePoker (0);
112                         Assert.IsTrue (sla.IsValid (null), "#B1-1");
113                         Assert.IsTrue (sla.IsValid (String.Empty), "#B1-2");
114                         Assert.IsFalse (sla.IsValid ("string"), "#B1-3");
115 #if NET_4_0
116                         sla = new StringLengthAttributePoker (-10);
117                         AssertExtensions.Throws <InvalidOperationException> (() => {
118                                 sla.IsValid ("123");
119                         }, "#C1-1");
120
121                         sla = new StringLengthAttributePoker (10);
122                         sla.MinimumLength = 20;
123                         AssertExtensions.Throws<InvalidOperationException> (() => {
124                                 sla.IsValid ("123");
125                         }, "#C1-2");
126
127                         sla.MinimumLength = 5;
128                         Assert.IsFalse (sla.IsValid ("123"), "#C2-1");
129                         Assert.IsTrue (sla.IsValid ("12345"), "#C2-2");
130 #endif
131                 }
132         }
133 }