Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
//
// AssociatedMetadataTypeTypeDescriptionProvider.cs
//
// Author:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2009 Novell Inc. http://novell.com
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if !MOBILE
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
[TestFixture]
public class AssociatedMetadataTypeTypeDescriptionProviderTests
{
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructors_Null_Type ()
{
var v = new AssociatedMetadataTypeTypeDescriptionProvider (null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructors_Null_Type_2 ()
{
var v = new AssociatedMetadataTypeTypeDescriptionProvider (null, null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructors_Null_AssociatedMetadataType_2 ()
{
var v = new AssociatedMetadataTypeTypeDescriptionProvider (typeof (string), null);
}
[Test]
public void GetTypeDescriptor ()
{
var v = new AssociatedMetadataTypeTypeDescriptionProvider (typeof (string));
Assert.IsTrue (v != null, "#A1");
string s = "test";
var o = v.GetTypeDescriptor (s.GetType (), s);
Assert.IsTrue (o != null, "#B1");
Type oType = o.GetType ();
Assert.AreEqual ("System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptor", oType.ToString (), "#C1");
Assert.IsTrue (typeof (ICustomTypeDescriptor).IsAssignableFrom (oType), "#C2");
Assert.IsTrue (typeof (CustomTypeDescriptor).IsAssignableFrom (oType), "#C3");
}
}
}
#endif

View File

@@ -0,0 +1,129 @@
//
// AssociationAttributeTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_0
[TestFixture]
public class AssociationAttributeTest
{
[Test]
public void Constructor ()
{
AssociationAttribute attr;
attr = new AssociationAttribute (null, "key1,key2", "key3,key4");
Assert.AreEqual (null, attr.Name, "#A1-1");
Assert.AreEqual ("key1,key2", attr.ThisKey, "#A1-2");
Assert.AreEqual ("key3,key4", attr.OtherKey, "#A1-3");
Assert.IsNotNull (attr.OtherKeyMembers, "#A2-1");
int count = 0;
var list = new List<string> ();
foreach (string m in attr.OtherKeyMembers) {
count++;
list.Add (m);
}
Assert.AreEqual (2, count, "#A2-2");
Assert.AreEqual ("key3", list [0], "#A2-3");
Assert.AreEqual ("key4", list [1], "#A2-4");
Assert.IsNotNull (attr.ThisKeyMembers, "#A3-1");
count = 0;
list = new List<string> ();
foreach (string m in attr.ThisKeyMembers) {
count++;
list.Add (m);
}
Assert.AreEqual (2, count, "#A3-2");
Assert.AreEqual ("key1", list [0], "#A3-3");
Assert.AreEqual ("key2", list [1], "#A3-4");
attr = new AssociationAttribute ("name", null, "key3,key4");
Assert.AreEqual ("name", attr.Name, "#B1-1");
Assert.AreEqual (null, attr.ThisKey, "#B1-2");
Assert.AreEqual ("key3,key4", attr.OtherKey, "#B1-3");
Assert.IsNotNull (attr.OtherKeyMembers, "#B2-1");
count = 0;
list = new List<string> ();
foreach (string m in attr.OtherKeyMembers) {
count++;
list.Add (m);
}
Assert.AreEqual (2, count, "#B2-2");
Assert.AreEqual ("key3", list [0], "#B2-3");
Assert.AreEqual ("key4", list [1], "#B2-4");
// this is just sad...
try {
var m = attr.ThisKeyMembers;
Assert.Fail ("#B2-5");
} catch (NullReferenceException) {
// success
}
attr = new AssociationAttribute ("name", " key1 , key 2 ,, ,key 3 ", " ");
Assert.IsNotNull (attr.ThisKeyMembers, "#C1");
count = 0;
list = new List<string> ();
foreach (string m in attr.ThisKeyMembers) {
count++;
list.Add (m);
}
// It seems all the whitespace is removed from key names
Assert.AreEqual (5, count, "#C2-1");
Assert.AreEqual ("key1", list [0], "#C2-2");
Assert.AreEqual ("key2", list [1], "#C2-3");
Assert.AreEqual (String.Empty, list [2], "#C2-4");
Assert.AreEqual (String.Empty, list [3], "#C2-5");
Assert.AreEqual ("key3", list [4], "#C2-6");
Assert.IsNotNull (attr.OtherKeyMembers, "#C3");
count = 0;
list = new List<string> ();
foreach (string m in attr.OtherKeyMembers) {
count++;
list.Add (m);
}
Assert.AreEqual (1, count, "#C4-1");
Assert.AreEqual (String.Empty, list [0], "#C4-2");
}
}
#endif
}

View File

@@ -0,0 +1,70 @@
//
// CompareAttributeTest.cs
//
// Authors:
// Pablo Ruiz GarcĂ­a <pablo.ruiz@gmail.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
// Copyright (C) 2013 Pablo Ruiz GarcĂ­a
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_5
[TestFixture]
public class CompareAttributeTest
{
public class TestModel {
public string A { get; set; }
[Display(Name = "TheB")]
public string B { get; set; }
}
[Test]
public void GetValidationResult ()
{
var sla = new CompareAttribute ("B");
var obj = new TestModel { A = "x", B = "x" };
var ctx = new ValidationContext(obj, null, null);
Assert.IsNotNull (sla.GetValidationResult (null, ctx), "#A1-1");
Assert.IsNotNull (sla.GetValidationResult (String.Empty, ctx), "#A1-2");
Assert.IsNotNull (sla.GetValidationResult (obj, ctx), "#A1-3");
Assert.IsNull (sla.GetValidationResult (obj.A, ctx), "#A1-4");
obj = new TestModel { A = "x", B = "n" };
Assert.IsNotNull (sla.GetValidationResult (null, ctx), "#B-1");
Assert.IsNotNull (sla.GetValidationResult (obj, ctx), "#B-2");
Assert.IsNotNull (sla.GetValidationResult (true, ctx), "#B-3");
Assert.IsNotNull (sla.GetValidationResult (DateTime.Now, ctx), "#B-4");
}
}
#endif
}

View File

@@ -0,0 +1,62 @@
//
// CreditCardAttributeTest.cs
//
// Authors:
// Pablo Ruiz GarcĂ­a <pablo.ruiz@gmail.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
// Copyright (C) 2013 Pablo Ruiz GarcĂ­a
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_5
[TestFixture]
public class CreditCardAttributeTest
{
[Test]
public void IsValid ()
{
var sla = new CreditCardAttribute ();
Assert.IsTrue (sla.IsValid (null), "#A1-1");
Assert.IsFalse (sla.IsValid (String.Empty), "#A1-2");
Assert.IsFalse (sla.IsValid ("string"), "#A1-3");
Assert.IsTrue (sla.IsValid ("378282246310005"), "#A1-4");
Assert.IsTrue (sla.IsValid ("3782-8224-6310-005"), "#A1-5");
Assert.IsTrue (sla.IsValid ("371449635398431"), "#A-6");
#if false
Assert.IsFalse (sla.IsValid ("371449635498431"), "#A-6b");
#endif
Assert.IsFalse (sla.IsValid (true), "#A1-7");
Assert.IsFalse (sla.IsValid (DateTime.Now), "#A1-8");
}
}
#endif
}

View File

