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");
61 Assert.AreEqual (0, sla.MinimumLength, "#A1-4");
63 sla = new StringLengthAttributePoker (-10);
64 Assert.AreEqual (-10, sla.MaximumLength, "#B1");
65 sla = new StringLengthAttributePoker (0);
66 Assert.AreEqual (0, sla.MaximumLength, "#C1");
70 public void FormatMessageString ()
72 var sla = new StringLengthAttributePoker (10);
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");
83 public void IsValid ()
85 var sla = new StringLengthAttributePoker (10);
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> (() => {
95 AssertExtensions.Throws<InvalidCastException> (() => {
96 sla.IsValid (DateTime.Now);
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> (() => {
108 sla = new StringLengthAttributePoker (10);
109 sla.MinimumLength = 20;
110 AssertExtensions.Throws<InvalidOperationException> (() => {
114 sla.MinimumLength = 5;
115 Assert.IsFalse (sla.IsValid ("123"), "#C2-1");
116 Assert.IsTrue (sla.IsValid ("12345"), "#C2-2");