Skip to content

Commit

Permalink
Fix XmlSerialization tests for MongoRegex.
Browse files Browse the repository at this point in the history
Add some more MongoRegex tests.
  • Loading branch information
lanwin committed May 22, 2010
1 parent 6dae6e1 commit fbe807b
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 23 deletions.
31 changes: 30 additions & 1 deletion source/MongoDB.Tests/UnitTests/TestMongoRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ namespace MongoDB.UnitTests
[TestFixture]
public class TestMongoRegex
{
[Test]
public void CanBeCunstructedFromNullExceptionAndOptions()
{
var regex = new MongoRegex(null, null);
Assert.IsNull(regex.Expression);
Assert.IsNull(regex.Options);
}

[Test]
public void CanBeConstructed()
{
var regex = new MongoRegex("expression", "options");
Assert.AreEqual("expression",regex.Expression);
Assert.AreEqual("options",regex.Options);
}

[Test]
public void CanBeBinarySerialized()
{
Expand All @@ -27,7 +43,20 @@ public void CanBeBinarySerialized()
public void CanBeXmlSerialized()
{
var source = new MongoRegex("exp", "opt");
var serializer = new XmlSerializer(typeof(Oid));
var serializer = new XmlSerializer(typeof(MongoRegex));

var writer = new StringWriter();
serializer.Serialize(writer, source);
var dest = (MongoRegex)serializer.Deserialize(new StringReader(writer.ToString()));

Assert.AreEqual(source, dest);
}

[Test]
public void CanBeXmlSerializedWhenNullPropertys()
{
var source = new MongoRegex(null, null);
var serializer = new XmlSerializer(typeof(MongoRegex));

var writer = new StringWriter();
serializer.Serialize(writer, source);
Expand Down
112 changes: 90 additions & 22 deletions source/MongoDB/MongoRegex.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace MongoDB
{
/// <summary>
/// </summary>
[Serializable]
public class MongoRegex
public class MongoRegex : IEquatable<MongoRegex>, IXmlSerializable
{
/// <summary>
/// Initializes a new instance of the <see cref = "MongoRegex" /> class.
Expand Down Expand Up @@ -35,22 +38,6 @@ public MongoRegex(string expression, string options)
Options = options;
}

/// <summary>
/// Initializes a new instance of the <see cref="MongoRegex"/> class.
/// </summary>
/// <param name="regex">The regex.</param>
public MongoRegex(Regex regex)
{
if(regex == null)
throw new ArgumentNullException("regex");

Expression = regex.ToString();
Options = string.Empty;

if((regex.Options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase)
Options += "i";
}

/// <summary>
/// A valid regex string including the enclosing / characters.
/// </summary>
Expand All @@ -75,11 +62,49 @@ public MongoRegex(Regex regex)
/// </exception>
public override bool Equals(object obj)
{
var other = obj as MongoRegex;
if(other == null)
if(ReferenceEquals(null, obj))
return false;
if(ReferenceEquals(this, obj))
return true;
return obj.GetType() == typeof(MongoRegex) && Equals((MongoRegex)obj);
}

return Expression == other.Expression && Options == other.Options;
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
public bool Equals(MongoRegex other)
{
if(ReferenceEquals(null, other))
return false;
if(ReferenceEquals(this, other))
return true;
return Equals(other.Expression, Expression) && Equals(other.Options, Options);
}

/// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(MongoRegex left, MongoRegex right)
{
return Equals(left, right);
}

/// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(MongoRegex left, MongoRegex right)
{
return !Equals(left, right);
}

/// <summary>
Expand All @@ -90,8 +115,10 @@ public override bool Equals(object obj)
/// </returns>
public override int GetHashCode()
{
return (Expression ?? string.Empty).GetHashCode()
^ (Options ?? string.Empty).GetHashCode();
unchecked
{
return ((Expression != null ? Expression.GetHashCode() : 0)*397) ^ (Options != null ? Options.GetHashCode() : 0);
}
}

/// <summary>
Expand All @@ -104,5 +131,46 @@ public override string ToString()
{
return string.Format("{0}{1}", Expression, Options);
}

/// <summary>
/// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
/// </summary>
/// <returns>
/// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
/// </returns>
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}

/// <summary>
/// Generates an object from its XML representation.
/// </summary>
/// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
void IXmlSerializable.ReadXml(XmlReader reader)
{
if(reader.MoveToAttribute("options"))
Options = reader.Value;

if(reader.IsEmptyElement)
return;

Expression = reader.ReadString();
}

/// <summary>
/// Converts an object into its XML representation.
/// </summary>
/// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
void IXmlSerializable.WriteXml(XmlWriter writer)
{
if(Options!=null)
writer.WriteAttributeString("options", Options);

if(Expression==null)
return;

writer.WriteString(Expression);
}
}
}

0 comments on commit fbe807b

Please sign in to comment.