@@ -0,0 +1,332 @@
//
// Authors:
// Marek Habersack <grendel@twistedcode.net>
//
// Copyright (C) 2011 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_0
[TestFixture]
public class CustomValidationAttributeTest
{
[Test]
public void Constructor ()
{
var attr = new CustomValidationAttribute (null, "MyMethod");
Assert.IsNull (attr.ValidatorType, "#A1-1");
Assert.AreEqual ("MyMethod", attr.Method, "#A1-2");
attr = new CustomValidationAttribute (typeof (string), null);
Assert.AreEqual (typeof (string), attr.ValidatorType, "#A2-1");
Assert.IsNull (attr.Method, "#A2-2");
attr = new CustomValidationAttribute (null, null);
Assert.IsNull (attr.ValidatorType, "#A3-1");
Assert.IsNull (attr.Method, "#A3-2");
attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
Assert.AreEqual (typeof (string), attr.ValidatorType, "#A5-1");
Assert.AreEqual ("NoSuchMethod", attr.Method, "#A5-2");
}
[Test]
public void TypeId ()
{
var attr = new CustomValidationAttribute (null, "MyMethod");
Assert.IsNotNull (attr.TypeId, "#A1-1");
Assert.AreEqual (typeof (Tuple<string, Type>), attr.TypeId.GetType (), "#A1-2");
var typeid = attr.TypeId as Tuple <string, Type>;
Assert.IsNotNull (typeid.Item1, "#A2-1");
Assert.AreEqual ("MyMethod", typeid.Item1, "#A2-2");
Assert.IsNull (typeid.Item2, "#A2-3");
attr = new CustomValidationAttribute (typeof (CustomValidationAttributeTest), "MyMethod");
typeid = attr.TypeId as Tuple<string, Type>;
Assert.IsNotNull (typeid.Item1, "#A3-1");
Assert.AreEqual ("MyMethod", typeid.Item1, "#A3-2");
Assert.IsNotNull (typeid.Item2, "#A3-3");
Assert.AreEqual (typeof (CustomValidationAttributeTest), typeid.Item2, "#A3-4");
var typeid2 = attr.TypeId as Tuple<string, Type>;
Assert.IsTrue (Object.ReferenceEquals (typeid, typeid2), "#A4");
}
[Test]
public void FormatErrorMessage ()
{
var attr = new CustomValidationAttribute (null, null);
string msg = null;
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute.ValidatorType was not specified.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 88
msg = attr.FormatErrorMessage (null);
}, "#A1");
attr = new CustomValidationAttribute (typeof (string), null);
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 102
msg = attr.FormatErrorMessage (null);
}, "#A2");
attr = new CustomValidationAttribute (typeof (string), String.Empty);
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute.Method was not specified.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 117
msg = attr.FormatErrorMessage (null);
}, "#A3");
attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute method 'NoSuchMethod' does not exist in type 'String' or is not public and static.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 126
msg = attr.FormatErrorMessage (null);
}, "#A4");
attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The custom validation type 'PrivateValidatorMethodContainer' must be public.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 138
msg = attr.FormatErrorMessage (null);
}, "#A5");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute method 'MethodOne' in type 'PublicValidatorMethodContainer'
// must return System.ComponentModel.DataAnnotations.ValidationResult. Use System.ComponentModel.DataAnnotations.ValidationResult.Success
// to represent success.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 150
msg = attr.FormatErrorMessage (null);
}, "#A6");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute method 'MethodTwo' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodTwo(object value, ValidationContext context). The value can be strongly typed. The ValidationContext parameter is optional.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 163
msg = attr.FormatErrorMessage (null);
}, "#A7");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
msg = attr.FormatErrorMessage (null);
Assert.IsNotNull (msg, "#A8-1");
Assert.IsTrue (msg.Length > 0, "#A8-2");
Assert.AreEqual (" is not valid.", msg, "#A8-3");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
msg = attr.FormatErrorMessage ("test");
Assert.IsNotNull (msg, "#A9-1");
Assert.IsTrue (msg.Length > 0, "#A9-2");
Assert.AreEqual ("test is not valid.", msg, "#A9-3");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute method 'MethodFive' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodFive(object value, ValidationContext context). The value can be strongly typed. The ValidationContext parameter is optional.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 180
msg = attr.FormatErrorMessage (null);
}, "#A10");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
AssertExtensions.Throws<InvalidOperationException> (() => {
// MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage:
// System.InvalidOperationException : The CustomValidationAttribute method 'MethodSix' in type 'PublicValidatorMethodContainer' must match the expected signature: public static ValidationResult MethodSix(object value, ValidationContext context). The value can be strongly typed. The ValidationContext parameter is optional.
//
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.ThrowIfAttributeNotWellFormed()
// at System.ComponentModel.DataAnnotations.CustomValidationAttribute.FormatErrorMessage(String name)
// at MonoTests.System.ComponentModel.DataAnnotations.CustomValidationAttributeTest.FormatErrorMessage() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\CustomValidationAttributeTest.cs:line 191
msg = attr.FormatErrorMessage (null);
}, "#A11");
}
[Test]
public void IsValid ()
{
var attr = new CustomValidationAttribute (null, null);
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A1");
attr = new CustomValidationAttribute (typeof (string), null);
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A2");
attr = new CustomValidationAttribute (typeof (string), String.Empty);
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A3");
attr = new CustomValidationAttribute (typeof (string), "NoSuchMethod");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A4");
attr = new CustomValidationAttribute (typeof (PrivateValidatorMethodContainer), "MethodOne");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A5");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodOne");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A6");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodTwo");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A7");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodThree");
bool valid = attr.IsValid ("test");
Assert.IsTrue (valid, "#A8-1");
valid = attr.IsValid (null);
Assert.IsFalse (valid, "#A8-2");
valid = attr.IsValid ("failTest");
Assert.IsFalse (valid, "#A8-3");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFour");
valid = attr.IsValid ("test");
Assert.IsTrue (valid, "#A9-1");
valid = attr.IsValid (null);
Assert.IsFalse (valid, "#A9-2");
valid = attr.IsValid ("failTest");
Assert.IsFalse (valid, "#A9-3");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodFive");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A10");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSix");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid ("test");
}, "#A11");
attr = new CustomValidationAttribute (typeof (PublicValidatorMethodContainer), "MethodSeven");
AssertExtensions.Throws<ApplicationException> (() => {
attr.IsValid ("test");
}, "#A12");
}
class PrivateValidatorMethodContainer
{
public static void MethodOne ()
{ }
}
public class PublicValidatorMethodContainer
{
public static void MethodOne ()
{ }
public static ValidationResult MethodTwo ()
{
return ValidationResult.Success;
}
public static ValidationResult MethodThree (object o)
{
if (o == null)
return new ValidationResult ("Object cannot be null");
string s = o as string;
if (s == null || s != "failTest")
return ValidationResult.Success;
return new ValidationResult ("Test failed as requested");
}
public static ValidationResult MethodFour (object o, ValidationContext ctx)
{
if (o == null)
return new ValidationResult ("Object cannot be null");
string s = o as string;
if (s == null || s != "failTest")
return ValidationResult.Success;
return new ValidationResult ("Test failed as requested");
}
public static ValidationResult MethodFive (object o, string s)
{
return ValidationResult.Success;
}
public static ValidationResult MethodSix (object o, ValidationContext ctx, string s)
{
return ValidationResult.Success;
}
public static ValidationResult MethodSeven (object o, ValidationContext ctx)
{
throw new ApplicationException ("SNAFU");
}
}
}
#endif
}

View File

