PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/SerializableConstraints.cs

#
C# | 170 lines | 91 code | 28 blank | 51 comment | 6 complexity | abb29ae9114b9950a6c589da18a656fd MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.IO;
  8. #if !NETCF
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. #endif
  12. using System.Xml.Serialization;
  13. namespace NUnit.Framework.Constraints
  14. {
  15. #if !NETCF
  16. #region BinarySerializableConstraint
  17. /// <summary>
  18. /// BinarySerializableConstraint tests whether
  19. /// an object is serializable in binary format.
  20. /// </summary>
  21. public class BinarySerializableConstraint : Constraint
  22. {
  23. readonly BinaryFormatter serializer = new BinaryFormatter();
  24. /// <summary>
  25. /// Test whether the constraint is satisfied by a given value
  26. /// </summary>
  27. /// <param name="actual">The value to be tested</param>
  28. /// <returns>True for success, false for failure</returns>
  29. public override bool Matches(object actual)
  30. {
  31. this.actual = actual;
  32. if(actual == null)
  33. throw new ArgumentException();
  34. MemoryStream stream = new MemoryStream();
  35. try
  36. {
  37. serializer.Serialize(stream, actual);
  38. stream.Seek(0, SeekOrigin.Begin);
  39. object value = serializer.Deserialize(stream);
  40. return value != null;
  41. }
  42. catch (SerializationException)
  43. {
  44. return false;
  45. }
  46. }
  47. /// <summary>
  48. /// Write the constraint description to a MessageWriter
  49. /// </summary>
  50. /// <param name="writer">The writer on which the description is displayed</param>
  51. public override void WriteDescriptionTo(MessageWriter writer)
  52. {
  53. writer.Write("binary serializable");
  54. }
  55. /// <summary>
  56. /// Write the actual value for a failing constraint test to a
  57. /// MessageWriter. The default implementation simply writes
  58. /// the raw value of actual, leaving it to the writer to
  59. /// perform any formatting.
  60. /// </summary>
  61. /// <param name="writer">The writer on which the actual value is displayed</param>
  62. public override void WriteActualValueTo(MessageWriter writer)
  63. {
  64. writer.Write("<{0}>", actual.GetType().Name);
  65. }
  66. /// <summary>
  67. /// Returns the string representation
  68. /// </summary>
  69. protected override string GetStringRepresentation()
  70. {
  71. return "<binaryserializable>";
  72. }
  73. }
  74. #endregion
  75. #endif
  76. #if !NETCF_1_0
  77. #region XmlSerializableConstraint
  78. /// <summary>
  79. /// BinarySerializableConstraint tests whether
  80. /// an object is serializable in binary format.
  81. /// </summary>
  82. public class XmlSerializableConstraint : Constraint
  83. {
  84. private XmlSerializer serializer;
  85. /// <summary>
  86. /// Test whether the constraint is satisfied by a given value
  87. /// </summary>
  88. /// <param name="actual">The value to be tested</param>
  89. /// <returns>True for success, false for failure</returns>
  90. public override bool Matches(object actual)
  91. {
  92. this.actual = actual;
  93. if(actual == null)
  94. throw new ArgumentException();
  95. MemoryStream stream = new MemoryStream();
  96. try
  97. {
  98. serializer = new XmlSerializer(actual.GetType());
  99. serializer.Serialize(stream, actual);
  100. stream.Seek(0, SeekOrigin.Begin);
  101. object value = serializer.Deserialize(stream);
  102. return value != null;
  103. }
  104. catch (NotSupportedException)
  105. {
  106. return false;
  107. }
  108. catch (InvalidOperationException)
  109. {
  110. return false;
  111. }
  112. }
  113. /// <summary>
  114. /// Write the constraint description to a MessageWriter
  115. /// </summary>
  116. /// <param name="writer">The writer on which the description is displayed</param>
  117. public override void WriteDescriptionTo(MessageWriter writer)
  118. {
  119. writer.Write("xml serializable");
  120. }
  121. /// <summary>
  122. /// Write the actual value for a failing constraint test to a
  123. /// MessageWriter. The default implementation simply writes
  124. /// the raw value of actual, leaving it to the writer to
  125. /// perform any formatting.
  126. /// </summary>
  127. /// <param name="writer">The writer on which the actual value is displayed</param>
  128. public override void WriteActualValueTo(MessageWriter writer)
  129. {
  130. writer.Write("<{0}>", actual.GetType().Name);
  131. }
  132. /// <summary>
  133. /// Returns the string representation of this constraint
  134. /// </summary>
  135. protected override string GetStringRepresentation()
  136. {
  137. return "<xmlserializable>";
  138. }
  139. }
  140. #endregion
  141. #endif
  142. }