using System.Text;
using NUnit.Framework;
+using MonoTests.Common;
namespace MonoTests.System.ComponentModel.DataAnnotations
{
attr.ErrorMessage = "Test";
Assert.AreEqual ("Test", attr.ErrorMessage, "#A2");
-#if NET_4_0
attr.ErrorMessage = String.Empty;
Assert.AreEqual (String.Empty, attr.ErrorMessage, "#A3");
attr.ErrorMessage = null;
Assert.IsNull (attr.ErrorMessage, "#A4");
-#else
- try {
- attr.ErrorMessage = String.Empty;
- Assert.Fail ("#A3");
- } catch (InvalidOperationException) {
- // success
- }
-
- attr = new ValidateFooAttribute ("Test");
- try {
- attr.ErrorMessage = null;
- Assert.Fail ("#A4");
- } catch (ArgumentException) {
- // success
- }
-
- attr = new ValidateFooAttribute ("Test");
- try {
- attr.ErrorMessage = String.Empty;
- Assert.Fail ("#A4");
- } catch (ArgumentException) {
- // success
- }
-
- attr = new ValidateFooAttribute ();
- attr.ErrorMessageResourceName = "ErrorProperty1";
-
- try {
- attr.ErrorMessage = "Test Message";
- Assert.Fail ("#E1");
- } catch (InvalidOperationException) {
- // success
- }
-#endif
}
attr.ErrorMessageResourceName = "Test";
Assert.IsNotNull (attr.ErrorMessageResourceName, "#A2-1");
Assert.AreEqual ("Test", attr.ErrorMessageResourceName, "#A2-2");
-#if NET_4_0
attr.ErrorMessageResourceName = String.Empty;
Assert.IsNotNull (attr.ErrorMessageResourceName, "#A3-1");
Assert.AreEqual (String.Empty, attr.ErrorMessageResourceName, "#A3-2");
attr.ErrorMessageResourceName = null;
Assert.IsNull (attr.ErrorMessageResourceName, "#A3-1");
-#else
- try {
- attr.ErrorMessageResourceName = String.Empty;
- Assert.Fail ("#A3-1");
- } catch (InvalidOperationException) {
- // success
- }
-
- attr = new ValidateFooAttribute ("Test");
- try {
- attr.ErrorMessageResourceName = String.Empty;
- Assert.Fail ("#A3-2");
- } catch (ArgumentException) {
- // success
- }
-
- attr = new ValidateFooAttribute ("Test");
- try {
- attr.ErrorMessageResourceName = null;
- Assert.Fail ("#A3-3");
- } catch (ArgumentException) {
- // success
- }
-
- attr = new ValidateFooAttribute ();
- attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
-
- try {
- attr.ErrorMessageResourceName = "NoSuchProperty";
- Assert.Fail ("#A3-4");
- } catch (InvalidOperationException) {
- // success
- }
-#endif
}
[Test]
attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
Assert.IsNotNull (attr.ErrorMessageResourceType, "#A2-1");
Assert.AreEqual (typeof (FooErrorMessageProvider), attr.ErrorMessageResourceType, "#A2-2");
-#if !NET_4_0
- attr = new ValidateFooAttribute ();
- attr.ErrorMessageResourceName = "NoSuchProperty";
-
- try {
- attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
- Assert.Fail ("#A3");
- } catch (InvalidOperationException) {
- // success
- }
-#endif
}
[Test]
} catch (InvalidOperationException) {
// success
}
-#if NET_4_0
attr = new ValidateFooAttribute ();
attr.ErrorMessageResourceName = String.Empty;
try {
} catch (InvalidOperationException) {
// success
}
-#endif
attr = new ValidateFooAttribute ();
attr.ErrorMessageResourceName = "ErrorProperty1";
attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
Assert.IsNotNull (attr.GetErrorMessageString (), "#D1-3");
Assert.AreEqual ("Error Message 1", attr.GetErrorMessageString (), "#D1-4");
-#if NET_4_0
attr.ErrorMessage = "Test Message";
try {
attr.GetErrorMessageString ();
} catch (InvalidOperationException) {
// success
}
-#endif
}
[Test]
attr.ErrorMessage = "Test: {0}";
Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#A2-1");
Assert.AreEqual ("Test: SomeField", attr.FormatErrorMessage ("SomeField"), "#A2-2");
-#if !NET_4_0
- attr = new ValidateFooAttribute ();
-#else
attr.ErrorMessage = null;
-#endif
attr.ErrorMessageResourceName = "ErrorProperty1";
attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B1-1");
Assert.AreEqual ("Error Message 1", attr.FormatErrorMessage ("SomeField"), "#B1-2");
-#if !NET_4_0
- attr = new ValidateFooAttribute ();
-#endif
attr.ErrorMessageResourceName = "ErrorProperty6";
attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B2-1");
Assert.AreEqual ("Error Message 6: SomeField", attr.FormatErrorMessage ("SomeField"), "#B2-2");
-#if !NET_4_0
- attr = new ValidateFooAttribute ();
-#endif
attr.ErrorMessageResourceName = "ErrorProperty6";
attr.ErrorMessageResourceType = typeof (FooErrorMessageProvider);
Assert.IsNotNull (attr.FormatErrorMessage ("SomeField"), "#B3-1");
Assert.AreEqual ("Error Message 6: ", attr.FormatErrorMessage (null), "#B3-2");
}
-#if NET_4_0
[Test]
public void GetValidationResult ()
{
{
var attr = new ValidateFooAttribute ();
- try {
+ AssertExtensions.Throws <NotImplementedException> (() => {
+ // It calls IsValid (object, validationContext) which throws the NIEX, but when that overload is called directly, there's
+ // no exception.
+ //
+ // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
+ // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 450
attr.IsValid (null);
- Assert.Fail ("#A1-1");
- } catch (NotImplementedException) {
- // success
- }
-
- try {
+ }, "#A1-1");
+
+ AssertExtensions.Throws <NotImplementedException> (() => {
attr.IsValid ("stuff");
- Assert.Fail ("#A1-2");
- } catch (NotImplementedException) {
- // success
- }
+ }, "#A1-2");
}
[Test]
{
var attr = new ValidateBarAttribute ();
- try {
- // ...
+ AssertExtensions.Throws <NullReferenceException> (() => {
attr.CallIsValid (null, null);
- Assert.Fail ("#A1");
- } catch (NullReferenceException) {
- // success
- }
+ }, "#A1");
var vc = new ValidationContext ("stuff", null, null);
var vr = attr.CallIsValid (null, vc);
Assert.AreEqual ("SomeMember", list [0], "#A3-6");
}
+ [Test]
+ public void IsValid_Object_ValidationContext_CrossCallsWithNIEX ()
+ {
+ var attr = new ValidateSomethingAttribute ();
+
+ AssertExtensions.Throws<NotImplementedException> (() => {
+ // Thrown from the IsValid (object, ValidationContext) overload!
+ //
+ // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02:
+ // System.NotImplementedException : IsValid(object value) has not been implemented by this class. The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
+ //
+ // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 639
+ // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext)
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 639
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_02() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 514
+ attr.IsValid ("stuff");
+ }, "#A1");
+
+ AssertExtensions.Throws<NotImplementedException> (() => {
+ // And this one is thrown from the IsValid (object) overload!
+ //
+ // MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX:
+ // System.NotImplementedException : IsValid(object value) has not been implemented by this class. The preferred entry point is GetValidationResult() and classes should override IsValid(object value, ValidationContext context).
+ //
+ // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 660
+ // at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value)
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.IsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 660
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidateSomethingAttribute.CallIsValid(Object value, ValidationContext validationContext) in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 667
+ // at MonoTests.System.ComponentModel.DataAnnotations.ValidationAttributeTest.IsValid_Object_ValidationContext_CrossCallsWithNIEX() in C:\Users\grendel\Documents\Visual Studio 2010\Projects\System.Web.Test\System.Web.Test\System.ComponentModel.DataAnnotations\ValidationAttributeTest.cs:line 530
+
+ attr.CallIsValid ("stuff", null);
+ }, "#A2");
+ }
[Test]
public void Validate_Object_ValidationContext ()
{
Assert.AreEqual ("bool IsValid (object value)", attr.Calls [1], "#A2-3");
Assert.AreEqual ("string FormatErrorMessage (string name)", attr.Calls [2], "#A2-4");
}
-#endif
[Test]
public void Validate_Object_String ()
{
{
return ErrorMessageString;
}
-#if !NET_4_0
- public override bool IsValid (object value)
- {
- return value != null;
- }
-#endif
}
class ValidateBarAttribute : ValidateFooAttribute
{
return value != null;
}
-#if NET_4_0
protected override ValidationResult IsValid (object value, ValidationContext validationContext)
{
if (!IsValid (value))
{
return base.IsValid (value, validationContext);
}
-#endif
}
class ValidateBazAttribute : ValidateBarAttribute
Calls.Add ("bool IsValid (object value)");
return base.IsValid (value);
}
-#if NET_4_0
protected override ValidationResult IsValid (object value, ValidationContext validationContext)
{
Calls.Add ("ValidationResult IsValid (object value, ValidationContext validationContext)");
return base.IsValid (value, validationContext);
}
-#endif
public override string FormatErrorMessage (string name)
{
Calls.Add ("string FormatErrorMessage (string name)");
return base.FormatErrorMessage (name);
}
}
+ class ValidateSomethingAttribute : ValidationAttribute
+ {
+ public override bool IsValid (object value)
+ {
+ return base.IsValid (value, null) == ValidationResult.Success;
+ }
+
+ protected override ValidationResult IsValid (object value, ValidationContext validationContext)
+ {
+ if (base.IsValid (value))
+ return ValidationResult.Success;
+ return new ValidationResult ("failed to validate in base class");
+ }
+ public ValidationResult CallIsValid (object value, ValidationContext validationContext)
+ {
+ return IsValid (value, validationContext);
+ }
+ }
class FooErrorMessageProvider
{
public static string ErrorProperty1