@@ -0,0 +1,278 @@
using System;
using NUnit.Framework;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_0
public class AttributeTargetValidation
{
[Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
public string WorksOnProperty { get; set; }
[Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
public string WorksOnMethod ()
{
return "";
}
[Display(ResourceType = typeof(GoodResources), Name = "NameKey")]
public string worksOnField;
public string WorksOnParameter ([Display(ResourceType = typeof(GoodResources), Name = "NameKey")] string parameter1)
{
return "";
}
}
public class GoodResources
{
public static string Name {
get { return "NameValue"; }
}
public static string ShortName {
get { return "ShortNameValue"; }
}
public static string Prompt {
get { return "PromptValue"; }
}
public static string Description {
get { return "DescriptionValue"; }
}
public static string GroupName {
get { return "GroupNameValue"; }
}
}
public class BadResources
{
private static string PrivateString {
get { return "Not a public string"; }
}
public string InstanceString {
get { return "Not a static string"; }
}
public string WriteOnlyString {
set { }
}
}
internal class InvisibleResources
{
public static string InvisibleResource {
get { return "Not a visible string "; }
}
}
[TestFixture]
public class DisplayAttributeTests
{
const string property_not_set_message = "The {0} property has not been set. Use the Get{0} method to get the value.";
const string localization_failed_message = "Cannot retrieve property '{0}' because localization failed. Type '{1} is not public or does not contain a public static string property with the name '{2}'.";
[Test]
public void StringProperties_ReturnLiteralValues_Success()
{
var display = new DisplayAttribute()
{
Name = "Name",
ShortName = "ShortName",
Prompt = "Prompt",
Description = "Description",
GroupName = "GroupName"
};
Assert.AreEqual("Name", display.GetName());
Assert.AreEqual("ShortName", display.GetShortName());
Assert.AreEqual("Prompt", display.GetPrompt());
Assert.AreEqual("Description", display.GetDescription());
Assert.AreEqual("GroupName", display.GetGroupName());
}
[Test]
public void StringProperties_ReturnLocalizedValues_Success()
{
var display = new DisplayAttribute()
{
ResourceType = typeof(GoodResources),
Name = "Name",
ShortName = "ShortName",
Prompt = "Prompt",
Description = "Description",
GroupName = "GroupName"
};
Assert.AreEqual(GoodResources.Name, display.GetName());
Assert.AreEqual(GoodResources.ShortName, display.GetShortName());
Assert.AreEqual(GoodResources.Prompt, display.GetPrompt());
Assert.AreEqual(GoodResources.Description, display.GetDescription());
Assert.AreEqual(GoodResources.GroupName, display.GetGroupName());
}
[Test]
public void ShortName_ReturnsName_WhenNotSet()
{
var display = new DisplayAttribute()
{
Name = "Name"
};
Assert.AreEqual("Name", display.GetShortName());
}
[Test]
public void OrderAndAutoGenerateProperties_Success()
{
var display = new DisplayAttribute()
{
Order = 1,
AutoGenerateField = true,
AutoGenerateFilter = false
};
Assert.AreEqual(1, display.Order);
Assert.AreEqual(1, display.GetOrder());
Assert.AreEqual(true, display.AutoGenerateField);
Assert.AreEqual(true, display.GetAutoGenerateField());
Assert.AreEqual(false, display.AutoGenerateFilter);
Assert.AreEqual(false, display.GetAutoGenerateFilter());
}
[Test]
public void StringProperties_GetUnSetProperties_ReturnsNull ()
{
var display = new DisplayAttribute ();
Assert.IsNull (display.Name);
Assert.IsNull (display.ShortName);
Assert.IsNull (display.Prompt);
Assert.IsNull (display.Description);
Assert.IsNull (display.GroupName);
Assert.IsNull (display.GetName ());
Assert.IsNull (display.GetShortName ());
Assert.IsNull (display.GetPrompt ());
Assert.IsNull (display.GetDescription ());
Assert.IsNull (display.GetGroupName ());
}
[Test]
public void OrderAndAutoGeneratedProperties_GetUnSetProperties_ThrowsInvalidOperationException ()
{
var display = new DisplayAttribute();
ExceptionAssert.Throws<InvalidOperationException>(() => display.Order.ToString(), string.Format(property_not_set_message, "Order"));
ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateField.ToString(), string.Format(property_not_set_message, "AutoGenerateField"));
ExceptionAssert.Throws<InvalidOperationException>(() => display.AutoGenerateFilter.ToString(), string.Format(property_not_set_message, "AutoGenerateFilter"));
}
[Test]
public void AllProperties_InvisibleResource_ThrowsInvalidOperationException ()
{
var resourceType = typeof(InvisibleResources);
var resourceKey = "InvisibleResource";
var display = new DisplayAttribute()
{
ResourceType = resourceType,
Name = resourceKey,
ShortName = resourceKey,
Prompt = resourceKey,
Description = resourceKey,
GroupName = resourceKey
};
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
}
[Test]
public void AllProperties_PrivateResource_ThrowsInvalidOperationException ()
{
var resourceType = typeof(BadResources);
var resourceKey = "InstanceString";
var display = new DisplayAttribute()
{
ResourceType = resourceType,
Name = resourceKey,
ShortName = resourceKey,
Prompt = resourceKey,
Description = resourceKey,
GroupName = resourceKey
};
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
}
[Test]
public void AllProperties_InstanceResource_ThrowsInvalidOperationException ()
{
var resourceType = typeof(BadResources);
var resourceKey = "InstanceString";
var display = new DisplayAttribute()
{
ResourceType = resourceType,
Name = resourceKey,
ShortName = resourceKey,
Prompt = resourceKey,
Description = resourceKey,
GroupName = resourceKey
};
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
}
[Test]
public void AllProperties_WriteOnlyResource_ThrowsInvalidOperationException ()
{
var resourceType = typeof(BadResources);
var resourceKey = "WriteOnlyString";
var display = new DisplayAttribute()
{
ResourceType = resourceType,
Name = resourceKey,
ShortName = resourceKey,
Prompt = resourceKey,
Description = resourceKey,
GroupName = resourceKey
};
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetName(), string.Format(localization_failed_message, "Name", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetShortName(), string.Format(localization_failed_message, "ShortName", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetPrompt(), string.Format(localization_failed_message, "Prompt", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetDescription(), string.Format(localization_failed_message, "Description", resourceType, resourceKey));
ExceptionAssert.Throws<InvalidOperationException>(() => display.GetGroupName(), string.Format(localization_failed_message, "GroupName", resourceType, resourceKey));
}
}
public static class ExceptionAssert
{
public static void Throws<TException> (Action action, string expectedMessage) where TException : Exception
{
try
{
action ();
}
catch (TException ex)
{
Assert.AreEqual (expectedMessage, ex.Message);
return;
}
Assert.Fail();
}
}
#endif
}

View File

@@ -0,0 +1,87 @@
//
// EmailAddressAttributeTest.cs
//
// Authors:
// Pablo Ruiz GarcĂ­a <pablo.ruiz@gmail.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
// Copyright (C) 2013 Pablo Ruiz GarcĂ­a
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_5
[TestFixture]
public class EmailAddressAttributeTest
{
static readonly object[] ValidAddresses = new object[] {
null,
"\"Abc\\@def\"@example.com",
"\"Fred Bloggs\"@example.com",
"\"Joe\\\\Blow\"@example.com",
"\"Abc@def\"@example.com",
"customer/department=shipping@example.com",
"$A12345@example.com",
"!def!xyz%abc@example.com",
"_somename@example.com",
"valid.ipv4.addr@[123.1.72.10]",
"valid.ipv6.addr@[IPv6:0::1]",
"valid.ipv6.addr@[IPv6:2607:f0d0:1002:51::4]",
"valid.ipv6.addr@[IPv6:fe80::230:48ff:fe33:bc33]",
"valid.ipv6v4.addr@[IPv6:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:127.0.0.1]",
};
static readonly object[] InvalidAddresses = new object[] {
"",
123,
DateTime.Now,
"invalid",
"invalid@",
"invalid @",
"invalid@[555.666.777.888]",
"invalid@[IPv6:123456]",
"invalid@[127.0.0.1.]",
"invalid@[127.0.0.1].",
"invalid@[127.0.0.1]x",
};
[Test]
public void IsValid ()
{
var sla = new EmailAddressAttribute ();
for (int i = 0; i < ValidAddresses.Length; i++)
Assert.IsTrue (sla.IsValid (ValidAddresses[i]), "#A1-{0}", i);
for (int i = 0; i < InvalidAddresses.Length; i++)
Assert.IsFalse (sla.IsValid (InvalidAddresses[i]), "#B1-{0}", i);
}
}
#endif
}

View File

@@ -0,0 +1,137 @@
//
// EnumDataTypeAttributeTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_0
[TestFixture]
public class EnumDataTypeAttributeTest
{
[Test]
public void Constructor ()
{
var attr = new EnumDataTypeAttribute (typeof (string));
Assert.AreEqual (DataType.Custom, attr.DataType, "#A1-1");
Assert.AreEqual (typeof (string), attr.EnumType, "#A1-2");
attr = new EnumDataTypeAttribute (typeof (TestEnum));
Assert.AreEqual (DataType.Custom, attr.DataType, "#B1-1");
Assert.AreEqual (typeof (TestEnum), attr.EnumType, "#B1-2");
attr = new EnumDataTypeAttribute (null);
Assert.AreEqual (DataType.Custom, attr.DataType, "#C1-1");
Assert.AreEqual (null, attr.EnumType, "#C1-2");
}
[Test]
public void IsValid ()
{
var attr = new EnumDataTypeAttribute (typeof (string));
try {
attr.IsValid (null);
Assert.Fail ("#A1-1");
} catch (InvalidOperationException) {
// success
}
try {
attr.IsValid ("stuff");
Assert.Fail ("#A1-2");
} catch (InvalidOperationException) {
// success
}
attr = new EnumDataTypeAttribute (typeof (TestEnum));
Assert.IsTrue (attr.IsValid (null), "#A2-1");
Assert.IsTrue (attr.IsValid (1), "#A2-2");
Assert.IsFalse (attr.IsValid (0), "#A2-3");
Assert.IsTrue (attr.IsValid (TestEnum.Two), "#A2-4");
Assert.IsFalse (attr.IsValid (AnotherEnum.Five), "#A2-5");
Assert.IsFalse (attr.IsValid ("stuff"), "#A2-5");
AnotherEnum val = AnotherEnum.Six;
Assert.IsFalse (attr.IsValid (val), "#A2-6");
Assert.IsTrue (attr.IsValid (String.Empty), "#A2-7");
Assert.IsTrue (attr.IsValid ("Three"), "#A2-8");
Assert.IsFalse (attr.IsValid ("Four"), "#A2-9");
Assert.IsFalse (attr.IsValid (true), "#A2-10");
Assert.IsFalse (attr.IsValid (' '), "#A2-11");
Assert.IsFalse (attr.IsValid (0.12F), "#A2-12");
Assert.IsTrue (attr.IsValid ((short) 1), "#A2-13");
Assert.IsFalse (attr.IsValid (12.3M), "#A2-14");
Assert.IsFalse (attr.IsValid (12.3D), "#A2-15");
Assert.IsTrue (attr.IsValid ((long) 1), "#A2-16");
attr = new EnumDataTypeAttribute (typeof (AnotherEnum));
Assert.IsTrue (attr.IsValid (null), "#A3-1");
Assert.IsTrue (attr.IsValid (4), "#A3-2");
Assert.IsFalse (attr.IsValid (0), "#A3-3");
Assert.IsTrue (attr.IsValid (AnotherEnum.Five), "#A3-4");
Assert.IsFalse (attr.IsValid (TestEnum.One), "#A3-5");
Assert.IsFalse (attr.IsValid ("stuff"), "#A3-5");
val = AnotherEnum.Four;
Assert.IsTrue (attr.IsValid (val), "#A3-6");
Assert.IsTrue (attr.IsValid (String.Empty), "#A3-7");
Assert.IsTrue (attr.IsValid ("Four"), "#A3-8");
Assert.IsFalse (attr.IsValid ("Three"), "#A3-9");
Assert.IsTrue (attr.IsValid (12), "#A3-10");
Assert.IsTrue (attr.IsValid ("Five, Six"), "#A3-11");
Assert.IsFalse (attr.IsValid (true), "#A3-12");
Assert.IsFalse (attr.IsValid (' '), "#A3-13");
Assert.IsFalse (attr.IsValid (0.12), "#A3-14");
}
}
enum TestEnum
{
One = 1,
Two,
Three
}
[Flags]
enum AnotherEnum
{
Four = 4,
Five = 8,
Six = 16
}
#endif
}

