Is.Serializable
"Simone Busoli" <[email protected]> Mon, 11 Aug 2008 16:11:08 +0200
| Newsgroups | gmane.comp.windows.dotnet.nunit.user |
|---|---|
| Message-ID | <[email protected]> |
With regards to: http://sourceforge.net/tracker/index.php?func=detail&aid=2028517&group_id=10749&atid=360749 I have implemented a SerializableConstraint accessible via Is.Serializable. The match is true if serialization to and deserialization from a memory stream via a BinaryFormatter both succeed and the deserialized object is not null. Otherwise the match returns false. ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ Nunit-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/nunit-users
isserializable.patch
(application/octet-stream, 7.5 KB)
Index: solutions/vs2008/NUnitFramework/framework/nunit.framework.dll.csproj
===================================================================
RCS file: /cvsroot/nunit/nunit20/solutions/vs2008/NUnitFramework/framework/nunit.framework.dll.csproj,v
retrieving revision 1.10
diff -u -r1.10 nunit.framework.dll.csproj
--- solutions/vs2008/NUnitFramework/framework/nunit.framework.dll.csproj 9 Aug 2008 04:31:19 -0000 1.10
+++ solutions/vs2008/NUnitFramework/framework/nunit.framework.dll.csproj 11 Aug 2008 14:03:38 -0000
@@ -173,6 +173,9 @@
<Link>Constraints\SameAsConstraint.cs</Link>
<SubType>Code</SubType>
</Compile>
+ <Compile Include="..\..\..\..\src\NUnitFramework\framework\Constraints\SerializableConstraint.cs">
+ <Link>Constraints\SerializableConstraint.cs</Link>
+ </Compile>
<Compile Include="..\..\..\..\src\NUnitFramework\framework\Constraints\StringConstraints.cs">
<Link>Constraints\StringConstraints.cs</Link>
<SubType>Code</SubType>
Index: src/NUnitFramework/framework/Constraints/ConstraintBuilder.cs
===================================================================
RCS file: /cvsroot/nunit/nunit20/src/NUnitFramework/framework/Constraints/ConstraintBuilder.cs,v
retrieving revision 1.16
diff -u -r1.16 ConstraintBuilder.cs
--- src/NUnitFramework/framework/Constraints/ConstraintBuilder.cs 9 Aug 2008 04:31:24 -0000 1.16
+++ src/NUnitFramework/framework/Constraints/ConstraintBuilder.cs 11 Aug 2008 13:47:42 -0000
@@ -142,6 +142,15 @@
{
return Resolve(new CollectionOrderedConstraint(comparer));
}
+
+ /// <summary>
+ /// Resolves the chain of constraints using a
+ /// SerializableConstraint as base.
+ /// </summary>
+ public Constraint Serializable
+ {
+ get { return Resolve(new SerializableConstraint()); }
+ }
#endregion
#region Constraints with an expected value
Index: src/NUnitFramework/framework/Syntax/CSharp/Is.cs
===================================================================
RCS file: /cvsroot/nunit/nunit20/src/NUnitFramework/framework/Syntax/CSharp/Is.cs,v
retrieving revision 1.7
diff -u -r1.7 Is.cs
--- src/NUnitFramework/framework/Syntax/CSharp/Is.cs 9 Aug 2008 04:31:27 -0000 1.7
+++ src/NUnitFramework/framework/Syntax/CSharp/Is.cs 11 Aug 2008 13:31:15 -0000
@@ -87,6 +87,14 @@
get { return new UniqueItemsConstraint(); }
}
+ /// <summary>
+ /// Is.Serializable returns a constraint that tests whether an object is serializable.
+ /// </summary>
+ public static Constraint Serializable
+ {
+ get { return new SerializableConstraint(); }
+ }
+
#endregion
#region Constraints with an expected value
Index: src/NUnitFramework/tests/AssertSyntaxTests.cs
===================================================================
RCS file: /cvsroot/nunit/nunit20/src/NUnitFramework/tests/AssertSyntaxTests.cs,v
retrieving revision 1.19
diff -u -r1.19 AssertSyntaxTests.cs
--- src/NUnitFramework/tests/AssertSyntaxTests.cs 9 Aug 2008 04:31:27 -0000 1.19
+++ src/NUnitFramework/tests/AssertSyntaxTests.cs 11 Aug 2008 14:03:38 -0000
@@ -905,24 +905,55 @@
#endif
}
#endregion
-
- #region Invalid Code Tests
- // This method contains assertions that should not compile
- // You can check by uncommenting the code.
- public void WillNotCompile()
- {
- // Assert.That(42, Is.Not);
- // Assert.That(42, Is.All);
- // Assert.That(42, Is.Null.Not);
- // Assert.That(42, Is.Not.Null.GreaterThan(10));
- // Assert.That(42, Is.GreaterThan(10).LessThan(99));
-
- // object[] c = new object[0];
- // Assert.That(c, Is.Null.All);
- // Assert.That(c, Is.Not.All);
- // Assert.That(c, Is.All.Not);
+
+ #region Serializable Tests
+
+ [Test]
+ public void SerializableClass_IsSerializable()
+ {
+ SerializableClass serializable = new SerializableClass();
+
+ Assert.That(serializable, Is.Serializable);
+ }
+
+ [Test]
+ public void NonSerializalebClass_IsNotSerializable()
+ {
+ NonSerializableClass serializable = new NonSerializableClass();
+
+ Assert.That(serializable, Is.Not.Serializable);
}
- #endregion
+
+ #endregion
+
+
+ #region Invalid Code Tests
+ // This method contains assertions that should not compile
+ // You can check by uncommenting the code.
+ public void WillNotCompile()
+ {
+ // Assert.That(42, Is.Not);
+ // Assert.That(42, Is.All);
+ // Assert.That(42, Is.Null.Not);
+ // Assert.That(42, Is.Not.Null.GreaterThan(10));
+ // Assert.That(42, Is.GreaterThan(10).LessThan(99));
+
+ // object[] c = new object[0];
+ // Assert.That(c, Is.Null.All);
+ // Assert.That(c, Is.Not.All);
+ // Assert.That(c, Is.All.Not);
+ }
+ #endregion
}
+ [Serializable]
+ public class SerializableClass
+ {
+ public string SomeProperty { get; set; }
+ }
+
+ public class NonSerializableClass
+ {
+ public string SomeProperty { get; set; }
+ }
}
--- SerializableConstraint.cs
+++ SerializableConstraint.cs
@@ -0,0 +1,66 @@
+using System;
+using System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+
+namespace NUnit.Framework.Constraints
+{
+ ///<summary>
+ ///</summary>
+ public class SerializableConstraint : Constraint
+ {
+ /// <summary>
+ /// Test whether the constraint is satisfied by a given value
+ /// </summary>
+ /// <param name="actual">The value to be tested</param>
+ /// <returns>True for success, false for failure</returns>
+ public override bool Matches(object actual)
+ {
+ BinaryFormatter formatter = new BinaryFormatter();
+
+ try
+ {
+ var stream = new MemoryStream();
+ formatter.Serialize(stream, actual);
+
+ stream.Seek(0, SeekOrigin.Begin);
+
+ object returned = formatter.Deserialize(stream);
+
+ if (returned == null)
+ return false;
+
+ return true;
+ }
+ catch (SerializationException)
+ {
+ return false;
+ }
+ catch (ArgumentNullException)
+ {
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// Write the constraint description to a MessageWriter
+ /// </summary>
+ /// <param name="writer">The writer on which the description is displayed</param>
+ public override void WriteDescriptionTo(MessageWriter writer)
+ {
+ writer.Write("serializable");
+ }
+
+ /// <summary>
+ /// Write the actual value for a failing constraint test to a
+ /// MessageWriter. The default implementation simply writes
+ /// the raw value of actual, leaving it to the writer to
+ /// perform any formatting.
+ /// </summary>
+ /// <param name="writer">The writer on which the actual value is displayed</param>
+ public override void WriteActualValueTo(MessageWriter writer)
+ {
+ writer.Write("non serializable");
+ }
+ }
+}