2 // StringLengthAttributeTest.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 namespace MonoTests.System.ComponentModel.DataAnnotations
39 public class StringLengthAttributeTest
41 class StringLengthAttributePoker : StringLengthAttribute
43 public StringLengthAttributePoker (int maximumLength)
44 : base (maximumLength)
47 public string GetErrorMessageString ()
49 return ErrorMessageString;
54 public void Constructor ()
56 var sla = new StringLengthAttributePoker (10);
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");
62 Assert.AreEqual (0, sla.MinimumLength, "#A1-4");
64 sla = new StringLengthAttributePoker (-10);
65 Assert.AreEqual (-10, sla.MaximumLength, "#B1");
67 AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
68 sla = new StringLengthAttributePoker (-10);
71 sla = new StringLengthAttributePoker (0);
72 Assert.AreEqual (0, sla.MaximumLength, "#C1");
76 public void FormatMessageString ()
78 var sla = new StringLengthAttributePoker (10);
80 Assert.AreEqual ("The field MyField must be a string with a maximum length of 10.", sla.FormatErrorMessage ("MyField"), "#A1-1");
82 sla = new StringLengthAttributePoker (10);
84 sla.ErrorMessage = "Param 0: {0}";
85 Assert.AreEqual ("Param 0: MyField", sla.FormatErrorMessage ("MyField"), "#A1-2");
87 sla = new StringLengthAttributePoker (10);
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");
95 public void IsValid ()
97 var sla = new StringLengthAttributePoker (10);
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> (() => {
107 AssertExtensions.Throws<InvalidCastException> (() => {
108 sla.IsValid (DateTime.Now);
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");
116 sla = new StringLengthAttributePoker (-10);
117 AssertExtensions.Throws <InvalidOperationException> (() => {
121 sla = new StringLengthAttributePoker (10);
122 sla.MinimumLength = 20;
123 AssertExtensions.Throws<InvalidOperationException> (() => {
127 sla.MinimumLength = 5;
128 Assert.IsFalse (sla.IsValid ("123"), "#C2-1");
129 Assert.IsTrue (sla.IsValid ("12345"), "#C2-2");