Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[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                         Assert.AreEqual (0, sla.MinimumLength, "#A1-4");
62
63                         sla = new StringLengthAttributePoker (-10);
64                         Assert.AreEqual (-10, sla.MaximumLength, "#B1");
65                         sla = new StringLengthAttributePoker (0);
66                         Assert.AreEqual (0, sla.MaximumLength, "#C1");
67                 }
68
69                 [Test]
70                 public void FormatMessageString ()
71                 {
72                         var sla = new StringLengthAttributePoker (10);
73
74                         Assert.AreEqual ("The field MyField must be a string with a maximum length of 10.", sla.FormatErrorMessage ("MyField"), "#A1-1");
75                         sla.ErrorMessage = "Param 0: {0}";
76                         Assert.AreEqual ("Param 0: MyField", sla.FormatErrorMessage ("MyField"), "#A1-2");
77                         sla.ErrorMessage = "Param 0: {0}; Param 1: {1}";
78                         Assert.AreEqual ("Param 0: MyField; Param 1: 10", sla.FormatErrorMessage ("MyField"), "#A1-2");
79                         Assert.AreEqual ("Param 0: ; Param 1: 10", sla.FormatErrorMessage (null), "#A1-3");
80                 }
81
82                 [Test]
83                 public void IsValid ()
84                 {
85                         var sla = new StringLengthAttributePoker (10);
86
87                         Assert.IsTrue (sla.IsValid (null), "#A1-1");
88                         Assert.IsTrue (sla.IsValid (String.Empty), "#A1-2");
89                         Assert.IsTrue (sla.IsValid ("string"), "#A1-3");
90                         Assert.IsTrue (sla.IsValid ("0123456789"), "#A1-4");
91                         Assert.IsFalse (sla.IsValid ("0123456789A"), "#A1-5");
92                         AssertExtensions.Throws<InvalidCastException> (() => {
93                                 sla.IsValid (123);
94                         }, "#A1-6");
95                         AssertExtensions.Throws<InvalidCastException> (() => {
96                                 sla.IsValid (DateTime.Now);
97                         }, "#A1-7");
98
99                         sla = new StringLengthAttributePoker (0);
100                         Assert.IsTrue (sla.IsValid (null), "#B1-1");
101                         Assert.IsTrue (sla.IsValid (String.Empty), "#B1-2");
102                         Assert.IsFalse (sla.IsValid ("string"), "#B1-3");
103                         sla = new StringLengthAttributePoker (-10);
104                         AssertExtensions.Throws <InvalidOperationException> (() => {
105                                 sla.IsValid ("123");
106                         }, "#C1-1");
107
108                         sla = new StringLengthAttributePoker (10);
109                         sla.MinimumLength = 20;
110                         AssertExtensions.Throws<InvalidOperationException> (() => {
111                                 sla.IsValid ("123");
112                         }, "#C1-2");
113
114                         sla.MinimumLength = 5;
115                         Assert.IsFalse (sla.IsValid ("123"), "#C2-1");
116                         Assert.IsTrue (sla.IsValid ("12345"), "#C2-2");
117                 }
118         }
119 }