Merge pull request #2338 from BogdanovKirill/httpwritefix3
[mono.git] / mcs / class / System.ComponentModel.DataAnnotations / Test / System.ComponentModel.DataAnnotations / RegularExpressionAttributeTest.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 RegularExpressionAttributeTest
40         {
41                 class RegularExpressionAttributePoker : RegularExpressionAttribute
42                 {
43                         public RegularExpressionAttributePoker (string pattern)
44                                 : base(pattern)
45                         { }
46
47                         public string GetErrorMessageString ()
48                         {
49                                 return ErrorMessageString;
50                         }
51                 }
52
53                 [Test]
54                 public void Constructor ()
55                 {
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.");
60                 }
61
62                 [Test]
63                 public void FormatMessageString ()
64                 {
65                         var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
66
67                         Assert.AreEqual ("The field MyField must match the regular expression '[A-Za-z]'.", 
68                                 rea.FormatErrorMessage ("MyField"), 
69                                 "Error message not correctly formatted.");
70
71                         rea.ErrorMessage = "Param 0: {0}";
72                         Assert.AreEqual ("Param 0: MyField", rea.FormatErrorMessage ("MyField"), "Error message not correctly updated.");
73
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.");
77                 }
78
79                 [Test]
80                 public void IsValid ()
81                 {
82                         var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
83
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");
94
95                         rea = new RegularExpressionAttributePoker ("");
96
97                         AssertExtensions.Throws<InvalidOperationException> (() => {
98                                 rea.IsValid (null);
99                         }, "null does not match empty pattern");
100
101                         AssertExtensions.Throws<InvalidOperationException> (() => {
102                                 rea.IsValid (String.Empty);
103                         }, "empty string does not match empty pattern");
104
105                         AssertExtensions.Throws<InvalidOperationException> (() => {
106                                 rea.IsValid ("string");
107                         }, "'string' does not match empty pattern");
108                         
109                         rea = new RegularExpressionAttributePoker (null);
110                 }
111         }
112 }