View File

@@ -0,0 +1,61 @@
//
// FileExtensionsAttributeTest.cs
//
// Authors:
// Pablo Ruiz GarcĂ­a <pablo.ruiz@gmail.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
// Copyright (C) 2013 Pablo Ruiz GarcĂ­a
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_5
[TestFixture]
public class FileExtensionsAttributeTest
{
[Test]
public void IsValid ()
{
var sla = new FileExtensionsAttribute () {
Extensions = "txt,jpg"
};
Assert.IsTrue (sla.IsValid (null), "#A1-1");
Assert.IsFalse (sla.IsValid (String.Empty), "#A1-2");
Assert.IsFalse (sla.IsValid ("string"), "#A1-3");
Assert.IsTrue (sla.IsValid ("file.txt"), "#A1-4");
Assert.IsTrue (sla.IsValid ("file.jpg"), "#A1-5");
Assert.IsTrue (sla.IsValid ("file.xxx.txt"), "#A-6");
Assert.IsFalse (sla.IsValid (true), "#A1-7");
Assert.IsFalse (sla.IsValid (DateTime.Now), "#A1-8");
}
}
#endif
}

View File

@@ -0,0 +1,93 @@
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
// https://silverlight.svn.codeplex.com/svn/Release/Silverlight4/Source/RiaClient.Tests/System.ComponentModel.DataAnnotations/FilterUIHintAttributeTest.cs
#if NET_4_0
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
[TestFixture]
public class FilterUIHintAttributeTest {
[Test]
public void FilterUIHintAttribute_Simple_Ctors_Set_Properties() {
var attr = new FilterUIHintAttribute(null, null);
Assert.IsNull(attr.FilterUIHint);
Assert.IsNull(attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
attr = new FilterUIHintAttribute(string.Empty, string.Empty);
Assert.AreEqual(string.Empty, attr.FilterUIHint);
Assert.AreEqual(string.Empty, attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
attr = new FilterUIHintAttribute("theHint");
Assert.AreEqual("theHint", attr.FilterUIHint);
Assert.IsNull(attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
attr = new FilterUIHintAttribute("theHint", "theLayer");
Assert.AreEqual("theHint", attr.FilterUIHint);
Assert.AreEqual("theLayer", attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
}
[Test]
public void ConstructorControlParameters() {
Assert.AreEqual(2, new FilterUIHintAttribute("", "", "a", 1, "b", 2).ControlParameters.Keys.Count);
}
[Test]
public void ConstructorControlParameters_NoParams() {
Assert.AreEqual(0, new FilterUIHintAttribute("", "", new object[0]).ControlParameters.Keys.Count);
Assert.AreEqual(0, new FilterUIHintAttribute("", "", (object[])null).ControlParameters.Keys.Count);
Assert.AreEqual(0, new FilterUIHintAttribute("", "").ControlParameters.Keys.Count);
Assert.AreEqual(0, new FilterUIHintAttribute("").ControlParameters.Keys.Count);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_UnevenNumber() {
var attr = new FilterUIHintAttribute("", "", "");
var v = attr.ControlParameters;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_NonStringKey() {
var attr = new FilterUIHintAttribute("", "", 1, "value");
var v = attr.ControlParameters;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_NullKey() {
var attr = new FilterUIHintAttribute("", "", null, "value");
var v = attr.ControlParameters;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_DuplicateKey() {
var attr = new FilterUIHintAttribute("", "", "key", "value1", "key", "value2");
var v = attr.ControlParameters;
}
#if !SILVERLIGHT
[Test]
public void TypeId_ReturnsDifferentValuesForDifferentInstances() {
Assert.AreNotEqual(new FilterUIHintAttribute("foo").TypeId, new FilterUIHintAttribute("bar").TypeId);
}
#endif
}
}
#endif

View File

@@ -0,0 +1,58 @@
//
// PhoneAttributeTest.cs
//
// Authors:
// Pablo Ruiz GarcĂ­a <pablo.ruiz@gmail.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
// Copyright (C) 2013 Pablo Ruiz GarcĂ­a
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_5
[TestFixture]
public class PhoneAttributeTest
{
[Test]
public void IsValid ()
{
var sla = new PhoneAttribute ();
Assert.IsTrue (sla.IsValid (null), "#A1-1");
Assert.IsFalse (sla.IsValid (String.Empty), "#A1-2");
Assert.IsFalse (sla.IsValid ("string"), "#A1-3");
Assert.IsTrue (sla.IsValid ("1-800-642-7676"), "#A1-4");
Assert.IsTrue (sla.IsValid ("+86-21-96081318"), "#A1-5");
Assert.IsFalse (sla.IsValid (true), "#A1-6");
Assert.IsFalse (sla.IsValid (DateTime.Now), "#A1-7");
}
}
#endif
}

View File

@@ -0,0 +1,254 @@
//
// RequiredAttributeTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
using DA = global::System.ComponentModel.DataAnnotations;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
class RangeAttributePoker : DA.RangeAttribute
{
public RangeAttributePoker (double min, double max)
: base (min, max)
{ }
public string GetErrorMessageString ()
{
return ErrorMessageString;
}
}
[TestFixture]
public class RangeAttributeTest
{
[Test]
public void ErrorMessage ()
{
var attr = new RangeAttributePoker (-10.123D, 10.123D);
Assert.IsNull (attr.ErrorMessage, "#A1-1");
Assert.AreEqual ("The field {0} must be between {1} and {2}.", attr.GetErrorMessageString (), "#A1-2");
}
[Test]
public void Constructor_Double_Double ()
{
var attr = new DA.RangeAttribute (-10.123D, 10.123D);
Assert.IsNotNull (attr.Minimum, "#A1-1");
Assert.IsNotNull (attr.Maximum, "#A1-2");
Assert.AreEqual (typeof (double), attr.Minimum.GetType (), "#A2-1");
Assert.AreEqual (typeof (double), attr.Maximum.GetType (), "#A2-2");
Assert.AreEqual (-10.123D, attr.Minimum, "#A3-1");
Assert.AreEqual (10.123D, attr.Maximum, "#A3-2");
Assert.IsNotNull (attr.OperandType, "#A4-1");
Assert.AreEqual (typeof (double), attr.OperandType, "#A4-2");
}
[Test]
public void Constructor_Int_Int ()
{
var attr = new DA.RangeAttribute (-10, 10);
Assert.IsNotNull (attr.Minimum, "#A1-1");
Assert.IsNotNull (attr.Maximum, "#A1-2");
Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A2-1");
Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A2-2");
Assert.AreEqual (-10, attr.Minimum, "#A3-1");
Assert.AreEqual (10, attr.Maximum, "#A3-2");
Assert.IsNotNull (attr.OperandType, "#A4-1");
Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
}
[Test]
public void Constructor_Type_String_String ()
{
var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
Assert.IsNotNull (attr.Minimum, "#A1-1");
Assert.IsNotNull (attr.Maximum, "#A1-2");
#if NET_4_0
Assert.AreEqual (typeof (string), attr.Minimum.GetType (), "#A2-1");
Assert.AreEqual (typeof (string), attr.Maximum.GetType (), "#A2-2");
Assert.AreEqual ("-10", attr.Minimum, "#A3-1");
Assert.AreEqual ("10", attr.Maximum, "#A3-2");
#else
Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A2-1");
Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A2-2");
Assert.AreEqual (-10, attr.Minimum, "#A3-1");
Assert.AreEqual (10, attr.Maximum, "#A3-2");
#endif
Assert.IsNotNull (attr.OperandType, "#A4-1");
Assert.AreEqual (typeof (int), attr.OperandType, "#A4-2");
}
[Test]
//LAMESPEC: documented to throw
#if !NET_4_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void Constructor_Type_String_String_Null_Type ()
{
var attr = new DA.RangeAttribute (null, "-10", "10");
Assert.IsNull (attr.OperandType, "#A1");
}
[Test]
public void IsValid ()
{
var attr = new DA.RangeAttribute (typeof (int), "-10", "10");
Assert.IsTrue (attr.IsValid ("0"), "#A1-1");
Assert.IsFalse (attr.IsValid ("12"), "#A1-2");
Assert.IsTrue (attr.IsValid (null), "#A1-3");
Assert.IsTrue (attr.IsValid (String.Empty), "#A1-4");
AssertExtensions.Throws <Exception> (() => {
attr.IsValid ("zero");
}, "#A1-5");
Assert.IsTrue (attr.IsValid (null), "#A1-6");
#if NET_4_0
attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
AssertExtensions.Throws<Exception> (() => {
attr.IsValid ("0");
}, "#A2-1");
AssertExtensions.Throws<Exception> (() => {
attr.IsValid ("12");
}, "#A2-2");
AssertExtensions.Throws<Exception> (() => {
attr.IsValid ("zero");
}, "#A2-3");
attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid (null);
}, "#A3-1");
AssertExtensions.Throws<InvalidOperationException> (() => {
attr.IsValid (String.Empty);
}, "#A3-2");
AssertExtensions.Throws<InvalidOperationException> (() => {
// The type MonoTests.System.ComponentModel.DataAnnotations.RangeAttributeTest must implement System.IComparable.
attr.IsValid ("10");
}, "#A3-3");
attr = new DA.RangeAttribute (null, "-10", "10");
AssertExtensions.Throws<InvalidOperationException> (() => {
// The OperandType must be set when strings are used for minimum and maximum values.
attr.IsValid ("10");
}, "#A4");
attr = new DA.RangeAttribute (typeof (int), null, "10");
AssertExtensions.Throws<InvalidOperationException> (() => {
// The minimum and maximum values must be set.
attr.IsValid (10);
}, "#A5-1");
attr = new DA.RangeAttribute (typeof (int), "10", null);
AssertExtensions.Throws<InvalidOperationException> (() => {
// The minimum and maximum values must be set.
attr.IsValid (10);
}, "#A5-2");
attr = new DA.RangeAttribute (typeof (int), "-10", "10");
Assert.AreEqual (typeof (string), attr.Minimum.GetType (), "#A6-1");
Assert.AreEqual (typeof (string), attr.Maximum.GetType (), "#A6-2");
// IsValid appears to reassign Minimum and Maximum with objects of the OperandType type, converted from the original strings
attr.IsValid (12);
Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A7-1");
Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A7-2");
#else
AssertExtensions.Throws<Exception> (() => {
attr = new DA.RangeAttribute (typeof (int), "minus ten", "ten");
}, "#A2");
AssertExtensions.Throws<ArgumentException> (() => {
attr = new DA.RangeAttribute (typeof (RangeAttributeTest), "-10", "10");
}, "#A3");
AssertExtensions.Throws<ArgumentNullException> (() => {
attr = new DA.RangeAttribute (null, "-10", "10");
}, "#A4");
AssertExtensions.Throws<NotSupportedException> (() => {
attr = new DA.RangeAttribute (typeof (int), null, "10");
}, "#A5-1");
AssertExtensions.Throws<NotSupportedException> (() => {
attr = new DA.RangeAttribute (typeof (int), "10", null);
}, "#A5-2");
attr = new DA.RangeAttribute (typeof (int), "-10", "10");
Assert.AreEqual (typeof (int), attr.Minimum.GetType (), "#A6-1");
Assert.AreEqual (typeof (int), attr.Maximum.GetType (), "#A6-2");
#endif
}
[Test]
public void FormatMessageString ()
{
var attr = new DA.RangeAttribute (-10, 10);
Assert.AreEqual ("The field MyField must be between -10 and 10.", attr.FormatErrorMessage ("MyField"), "#A1");
attr.ErrorMessage = "Param 0: {0}";
Assert.AreEqual ("Param 0: MyField", attr.FormatErrorMessage ("MyField"), "#A2-1");
#if !NET_4_0
attr = new DA.RangeAttribute (-10, 10);
#endif
attr.ErrorMessage = "Param 0: {0}; Param 1: {1}";
Assert.AreEqual ("Param 0: MyField; Param 1: -10", attr.FormatErrorMessage ("MyField"), "#A2-2");
#if !NET_4_0
attr = new DA.RangeAttribute (-10, 10);
#endif
attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}";
Assert.AreEqual ("Param 0: MyField; Param 1: -10; Param 2: 10", attr.FormatErrorMessage ("MyField"), "#A2-3");
#if !NET_4_0
attr = new DA.RangeAttribute (-10, 10);
#endif
attr.ErrorMessage = "Param 0: {0}; Param 1: {1}; Param 2: {2}; Param 3: {3}";
AssertExtensions.Throws<FormatException> (() => {
attr.FormatErrorMessage ("MyField");
}, "#A2-1");
}
}
}

View File

@@ -0,0 +1,115 @@
//
// StringLengthAttributeTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
[TestFixture]
public class RegularExpressionAttributeTest
{
class RegularExpressionAttributePoker : RegularExpressionAttribute
{
public RegularExpressionAttributePoker (string pattern)
: base(pattern)
{ }
public string GetErrorMessageString ()
{
return ErrorMessageString;
}
}
[Test]
public void Constructor ()
{
var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
Assert.AreEqual (@"[A-Za-z]", rea.Pattern, "Patterns not saved correctly.");
Assert.AreEqual (null, rea.ErrorMessage, "Error message not null when not yet matched.");
Assert.AreEqual ("The field {0} must match the regular expression {1}.", rea.GetErrorMessageString (), "Error message not valid.");
}
[Test]
public void FormatMessageString ()
{
var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
Assert.AreEqual ("The field MyField must match the regular expression [A-Za-z].",
rea.FormatErrorMessage ("MyField"),
"Error message not correctly formatted.");
#if !NET_4_0
rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
#endif
rea.ErrorMessage = "Param 0: {0}";
Assert.AreEqual ("Param 0: MyField", rea.FormatErrorMessage ("MyField"), "Error message not correctly updated.");
#if !NET_4_0
rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
#endif
rea.ErrorMessage = "Param 0: {0}; Param 1: {1}";
Assert.AreEqual ("Param 0: MyField; Param 1: [A-Za-z]", rea.FormatErrorMessage ("MyField"), "Error message not correctly updated.");
Assert.AreEqual ("Param 0: ; Param 1: [A-Za-z]", rea.FormatErrorMessage (null), "Error message fails on null value.");
}
[Test]
public void IsValid ()
{
var rea = new RegularExpressionAttributePoker (@"[A-Za-z]");
Assert.IsTrue (rea.IsValid (null), "Null does not match [A-Za-z].");
Assert.IsTrue (rea.IsValid ("A"), "'A' does not match [A-Za-z].");
Assert.IsTrue (rea.IsValid ("a"), "'a' does not match [A-Za-z].");
Assert.IsTrue (rea.IsValid ("Bz"), "'Bz' does not match [A-Za-z].");
Assert.IsTrue (rea.IsValid ("string"), "'string' does not match [A-Za-z].");
Assert.IsFalse (rea.IsValid (String.Empty), "Empty string matches [A-Za-z].");
Assert.IsFalse (rea.IsValid ("0123456789"), "'0123456789' matches [A-Za-z].");
Assert.IsFalse (rea.IsValid ("0123456789"), "'0123456789A' matches [A-Za-z].");
AssertExtensions.Throws<InvalidCastException> (() => {
rea.IsValid (123);
}, "Casting does not fails");
AssertExtensions.Throws<InvalidCastException> (() => {
rea.IsValid (DateTime.Now);
}, "Casting does not fails");
rea = new RegularExpressionAttributePoker ("");
Assert.IsTrue (rea.IsValid (null), "null does not match empty pattern");
Assert.IsTrue (rea.IsValid (String.Empty), "empty string does not match empty pattern");
Assert.IsTrue (rea.IsValid ("string"), "'string' does not match empty pattern");
AssertExtensions.Throws<ArgumentNullException> (() => {
rea = new RegularExpressionAttributePoker (null);
}, "Null pattern allowed");
}
}
}

View File

@@ -0,0 +1,65 @@
//
// RequiredAttributeTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
[TestFixture]
public class RequiredAttributeTest
{
[Test]
public void IsRequired ()
{
var attr = new RequiredAttribute ();
Assert.IsFalse (attr.IsValid (null), "#A1");
Assert.IsFalse (attr.IsValid (String.Empty), "#A2");
Assert.IsTrue (attr.IsValid ("string"), "#A3");
Assert.IsTrue (attr.IsValid (1), "#A4");
#if NET_4_0
attr.AllowEmptyStrings = true;
Assert.IsTrue (attr.IsValid (String.Empty), "#A5");
#endif
}
#if NET_4_0
[Test]
public void AllowEmptyStrings ()
{
var attr = new RequiredAttribute ();
Assert.IsFalse (attr.AllowEmptyStrings, "#A1");
attr.AllowEmptyStrings = true;
Assert.IsTrue (attr.AllowEmptyStrings, "#A2");
}
#endif
}
}

