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 RegularExpressionAttributeTest
41 class RegularExpressionAttributePoker : RegularExpressionAttribute
43 public RegularExpressionAttributePoker (string pattern)
47 public string GetErrorMessageString ()
49 return ErrorMessageString;
54 public void Constructor ()
56 var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
57 Assert.AreEqual (@"[A-Za-z]", rea.Pattern, "Patterns not saved correctly.");
58 Assert.AreEqual (null, rea.ErrorMessage, "Error message not null when not yet matched.");
59 Assert.AreEqual ("The field {0} must match the regular expression '{1}'.", rea.GetErrorMessageString (), "Error message not valid.");
63 public void FormatMessageString ()
65 var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
67 Assert.AreEqual ("The field MyField must match the regular expression '[A-Za-z]'.",
68 rea.FormatErrorMessage ("MyField"),
69 "Error message not correctly formatted.");
71 rea.ErrorMessage = "Param 0: {0}";
72 Assert.AreEqual ("Param 0: MyField", rea.FormatErrorMessage ("MyField"), "Error message not correctly updated.");
74 rea.ErrorMessage = "Param 0: {0}; Param 1: {1}";
75 Assert.AreEqual ("Param 0: MyField; Param 1: [A-Za-z]", rea.FormatErrorMessage ("MyField"), "Error message not correctly updated.");
76 Assert.AreEqual ("Param 0: ; Param 1: [A-Za-z]", rea.FormatErrorMessage (null), "Error message fails on null value.");
80 public void IsValid ()
82 var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
84 Assert.IsTrue (rea.IsValid (null), "Null does not match [A-Za-z].");
85 Assert.IsTrue (rea.IsValid ("A"), "'A' does not match [A-Za-z].");
86 Assert.IsTrue (rea.IsValid ("a"), "'a' does not match [A-Za-z].");
87 Assert.IsFalse (rea.IsValid ("Bz"), "'Bz' does not match [A-Za-z].");
88 Assert.IsFalse (rea.IsValid ("string"), "'string' does not match [A-Za-z].");
89 Assert.IsTrue (rea.IsValid (String.Empty), "Empty string matches [A-Za-z].");
90 Assert.IsFalse (rea.IsValid ("0123456789"), "'0123456789' matches [A-Za-z].");
91 Assert.IsFalse (rea.IsValid ("0123456789"), "'0123456789A' matches [A-Za-z].");
92 Assert.IsFalse (rea.IsValid (123), "Casting does not fails");
93 Assert.IsFalse (rea.IsValid (DateTime.Now), "Casting does not fails");
95 rea = new RegularExpressionAttributePoker ("");
97 AssertExtensions.Throws<InvalidOperationException> (() => {
99 }, "null does not match empty pattern");
101 AssertExtensions.Throws<InvalidOperationException> (() => {
102 rea.IsValid (String.Empty);
103 }, "empty string does not match empty pattern");
105 AssertExtensions.Throws<InvalidOperationException> (() => {
106 rea.IsValid ("string");
107 }, "'string' does not match empty pattern");
109 rea = new RegularExpressionAttributePoker (null);