View File

@@ -0,0 +1,133 @@
//
// StringLengthAttributeTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using NUnit.Framework;
using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
[TestFixture]
public class StringLengthAttributeTest
{
class StringLengthAttributePoker : StringLengthAttribute
{
public StringLengthAttributePoker (int maximumLength)
: base (maximumLength)
{ }
public string GetErrorMessageString ()
{
return ErrorMessageString;
}
}
[Test]
public void Constructor ()
{
var sla = new StringLengthAttributePoker (10);
Assert.AreEqual (10, sla.MaximumLength, "#A1-1");
Assert.AreEqual (null, sla.ErrorMessage, "#A1-2");
Assert.AreEqual ("The field {0} must be a string with a maximum length of {1}.", sla.GetErrorMessageString (), "#A1-3");
#if NET_4_0
Assert.AreEqual (0, sla.MinimumLength, "#A1-4");
sla = new StringLengthAttributePoker (-10);
Assert.AreEqual (-10, sla.MaximumLength, "#B1");
#else
AssertExtensions.Throws<ArgumentOutOfRangeException> (() => {
sla = new StringLengthAttributePoker (-10);
}, "#B1");
#endif
sla = new StringLengthAttributePoker (0);
Assert.AreEqual (0, sla.MaximumLength, "#C1");
}
[Test]
public void FormatMessageString ()
{
var sla = new StringLengthAttributePoker (10);
Assert.AreEqual ("The field MyField must be a string with a maximum length of 10.", sla.FormatErrorMessage ("MyField"), "#A1-1");
#if !NET_4_0
sla = new StringLengthAttributePoker (10);
#endif
sla.ErrorMessage = "Param 0: {0}";
Assert.AreEqual ("Param 0: MyField", sla.FormatErrorMessage ("MyField"), "#A1-2");
#if !NET_4_0
sla = new StringLengthAttributePoker (10);
#endif
sla.ErrorMessage = "Param 0: {0}; Param 1: {1}";
Assert.AreEqual ("Param 0: MyField; Param 1: 10", sla.FormatErrorMessage ("MyField"), "#A1-2");
Assert.AreEqual ("Param 0: ; Param 1: 10", sla.FormatErrorMessage (null), "#A1-3");
}
[Test]
public void IsValid ()
{
var sla = new StringLengthAttributePoker (10);
Assert.IsTrue (sla.IsValid (null), "#A1-1");
Assert.IsTrue (sla.IsValid (String.Empty), "#A1-2");
Assert.IsTrue (sla.IsValid ("string"), "#A1-3");
Assert.IsTrue (sla.IsValid ("0123456789"), "#A1-4");
Assert.IsFalse (sla.IsValid ("0123456789A"), "#A1-5");
AssertExtensions.Throws<InvalidCastException> (() => {
sla.IsValid (123);
}, "#A1-6");
AssertExtensions.Throws<InvalidCastException> (() => {
sla.IsValid (DateTime.Now);
}, "#A1-7");
sla = new StringLengthAttributePoker (0);
Assert.IsTrue (sla.IsValid (null), "#B1-1");
Assert.IsTrue (sla.IsValid (String.Empty), "#B1-2");
Assert.IsFalse (sla.IsValid ("string"), "#B1-3");
#if NET_4_0
sla = new StringLengthAttributePoker (-10);
AssertExtensions.Throws <InvalidOperationException> (() => {
sla.IsValid ("123");
}, "#C1-1");
sla = new StringLengthAttributePoker (10);
sla.MinimumLength = 20;
AssertExtensions.Throws<InvalidOperationException> (() => {
sla.IsValid ("123");
}, "#C1-2");
sla.MinimumLength = 5;
Assert.IsFalse (sla.IsValid ("123"), "#C2-1");
Assert.IsTrue (sla.IsValid ("12345"), "#C2-2");
#endif
}
}
}

View File

@@ -0,0 +1,139 @@
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
// https://silverlight.svn.codeplex.com/svn/Release/Silverlight4/Source/RiaClient.Tests/System.ComponentModel.DataAnnotations/UIHintAttributeTest.cs
#if NET_4_0
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
[TestFixture]
public class UIHintAttributeTest {
[Test]
public void UIHintAttribute_Simple_Ctors_Set_Properties() {
var attr = new UIHintAttribute(null, null);
Assert.IsNull(attr.UIHint);
Assert.IsNull(attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
attr = new UIHintAttribute(string.Empty, string.Empty);
Assert.AreEqual(string.Empty, attr.UIHint);
Assert.AreEqual(string.Empty, attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
attr = new UIHintAttribute("theHint");
Assert.AreEqual("theHint", attr.UIHint);
Assert.IsNull(attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
attr = new UIHintAttribute("theHint", "theLayer");
Assert.AreEqual("theHint", attr.UIHint);
Assert.AreEqual("theLayer", attr.PresentationLayer);
Assert.AreEqual(0, attr.ControlParameters.Count);
}
[Test]
public void ConstructorControlParameters() {
Assert.AreEqual(2, new UIHintAttribute("", "", "a", 1, "b", 2).ControlParameters.Keys.Count);
}
[Test]
public void ConstructorControlParameters_NoParams() {
Assert.AreEqual(0, new UIHintAttribute("", "", new object[0]).ControlParameters.Keys.Count);
Assert.AreEqual(0, new UIHintAttribute("", "", (object[])null).ControlParameters.Keys.Count);
Assert.AreEqual(0, new UIHintAttribute("", "").ControlParameters.Keys.Count);
Assert.AreEqual(0, new UIHintAttribute("").ControlParameters.Keys.Count);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_UnevenNumber() {
var attr = new UIHintAttribute("", "", "");
var v = attr.ControlParameters;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_NonStringKey() {
var attr = new UIHintAttribute("", "", 1, "value");
var v = attr.ControlParameters;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_NullKey() {
var attr = new UIHintAttribute("", "", null, "value");
var v = attr.ControlParameters;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ConstructorControlParameters_DuplicateKey() {
var attr = new UIHintAttribute("", "", "key", "value1", "key", "value2");
var v = attr.ControlParameters;
}
[Test]
public void Equals_DifferentObjectType() {
Assert.IsFalse(new UIHintAttribute("foo", "bar").Equals(new object()));
}
[Test]
public void Equals_NullObject() {
Assert.IsFalse(new UIHintAttribute("foo").Equals(null));
}
[Test]
public void Equals_SameObjectType() {
var a1 = new UIHintAttribute("foo");
var a2 = new UIHintAttribute("foo");
var b1 = new UIHintAttribute("foo", "bar");
var b2 = new UIHintAttribute("foo", "bar");
Assert.IsTrue(a1.Equals(a2));
Assert.IsTrue(a2.Equals(a1));
Assert.IsTrue(b1.Equals(b2));
Assert.IsTrue(b2.Equals(b1));
Assert.IsFalse(a1.Equals(b1));
Assert.IsFalse(b1.Equals(a1));
}
[Test]
public void Equals_SameObjectType_WithParamsDictionary() {
var a1 = new UIHintAttribute("foo", "bar", "a", 1, "b", false);
var a2 = new UIHintAttribute("foo", "bar", "b", false, "a", 1);
Assert.IsTrue(a1.Equals(a2));
Assert.IsTrue(a2.Equals(a1));
}
[Test]
public void Equals_DoesNotThrow() {
var a1 = new UIHintAttribute("foo", "bar");
var a2 = new UIHintAttribute("foo", "bar", 1);
Assert.IsFalse(a1.Equals(a2));
Assert.IsFalse(a2.Equals(a1));
}
#if !SILVERLIGHT
[Test]
public void TypeId_ReturnsDifferentValuesForDifferentInstances()
{
Assert.AreNotEqual(new UIHintAttribute("foo").TypeId, new UIHintAttribute("bar").TypeId);
}
#endif
}
}
#endif

View File

@@ -0,0 +1,242 @@
//
// ValidationContextTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_0
[TestFixture]
public class ValidationContextTest
{
[Test]
public void Constructor ()
{
ValidationContext vc;
try {
vc = new ValidationContext (null, new FakeServiceProvider (), new Dictionary <object, object> ());
Assert.Fail ("#A1-1");
} catch (ArgumentNullException) {
// success
}
try {
vc = new ValidationContext ("stuff", null, new Dictionary<object, object> ());
} catch {
Assert.Fail ("#A1-2");
}
try {
vc = new ValidationContext ("stuff", new FakeServiceProvider (), null);
} catch {
Assert.Fail ("#A1-3");
}
object o = "stuff";
vc = new ValidationContext (o, null, null);
Assert.AreSame (o, vc.ObjectInstance, "#A2-1");
Assert.AreEqual (o.GetType ().Name, vc.DisplayName, "#A2-2");
Assert.AreEqual (o.GetType (), vc.ObjectType, "#A2-3");
o = 12;
var dict = new Dictionary<object, object> () {
{"stuff", 1}
};
vc = new ValidationContext (o, null, dict);
Assert.IsNotNull (vc.Items, "#A3-1");
Assert.AreEqual (1, vc.Items.Count, "#A3-2");
Assert.AreNotSame (dict, vc.Items, "#A3-3");
Assert.AreEqual (typeof (Dictionary <object, object>), vc.Items.GetType (), "#A3-4");
Assert.AreEqual (1, vc.Items ["stuff"], "#A3-5");
Assert.AreEqual (o.GetType ().Name, vc.DisplayName, "#A3-6");
Assert.AreEqual (o.GetType (), vc.ObjectType, "#A3-7");
Assert.AreEqual (null, vc.MemberName, "#A3-8");
Assert.IsNotNull (vc.ServiceContainer, "#A3-9");
Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", vc.ServiceContainer.GetType ().FullName, "#A3-10");
}
[Test]
public void ServiceContainer ()
{
var vc = new ValidationContext ("stuff", null, null);
// Test the default container
IServiceContainer container = vc.ServiceContainer;
Assert.AreEqual ("System.ComponentModel.DataAnnotations.ValidationContext+ValidationContextServiceContainer", container.GetType ().FullName, "#A1");
var fs1 = new FakeService1 ();
container.AddService (typeof (FakeService1), fs1);
container.AddService (typeof (FakeService2), CreateFakeService);
var fs3 = new FakeService3 ();
container.AddService (typeof (FakeService3), fs3, false);
container.AddService (typeof (FakeService4), CreateFakeService, false);
try {
container.AddService (null, CreateFakeService);
Assert.Fail ("#A2-1");
} catch (ArgumentNullException) {
// success
}
try {
container.AddService (typeof (string), null);
} catch {
Assert.Fail ("#A2-2");
}
try {
container.AddService (typeof (FakeService2), CreateFakeService);
Assert.Fail ("#A2-3");
} catch (ArgumentException) {
// success
}
try {
container.RemoveService (GetType ());
} catch {
Assert.Fail ("#A2-4");
}
try {
container.RemoveService (null);
Assert.Fail ("#A2-5");
} catch (ArgumentNullException) {
// success
}
try {
container.GetService (null);
} catch (ArgumentNullException) {
// success
}
object o = container.GetService (typeof (FakeService1));
Assert.IsNotNull (o, "#B1-1");
Assert.AreSame (fs1, o, "#B1-2");
o = container.GetService (typeof (FakeService2));
Assert.IsNotNull (o, "#B2-1");
Assert.AreEqual (typeof (FakeService2), o.GetType (), "#B2-2");
o = container.GetService (typeof (FakeService3));
Assert.IsNotNull (o, "#B3-1");
Assert.AreSame (fs3, o, "#B3-2");
o = container.GetService (typeof (FakeService4));
Assert.IsNotNull (o, "#B4-1");
Assert.AreEqual (typeof (FakeService4), o.GetType (), "#B4-2");
o = container.GetService (GetType ());
Assert.IsNull (o, "#B5");
// Test custom container
var fsc = new FakeServiceContainer ();
vc = new ValidationContext ("stuff", fsc, null);
container = vc.ServiceContainer;
Assert.IsNotNull (container, "#B6-1");
// LAMESPEC: MSDN says vc.ServiceContainer should be initialized with the passed container if it implements
// the IServiceContainer interface - not the case, though.
Assert.AreNotSame (fsc, container, "#B6-2");
}
object CreateFakeService (IServiceContainer container, Type serviceType)
{
if (serviceType == typeof (FakeService2))
return Activator.CreateInstance (serviceType);
if (serviceType == typeof (FakeService4))
return Activator.CreateInstance (serviceType);
return null;
}
}
class FakeService1
{ }
class FakeService2
{ }
class FakeService3
{ }
class FakeService4
{ }
class FakeServiceProvider : IServiceProvider
{
#region IServiceProvider Members
public object GetService (Type serviceType)
{
return Activator.CreateInstance (serviceType);
}
#endregion
}
class FakeServiceContainer : IServiceContainer
{
public void AddService (Type serviceType, ServiceCreatorCallback callback, bool promote)
{
}
public void AddService (Type serviceType, ServiceCreatorCallback callback)
{
}
public void AddService (Type serviceType, object serviceInstance, bool promote)
{
}
public void AddService (Type serviceType, object serviceInstance)
{
}
public void RemoveService (Type serviceType, bool promote)
{
}
public void RemoveService (Type serviceType)
{
}
public object GetService (Type serviceType)
{
return null;
}
}
#endif
}

View File

@@ -0,0 +1,97 @@
//
// ValidationResultTest.cs
//
// Authors:
// Marek Habersack <mhabersack@novell.com>
//
// Copyright (C) 2010 Novell, Inc. (http://novell.com/)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.Design;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
#if NET_4_0
[TestFixture]
public class ValidationResultTest
{
[Test]
public void Constructor_String ()
{
var vr = new ValidationResult ("message");
Assert.AreEqual ("message", vr.ErrorMessage, "#A1");
Assert.IsNotNull (vr.MemberNames, "#A2-1");
int count = 0;
foreach (string m in vr.MemberNames)
count++;
Assert.AreEqual (0, count, "#A2-2");
vr = new ValidationResult (null);
Assert.AreEqual (null, vr.ErrorMessage, "#A3");
}
[Test]
public void Constructor_String_IEnumerable ()
{
var vr = new ValidationResult ("message", null);
Assert.AreEqual ("message", vr.ErrorMessage, "#A1");
Assert.IsNotNull (vr.MemberNames, "#A2-1");
int count = 0;
foreach (string m in vr.MemberNames)
count++;
Assert.AreEqual (0, count, "#A2-2");
var names = new string[] { "one", "two" };
vr = new ValidationResult ("message", names);
Assert.AreEqual ("message", vr.ErrorMessage, "#A1");
Assert.IsNotNull (vr.MemberNames, "#A2-1");
Assert.AreSame (names, vr.MemberNames, "#A2-2");
count = 0;
foreach (string m in vr.MemberNames)
count++;
Assert.AreEqual (2, count, "#A2-3");
vr = new ValidationResult (null, null);
Assert.AreEqual (null, vr.ErrorMessage, "#A3");
}
[Test]
public void Success ()
{
ValidationResult success = ValidationResult.Success;
Assert.IsNull (success, "#A1");
}
}
#endif
}