diff --git a/src/MongoDB.Bson/MongoDB.Bson.csproj b/src/MongoDB.Bson/MongoDB.Bson.csproj index 6b88210e189..71ffb458fa9 100644 --- a/src/MongoDB.Bson/MongoDB.Bson.csproj +++ b/src/MongoDB.Bson/MongoDB.Bson.csproj @@ -20,7 +20,9 @@ - + + + diff --git a/src/MongoDB.Bson/Serialization/BsonClassMap.cs b/src/MongoDB.Bson/Serialization/BsonClassMap.cs index 61695ad3966..daedccbdbe3 100644 --- a/src/MongoDB.Bson/Serialization/BsonClassMap.cs +++ b/src/MongoDB.Bson/Serialization/BsonClassMap.cs @@ -19,10 +19,10 @@ using System.Linq; using System.Linq.Expressions; using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.Serialization; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization.Conventions; +using MongoDB.Shared; namespace MongoDB.Bson.Serialization { @@ -542,6 +542,35 @@ public object CreateInstance() return creator.Invoke(); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is BsonClassMap other && + _frozen.Equals(true) && other._frozen.Equals(true) && // BsonClassMaps should only be equal if they are frozen + object.Equals(_baseClassMap, other._baseClassMap) && + object.Equals(_classType, other._classType) && + object.Equals(_creator, other._creator) && + SequenceComparer.Equals(_creatorMaps, other._creatorMaps) && + SequenceComparer.Equals(_declaredMemberMaps, other._declaredMemberMaps) && + object.Equals(_discriminator, other._discriminator) && + _discriminatorIsRequired.Equals(other._discriminatorIsRequired) && + _extraElementsMemberIndex.Equals(other._extraElementsMemberIndex) && + object.Equals(_extraElementsMemberMap, other._extraElementsMemberMap) && + _hasRootClass.Equals(other._hasRootClass) && + object.Equals(_idMemberMap, other._idMemberMap) && + _ignoreExtraElements.Equals(other._ignoreExtraElements) && + _ignoreExtraElementsIsInherited.Equals(other._ignoreExtraElementsIsInherited) && + _isRootClass.Equals(other._isRootClass) && + SequenceComparer.Equals(_knownTypes, other._knownTypes); + } + + /// + public override int GetHashCode() => 0; + /// /// Freezes the class map. /// diff --git a/src/MongoDB.Bson/Serialization/BsonMemberMap.cs b/src/MongoDB.Bson/Serialization/BsonMemberMap.cs index 734622d6f4e..0aab89027e6 100644 --- a/src/MongoDB.Bson/Serialization/BsonMemberMap.cs +++ b/src/MongoDB.Bson/Serialization/BsonMemberMap.cs @@ -264,6 +264,32 @@ public void Freeze() _frozen = true; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is BsonMemberMap other && + _frozen.Equals(true) && other._frozen.Equals(true) && // BsonMemberMaps should only be equal if they are frozen + object.Equals(_defaultValue, other._defaultValue) && + object.Equals(_defaultValueCreator, other._defaultValueCreator) && + _defaultValueSpecified.Equals(other._defaultValueSpecified) && + object.Equals(_elementName, other._elementName) && + object.Equals(_idGenerator, other._idGenerator) && + _ignoreIfDefault.Equals(other._ignoreIfDefault) && + _ignoreIfNull.Equals(other._ignoreIfNull) && + _isRequired.Equals(other._isRequired) && + object.Equals(_memberInfo, other._memberInfo) && + _order.Equals(other._order) && + object.Equals(_serializer, other._serializer) && + object.Equals(_shouldSerializeMethod, other._shouldSerializeMethod); + } + + /// + public override int GetHashCode() => 0; + /// /// Gets the serializer. /// diff --git a/src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs b/src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs index 6f4e7952c10..c3a79af5d02 100644 --- a/src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs +++ b/src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs @@ -127,6 +127,23 @@ public object DeserializeValue(BsonValue value) } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is BsonSerializationInfo other && + object.Equals(_elementName, other._elementName) && + object.Equals(_elementPath, other._elementPath) && + object.Equals(_nominalType, other._nominalType) && + object.Equals(_serializer, other._serializer); + } + + /// + public override int GetHashCode() => 0; + /// /// Merges the new BsonSerializationInfo by taking its properties and concatenating its ElementName. /// diff --git a/src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs b/src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs index 0742db7b3bb..9a0a7683368 100644 --- a/src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs +++ b/src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs @@ -69,6 +69,17 @@ public string ElementName } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is ObjectDiscriminatorConvention other && + object.Equals(_elementName, other._elementName); + } + /// /// Gets the actual type of an object by reading the discriminator from a BsonReader. /// @@ -143,5 +154,8 @@ public BsonValue GetDiscriminator(Type nominalType, Type actualType) { return TypeNameDiscriminator.GetDiscriminator(actualType); } + + /// + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Bson/Serialization/Conventions/StandardDiscriminatorConvention.cs b/src/MongoDB.Bson/Serialization/Conventions/StandardDiscriminatorConvention.cs index f61ded8ac81..ca4d2cbc117 100644 --- a/src/MongoDB.Bson/Serialization/Conventions/StandardDiscriminatorConvention.cs +++ b/src/MongoDB.Bson/Serialization/Conventions/StandardDiscriminatorConvention.cs @@ -77,6 +77,17 @@ public string ElementName } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is StandardDiscriminatorConvention other && + object.Equals(_elementName, other._elementName); + } + /// /// Gets the actual type of an object by reading the discriminator from a BsonReader. /// @@ -123,5 +134,8 @@ public Type GetActualType(IBsonReader bsonReader, Type nominalType) /// The actual type. /// The discriminator value. public abstract BsonValue GetDiscriminator(Type nominalType, Type actualType); + + /// + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs b/src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs index 801dc2b604e..93c4a5cc77f 100644 --- a/src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs +++ b/src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs @@ -14,7 +14,6 @@ */ using System; -using MongoDB.Bson.Serialization.Attributes; namespace MongoDB.Bson.Serialization.Options { @@ -57,6 +56,21 @@ public bool AllowTruncation } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is RepresentationConverter other && + _allowOverflow.Equals(other._allowOverflow) && + _allowTruncation.Equals(other._allowTruncation); + } + + /// + public override int GetHashCode() => 0; + /// /// Converts a Decimal128 to a Decimal. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BitArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BitArraySerializer.cs index eed92f68d26..779d169f3dc 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BitArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BitArraySerializer.cs @@ -34,7 +34,6 @@ private static class Flags // private fields private readonly SerializerHelper _helper; - private readonly Int32Serializer _int32Serializer = new Int32Serializer(); private readonly BsonType _representation; // constructors @@ -85,6 +84,21 @@ public BsonType Representation } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is BitArraySerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + + // protected methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// /// Deserializes a value. @@ -110,7 +124,7 @@ protected override BitArray DeserializeValue(BsonDeserializationContext context, { switch (flag) { - case Flags.Length: length = _int32Serializer.Deserialize(context); break; + case Flags.Length: length = Int32Serializer.Instance.Deserialize(context); break; case Flags.Bytes: bytes = bsonReader.ReadBytes(); break; } }); diff --git a/src/MongoDB.Bson/Serialization/Serializers/BooleanSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BooleanSerializer.cs index e476061bc85..340027eba6b 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BooleanSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BooleanSerializer.cs @@ -121,6 +121,20 @@ public override bool Deserialize(BsonDeserializationContext context, BsonDeseria } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is BooleanSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs index a1ae0ffe900..d667894ce62 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs index 341f388df73..2dbef9d0946 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs index a3747f6ee2a..65d2cad5cc0 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs index bc1ad98ab47..1a536f29dec 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs @@ -295,6 +295,20 @@ public TClass DeserializeClass(BsonDeserializationContext context) return CreateInstanceUsingCreator(values); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is BsonClassMapSerializer other && + object.Equals(_classMap, other._classMap); + } + + /// + public override int GetHashCode() => 0; + /// /// Gets the document Id. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonDateTimeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonDateTimeSerializer.cs index f6faa268d7e..ffb13c7f124 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonDateTimeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonDateTimeSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentBackedClassSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentBackedClassSerializer.cs index e8816611c89..f3df24f3559 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentBackedClassSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentBackedClassSerializer.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Shared; namespace MongoDB.Bson.Serialization { @@ -39,6 +40,20 @@ protected BsonDocumentBackedClassSerializer() } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is BsonDocumentBackedClassSerializer other && + DictionaryComparer.Equals(_memberSerializationInfo, other._memberSerializationInfo); + } + + /// + public override int GetHashCode() => 0; + /// /// Tries to get the serialization info for a member. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs index 57bd01ac8a8..e053c26fe10 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs @@ -14,8 +14,6 @@ */ using System; -using System.Collections.Generic; -using System.Linq; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization.IdGenerators; @@ -47,7 +45,7 @@ public static BsonDocumentSerializer Instance get { return __instance; } } - // public methods + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs index 4ad357c7201..79acf5d2572 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs index fe44d9b5ce7..38d1c300a91 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs index 60c9311ea22..c16cc4ed82c 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptSerializer.cs index b4fb4754b1f..8a66794a88b 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptWithScopeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptWithScopeSerializer.cs index ed9aacda925..229e862e6d9 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptWithScopeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptWithScopeSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonMaxKeySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonMaxKeySerializer.cs index cb2fb896f24..7da69f060a4 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonMaxKeySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonMaxKeySerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonMinKeySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonMinKeySerializer.cs index 0603b75c9b9..8c9452d1f79 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonMinKeySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonMinKeySerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonNullSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonNullSerializer.cs index 9c228e52ef9..a3e487b9123 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonNullSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonNullSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonObjectIdSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonObjectIdSerializer.cs index a2de78411aa..ea64294d12b 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonObjectIdSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonObjectIdSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonRegularExpressionSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonRegularExpressionSerializer.cs index e5f1d6bcecb..421f1c658bd 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonRegularExpressionSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonRegularExpressionSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonStringSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonStringSerializer.cs index fd5dc3e5f9a..40c310436f9 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonStringSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonStringSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonSymbolSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonSymbolSerializer.cs index e0966dd44e0..26da0e4ec12 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonSymbolSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonSymbolSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonTimestampSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonTimestampSerializer.cs index d4d68c90b24..5caf461bd62 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonTimestampSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonTimestampSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonUndefinedSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonUndefinedSerializer.cs index f3c7e5e3ea1..375162ed2c9 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonUndefinedSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonUndefinedSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonValueCSharpNullSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonValueCSharpNullSerializer.cs index f639fb2c1c7..46a0dfb4d55 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonValueCSharpNullSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonValueCSharpNullSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ -using System.Collections.Generic; using MongoDB.Bson.IO; namespace MongoDB.Bson.Serialization.Serializers @@ -65,6 +64,20 @@ public override TBsonValue Deserialize(BsonDeserializationContext context, BsonD return _wrappedSerializer.Deserialize(context); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is BsonValueCSharpNullSerializer other && + object.Equals(_wrappedSerializer, other._wrappedSerializer); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializer.cs index f1384a7ad56..7cc652ec5bf 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializer.cs @@ -13,9 +13,6 @@ * limitations under the License. */ - -using System.Collections.Generic; -using System.Linq; namespace MongoDB.Bson.Serialization.Serializers { /// @@ -44,7 +41,7 @@ public static BsonValueSerializer Instance get { return __instance; } } - // public methods + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs index d106ab7c0e9..e3a5776870c 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/ByteArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ByteArraySerializer.cs index ce17a67ab89..ff3c051192c 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ByteArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ByteArraySerializer.cs @@ -78,6 +78,21 @@ public BsonType Representation } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ByteArraySerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + + // protected methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// /// Deserializes a value. diff --git a/src/MongoDB.Bson/Serialization/Serializers/ByteSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ByteSerializer.cs index aba46107f76..1abd856d086 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ByteSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ByteSerializer.cs @@ -15,10 +15,6 @@ using System; using System.Globalization; -using System.IO; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { @@ -132,6 +128,20 @@ public override byte Deserialize(BsonDeserializationContext context, BsonDeseria return value; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ByteSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/CharSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/CharSerializer.cs index fef5ad10f8c..796fbd0cafa 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/CharSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/CharSerializer.cs @@ -14,10 +14,6 @@ */ using System; -using System.IO; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { @@ -99,6 +95,20 @@ public override char Deserialize(BsonDeserializationContext context, BsonDeseria } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is CharSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/CultureInfoSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/CultureInfoSerializer.cs index cdc853e1c41..e02ab205706 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/CultureInfoSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/CultureInfoSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ -using System; using System.Globalization; using MongoDB.Bson.IO; @@ -32,7 +31,6 @@ private static class Flags } // private fields - private readonly BooleanSerializer _booleanSerializer = new BooleanSerializer(); private readonly SerializerHelper _helper; // constructors @@ -48,7 +46,7 @@ public CultureInfoSerializer() ); } - // public methods + // protected methods /// /// Deserializes a value. /// @@ -70,7 +68,7 @@ protected override CultureInfo DeserializeValue(BsonDeserializationContext conte switch (flag) { case Flags.Name: name = bsonReader.ReadString(); break; - case Flags.UseUserOverride: useUserOverride = _booleanSerializer.Deserialize(context); break; + case Flags.UseUserOverride: useUserOverride = BooleanSerializer.Instance.Deserialize(context); break; } }); return new CultureInfo(name, useUserOverride); diff --git a/src/MongoDB.Bson/Serialization/Serializers/DateTimeOffsetSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DateTimeOffsetSerializer.cs index 48ecf234a86..0aa1052c7f2 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DateTimeOffsetSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DateTimeOffsetSerializer.cs @@ -33,8 +33,6 @@ private static class Flags // private fields private readonly SerializerHelper _helper; - private readonly Int32Serializer _int32Serializer = new Int32Serializer(); - private readonly Int64Serializer _int64Serializer = new Int64Serializer(); private readonly BsonType _representation; // constructors @@ -105,8 +103,8 @@ public override DateTimeOffset Deserialize(BsonDeserializationContext context, B { case BsonType.Array: bsonReader.ReadStartArray(); - ticks = _int64Serializer.Deserialize(context); - offset = TimeSpan.FromMinutes(_int32Serializer.Deserialize(context)); + ticks = Int64Serializer.Instance.Deserialize(context); + offset = TimeSpan.FromMinutes(Int32Serializer.Instance.Deserialize(context)); bsonReader.ReadEndArray(); return new DateTimeOffset(ticks, offset); @@ -122,8 +120,8 @@ public override DateTimeOffset Deserialize(BsonDeserializationContext context, B switch (flag) { case Flags.DateTime: bsonReader.SkipValue(); break; // ignore value - case Flags.Ticks: ticks = _int64Serializer.Deserialize(context); break; - case Flags.Offset: offset = TimeSpan.FromMinutes(_int32Serializer.Deserialize(context)); break; + case Flags.Ticks: ticks = Int64Serializer.Instance.Deserialize(context); break; + case Flags.Offset: offset = TimeSpan.FromMinutes(Int32Serializer.Instance.Deserialize(context)); break; } }); return new DateTimeOffset(ticks, offset); @@ -136,6 +134,20 @@ public override DateTimeOffset Deserialize(BsonDeserializationContext context, B } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DateTimeOffsetSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/DateTimeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DateTimeSerializer.cs index 3621681c587..b103be5e48c 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DateTimeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DateTimeSerializer.cs @@ -14,11 +14,7 @@ */ using System; -using System.Globalization; -using System.IO; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { @@ -53,7 +49,6 @@ private static class Flags // private fields private readonly bool _dateOnly; private readonly SerializerHelper _helper; - private readonly Int64Serializer _int64Serializer = new Int64Serializer(); private readonly DateTimeKind _kind; private readonly BsonType _representation; @@ -219,7 +214,7 @@ public override DateTime Deserialize(BsonDeserializationContext context, BsonDes switch (flag) { case Flags.DateTime: bsonReader.SkipValue(); break; // ignore value (use Ticks instead) - case Flags.Ticks: value = new DateTime(_int64Serializer.Deserialize(context), DateTimeKind.Utc); break; + case Flags.Ticks: value = new DateTime(Int64Serializer.Instance.Deserialize(context), DateTimeKind.Utc); break; } }); break; @@ -268,6 +263,22 @@ public override DateTime Deserialize(BsonDeserializationContext context, BsonDes return value; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DateTimeSerializer other && + _dateOnly.Equals(other._dateOnly) && + _kind.Equals(other._kind) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/Decimal128Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/Decimal128Serializer.cs index 497e7a06a62..e8e232f799e 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/Decimal128Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/Decimal128Serializer.cs @@ -137,6 +137,21 @@ public override Decimal128 Deserialize(BsonDeserializationContext context, BsonD } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is Decimal128Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/DecimalSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DecimalSerializer.cs index 75508477139..a6b715cf596 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DecimalSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DecimalSerializer.cs @@ -147,6 +147,21 @@ public override decimal Deserialize(BsonDeserializationContext context, BsonDese } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DecimalSerializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs index 385e8ba007d..5d07b4e5bad 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs @@ -16,7 +16,6 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Collections.ObjectModel; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers diff --git a/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs index 9b04916444a..3db1d87e95a 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs @@ -116,6 +116,22 @@ public IBsonSerializer ValueSerializer } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DictionarySerializerBase other && + _dictionaryRepresentation.Equals(other._dictionaryRepresentation) && + object.Equals(_keySerializer, other._keySerializer) && + object.Equals(_valueSerializer, other._valueSerializer); + } + + /// + public override int GetHashCode() => 0; + /// public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo) { @@ -464,6 +480,22 @@ public IBsonSerializer ValueSerializer } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DictionarySerializerBase other && + _dictionaryRepresentation.Equals(other._dictionaryRepresentation) && + object.Equals(_lazyKeySerializer.Value, other._lazyKeySerializer.Value) && + object.Equals(_lazyValueSerializer.Value, other._lazyValueSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public bool TryGetItemSerializationInfo(out BsonSerializationInfo serializationInfo) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs index 91ca46ef2d0..e02e4297e2e 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs @@ -151,6 +151,21 @@ public override TInterface Deserialize(BsonDeserializationContext context, BsonD } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DiscriminatedInterfaceSerializer other && + object.Equals(_discriminatorConvention, other._discriminatorConvention) && + object.Equals(_interfaceSerializer, other._interfaceSerializer); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs index a6fd04d58ce..d3d45d28e89 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs @@ -13,8 +13,6 @@ * limitations under the License. */ -using System; -using MongoDB.Bson.IO; using MongoDB.Bson.Serialization.Conventions; namespace MongoDB.Bson.Serialization.Serializers @@ -96,6 +94,21 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser return value; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DiscriminatedWrapperSerializer other && + object.Equals(_discriminatorConvention, other._discriminatorConvention) && + object.Equals(_wrappedSerializer, other._wrappedSerializer); + } + + /// + public override int GetHashCode() => 0; + /// /// Determines whether the reader is positioned at a discriminated wrapper. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/DoubleSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DoubleSerializer.cs index b078c8ab45d..8004cea5b46 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DoubleSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DoubleSerializer.cs @@ -137,6 +137,21 @@ public override double Deserialize(BsonDeserializationContext context, BsonDeser } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DoubleSerializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/DowncastingSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DowncastingSerializer.cs index acfa77ed7d7..a1f1ff0258a 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DowncastingSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DowncastingSerializer.cs @@ -99,6 +99,20 @@ public override TBase Deserialize(BsonDeserializationContext context, BsonDeseri return _derivedSerializer.Deserialize(context); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DowncastingSerializer other && + object.Equals(_derivedSerializer, other._derivedSerializer); + } + + /// + public override int GetHashCode() => 0; + /// public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBase value) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/DynamicDocumentBaseSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DynamicDocumentBaseSerializer.cs index 0c1cf0bf4e0..d020fd3895d 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DynamicDocumentBaseSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DynamicDocumentBaseSerializer.cs @@ -27,7 +27,7 @@ namespace MongoDB.Bson.Serialization.Serializers public abstract class DynamicDocumentBaseSerializer : SerializerBase where T : class, IDynamicMetaObjectProvider { // private static fields - private static readonly IBsonSerializer _objectSerializer = BsonSerializer.LookupSerializer(); + private static readonly IBsonSerializer __objectSerializer = BsonSerializer.LookupSerializer(); // constructors /// @@ -58,7 +58,7 @@ public override T Deserialize(BsonDeserializationContext context, BsonDeserializ while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); - var value = _objectSerializer.Deserialize(dynamicContext); + var value = __objectSerializer.Deserialize(dynamicContext); SetValueForMember(document, name, value); } bsonReader.ReadEndDocument(); @@ -101,7 +101,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati if (TryGetValueForMember(value, memberName, out memberValue)) { bsonWriter.WriteName(memberName); - _objectSerializer.Serialize(dynamicContext, memberValue); + __objectSerializer.Serialize(dynamicContext, memberValue); } } bsonWriter.WriteEndDocument(); diff --git a/src/MongoDB.Bson/Serialization/Serializers/ElementAppendingSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ElementAppendingSerializer.cs index c9552be1d23..3bb800f6c8d 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ElementAppendingSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ElementAppendingSerializer.cs @@ -17,6 +17,7 @@ using System.Collections.Generic; using System.Linq; using MongoDB.Bson.IO; +using MongoDB.Shared; namespace MongoDB.Bson.Serialization.Serializers { @@ -67,6 +68,22 @@ object IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeser throw new NotSupportedException(); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is ElementAppendingSerializer other && + object.Equals(_documentSerializer, other._documentSerializer) && + SequenceComparer.Equals(_elements, other._elements) && + object.Equals(_writerSettingsConfigurator, other._writerSettingsConfigurator); + } + + /// + public override int GetHashCode() => 0; + /// public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TDocument value) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/EnumSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/EnumSerializer.cs index 7357fd33d43..46bceb0a7d4 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/EnumSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/EnumSerializer.cs @@ -110,16 +110,16 @@ public override TEnum Deserialize(BsonDeserializationContext context, BsonDeseri /// public override bool Equals(object obj) { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } return + base.Equals(obj) && obj is EnumSerializer other && - _representation == other.Representation; + _representation.Equals(other.Representation); } /// - public override int GetHashCode() - { - return _representation.GetHashCode(); - } + public override int GetHashCode() => 0; /// /// Serializes a value. diff --git a/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs index 1cf0f239352..299f60dc208 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs @@ -123,6 +123,20 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is EnumerableSerializerBase other && + object.Equals(_lazyItemSerializer.Value, other._lazyItemSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// /// Tries to get the serialization info for the individual items of the array. /// @@ -306,6 +320,20 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is EnumerableSerializerBase other && + object.Equals(_lazyItemSerializer.Value, other._lazyItemSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// /// Tries to get the serialization info for the individual items of the array. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/GuidSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/GuidSerializer.cs index 2cd37f3465f..2d6390100c0 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/GuidSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/GuidSerializer.cs @@ -159,6 +159,21 @@ public override Guid Deserialize(BsonDeserializationContext context, BsonDeseria } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GuidSerializer other && + _guidRepresentation.Equals(other._guidRepresentation) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs index d2ca097339a..64d7cdd435a 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs @@ -119,6 +119,20 @@ public override TIEnumerable Deserialize(BsonDeserializationContext context, Bso } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is IEnumerableDeserializingAsCollectionSerializer other && + object.Equals(_lazyItemSerializer.Value, other._lazyItemSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TIEnumerable value) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/IOrderedEnumerableSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/IOrderedEnumerableSerializer.cs index dcf2b02d7cf..a8a8c50da80 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/IOrderedEnumerableSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/IOrderedEnumerableSerializer.cs @@ -58,6 +58,21 @@ public override IOrderedEnumerable Deserialize(BsonDeserializationContext return new OrderedEnumerableListWrapper(list, _thenByExceptionMessage); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is IOrderedEnumerableSerializer other && + object.Equals(_itemSerializer, other._itemSerializer) && + object.Equals(_thenByExceptionMessage, other._thenByExceptionMessage); + } + + /// + public override int GetHashCode() => 0; + /// public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IOrderedEnumerable value) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/IPAddressSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/IPAddressSerializer.cs index 5939282d49f..25d318dfd03 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/IPAddressSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/IPAddressSerializer.cs @@ -32,7 +32,7 @@ public IPAddressSerializer() { } - // public methods + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/IPEndPointSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/IPEndPointSerializer.cs index 84fcf441d9a..5341efe21ab 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/IPEndPointSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/IPEndPointSerializer.cs @@ -33,7 +33,7 @@ public IPEndPointSerializer() { } - // public methods + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs index 5049616034c..0b94eae0dc6 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs @@ -203,6 +203,20 @@ public override TInterface Deserialize(BsonDeserializationContext context, BsonD } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ImpliedImplementationInterfaceSerializer other && + object.Equals(_lazyImplementationSerializer.Value, other._lazyImplementationSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// /// Tries to get the serialization info for the individual items of the array. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/Int16Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/Int16Serializer.cs index b81a77da546..a12d90126b3 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/Int16Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/Int16Serializer.cs @@ -14,9 +14,7 @@ */ using System; -using System.IO; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers @@ -130,6 +128,21 @@ public override short Deserialize(BsonDeserializationContext context, BsonDeseri } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is Int16Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/Int32Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/Int32Serializer.cs index ee6a23c2d86..515762f5caa 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/Int32Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/Int32Serializer.cs @@ -137,6 +137,21 @@ public override int Deserialize(BsonDeserializationContext context, BsonDeserial } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is Int32Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/Int64Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/Int64Serializer.cs index 96674fb18c3..ae8d5d995ff 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/Int64Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/Int64Serializer.cs @@ -137,6 +137,21 @@ public override long Deserialize(BsonDeserializationContext context, BsonDeseria } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is Int64Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs index d0c15f09b1b..971f6693b57 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs @@ -189,6 +189,22 @@ public override KeyValuePair Deserialize(BsonDeserializationContex } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is KeyValuePairSerializer other && + object.Equals(_lazyKeySerializer.Value, other._lazyKeySerializer.Value) && + object.Equals(_lazyValueSerializer.Value, other._lazyValueSerializer.Value) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/LazyBsonArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/LazyBsonArraySerializer.cs index 92d01d119d2..534a2b22f4e 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/LazyBsonArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/LazyBsonArraySerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/LazyBsonDocumentSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/LazyBsonDocumentSerializer.cs index e2bf3e3a121..5162985263e 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/LazyBsonDocumentSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/LazyBsonDocumentSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/MemorySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/MemorySerializer.cs index 9504f871493..f872938109d 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/MemorySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/MemorySerializer.cs @@ -119,10 +119,11 @@ public abstract class MemorySerializerBase : StructSerializerBas private static readonly bool __isByte = (typeof(TItem) == typeof(byte)); private readonly Func _readItems; + private readonly BsonType _representation; private readonly Action> _writeItems; /// - public BsonType Representation { get; } + public BsonType Representation => _representation; // constructors /// @@ -137,7 +138,7 @@ public MemorySerializerBase(BsonType representation) } (_readItems, _writeItems) = GetReaderAndWriter(); - Representation = representation; + _representation = representation; } /// @@ -173,6 +174,20 @@ public override TMemory Deserialize(BsonDeserializationContext context, BsonDese } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is MemorySerializerBase other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TMemory value) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/NullableGenericSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs similarity index 95% rename from src/MongoDB.Bson/Serialization/Serializers/NullableGenericSerializer.cs rename to src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs index a527d915b95..760066cfae8 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/NullableGenericSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs @@ -131,13 +131,16 @@ public NullableSerializer(IBsonSerializerRegistry serializerRegistry) /// public override bool Equals(object obj) { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } return + base.Equals(obj) && obj is NullableSerializer other && - ValueSerializer.Equals(other.ValueSerializer); + object.Equals(_lazySerializer.Value, other._lazySerializer.Value); } /// - public override int GetHashCode() => ValueSerializer.GetHashCode(); + public override int GetHashCode() => 0; /// /// Serializes a value. diff --git a/src/MongoDB.Bson/Serialization/Serializers/ObjectIdSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ObjectIdSerializer.cs index 07446032228..45dc8f8544b 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ObjectIdSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ObjectIdSerializer.cs @@ -101,6 +101,20 @@ public override ObjectId Deserialize(BsonDeserializationContext context, BsonDes } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ObjectIdSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs index 252894171f1..88ed8536c72 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs @@ -248,20 +248,21 @@ public override object Deserialize(BsonDeserializationContext context, BsonDeser } /// - public override bool Equals(object obj) => - obj is ObjectSerializer other && - GetType() == other.GetType() && - _allowedDeserializationTypes.Equals(other._allowedDeserializationTypes) && - _allowedSerializationTypes.Equals(other._allowedSerializationTypes) && - _discriminatorConvention.Equals(other._discriminatorConvention) && - _guidRepresentation == other._guidRepresentation; + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ObjectSerializer other && + object.Equals(_allowedDeserializationTypes, other._allowedDeserializationTypes) && + object.Equals(_allowedSerializationTypes, other._allowedSerializationTypes) && + object.Equals(_discriminatorConvention, other._discriminatorConvention) && + _guidRepresentation.Equals(other._guidRepresentation); + } /// - public override int GetHashCode() => - new Hasher() - .Hash(_discriminatorConvention) - .Hash(_guidRepresentation) - .GetHashCode(); + public override int GetHashCode() => 0; /// /// Serializes a value. diff --git a/src/MongoDB.Bson/Serialization/Serializers/PartiallyRawBsonDocumentSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/PartiallyRawBsonDocumentSerializer.cs index be694869bd6..4fe9519c3a4 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/PartiallyRawBsonDocumentSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/PartiallyRawBsonDocumentSerializer.cs @@ -73,6 +73,21 @@ public override BsonDocument Deserialize(BsonDeserializationContext context, Bso return document; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is PartiallyRawBsonDocumentSerializer other && + object.Equals(_name, other._name) && + object.Equals(_rawSerializer, other._rawSerializer); + } + + /// + public override int GetHashCode() => 0; + private IBsonSerializer ChooseSerializer(string name) { if (name == _name) diff --git a/src/MongoDB.Bson/Serialization/Serializers/ProjectingDeserializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ProjectingDeserializer.cs index 7a426f08b21..744b3ebae18 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ProjectingDeserializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ProjectingDeserializer.cs @@ -14,12 +14,6 @@ */ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.Serializers; namespace MongoDB.Bson.Serialization.Serializers { @@ -57,5 +51,20 @@ public override TTo Deserialize(BsonDeserializationContext context, BsonDeserial var from = _fromSerializer.Deserialize(context); return _projector(from); } + + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ProjectingDeserializer other && + object.Equals(_fromSerializer, other._fromSerializer) && + object.Equals(_projector, other._projector); + } + + /// + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Bson/Serialization/Serializers/RawBsonArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/RawBsonArraySerializer.cs index 8e358c433e1..17294f74651 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/RawBsonArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/RawBsonArraySerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/RawBsonDocumentSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/RawBsonDocumentSerializer.cs index beba07d8893..d8ba6ea160d 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/RawBsonDocumentSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/RawBsonDocumentSerializer.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializer.cs index a16cf080ccb..da1cbb5d9e7 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializer.cs @@ -14,10 +14,7 @@ */ using System; -using System.Collections; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Reflection; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers diff --git a/src/MongoDB.Bson/Serialization/Serializers/RegexSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/RegexSerializer.cs index a6c9661ebcf..0fedd1237e0 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/RegexSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/RegexSerializer.cs @@ -105,6 +105,20 @@ public override Regex Deserialize(BsonDeserializationContext context, BsonDeseri } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is RegexSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/SByteSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/SByteSerializer.cs index bd6c7074307..af5ef7e2250 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/SByteSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/SByteSerializer.cs @@ -15,10 +15,6 @@ using System; using System.Globalization; -using System.IO; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { @@ -133,6 +129,20 @@ public override sbyte Deserialize(BsonDeserializationContext context, BsonDeseri return value; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is SByteSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/SealedClassSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/SealedClassSerializerBase.cs index e438975b0e3..33f5c1e2127 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/SealedClassSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/SealedClassSerializerBase.cs @@ -13,7 +13,6 @@ * limitations under the License. */ - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs index ff70a96aa54..8d397b9ae72 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs @@ -65,6 +65,20 @@ public SerializeAsNominalTypeSerializer(IBsonSerializerRegistry serializerRegist } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is SerializeAsNominalTypeSerializer other && + object.Equals(_lazyNominalTypeSerializer.Value, other._lazyNominalTypeSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/SerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/SerializerBase.cs index 8e69b44f533..cf1bb1cb641 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/SerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/SerializerBase.cs @@ -15,7 +15,6 @@ using System; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Conventions; namespace MongoDB.Bson.Serialization.Serializers { @@ -49,6 +48,17 @@ public virtual TValue Deserialize(BsonDeserializationContext context, BsonDeseri throw CreateCannotBeDeserializedException(); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return GetType().Equals(obj.GetType()); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/SingleSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/SingleSerializer.cs index ab6c20ce51d..61b0ba6a981 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/SingleSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/SingleSerializer.cs @@ -14,10 +14,7 @@ */ using System; -using System.Globalization; -using System.IO; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers @@ -131,6 +128,21 @@ public override float Deserialize(BsonDeserializationContext context, BsonDeseri } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is SingleSerializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/StackSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/StackSerializer.cs index d8829046f03..bf316be6f10 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/StackSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/StackSerializer.cs @@ -17,8 +17,6 @@ using System.Collections; using System.Collections.Generic; using System.Linq; -using MongoDB.Bson.Serialization.Conventions; -using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers { diff --git a/src/MongoDB.Bson/Serialization/Serializers/StringSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/StringSerializer.cs index 3d4f1afc6ba..0e18d3988ba 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/StringSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/StringSerializer.cs @@ -78,6 +78,21 @@ public BsonType Representation } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is StringSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/StructSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/StructSerializerBase.cs index 371abc26e47..fd2376d6b53 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/StructSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/StructSerializerBase.cs @@ -13,10 +13,6 @@ * limitations under the License. */ -using System; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Conventions; - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs index 356523d2fb4..4219f18e113 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs @@ -79,6 +79,21 @@ public IBsonSerializer ItemSerializer } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ThreeDimensionalArraySerializer other && + object.Equals(_lazyItemSerializer.Value, other._lazyItemSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/TimeSpanSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/TimeSpanSerializer.cs index f8e17beeb3c..eb972e0b5e8 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/TimeSpanSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/TimeSpanSerializer.cs @@ -14,9 +14,6 @@ */ using System; -using System.IO; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers @@ -126,6 +123,21 @@ public override TimeSpan Deserialize(BsonDeserializationContext context, BsonDes } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TimeSpanSerializer other && + _representation.Equals(other._representation) && + _units.Equals(other._units); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs b/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs index 104fe6f13de..212d9c605b5 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs @@ -149,6 +149,21 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); @@ -238,6 +253,22 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); @@ -353,6 +384,22 @@ protected override Tuple DeserializeValue(BsonDeserializationContext return new Tuple(item1, item2, item3); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.Equals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -461,6 +508,24 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.Equals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.Equals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); @@ -595,6 +660,25 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.Equals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.Equals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.Equals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); @@ -744,6 +828,26 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.Equals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.Equals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.Equals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value) && + object.Equals(_lazyItem6Serializer.Value, other._lazyItem6Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); @@ -908,6 +1012,27 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.Equals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.Equals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.Equals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value) && + object.Equals(_lazyItem6Serializer.Value, other._lazyItem6Serializer.Value) && + object.Equals(_lazyItem7Serializer.Value, other._lazyItem7Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); @@ -1087,6 +1212,28 @@ public TupleSerializer(IBsonSerializerRegistry serializerRegistry) // public methods /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TupleSerializer other && + object.Equals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.Equals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.Equals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.Equals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.Equals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value) && + object.Equals(_lazyItem6Serializer.Value, other._lazyItem6Serializer.Value) && + object.Equals(_lazyItem7Serializer.Value, other._lazyItem7Serializer.Value) && + object.Equals(_lazyRestSerializer.Value, other._lazyRestSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods + /// protected override Tuple DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { context.Reader.ReadStartArray(); diff --git a/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs index 2e44e6a06d0..108a52cb33e 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs @@ -79,6 +79,21 @@ public IBsonSerializer ItemSerializer } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is TwoDimensionalArraySerializer other && + object.Equals(_lazyItemSerializer.Value, other._lazyItemSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/UInt16Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/UInt16Serializer.cs index 5445b7d72cb..9e071b6baa1 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/UInt16Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/UInt16Serializer.cs @@ -14,9 +14,7 @@ */ using System; -using System.IO; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers @@ -131,6 +129,21 @@ public override ushort Deserialize(BsonDeserializationContext context, BsonDeser } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is UInt16Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/UInt32Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/UInt32Serializer.cs index e87ca334887..c0a3eff55e2 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/UInt32Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/UInt32Serializer.cs @@ -14,9 +14,7 @@ */ using System; -using System.IO; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers @@ -131,6 +129,21 @@ public override uint Deserialize(BsonDeserializationContext context, BsonDeseria } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is UInt32Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/UInt64Serializer.cs b/src/MongoDB.Bson/Serialization/Serializers/UInt64Serializer.cs index a3f3e4a8cd0..d27efc412e8 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/UInt64Serializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/UInt64Serializer.cs @@ -14,9 +14,7 @@ */ using System; -using System.IO; using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Options; namespace MongoDB.Bson.Serialization.Serializers @@ -131,6 +129,21 @@ public override ulong Deserialize(BsonDeserializationContext context, BsonDeseri } } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is UInt64Serializer other && + object.Equals(_converter, other._converter) && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + /// /// Serializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs index e54716859fd..8fb81be5466 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs @@ -13,15 +13,6 @@ * limitations under the License. */ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using MongoDB.Bson.IO; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.Conventions; - namespace MongoDB.Bson.Serialization.Serializers { /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/UriSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/UriSerializer.cs index ebf91a30f23..40e276ad36b 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/UriSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/UriSerializer.cs @@ -30,7 +30,7 @@ public UriSerializer() { } - // public methods + // protected methods /// /// Deserializes a value. /// diff --git a/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs b/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs index da205b24f2b..d0f52484e58 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs @@ -144,6 +144,20 @@ public override ValueTuple Deserialize(BsonDeserializationContext context, B return new ValueTuple(item1); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -261,6 +275,21 @@ public override ValueTuple Deserialize(BsonDeserializationContext contex return new ValueTuple(item1, item2); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -395,6 +424,22 @@ public override ValueTuple Deserialize(BsonDeserializationContext co return new ValueTuple(item1, item2, item3); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.ReferenceEquals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -546,6 +591,23 @@ public override ValueTuple Deserialize(BsonDeserializationContex return new ValueTuple(item1, item2, item3, item4); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.ReferenceEquals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.ReferenceEquals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -714,6 +776,24 @@ public override ValueTuple Deserialize(BsonDeserializationCo return new ValueTuple(item1, item2, item3, item4, item5); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.ReferenceEquals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.ReferenceEquals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.ReferenceEquals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -899,6 +979,25 @@ public override ValueTuple Deserialize(BsonDeserializati return new ValueTuple(item1, item2, item3, item4, item5, item6); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.ReferenceEquals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.ReferenceEquals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.ReferenceEquals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value) && + object.ReferenceEquals(_lazyItem6Serializer.Value, other._lazyItem6Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -1101,6 +1200,26 @@ public override ValueTuple Deserialize(BsonDeseriali return new ValueTuple(item1, item2, item3, item4, item5, item6, item7); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.ReferenceEquals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.ReferenceEquals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.ReferenceEquals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value) && + object.ReferenceEquals(_lazyItem6Serializer.Value, other._lazyItem6Serializer.Value) && + object.ReferenceEquals(_lazyItem7Serializer.Value, other._lazyItem7Serializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { @@ -1321,6 +1440,27 @@ public override ValueTuple Deserialize(BsonDe return new ValueTuple(item1, item2, item3, item4, item5, item6, item7, rest); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueTupleSerializer other && + object.ReferenceEquals(_lazyItem1Serializer.Value, other._lazyItem1Serializer.Value) && + object.ReferenceEquals(_lazyItem2Serializer.Value, other._lazyItem2Serializer.Value) && + object.ReferenceEquals(_lazyItem3Serializer.Value, other._lazyItem3Serializer.Value) && + object.ReferenceEquals(_lazyItem4Serializer.Value, other._lazyItem4Serializer.Value) && + object.ReferenceEquals(_lazyItem5Serializer.Value, other._lazyItem5Serializer.Value) && + object.ReferenceEquals(_lazyItem6Serializer.Value, other._lazyItem6Serializer.Value) && + object.ReferenceEquals(_lazyItem7Serializer.Value, other._lazyItem7Serializer.Value) && + object.ReferenceEquals(_lazyRestSerializer.Value, other._lazyRestSerializer.Value); + } + + /// + public override int GetHashCode() => 0; + /// public IBsonSerializer GetItemSerializer(int itemNumber) { diff --git a/src/MongoDB.Bson/Serialization/Serializers/VersionSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/VersionSerializer.cs index 689b9a46b35..8d84f96487f 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/VersionSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/VersionSerializer.cs @@ -37,7 +37,6 @@ private static class Flags // private fields private readonly SerializerHelper _helper; - private readonly Int32Serializer _int32Serializer = new Int32Serializer(); private readonly BsonType _representation; // constructors @@ -90,6 +89,21 @@ public BsonType Representation } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is VersionSerializer other && + _representation.Equals(other._representation); + } + + /// + public override int GetHashCode() => 0; + + // protected methods /// /// Deserializes a value. /// @@ -109,10 +123,10 @@ protected override Version DeserializeValue(BsonDeserializationContext context, { switch (flag) { - case Flags.Major: major = _int32Serializer.Deserialize(context); break; - case Flags.Minor: minor = _int32Serializer.Deserialize(context); break; - case Flags.Build: build = _int32Serializer.Deserialize(context); break; - case Flags.Revision: revision = _int32Serializer.Deserialize(context); break; + case Flags.Major: major = Int32Serializer.Instance.Deserialize(context); break; + case Flags.Minor: minor = Int32Serializer.Instance.Deserialize(context); break; + case Flags.Build: build = Int32Serializer.Instance.Deserialize(context); break; + case Flags.Revision: revision = Int32Serializer.Instance.Deserialize(context); break; } }); switch (foundMemberFlags) diff --git a/src/MongoDB.Driver.Core/ChangeStreamDocumentSerializer.cs b/src/MongoDB.Driver.Core/ChangeStreamDocumentSerializer.cs index 22b2a06fb6b..06bc363ebdf 100644 --- a/src/MongoDB.Driver.Core/ChangeStreamDocumentSerializer.cs +++ b/src/MongoDB.Driver.Core/ChangeStreamDocumentSerializer.cs @@ -65,6 +65,20 @@ public override ChangeStreamDocument Deserialize(BsonDeserializationC return base.Deserialize(context, args); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ChangeStreamDocumentSerializer other && + object.Equals(_documentSerializer, other._documentSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// protected override ChangeStreamDocument CreateInstance(BsonDocument backingDocument) diff --git a/src/MongoDB.Driver.Core/ChangeStreamUpdateDescriptionSerializer.cs b/src/MongoDB.Driver.Core/ChangeStreamUpdateDescriptionSerializer.cs index f49813e1625..9fec4c14bc8 100644 --- a/src/MongoDB.Driver.Core/ChangeStreamUpdateDescriptionSerializer.cs +++ b/src/MongoDB.Driver.Core/ChangeStreamUpdateDescriptionSerializer.cs @@ -13,12 +13,11 @@ * limitations under the License. */ +using System; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; -using MongoDB.Driver.Core.Misc; -using System; namespace MongoDB.Driver { diff --git a/src/MongoDB.Driver.Core/Core/Misc/FixedCountBatchableSourceSerializer.cs b/src/MongoDB.Driver.Core/Core/Misc/FixedCountBatchableSourceSerializer.cs index db7c6f8d261..93de22ccea9 100644 --- a/src/MongoDB.Driver.Core/Core/Misc/FixedCountBatchableSourceSerializer.cs +++ b/src/MongoDB.Driver.Core/Core/Misc/FixedCountBatchableSourceSerializer.cs @@ -45,6 +45,22 @@ public FixedCountBatchableSourceSerializer(IBsonSerializer itemSerializer } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is FixedCountBatchableSourceSerializer other && + _count.Equals(other._count) && + object.Equals(_itemElementNameValidator, other._itemElementNameValidator) && + object.Equals(_itemSerializer, other._itemSerializer); + } + + /// + public override int GetHashCode() => 0; + /// public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BatchableSource value) { diff --git a/src/MongoDB.Driver.Core/Core/Misc/SizeLimitingBatchableSourceSerializer.cs b/src/MongoDB.Driver.Core/Core/Misc/SizeLimitingBatchableSourceSerializer.cs index e5138650b1d..14e8937fd2c 100644 --- a/src/MongoDB.Driver.Core/Core/Misc/SizeLimitingBatchableSourceSerializer.cs +++ b/src/MongoDB.Driver.Core/Core/Misc/SizeLimitingBatchableSourceSerializer.cs @@ -57,6 +57,24 @@ public SizeLimitingBatchableSourceSerializer( } // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is SizeLimitingBatchableSourceSerializer other && + object.Equals(_itemElementNameValidator, other._itemElementNameValidator) && + object.Equals(_itemSerializer, other._itemSerializer) && + _maxBatchCount.Equals(other._maxBatchCount) && + _maxBatchSize.Equals(other._maxBatchSize) && + _maxItemSize.Equals(other._maxItemSize); + } + + /// + public override int GetHashCode() => 0; + /// public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, BatchableSource value) { diff --git a/src/MongoDB.Driver.Core/Core/Operations/AggregateOperation.cs b/src/MongoDB.Driver.Core/Core/Operations/AggregateOperation.cs index 5f682037025..e07003b5c1a 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/AggregateOperation.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/AggregateOperation.cs @@ -440,7 +440,7 @@ private void EnsureIsReadOnlyPipeline() } } - private class AggregateResult + internal class AggregateResult { public BsonTimestamp AtClusterTime; public long? CursorId; @@ -449,7 +449,7 @@ private class AggregateResult public TResult[] Results; } - private class AggregateResultDeserializer : SerializerBase + internal class AggregateResultDeserializer : SerializerBase { private readonly IBsonSerializer _resultSerializer; @@ -485,9 +485,21 @@ public override AggregateResult Deserialize(BsonDeserializationContext context, reader.ReadEndDocument(); return result; } + + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is AggregateResultDeserializer other && + object.Equals(_resultSerializer, other._resultSerializer); + } + + public override int GetHashCode() => 0; } - private class CursorDeserializer : SerializerBase + internal class CursorDeserializer : SerializerBase { private readonly IBsonSerializer _resultSerializer; @@ -536,6 +548,18 @@ public override AggregateResult Deserialize(BsonDeserializationContext context, reader.ReadEndDocument(); return result; } + + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is CursorDeserializer other && + object.Equals(_resultSerializer, other._resultSerializer); + } + + public override int GetHashCode() => 0; } } } diff --git a/src/MongoDB.Driver.Core/Core/Operations/DelayedEvaluationWriteConcernSerializer.cs b/src/MongoDB.Driver.Core/Core/Operations/DelayedEvaluationWriteConcernSerializer.cs index 067273f67c8..237fb1988b7 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/DelayedEvaluationWriteConcernSerializer.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/DelayedEvaluationWriteConcernSerializer.cs @@ -21,7 +21,7 @@ namespace MongoDB.Driver.Core.Operations { internal class DelayedEvaluationWriteConcernSerializer : SealedClassSerializerBase> { - // private fields + // protected methods protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, Func value) { var writeConcern = value(); diff --git a/src/MongoDB.Driver.Core/Core/Operations/DistinctOperation.cs b/src/MongoDB.Driver.Core/Core/Operations/DistinctOperation.cs index ee923125a13..5bd98bb6531 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/DistinctOperation.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/DistinctOperation.cs @@ -283,6 +283,18 @@ public override DistinctResult Deserialize(BsonDeserializationContext context, B reader.ReadEndDocument(); return result; } + + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is DistinctResultDeserializer other && + object.Equals(_valueSerializer, other._valueSerializer); + } + + public override int GetHashCode() => 0; } } } diff --git a/src/MongoDB.Driver.Core/Core/Operations/ElementDeserializer.cs b/src/MongoDB.Driver.Core/Core/Operations/ElementDeserializer.cs index 6d97ac00145..3413938a824 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/ElementDeserializer.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/ElementDeserializer.cs @@ -13,11 +13,6 @@ * limitations under the License. */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; @@ -72,5 +67,19 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser return value; } + + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ElementDeserializer other && + _deserializeNull.Equals(other._deserializeNull) && + object.Equals(_elementName, other._elementName) && + object.Equals(_valueSerializer, other._valueSerializer); + } + + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Driver.Core/Core/Operations/FindAndModifyValueDeserializer.cs b/src/MongoDB.Driver.Core/Core/Operations/FindAndModifyValueDeserializer.cs index 836b105adda..efd2bfde80d 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/FindAndModifyValueDeserializer.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/FindAndModifyValueDeserializer.cs @@ -43,5 +43,19 @@ public override TResult Deserialize(BsonDeserializationContext context, BsonDese { return _resultSerializer.Deserialize(context); } + + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is FindAndModifyValueDeserializer other && + object.Equals(_resultSerializer, other._resultSerializer); + } + + /// + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Driver.Core/Core/Operations/RetryableInsertCommandOperation.cs b/src/MongoDB.Driver.Core/Core/Operations/RetryableInsertCommandOperation.cs index 2ec35f68ef1..d1cd98c640e 100644 --- a/src/MongoDB.Driver.Core/Core/Operations/RetryableInsertCommandOperation.cs +++ b/src/MongoDB.Driver.Core/Core/Operations/RetryableInsertCommandOperation.cs @@ -155,6 +155,18 @@ public InsertSerializer(IBsonSerializer documentSerializer) } // public methods + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is InsertSerializer other && + object.Equals(_documentSerializer, other._documentSerializer); + } + + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TDocument value) { IBsonSerializer serializer; diff --git a/src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj b/src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj index 5b996d1a679..da8b2db1984 100644 --- a/src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj +++ b/src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj @@ -20,6 +20,10 @@ NU5100 + + + + @@ -40,10 +44,6 @@ - - - - diff --git a/src/MongoDB.Driver.GridFS/GridFSFileInfoSerializerCompat.cs b/src/MongoDB.Driver.GridFS/GridFSFileInfoSerializerCompat.cs index 312bff00691..9142490a548 100644 --- a/src/MongoDB.Driver.GridFS/GridFSFileInfoSerializerCompat.cs +++ b/src/MongoDB.Driver.GridFS/GridFSFileInfoSerializerCompat.cs @@ -50,6 +50,7 @@ public GridFSFileInfoSerializer() RegisterMember("UploadDateTime", "uploadDate", new DateTimeSerializer()); } + // protected methods /// protected override GridFSFileInfo CreateInstance(BsonDocument backingDocument) { diff --git a/src/MongoDB.Driver/AggregateBucketAutoResultIdSerializer.cs b/src/MongoDB.Driver/AggregateBucketAutoResultIdSerializer.cs index 7185dbdd45d..dd1c150d216 100644 --- a/src/MongoDB.Driver/AggregateBucketAutoResultIdSerializer.cs +++ b/src/MongoDB.Driver/AggregateBucketAutoResultIdSerializer.cs @@ -55,6 +55,22 @@ public AggregateBucketAutoResultIdSerializer(IBsonSerializer valueSerial _valueSerializer = Ensure.IsNotNull(valueSerializer, nameof(valueSerializer)); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is AggregateBucketAutoResultIdSerializer other && + object.Equals(_valueSerializer, other._valueSerializer); + } + + /// + public override int GetHashCode() => 0; + + // protected methods /// protected override AggregateBucketAutoResultId DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) { diff --git a/src/MongoDB.Driver/AggregateFacetResults.cs b/src/MongoDB.Driver/AggregateFacetResults.cs index 07dacdfb67a..0f941bf97e1 100644 --- a/src/MongoDB.Driver/AggregateFacetResults.cs +++ b/src/MongoDB.Driver/AggregateFacetResults.cs @@ -22,6 +22,7 @@ using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver.Core.Misc; +using MongoDB.Shared; namespace MongoDB.Driver { @@ -98,5 +99,18 @@ public override AggregateFacetResults Deserialize(BsonDeserializationContext con return new AggregateFacetResults(facets); } + + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is AggregateFacetResultsSerializer other && + SequenceComparer.Equals(_names, other._names) && + SequenceComparer.Equals(_serializers, other._serializers); + } + + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Driver/ChangeStreamPreAndPostImagesOptions.cs b/src/MongoDB.Driver/ChangeStreamPreAndPostImagesOptions.cs index 3c95a7e4f80..675ba7074c7 100644 --- a/src/MongoDB.Driver/ChangeStreamPreAndPostImagesOptions.cs +++ b/src/MongoDB.Driver/ChangeStreamPreAndPostImagesOptions.cs @@ -74,7 +74,7 @@ public ChangeStreamPreAndPostImagesOptionsSerializer() RegisterMember("Enabled", "enabled", new BooleanSerializer()); } - // methods + // protected methods protected override ChangeStreamPreAndPostImagesOptions CreateInstance(BsonDocument backingDocument) => new ChangeStreamPreAndPostImagesOptions(backingDocument); } diff --git a/src/MongoDB.Driver/FieldValueSerializerHelper.cs b/src/MongoDB.Driver/FieldValueSerializerHelper.cs index 8a634cd7ea1..16d95460897 100644 --- a/src/MongoDB.Driver/FieldValueSerializerHelper.cs +++ b/src/MongoDB.Driver/FieldValueSerializerHelper.cs @@ -195,7 +195,7 @@ public static IBsonSerializer Create( } } - private class ConvertIfPossibleSerializer : SerializerBase + internal class ConvertIfPossibleSerializer : SerializerBase { private readonly IBsonSerializer _serializer; private readonly IBsonSerializerRegistry _serializerRegistry; @@ -206,6 +206,19 @@ public ConvertIfPossibleSerializer(IBsonSerializer serializer, IBsonSeriali _serializerRegistry = serializerRegistry; } + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ConvertIfPossibleSerializer other && + object.Equals(_serializer, other._serializer) && + object.ReferenceEquals(_serializerRegistry, other._serializerRegistry); + } + + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value) { TTo convertedValue; @@ -264,7 +277,7 @@ private bool TryConvertValue(TFrom value, out TTo convertedValue) } } - private class EnumConvertingSerializer : SerializerBase + internal class EnumConvertingSerializer : SerializerBase { private readonly IBsonSerializer _serializer; @@ -273,6 +286,18 @@ public EnumConvertingSerializer(IBsonSerializer serializer) _serializer = serializer; } + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is EnumConvertingSerializer other && + object.Equals(_serializer, other._serializer); + } + + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TFrom value) { TTo convertedValue; @@ -288,7 +313,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati } } - private class IEnumerableSerializer : SerializerBase> + internal class IEnumerableSerializer : SerializerBase> { private readonly IBsonSerializer _itemSerializer; @@ -297,6 +322,18 @@ public IEnumerableSerializer(IBsonSerializer itemSerializer) _itemSerializer = itemSerializer; } + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is IEnumerableSerializer other && + object.Equals(_itemSerializer, other._itemSerializer); + } + + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IEnumerable value) { var bsonWriter = context.Writer; @@ -316,7 +353,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati } } - private class NullableEnumConvertingSerializer : SerializerBase> where TFrom : struct where TTo : struct + internal class NullableEnumConvertingSerializer : SerializerBase> where TFrom : struct where TTo : struct { private readonly IBsonSerializer _nonNullableEnumSerializer; @@ -325,6 +362,18 @@ public NullableEnumConvertingSerializer(IBsonSerializer nonNullableEnumSeri _nonNullableEnumSerializer = nonNullableEnumSerializer; } + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is NullableEnumConvertingSerializer other && + object.Equals(_nonNullableEnumSerializer, other._nonNullableEnumSerializer); + } + + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Nullable value) { if (value == null) diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonBoundingBoxSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonBoundingBoxSerializer.cs index 300107a82d9..1c35c0eadec 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonBoundingBoxSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonBoundingBoxSerializer.cs @@ -31,6 +31,21 @@ public class GeoJsonBoundingBoxSerializer : ClassSerializerBase _coordinatesSerializer = BsonSerializer.LookupSerializer(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonBoundingBoxSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonCoordinateReferenceSystemSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonCoordinateReferenceSystemSerializer.cs index 6d1a3004d3a..a192175b92e 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonCoordinateReferenceSystemSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonCoordinateReferenceSystemSerializer.cs @@ -14,7 +14,6 @@ */ using System; -using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureCollectionSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureCollectionSerializer.cs index d3bb27b5384..6ceaea45faf 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureCollectionSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureCollectionSerializer.cs @@ -49,6 +49,21 @@ public GeoJsonFeatureCollectionSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonFeatureCollectionSerializer other && + object.ReferenceEquals(_featureSerializer, other._featureSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureSerializer.cs index bc9c89fde35..78c7cbfc64a 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonFeatureSerializer.cs @@ -52,6 +52,21 @@ public GeoJsonFeatureSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonFeatureSerializer other && + object.Equals(_geometrySerializer, other._geometrySerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometryCollectionSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometryCollectionSerializer.cs index 3ed2d11826d..4aff9b8dd12 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometryCollectionSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometryCollectionSerializer.cs @@ -49,6 +49,21 @@ public GeoJsonGeometryCollectionSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonGeometryCollectionSerializer other && + object.Equals(_geometrySerializer, other._geometrySerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometrySerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometrySerializer.cs index 810d2a1d13c..bffbaa808fe 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometrySerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonGeometrySerializer.cs @@ -14,8 +14,6 @@ */ using System; -using System.Collections.Generic; -using System.Linq; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; @@ -28,6 +26,7 @@ namespace MongoDB.Driver.GeoJsonObjectModel.Serializers /// The type of the coordinates. public class GeoJsonGeometrySerializer : ClassSerializerBase> where TCoordinates : GeoJsonCoordinates { + // protected methods /// /// Gets the actual type. /// diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringCoordinatesSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringCoordinatesSerializer.cs index 80fd35dc07c..7345b2bf198 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringCoordinatesSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringCoordinatesSerializer.cs @@ -29,6 +29,21 @@ public class GeoJsonLineStringCoordinatesSerializer : ClassSeriali // private fields private readonly IBsonSerializer _coordinatesSerializer = BsonSerializer.LookupSerializer(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonLineStringCoordinatesSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringSerializer.cs index f6fc8a1f8ff..b307117d5e9 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLineStringSerializer.cs @@ -47,6 +47,21 @@ public GeoJsonLineStringSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonLineStringSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLinearRingCoordinatesSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLinearRingCoordinatesSerializer.cs index 358e06bf36b..73f4e183494 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLinearRingCoordinatesSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonLinearRingCoordinatesSerializer.cs @@ -29,6 +29,21 @@ public class GeoJsonLinearRingCoordinatesSerializer : ClassSeriali // private fields private readonly IBsonSerializer _coordinatesSerializer = BsonSerializer.LookupSerializer(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonLinearRingCoordinatesSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringCoordinatesSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringCoordinatesSerializer.cs index 3d38248dc93..7b43fb55f51 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringCoordinatesSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringCoordinatesSerializer.cs @@ -29,6 +29,21 @@ public class GeoJsonMultiLineStringCoordinatesSerializer : ClassSe // private fields private readonly IBsonSerializer> _lineStringCoordinatesSerializer = BsonSerializer.LookupSerializer>(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonMultiLineStringCoordinatesSerializer other && + object.Equals(_lineStringCoordinatesSerializer, other._lineStringCoordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringSerializer.cs index 91594ff983e..d4c7df3ccd0 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiLineStringSerializer.cs @@ -47,6 +47,21 @@ public GeoJsonMultiLineStringSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonMultiLineStringSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointCoordinatesSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointCoordinatesSerializer.cs index de4e711afe2..bf59975b455 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointCoordinatesSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointCoordinatesSerializer.cs @@ -29,6 +29,21 @@ public class GeoJsonMultiPointCoordinatesSerializer : ClassSeriali // private fields private readonly IBsonSerializer _coordinatesSerializer = BsonSerializer.LookupSerializer(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonMultiPointCoordinatesSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointSerializer.cs index a1e11cf57e1..7f179d1e032 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPointSerializer.cs @@ -47,6 +47,21 @@ public GeoJsonMultiPointSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonMultiPointSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonCoordinatesSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonCoordinatesSerializer.cs index d803a2eb7b8..11018737858 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonCoordinatesSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonCoordinatesSerializer.cs @@ -29,6 +29,21 @@ public class GeoJsonMultiPolygonCoordinatesSerializer : ClassSeria // private fields private readonly IBsonSerializer> _polygonCoordinatesSerializer = BsonSerializer.LookupSerializer>(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonMultiPolygonCoordinatesSerializer other && + object.Equals(_polygonCoordinatesSerializer, other._polygonCoordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonSerializer.cs index 4b00a45ec86..64b11060b26 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonMultiPolygonSerializer.cs @@ -47,6 +47,21 @@ public GeoJsonMultiPolygonSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonMultiPolygonSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPointSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPointSerializer.cs index 16eb274ba5c..cf28467eff8 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPointSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPointSerializer.cs @@ -47,6 +47,21 @@ public GeoJsonPointSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonPointSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonCoordinatesSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonCoordinatesSerializer.cs index 31b2ffc8ace..7dff956431a 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonCoordinatesSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonCoordinatesSerializer.cs @@ -29,6 +29,21 @@ public class GeoJsonPolygonCoordinatesSerializer : ClassSerializer // private fields private readonly IBsonSerializer> _linearRingCoordinatesSerializer = BsonSerializer.LookupSerializer>(); + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonPolygonCoordinatesSerializer other && + object.Equals(_linearRingCoordinatesSerializer, other._linearRingCoordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonSerializer.cs b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonSerializer.cs index b38f3869c86..7a204283f47 100644 --- a/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonSerializer.cs +++ b/src/MongoDB.Driver/GeoJsonObjectModel/Serializers/GeoJsonPolygonSerializer.cs @@ -47,6 +47,21 @@ public GeoJsonPolygonSerializer() ); } + // public methods + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is GeoJsonPolygonSerializer other && + object.Equals(_coordinatesSerializer, other._coordinatesSerializer); + } + + /// + public override int GetHashCode() => 0; + // protected methods /// /// Deserializes a value. diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstFindProjection.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstFindProjection.cs deleted file mode 100644 index 93276918bc2..00000000000 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstFindProjection.cs +++ /dev/null @@ -1,117 +0,0 @@ -/* Copyright 2010-present MongoDB Inc. -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using MongoDB.Bson; -using MongoDB.Bson.Serialization; -using MongoDB.Bson.Serialization.Serializers; -using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors; -using MongoDB.Driver.Linq.Linq3Implementation.Misc; - -namespace MongoDB.Driver.Linq.Linq3Implementation.Ast -{ - internal class AstFindProjection : AstNode - { - private readonly IReadOnlyList _fields; - private readonly IBsonSerializer _projectionSerializer; - - public AstFindProjection( - IEnumerable fields, - IBsonSerializer projectionSerializer) - { - _fields = Ensure.IsNotNull(fields, nameof(fields)).AsReadOnlyList(); - _projectionSerializer = Ensure.IsNotNull(projectionSerializer, nameof(projectionSerializer)); - } - - public IReadOnlyList Fields => _fields; - public override AstNodeType NodeType => AstNodeType.FindProjection; - public IBsonSerializer ProjectionSerializer => _projectionSerializer; - - public override AstNode Accept(AstNodeVisitor visitor) - { - return visitor.VisitFindProjection(this); - } - - public override BsonValue Render() - { - return new BsonDocument(_fields.Select(f => f.Render())); - } - } - - internal abstract class AstFindProjectionField - { - public abstract BsonElement Render(); - } - - internal class AstFindProjectionExcludeField : AstFindProjectionField - { - private readonly string _path; - - public AstFindProjectionExcludeField(string path) - { - _path = Ensure.IsNotNull(path, nameof(path)); - } - - public override BsonElement Render() - { - return new BsonElement(_path, 0); - } - } - - internal class AstFindProjectionIncludeField : AstFindProjectionField - { - private readonly string _path; - - public AstFindProjectionIncludeField(string path) - { - _path = Ensure.IsNotNull(path, nameof(path)); - } - - public override BsonElement Render() - { - return new BsonElement(_path, 1); - } - } - - internal class AstFindProjectionSerializer : SerializerBase - { - private readonly Expression> _clientSideProjectorExpression; - private readonly Func _clientSideProjectorFunc; - private readonly IBsonSerializer _outputSerializer; - - public AstFindProjectionSerializer( - IBsonSerializer outputSerializer, - Expression> clientSideProjectorExpression) - { - _outputSerializer = Ensure.IsNotNull(outputSerializer, nameof(outputSerializer)); - _clientSideProjectorExpression = Ensure.IsNotNull(clientSideProjectorExpression, nameof(clientSideProjectorExpression)); - _clientSideProjectorFunc = clientSideProjectorExpression.Compile(); - } - - public Expression> ClientSideProjectorExpression => _clientSideProjectorExpression; - public Func ClientSideProjectorFunc => _clientSideProjectorFunc; - public IBsonSerializer OutputSerializer => _outputSerializer; - - public override TProjection Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) - { - var outputValue = _outputSerializer.Deserialize(context, args); - return _clientSideProjectorFunc(outputValue); - } - } -} diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs index 92e3b7d4a74..4593e500734 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Visitors/AstNodeVisitor.cs @@ -354,11 +354,6 @@ public virtual AstNode VisitFilterField(AstFilterField node) return node; } - public virtual AstNode VisitFindProjection(AstFindProjection node) - { - throw new NotImplementedException(); - } - public virtual AstNode VisitFunctionExpression(AstFunctionExpression node) { return node.Update(VisitAndConvert(node.Args)); diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializer.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializer.cs index a862b574a43..577ddef0c82 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializer.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializer.cs @@ -14,11 +14,9 @@ */ using System; -using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver.Core.Misc; -using MongoDB.Driver.Linq.Linq3Implementation.Misc; namespace MongoDB.Driver.Linq.Linq3Implementation.Serializers { @@ -60,9 +58,12 @@ public override TEnumUnderlyingType Deserialize(BsonDeserializationContext conte /// public override bool Equals(object obj) { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } return + base.Equals(obj) && obj is EnumUnderlyingTypeSerializer other && - _enumSerializer.Equals(other._enumSerializer); + object.Equals(_enumSerializer, other._enumSerializer); } /// diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBase.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBase.cs index 559fb696f49..28e29f70e41 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBase.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBase.cs @@ -47,6 +47,20 @@ public override TEnumerable Deserialize(BsonDeserializationContext context, Bson return CreateDeserializedValue(items); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is IEnumerableSerializerBase other && + object.Equals(_itemSerializer, other._itemSerializer); + } + + /// + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TEnumerable value) { var writer = context.Writer; diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IGroupingSerializer.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IGroupingSerializer.cs index fe13ed0a33b..c13060233ce 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IGroupingSerializer.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/IGroupingSerializer.cs @@ -63,6 +63,21 @@ public override IGrouping Deserialize(BsonDeserializationContext return new Grouping(key, elements); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is IGroupingSerializer other && + object.Equals(_elementSerializer, other._elementSerializer) && + object.Equals(_keySerializer, other._keySerializer); + } + + /// + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IGrouping value) { var writer = context.Writer; diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializer.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializer.cs index 522294c7a64..2be9f49a1b3 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializer.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializer.cs @@ -42,6 +42,20 @@ public ISetWindowFieldsPartition Deserialize(BsonDeserializationContext throw new InvalidOperationException("This serializer is not intended to be used."); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + GetType().Equals(obj.GetType()) && + obj is ISetWindowFieldsPartitionSerializer other && + object.Equals(_inputSerializer, other._inputSerializer); + } + + /// + public override int GetHashCode() => 0; + public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, ISetWindowFieldsPartition value) { throw new InvalidOperationException("This serializer is not intended to be used."); diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/WrappedValueSerializer.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/WrappedValueSerializer.cs index 25f0b9af1e3..f3bb40aaf3a 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/WrappedValueSerializer.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Serializers/WrappedValueSerializer.cs @@ -67,6 +67,21 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser return value; } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is WrappedValueSerializer other && + object.Equals(_fieldName, other._fieldName) && + object.Equals(_valueSerializer, other._valueSerializer); + } + + /// + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value) { var writer = context.Writer; diff --git a/src/MongoDB.Driver/MongoDB.Driver.csproj b/src/MongoDB.Driver/MongoDB.Driver.csproj index c06572f1195..cdf376afe10 100644 --- a/src/MongoDB.Driver/MongoDB.Driver.csproj +++ b/src/MongoDB.Driver/MongoDB.Driver.csproj @@ -13,6 +13,11 @@ true + + + + + diff --git a/src/MongoDB.Driver/NoPipelineInput.cs b/src/MongoDB.Driver/NoPipelineInput.cs index 2285d989a91..f7881ece90e 100644 --- a/src/MongoDB.Driver/NoPipelineInput.cs +++ b/src/MongoDB.Driver/NoPipelineInput.cs @@ -60,6 +60,17 @@ public NoPipelineInput Deserialize(BsonDeserializationContext context, BsonDeser throw new NotSupportedException(); } + /// + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return GetType().Equals(obj.GetType()); + } + + /// + public override int GetHashCode() => 0; + void IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value) { throw new NotSupportedException(); diff --git a/src/MongoDB.Driver/OfTypeSerializer.cs b/src/MongoDB.Driver/OfTypeSerializer.cs index 1827b21d67c..1ff1b95e4c2 100644 --- a/src/MongoDB.Driver/OfTypeSerializer.cs +++ b/src/MongoDB.Driver/OfTypeSerializer.cs @@ -35,6 +35,16 @@ public override TDerivedDocument Deserialize(BsonDeserializationContext context, return _derivedDocumentSerializer.Deserialize(context, args); } + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is OfTypeSerializer other && + object.Equals(_derivedDocumentSerializer, other._derivedDocumentSerializer); + } + public bool GetDocumentId(object document, out object id, out Type idNominalType, out IIdGenerator idGenerator) { if (_derivedDocumentSerializer is IBsonIdProvider idProvider) @@ -50,6 +60,8 @@ public bool GetDocumentId(object document, out object id, out Type idNominalType } } + public override int GetHashCode() => 0; + public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TDerivedDocument value) { args.NominalType = typeof(TRootDocument); diff --git a/src/MongoDB.Driver/RangeWindowBoundary.cs b/src/MongoDB.Driver/RangeWindowBoundary.cs index 6940ac5617b..004a077030b 100644 --- a/src/MongoDB.Driver/RangeWindowBoundary.cs +++ b/src/MongoDB.Driver/RangeWindowBoundary.cs @@ -200,5 +200,17 @@ private static TSortBy Coerce(TValue value) { return (TSortBy)Convert.ChangeType(value, typeof(TSortBy)); } + + public override bool Equals(object obj) + { + if (object.ReferenceEquals(obj, null)) { return false; } + if (object.ReferenceEquals(this, obj)) { return true; } + return + base.Equals(obj) && + obj is ValueRangeWindowBoundaryConvertingValueSerializer other && + object.Equals(_sortBySerializer, other._sortBySerializer); + } + + public override int GetHashCode() => 0; } } diff --git a/src/MongoDB.Shared/DictionaryComparer.cs b/src/MongoDB.Shared/DictionaryComparer.cs new file mode 100644 index 00000000000..0c11cff02c9 --- /dev/null +++ b/src/MongoDB.Shared/DictionaryComparer.cs @@ -0,0 +1,45 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Collections.Generic; + +namespace MongoDB.Shared +{ + internal static class DictionaryComparer + { + public static bool Equals(Dictionary x, Dictionary y) + { + if (object.ReferenceEquals(x, y)) + { + return true; + } + + if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null) || x.Count != y.Count) + { + return false; + } + + foreach (var kv in x) + { + if (!y.TryGetValue(kv.Key, out var yValue) || !object.Equals(kv.Value, yValue)) + { + return false; + } + } + + return true; + } + } +} diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFindProjectionTranslators/ExpressionToFindProjectionTranslator.cs b/src/MongoDB.Shared/SequenceComparer.cs similarity index 55% rename from src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFindProjectionTranslators/ExpressionToFindProjectionTranslator.cs rename to src/MongoDB.Shared/SequenceComparer.cs index 902595af467..b5805fee4ff 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFindProjectionTranslators/ExpressionToFindProjectionTranslator.cs +++ b/src/MongoDB.Shared/SequenceComparer.cs @@ -13,17 +13,26 @@ * limitations under the License. */ -using System; -using System.Linq.Expressions; -using MongoDB.Driver.Linq.Linq3Implementation.Ast; +using System.Collections.Generic; +using System.Linq; -namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFindProjectionTranslators +namespace MongoDB.Shared { - internal static class ExpressionToFindProjectionTranslator + internal static class SequenceComparer { - public static AstFindProjection Translate(TranslationContext context, Expression expression) + public static bool Equals(IEnumerable x, IEnumerable y) { - throw new NotImplementedException(); + if (object.ReferenceEquals(x, y)) + { + return true; + } + + if (object.ReferenceEquals(x, null) || object.ReferenceEquals(y, null)) + { + return false; + } + + return x.SequenceEqual(y); } } } diff --git a/tests/MongoDB.Bson.Tests/ObjectModel/MaterializedOnDemandBsonArrayTests.cs b/tests/MongoDB.Bson.Tests/ObjectModel/MaterializedOnDemandBsonArrayTests.cs new file mode 100644 index 00000000000..5957f9ac1bc --- /dev/null +++ b/tests/MongoDB.Bson.Tests/ObjectModel/MaterializedOnDemandBsonArrayTests.cs @@ -0,0 +1,90 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Tests.ObjectModel +{ + public class MaterializedOnDemandBsonArraySerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + var y = new DerivedFromMaterializedOnDemandBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + var y = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private class DerivedFromMaterializedOnDemandBsonArraySerializer : MaterializedOnDemandBsonArray.MaterializedOnDemandBsonArraySerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/ObjectModel/MaterializedOnDemandBsonDocumentTests.cs b/tests/MongoDB.Bson.Tests/ObjectModel/MaterializedOnDemandBsonDocumentTests.cs new file mode 100644 index 00000000000..114c3721a41 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/ObjectModel/MaterializedOnDemandBsonDocumentTests.cs @@ -0,0 +1,90 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Tests.ObjectModel +{ + public class MaterializedOnDemandBsonDocumentSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new MaterializedOnDemandBsonDocumentSerializer(); + var y = new DerivedFromMaterializedOnDemandBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new MaterializedOnDemandBsonDocumentSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new MaterializedOnDemandBsonDocumentSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new MaterializedOnDemandBsonDocumentSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new MaterializedOnDemandBsonDocumentSerializer(); + var y = new MaterializedOnDemandBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new MaterializedOnDemandBsonDocumentSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private class DerivedFromMaterializedOnDemandBsonDocumentSerializer : MaterializedOnDemandBsonDocumentSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs index a9b836302a1..8a4c0f1e94c 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs @@ -16,12 +16,28 @@ using FluentAssertions; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization { public class BsonClassMapSerializerTests { + private static readonly BsonClassMap __classMap1; + private static readonly BsonClassMap __classMap2; + + static BsonClassMapSerializerTests() + { + __classMap1 = new BsonClassMap(typeof(MyModel)); + __classMap1.AutoMap(); + __classMap1.Freeze(); + + __classMap2 = new BsonClassMap(typeof(MyModel)); + __classMap2.AutoMap(); + __classMap2.MapProperty("Id").SetSerializer(new StringSerializer(BsonType.ObjectId)); + __classMap2.Freeze(); + } + // public methods [Fact] public void Deserialize_should_throw_invalidOperationException_when_creator_returns_null() @@ -56,6 +72,88 @@ public void Deserialize_should_throw_when_no_creators_found() .Subject.Message.Should().Be($"No matching creator found for class {typeof(ModelWithCtor).FullName}."); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonClassMapSerializer(__classMap1); + var y = new DerivedFromBsonClassMapSerializer(__classMap1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonClassMapSerializer(__classMap1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonClassMapSerializer(__classMap1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonClassMapSerializer(__classMap1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonClassMapSerializer(__classMap1); + var y = new BsonClassMapSerializer(__classMap1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BsonClassMapSerializer(__classMap1); + var y = new BsonClassMapSerializer(__classMap2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonClassMapSerializer(__classMap1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private class DerivedFromBsonClassMapSerializer : BsonClassMapSerializer + { + public DerivedFromBsonClassMapSerializer(BsonClassMap classMap) + : base(classMap) + { + } + } + // nested classes private class MyModel { diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs index 8e340808be7..fedc57318f2 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs @@ -17,9 +17,11 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; -using MongoDB.Bson; +using System.Runtime.Serialization; +using FluentAssertions; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson.TestHelpers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -443,4 +445,291 @@ private class TestClass private class InheritedTestClass : TestClass { } } + + public class BsonClassMapEqualsTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = CreateBsonClassMap(typeof(C)); + var y = CreateDerivedFromBsonClassMap(typeof(C)); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = CreateBsonClassMap(typeof(C)); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = CreateBsonClassMap(typeof(C)); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = CreateBsonClassMap(typeof(C)); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = CreateBsonClassMap(typeof(C)); + var y = Clone(x); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("baseClassMap")] + [InlineData("classType")] + [InlineData("creator")] + [InlineData("creatorMaps")] + [InlineData("declaredMemberMaps")] + [InlineData("discriminator")] + [InlineData("discriminatorIsRequired")] + [InlineData("extraElementsMemberIndex")] + [InlineData("extraElementsMemberMap")] + [InlineData("frozen")] + [InlineData("hasRootClass")] + [InlineData("idMemberMap")] + [InlineData("ignoreExtraElements")] + [InlineData("ignoreExtraElementsIsInherited")] + [InlineData("isRootClass")] + [InlineData("knownTypes")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = CreateBsonClassMap(typeof(C)); + var y = notEqualFieldName switch + { + "baseClassMap" => WithBaseClassMap(x, null), + "classType" => WithClassType(x, typeof(D)), + "creator" => WithCreator(x, () => null), + "creatorMaps" => WithCreatorMaps(x, null), + "declaredMemberMaps" => WithDeclaredMemberMaps(x, null), + "discriminator" => WithDiscriminator(x, null), + "discriminatorIsRequired" => WithDiscriminatorIsRequired(x, false), + "extraElementsMemberIndex" => WithExtraElementsMemberIndex(x, 1), + "extraElementsMemberMap" => WithExtraElementsMemberMap(x, null), + "frozen" => WithFrozen(x, false), + "hasRootClass" => WithHasRootClass(x, false), + "idMemberMap" => WithIdMemberMap(x, null), + "ignoreExtraElements" => WithIgnoreExtraElements(x, false), + "ignoreExtraElementsIsInherited" => WithIgnoreExtraElementsIsInherited(x, false), + "isRootClass" => WithIsRootClass(x, false), + "knownTypes" => WithKnownTypes(x, null), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = CreateBsonClassMap(typeof(C)); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private BsonClassMap CreateBsonClassMap(Type classType) + { + var classMap = new BsonClassMap(classType); + classMap.AutoMap(); + classMap.SetCreator(() => new BsonClassMap(classType)); + classMap.Freeze(); + return classMap; + } + + private BsonClassMap WithBaseClassMap(BsonClassMap classMap, BsonClassMap value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_baseClassMap", value); + return clone; + } + + private BsonClassMap WithClassType(BsonClassMap classMap, Type value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_classType", value); + return clone; + } + + private BsonClassMap WithCreator(BsonClassMap classMap, Func value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_creator", value); + return clone; + } + + private BsonClassMap WithCreatorMaps(BsonClassMap classMap, List value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_creatorMaps", value); + return clone; + } + + private BsonClassMap WithDeclaredMemberMaps(BsonClassMap classMap, List value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_declaredMemberMaps", value); + return clone; + } + + private BsonClassMap WithDiscriminator(BsonClassMap classMap, string value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_discriminator", value); + return clone; + } + + private BsonClassMap WithDiscriminatorIsRequired(BsonClassMap classMap, bool value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_discriminatorIsRequired", value); + return clone; + } + + private BsonClassMap WithExtraElementsMemberIndex(BsonClassMap classMap, int value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_extraElementsMemberIndex", value); + return clone; + } + + private BsonClassMap WithExtraElementsMemberMap(BsonClassMap classMap, BsonMemberMap value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_extraElementsMemberMap", value); + return clone; + } + + private BsonClassMap WithFrozen(BsonClassMap classMap, bool value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_frozen", value); + return clone; + } + + private BsonClassMap WithHasRootClass(BsonClassMap classMap, bool value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_hasRootClass", value); + return clone; + } + + private BsonClassMap WithIdMemberMap(BsonClassMap classMap, BsonMemberMap value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_idMemberMap", value); + return clone; + } + + private BsonClassMap WithIgnoreExtraElements(BsonClassMap classMap, bool value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_ignoreExtraElements", value); + return clone; + } + + private BsonClassMap WithIgnoreExtraElementsIsInherited(BsonClassMap classMap, bool value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_ignoreExtraElementsIsInherited", value); + return clone; + } + + private BsonClassMap WithIsRootClass(BsonClassMap classMap, bool value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_isRootClass", value); + return clone; + } + + private BsonClassMap WithKnownTypes(BsonClassMap classMap, List value) + { + var clone = Clone(classMap); + Reflector.SetFieldValue(classMap, "_knownTypes", value); + return clone; + } + + private BsonClassMap Clone(BsonClassMap classMap) + { + var clone = (BsonClassMap)FormatterServices.GetUninitializedObject(classMap.GetType()); + Reflector.SetFieldValue(clone, "_baseClassMap", Reflector.GetFieldValue(classMap, "_baseClassMap")); + Reflector.SetFieldValue(clone, "_classType", Reflector.GetFieldValue(classMap, "_classType")); + Reflector.SetFieldValue(clone, "_creator", Reflector.GetFieldValue(classMap, "_creator")); + Reflector.SetFieldValue(clone, "_creatorMaps", Reflector.GetFieldValue(classMap, "_creatorMaps")); + Reflector.SetFieldValue(clone, "_declaredMemberMaps", Reflector.GetFieldValue(classMap, "_declaredMemberMaps")); + Reflector.SetFieldValue(clone, "_discriminator", Reflector.GetFieldValue(classMap, "_discriminator")); + Reflector.SetFieldValue(clone, "_discriminatorIsRequired", Reflector.GetFieldValue(classMap, "_discriminatorIsRequired")); + Reflector.SetFieldValue(clone, "_extraElementsMemberIndex", Reflector.GetFieldValue(classMap, "_extraElementsMemberIndex")); + Reflector.SetFieldValue(clone, "_extraElementsMemberMap", Reflector.GetFieldValue(classMap, "_extraElementsMemberMap")); + Reflector.SetFieldValue(clone, "_frozen", Reflector.GetFieldValue(classMap, "_frozen")); + Reflector.SetFieldValue(clone, "_hasRootClass", Reflector.GetFieldValue(classMap, "_hasRootClass")); + Reflector.SetFieldValue(clone, "_idMemberMap", Reflector.GetFieldValue(classMap, "_idMemberMap")); + Reflector.SetFieldValue(clone, "_ignoreExtraElements", Reflector.GetFieldValue(classMap, "_ignoreExtraElements")); + Reflector.SetFieldValue(clone, "_ignoreExtraElementsIsInherited", Reflector.GetFieldValue(classMap, "_ignoreExtraElementsIsInherited")); + Reflector.SetFieldValue(clone, "_isRootClass", Reflector.GetFieldValue(classMap, "_isRootClass")); + Reflector.SetFieldValue(clone, "_knownTypes", Reflector.GetFieldValue(classMap, "_knownTypes")); + return clone; + } + + private BsonClassMap CreateDerivedFromBsonClassMap(Type classType) + { + var classMap = new DerivedFromBsonClassMap(classType); + classMap.AutoMap(); + classMap.Freeze(); + return classMap; + } + + private class DerivedFromBsonClassMap : BsonClassMap + { + public DerivedFromBsonClassMap(Type classType) : base(classType) + { + } + } + + [BsonDiscriminator(Required = true, RootClass = true)] + [BsonIgnoreExtraElements(Inherited = true)] + private class C + { + [BsonConstructor] + public C() { } + public int Id { get; set; } + public int X { get; set; } + public int Y { get; set; } + [BsonExtraElements] + BsonDocument ExtraElements { get; set; } + } + + private class D : C + { + } + } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs index 46ca3cc6e4a..d5aa2ab523d 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs @@ -14,11 +14,14 @@ */ using System; -using MongoDB.Bson; +using System.Reflection; +using System.Runtime.Serialization; +using FluentAssertions; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.IdGenerators; -using MongoDB.Bson.Serialization.Options; using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Bson.TestHelpers; +using Moq; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -290,4 +293,243 @@ public void TestReset() Assert.Null(memberMap.ShouldSerializeMethod); } } + + public class BsonMemberMapEqualsTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = CreateBsonMemberMap(); + var y = new DerivedFromBsonMemberMap(x.ClassMap, x.MemberInfo); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = CreateBsonMemberMap(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = CreateBsonMemberMap(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = CreateBsonMemberMap(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = CreateBsonMemberMap(); + var y = Clone(x); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("defaultValue")] + [InlineData("defaultValueCreator")] + [InlineData("defaultValueSpecified")] + [InlineData("elementName")] + [InlineData("frozen")] + [InlineData("idGenerator")] + [InlineData("ignoreIfDefault")] + [InlineData("ignoreIfNull")] + [InlineData("isRequired")] + [InlineData("memberInfo")] + [InlineData("order")] + [InlineData("serializer")] + [InlineData("shouldSerializeMethod")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = CreateBsonMemberMap(); + var y = notEqualFieldName switch + { + "defaultValue" => WithDefaultValue(x, 1), + "defaultValueCreator" => WithDefaultValueCreator(x, () => 1), + "defaultValueSpecified" => WithDefaultValueSpecified(x, true), + "elementName" => WithElementName(x, null), + "frozen" => WithFrozen(x, false), + "idGenerator" => WithIdGenerator(x, Mock.Of()), + "ignoreIfDefault" => WithIgnoreIfDefault(x, true), + "ignoreIfNull" => WithIgnoreIfNull(x, true), + "isRequired" => WithIsRequired(x, true), + "memberInfo" => WithMemberInfo(x, null), + "order" => WithOrder(x, 1), + "serializer" => WithSerializerMethod(x, null), + "shouldSerializeMethod" => WithShouldSerializeMethod(x, x => true), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(notEqualFieldName == null ? true : false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = CreateBsonMemberMap(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private BsonMemberMap CreateBsonMemberMap() + { + var classMap = new BsonClassMap(typeof(C)); + classMap.AutoMap(); + var memberMap = classMap.GetMemberMap("X"); + memberMap.SetSerializer(Int32Serializer.Instance); + classMap.Freeze(); // also freezes the member + return memberMap; + } + + private BsonMemberMap Clone(BsonMemberMap memberMap) + { + var clone = (BsonMemberMap)FormatterServices.GetUninitializedObject(memberMap.GetType()); + Reflector.SetFieldValue(clone, "_classMap", Reflector.GetFieldValue(memberMap, "_classMap")); + Reflector.SetFieldValue(clone, "_defaultValue", Reflector.GetFieldValue(memberMap, "_defaultValue")); + Reflector.SetFieldValue(clone, "_defaultValueCreator", Reflector.GetFieldValue(memberMap, "_defaultValueCreator")); + Reflector.SetFieldValue(clone, "_defaultValueSpecified", Reflector.GetFieldValue(memberMap, "_defaultValueSpecified")); + Reflector.SetFieldValue(clone, "_elementName", Reflector.GetFieldValue(memberMap, "_elementName")); + Reflector.SetFieldValue(clone, "_frozen", Reflector.GetFieldValue(memberMap, "_frozen")); + Reflector.SetFieldValue(clone, "_idGenerator", Reflector.GetFieldValue(memberMap, "_idGenerator")); + Reflector.SetFieldValue(clone, "_ignoreIfDefault", Reflector.GetFieldValue(memberMap, "_ignoreIfDefault")); + Reflector.SetFieldValue(clone, "_ignoreIfNull", Reflector.GetFieldValue(memberMap, "_ignoreIfNull")); + Reflector.SetFieldValue(clone, "_isRequired", Reflector.GetFieldValue(memberMap, "_isRequired")); + Reflector.SetFieldValue(clone, "_memberInfo", Reflector.GetFieldValue(memberMap, "_memberInfo")); + Reflector.SetFieldValue(clone, "_order", Reflector.GetFieldValue(memberMap, "_order")); + Reflector.SetFieldValue(clone, "_serializer", Reflector.GetFieldValue(memberMap, "_serializer")); + Reflector.SetFieldValue(clone, "_shouldSerializeMethod", Reflector.GetFieldValue(memberMap, "_shouldSerializeMethod")); + return clone; + } + + private BsonMemberMap WithDefaultValue(BsonMemberMap memberMap, object value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_defaultValue", value); + return clone; + } + + private BsonMemberMap WithDefaultValueCreator(BsonMemberMap memberMap, Func value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_defaultValueCreator", value); + return clone; + } + + private BsonMemberMap WithDefaultValueSpecified(BsonMemberMap memberMap, bool value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_defaultValueSpecified", value); + return clone; + } + + private BsonMemberMap WithElementName(BsonMemberMap memberMap, string value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_elementName", value); + return clone; + } + + private BsonMemberMap WithFrozen(BsonMemberMap memberMap, bool value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_frozen", value); + return clone; + } + + private BsonMemberMap WithIdGenerator(BsonMemberMap memberMap, IIdGenerator value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_idGenerator", value); + return clone; + } + + private BsonMemberMap WithIgnoreIfDefault(BsonMemberMap memberMap, bool value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_ignoreIfDefault", value); + return clone; + } + + private BsonMemberMap WithIgnoreIfNull(BsonMemberMap memberMap, bool value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_ignoreIfNull", value); + return clone; + } + + private BsonMemberMap WithIsRequired(BsonMemberMap memberMap, bool value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_isRequired", value); + return clone; + } + + private BsonMemberMap WithMemberInfo(BsonMemberMap memberMap, MemberInfo value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_memberInfo", value); + return clone; + } + + private BsonMemberMap WithOrder(BsonMemberMap memberMap, int value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_order", value); + return clone; + } + + private BsonMemberMap WithSerializerMethod(BsonMemberMap memberMap, IBsonSerializer value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_serializer", value); + return clone; + } + + private BsonMemberMap WithShouldSerializeMethod(BsonMemberMap memberMap, Func value) + { + var clone = Clone(memberMap); + Reflector.SetFieldValue(memberMap, "_shouldSerializeMethod", value); + return clone; + } + + private class DerivedFromBsonMemberMap : BsonMemberMap + { + public DerivedFromBsonMemberMap(BsonClassMap classMap, MemberInfo memberInfo) : base(classMap, memberInfo) + { + } + } + + private class C + { + public int X { get; set; } + } + } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonSerializationInfoTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonSerializationInfoTests.cs new file mode 100644 index 00000000000..7a11a06eeaa --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonSerializationInfoTests.cs @@ -0,0 +1,126 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization +{ + public class BsonSerializationInfoTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + var y = new DerivedFromBsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + var y = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("elementName")] + [InlineData("elementPath")] + [InlineData("serializer")] + [InlineData("nominalType")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var elementPath1 = new[] { "elementName1" }; + var elementPath2 = new[] { "elementName2" }; + var serializer1 = new Int32Serializer(BsonType.Int32); + var serializer2 = new Int32Serializer(BsonType.String); + var nominalType1 = typeof(int); + var nominalType2 = typeof(object); + var x = notEqualFieldName == "elementPath" ? + BsonSerializationInfo.CreateWithPath(elementPath1, serializer1, nominalType1) : + new BsonSerializationInfo("elementName1", serializer1, nominalType1); + var y = notEqualFieldName switch + { + "elementName" => new BsonSerializationInfo("elementName2", serializer1, nominalType1), + "elementPath" => BsonSerializationInfo.CreateWithPath(elementPath2, serializer1, nominalType1), + "serializer" => new BsonSerializationInfo("elementName1", serializer2, nominalType1), + "nominalType" => new BsonSerializationInfo("elementName1", serializer1, nominalType2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(notEqualFieldName == null ? true : false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonSerializationInfo("elementName", Int32Serializer.Instance, typeof(int)); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private class DerivedFromBsonSerializationInfo : BsonSerializationInfo + { + public DerivedFromBsonSerializationInfo(string elementName, IBsonSerializer serializer, Type nominalType) : base(elementName, serializer, nominalType) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Conventions/ObjectDiscriminatorConventionTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Conventions/ObjectDiscriminatorConventionTests.cs new file mode 100644 index 00000000000..373922ed4d3 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Conventions/ObjectDiscriminatorConventionTests.cs @@ -0,0 +1,106 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Conventions; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Conventions +{ + public class ObjectDiscriminatorConventionTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ObjectDiscriminatorConvention("_t"); + var y = new DerivedFromObjectDiscriminatorConvention("_t"); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ObjectDiscriminatorConvention("_t"); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ObjectDiscriminatorConvention("_t"); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ObjectDiscriminatorConvention("_t"); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ObjectDiscriminatorConvention("_t"); + var y = new ObjectDiscriminatorConvention("_t"); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ObjectDiscriminatorConvention("_t"); + var y = new ObjectDiscriminatorConvention("_u"); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ObjectDiscriminatorConvention("_t"); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromObjectDiscriminatorConvention : ObjectDiscriminatorConvention + { + public DerivedFromObjectDiscriminatorConvention(string elementName) + : base(elementName) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Conventions/StandardDiscriminatorConventionTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Conventions/StandardDiscriminatorConventionTests.cs index 466ed678dde..f1d633bd724 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Conventions/StandardDiscriminatorConventionTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Conventions/StandardDiscriminatorConventionTests.cs @@ -14,6 +14,7 @@ */ using System; +using FluentAssertions; using MongoDB.Bson.Serialization.Conventions; using Xunit; @@ -32,5 +33,99 @@ public void TestConstructorThrowsWhenElementNameIsNull() { Assert.Throws(() => new ScalarDiscriminatorConvention(null)); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + var y = new DerivedFromConcreteStandardDiscriminatorConvention("_t"); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + var y = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + var y = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_u"); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (StandardDiscriminatorConvention)new ConcreteStandardDiscriminatorConvention("_t"); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteStandardDiscriminatorConvention : StandardDiscriminatorConvention + { + public ConcreteStandardDiscriminatorConvention(string elementName) + : base(elementName) + { + } + + public override BsonValue GetDiscriminator(Type nominalType, Type actualType) => throw new NotImplementedException(); + } + + public class DerivedFromConcreteStandardDiscriminatorConvention : ConcreteStandardDiscriminatorConvention + { + public DerivedFromConcreteStandardDiscriminatorConvention(string elementName) + : base(elementName) + { + } + + public override BsonValue GetDiscriminator(Type nominalType, Type actualType) => throw new NotImplementedException(); + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Options/RepresentationConverterTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Options/RepresentationConverterTests.cs index 958ea7b3685..5faa00f6b64 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Options/RepresentationConverterTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Options/RepresentationConverterTests.cs @@ -14,7 +14,7 @@ */ using System; -using MongoDB.Bson; +using FluentAssertions; using MongoDB.Bson.Serialization.Options; using Xunit; @@ -263,5 +263,94 @@ public void TestAllowTruncationTrue() Assert.Equal((uint)1, converter.ToUInt32((double)1.5)); Assert.Equal((ulong)1, converter.ToUInt64((double)1.5)); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new RepresentationConverter(false, false); + var y = new DerivedFromRepresentationConverter(false, false); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new RepresentationConverter(false, false); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new RepresentationConverter(false, false); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new RepresentationConverter(false, false); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new RepresentationConverter(false, false); + var y = new RepresentationConverter(false, false); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("allowOverflow")] + [InlineData("allowTruncation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new RepresentationConverter(false, false); + var y = notEqualFieldName switch + { + "allowOverflow" => new RepresentationConverter(true, false), + "allowTruncation" => new RepresentationConverter(false, true), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new RepresentationConverter(false, false); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromRepresentationConverter : RepresentationConverter + { + public DerivedFromRepresentationConverter(bool allowOverflow, bool allowTruncation) + : base(allowOverflow, allowTruncation) + { + } + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/AbstractClassSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/AbstractClassSerializerTests.cs new file mode 100644 index 00000000000..480521c3de4 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/AbstractClassSerializerTests.cs @@ -0,0 +1,101 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class AbstractClassSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + var y = new DerivedFromConcreteAbstractClassSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + var y = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (AbstractClassSerializer)new ConcreteAbstractClassSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteAbstractClassSerializer : AbstractClassSerializer + where TClass : class + { + } + + public class DerivedFromConcreteAbstractClassSerializer : ConcreteAbstractClassSerializer + where TClass : class + { + } + + public class C + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ArraySerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ArraySerializerTests.cs index 697074ca478..be5c7fa7f9d 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ArraySerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ArraySerializerTests.cs @@ -13,10 +13,12 @@ * limitations under the License. */ +using System; using System.Linq; -using MongoDB.Bson; +using FluentAssertions; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -351,4 +353,85 @@ public void TestSerialize2Mixed() Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } } + + public class ArraySerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ArraySerializer(); + var y = new DerivedFromArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ArraySerializer(); + var y = new ArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ArraySerializer(new Int32Serializer(BsonType.Int32)); + var y = new ArraySerializer(new Int32Serializer(BsonType.String)); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromArraySerializer : ArraySerializer + { + } + } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BooleanSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BooleanSerializerTests.cs new file mode 100644 index 00000000000..7d0ab8d97e5 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BooleanSerializerTests.cs @@ -0,0 +1,101 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class BooleanSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BooleanSerializer(); + var y = new DerivedFromBooleanSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BooleanSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BooleanSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BooleanSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BooleanSerializer(); + var y = new BooleanSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BooleanSerializer(BsonType.Boolean); + var y = new BooleanSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BooleanSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBooleanSerializer : BooleanSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonBinaryDataSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonBinaryDataSerializerTests.cs index e970e7e3c33..b59ae4c6b15 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonBinaryDataSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonBinaryDataSerializerTests.cs @@ -68,6 +68,73 @@ public void DeserializeValue_should_call_ReadBinaryData() result.Should().BeSameAs(binaryData); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonBinaryDataSerializer(); + var y = new DerivedFromBsonBinaryDataSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonBinaryDataSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonBinaryDataSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonBinaryDataSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonBinaryDataSerializer(); + var y = new BsonBinaryDataSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonBinaryDataSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonBinaryDataSerializer : BsonBinaryDataSerializer + { + } + [Theory] [ParameterAttributeData] [ResetGuidModeAfterTest] diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonDecimal128SerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonDecimal128SerializerTests.cs new file mode 100644 index 00000000000..989ed247b40 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonDecimal128SerializerTests.cs @@ -0,0 +1,91 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class BsonDecimal128SerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonDecimal128Serializer(); + var y = new DerivedFromBsonDecimal128Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonDecimal128Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonDecimal128Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonDecimal128Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonDecimal128Serializer(); + var y = new BsonDecimal128Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonDecimal128Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonDecimal128Serializer : BsonDecimal128Serializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonDocumentBackedClassSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonDocumentBackedClassSerializerTests.cs new file mode 100644 index 00000000000..f4b60cdab16 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonDocumentBackedClassSerializerTests.cs @@ -0,0 +1,125 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Reflection; +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class BsonDocumentBackedClassSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + var y = new DerivedFromConcreteBsonDocumentBackedClassSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + var y = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + var y = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + var registerMemberMethod = typeof(BsonDocumentBackedClassSerializer).GetMethod("RegisterMember", BindingFlags.NonPublic | BindingFlags.Instance); + registerMemberMethod.Invoke(y, new object[] { "Y", "y", Int32Serializer.Instance }); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (BsonDocumentBackedClassSerializer)new ConcreteBsonDocumentBackedClassSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + private class ConcreteBsonDocumentBackedClassSerializer : BsonDocumentBackedClassSerializer + where TClass : BsonDocumentBackedClass + { + public ConcreteBsonDocumentBackedClassSerializer() + { + RegisterMember("X", "x", Int32Serializer.Instance); + } + + protected override TClass CreateInstance(BsonDocument backingDocument) => throw new NotImplementedException(); + } + + private class DerivedFromConcreteBsonDocumentBackedClassSerializer : ConcreteBsonDocumentBackedClassSerializer + where TClass : BsonDocumentBackedClass + { + public DerivedFromConcreteBsonDocumentBackedClassSerializer() + { + } + } + + private class C : BsonDocumentBackedClass + { + public C(BsonDocument document) : base(document, new ConcreteBsonDocumentBackedClassSerializer()) { } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueCSharpValueNullSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueCSharpValueNullSerializerTests.cs new file mode 100644 index 00000000000..5bd6717da15 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueCSharpValueNullSerializerTests.cs @@ -0,0 +1,369 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class BsonValueCSharpNullSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + var y = new DerivedFromBsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + var y = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BsonValueCSharpNullSerializer(new BsonValueSerializer1()); + var y = new BsonValueCSharpNullSerializer(new BsonValueSerializer2()); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonValueCSharpNullSerializer(BsonValueSerializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonValueCSharpNullSerializer : BsonValueCSharpNullSerializer + where TBsonValue : BsonValue + { + public DerivedFromBsonValueCSharpNullSerializer(IBsonSerializer wrappedSerializer) + : base(wrappedSerializer) + { + } + } + } + + public class BsonValueCSharpNullArrayAndDocumentSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + var y = new DerivedFromBsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + var y = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(new BsonValueSerializer1()); + var y = new BsonValueCSharpNullArrayAndDocumentSerializer(new BsonValueSerializer2()); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonValueCSharpNullArrayAndDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonValueCSharpNullArrayAndDocumentSerializer : BsonValueCSharpNullArrayAndDocumentSerializer + where TBsonValue : BsonValue + { + public DerivedFromBsonValueCSharpNullArrayAndDocumentSerializer(IBsonSerializer wrappedSerializer) + : base(wrappedSerializer) + { + } + } + } + + public class BsonValueCSharpNullArraySerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + var y = new DerivedFromBsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + var y = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BsonValueCSharpNullArraySerializer(new BsonValueSerializer1()); + var y = new BsonValueCSharpNullArraySerializer(new BsonValueSerializer2()); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonValueCSharpNullArraySerializer(BsonValueSerializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonValueCSharpNullArraySerializer : BsonValueCSharpNullArraySerializer + where TBsonValue : BsonValue + { + public DerivedFromBsonValueCSharpNullArraySerializer(IBsonSerializer wrappedSerializer) + : base(wrappedSerializer) + { + } + } + } + + public class BsonValueCSharpNullDocumentSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + var y = new DerivedFromBsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + var y = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BsonValueCSharpNullDocumentSerializer(new BsonValueSerializer1()); + var y = new BsonValueCSharpNullDocumentSerializer(new BsonValueSerializer2()); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonValueCSharpNullDocumentSerializer(BsonValueSerializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonValueCSharpNullDocumentSerializer : BsonValueCSharpNullDocumentSerializer + where TBsonValue : BsonValue + { + public DerivedFromBsonValueCSharpNullDocumentSerializer(IBsonSerializer wrappedSerializer) + : base(wrappedSerializer) + { + } + } + } + + public class BsonValueSerializer1 : SerializerBase { } + + public class BsonValueSerializer2 : SerializerBase { } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerBaseTests.cs index 150f9997f89..ca2ccbea041 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerBaseTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerBaseTests.cs @@ -13,11 +13,95 @@ * limitations under the License. */ +using FluentAssertions; +using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Serializers; using MongoDB.Bson.TestHelpers; +using Xunit; namespace MongoDB.Bson.Tests.Serialization.Serializers { + public class BsonValueSerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + var y = new DerivedFromConcreteBsonValueSerializerBase(BsonType.Int32); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + var y = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (BsonValueSerializerBase)new ConcreteBsonValueSerializerBase(BsonType.Int32); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteBsonValueSerializerBase : BsonValueSerializerBase + where TBsonValue : BsonValue + { + public ConcreteBsonValueSerializerBase(BsonType representation) : base(representation) { } + + protected override TBsonValue DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) => throw new System.NotImplementedException(); + protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value) => throw new System.NotImplementedException(); + } + + public class DerivedFromConcreteBsonValueSerializerBase : ConcreteBsonValueSerializerBase + where TBsonValue : BsonValue + { + public DerivedFromConcreteBsonValueSerializerBase(BsonType representation) : base(representation) { } + } + } + public static class BsonValueSerializerBaseReflector { public static BsonType? _bsonType(this BsonValueSerializerBase obj) where TBsonValue : BsonValue diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerTests.cs index 1278d851aa6..0bbadd681f9 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/BsonValueSerializerTests.cs @@ -28,6 +28,76 @@ namespace MongoDB.Bson.Tests.Serialization { + public class BsonValueSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonValueSerializer(); + var y = new DerivedFromBsonValueSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonValueSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonValueSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonValueSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonValueSerializer(); + var y = new BsonValueSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonValueSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonValueSerializer : BsonValueSerializer + { + } + } + public class BsonArraySerializerTests { public class TestClass @@ -84,6 +154,73 @@ public void TestNotEmpty() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonArraySerializer(); + var y = new DerivedFromBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonArraySerializer(); + var y = new BsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonArraySerializer : BsonArraySerializer + { + } } public class BsonBinaryGuidSerializerTests @@ -255,6 +392,73 @@ public void TestTrue() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonBooleanSerializer(); + var y = new DerivedFromBsonBooleanSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonBooleanSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonBooleanSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonBooleanSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonBooleanSerializer(); + var y = new BsonBooleanSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonBooleanSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonBooleanSerializer : BsonBooleanSerializer + { + } } public class BsonDateTimeSerializerTests @@ -434,6 +638,73 @@ public void TestUtc() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonDateTimeSerializer(); + var y = new DerivedFromBsonDateTimeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonDateTimeSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonDateTimeSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonDateTimeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonDateTimeSerializer(); + var y = new BsonDateTimeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonDateTimeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonDateTimeSerializer : BsonDateTimeSerializer + { + } } public class BsonDocumentSerializerTests @@ -496,6 +767,73 @@ public void TestNotEmpty() Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonDocumentSerializer(); + var y = new DerivedFromBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonDocumentSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonDocumentSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonDocumentSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonDocumentSerializer(); + var y = new BsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonDocumentSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonDocumentSerializer : BsonDocumentSerializer + { + } + [Fact] public void GetDocumentId_should_return_expected_result_when_id_is_missing() { @@ -679,6 +1017,73 @@ public void TestNotEmpty() Assert.Equal(expectedMessage, ex.Message.Substring(0, ex.Message.IndexOf(':'))); } } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonDocumentWrapperSerializer(); + var y = new DerivedFromBsonDocumentWrapperSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonDocumentWrapperSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonDocumentWrapperSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonDocumentWrapperSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonDocumentWrapperSerializer(); + var y = new BsonDocumentWrapperSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonDocumentWrapperSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonDocumentWrapperSerializer : BsonDocumentWrapperSerializer + { + } } public class BsonDoubleSerializerTests @@ -815,34 +1220,101 @@ public void TestPositiveInfinity() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } - } - public class BsonInt32SerializerTests - { - public class TestClass + [Fact] + public void Equals_derived_should_return_false() { - public TestClass() { } + var x = new BsonDoubleSerializer(); + var y = new DerivedFromBsonDoubleSerializer(); - public TestClass(BsonInt32 value) - { - this.B = value; - this.V = value; - } + var result = x.Equals(y); - public BsonValue B { get; set; } - public BsonInt32 V { get; set; } + result.Should().Be(false); } [Fact] - public void TestNull() + public void Equals_null_should_return_false() { - var obj = new TestClass(null); - var json = obj.ToJson(); - var expected = "{ 'B' : #, 'V' : # }".Replace("#", "{ '_csharpnull' : true }").Replace("'", "\""); - Assert.Equal(expected, json); + var x = new BsonDoubleSerializer(); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonDoubleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonDoubleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonDoubleSerializer(); + var y = new BsonDoubleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonDoubleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonDoubleSerializer : BsonDoubleSerializer + { + } + } + + public class BsonInt32SerializerTests + { + public class TestClass + { + public TestClass() { } + + public TestClass(BsonInt32 value) + { + this.B = value; + this.V = value; + } + + public BsonValue B { get; set; } + public BsonInt32 V { get; set; } + } + + [Fact] + public void TestNull() + { + var obj = new TestClass(null); + var json = obj.ToJson(); + var expected = "{ 'B' : #, 'V' : # }".Replace("#", "{ '_csharpnull' : true }").Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); Assert.Equal(null, rehydrated.B); Assert.Equal(null, rehydrated.V); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); @@ -912,6 +1384,73 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonInt32Serializer(); + var y = new DerivedFromBsonInt32Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonInt32Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonInt32Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonInt32Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonInt32Serializer(); + var y = new BsonInt32Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonInt32Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonInt32Serializer : BsonInt32Serializer + { + } } public class BsonInt64SerializerTests @@ -1009,6 +1548,73 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonInt64Serializer(); + var y = new DerivedFromBsonInt64Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonInt64Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonInt64Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonInt64Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonInt64Serializer(); + var y = new BsonInt64Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonInt64Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonInt64Serializer : BsonInt64Serializer + { + } } public class BsonJavaScriptSerializerTests @@ -1054,6 +1660,73 @@ public void TestNotNull() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonJavaScriptSerializer(); + var y = new DerivedFromBsonJavaScriptSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonJavaScriptSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonJavaScriptSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonJavaScriptSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonJavaScriptSerializer(); + var y = new BsonJavaScriptSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonJavaScriptSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonJavaScriptSerializer : BsonJavaScriptSerializer + { + } } public class BsonJavaScriptWithScopeSerializerTests @@ -1100,6 +1773,73 @@ public void TestNotNull() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonJavaScriptWithScopeSerializer(); + var y = new DerivedFromBsonJavaScriptWithScopeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonJavaScriptWithScopeSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonJavaScriptWithScopeSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonJavaScriptWithScopeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonJavaScriptWithScopeSerializer(); + var y = new BsonJavaScriptWithScopeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonJavaScriptWithScopeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonJavaScriptWithScopeSerializer : BsonJavaScriptWithScopeSerializer + { + } } public class BsonMaxKeySerializerTests @@ -1146,6 +1886,73 @@ public void TestValue() Assert.Same(obj.V, rehydrated.V); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonMaxKeySerializer(); + var y = new DerivedFromBsonMaxKeySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonMaxKeySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonMaxKeySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonMaxKeySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonMaxKeySerializer(); + var y = new BsonMaxKeySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonMaxKeySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonMaxKeySerializer : BsonMaxKeySerializer + { + } } public class BsonMinKeySerializerTests @@ -1192,6 +1999,73 @@ public void TestValue() Assert.Same(obj.V, rehydrated.V); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonMinKeySerializer(); + var y = new DerivedFromBsonMinKeySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonMinKeySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonMinKeySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonMinKeySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonMinKeySerializer(); + var y = new BsonMinKeySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonMinKeySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonMinKeySerializer : BsonMinKeySerializer + { + } } public class BsonNullSerializerTests @@ -1218,31 +2092,98 @@ public void TestNull() var expected = "{ 'B' : #, 'V' : # }".Replace("#", "{ '_csharpnull' : true }").Replace("'", "\""); Assert.Equal(expected, json); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.Equal(null, rehydrated.B); - Assert.Equal(null, rehydrated.V); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.Equal(null, rehydrated.B); + Assert.Equal(null, rehydrated.V); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + + // test that we can still deserialize the legacy representation for a BsonNull value of C# null + var legacy = expected.Replace("_csharpnull", "$csharpnull"); + rehydrated = BsonSerializer.Deserialize(legacy); + Assert.Equal(null, rehydrated.B); + Assert.Equal(null, rehydrated.V); + } + + [Fact] + public void TestValue() + { + var obj = new TestClass(BsonNull.Value); + var json = obj.ToJson(); + var expected = "{ 'B' : #, 'V' : # }".Replace("#", "null").Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.Same(obj.V, rehydrated.V); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonNullSerializer(); + var y = new DerivedFromBsonNullSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonNullSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonNullSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonNullSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonNullSerializer(); + var y = new BsonNullSerializer(); + + var result = x.Equals(y); - // test that we can still deserialize the legacy representation for a BsonNull value of C# null - var legacy = expected.Replace("_csharpnull", "$csharpnull"); - rehydrated = BsonSerializer.Deserialize(legacy); - Assert.Equal(null, rehydrated.B); - Assert.Equal(null, rehydrated.V); + result.Should().Be(true); } [Fact] - public void TestValue() + public void GetHashCode_should_return_zero() { - var obj = new TestClass(BsonNull.Value); - var json = obj.ToJson(); - var expected = "{ 'B' : #, 'V' : # }".Replace("#", "null").Replace("'", "\""); - Assert.Equal(expected, json); + var x = new BsonNullSerializer(); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.Same(obj.V, rehydrated.V); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonNullSerializer : BsonNullSerializer + { } } @@ -1291,6 +2232,73 @@ public void TestNotNull() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonObjectIdSerializer(); + var y = new DerivedFromBsonObjectIdSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonObjectIdSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonObjectIdSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonObjectIdSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonObjectIdSerializer(); + var y = new BsonObjectIdSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonObjectIdSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonObjectIdSerializer : BsonObjectIdSerializer + { + } } public class BsonRegularExpressionSerializerTests @@ -1349,6 +2357,73 @@ public void TestWithOptions() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonRegularExpressionSerializer(); + var y = new DerivedFromBsonRegularExpressionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonRegularExpressionSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonRegularExpressionSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonRegularExpressionSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonRegularExpressionSerializer(); + var y = new BsonRegularExpressionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonRegularExpressionSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonRegularExpressionSerializer : BsonRegularExpressionSerializer + { + } } public class BsonStringObjectIdTests @@ -1444,6 +2519,73 @@ public void TestHelloWorld() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonStringSerializer(); + var y = new DerivedFromBsonStringSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonStringSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonStringSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonStringSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonStringSerializer(); + var y = new BsonStringSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonStringSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonStringSerializer : BsonStringSerializer + { + } } public class BsonSymbolSerializerTests @@ -1504,6 +2646,73 @@ public void TestHelloWorld() Assert.Same(obj.V, rehydrated.V); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonSymbolSerializer(); + var y = new DerivedFromBsonSymbolSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonSymbolSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonSymbolSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonSymbolSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonSymbolSerializer(); + var y = new BsonSymbolSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonSymbolSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonSymbolSerializer : BsonSymbolSerializer + { + } } public class BsonTimestampSerializerTests @@ -1614,6 +2823,73 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonTimestampSerializer(); + var y = new DerivedFromBsonTimestampSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonTimestampSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonTimestampSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonTimestampSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonTimestampSerializer(); + var y = new BsonTimestampSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonTimestampSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonTimestampSerializer : BsonTimestampSerializer + { + } } public class BsonUndefinedSerializerTests @@ -1660,5 +2936,72 @@ public void TestValue() Assert.Same(obj.V, rehydrated.V); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BsonUndefinedSerializer(); + var y = new DerivedFromBsonUndefinedSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BsonUndefinedSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BsonUndefinedSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BsonUndefinedSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BsonUndefinedSerializer(); + var y = new BsonUndefinedSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BsonUndefinedSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBsonUndefinedSerializer : BsonUndefinedSerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ClassSerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ClassSerializerBaseTests.cs new file mode 100644 index 00000000000..487e0b06c79 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ClassSerializerBaseTests.cs @@ -0,0 +1,96 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class ClassSerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (ClassSerializerBase)new ConcreteClassSerializerBase(); + var y = new DerivedFromConcreteClassSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (ClassSerializerBase)new ConcreteClassSerializerBase(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (ClassSerializerBase)new ConcreteClassSerializerBase(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (ClassSerializerBase)new ConcreteClassSerializerBase(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (ClassSerializerBase)new ConcreteClassSerializerBase(); + var y = (ClassSerializerBase)new ConcreteClassSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (ClassSerializerBase)new ConcreteClassSerializerBase(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteClassSerializerBase : ClassSerializerBase + where TValue : class + { + } + + public class DerivedFromConcreteClassSerializerBase : ConcreteClassSerializerBase + where TValue : class + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeSerializerTests.cs index ef7a11d305a..c8d576fab49 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DateTimeSerializerTests.cs @@ -15,14 +15,106 @@ using System; using System.Linq; +using FluentAssertions; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson.Serialization.Serializers; using Xunit; -namespace MongoDB.Bson.Tests.Serialization.Conventions.DateTimeSerializationOptions +namespace MongoDB.Bson.Tests.Serialization.Serializers { + public class DateTimeSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DateTimeSerializer(); + var y = new DerivedFromDateTimeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DateTimeSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DateTimeSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DateTimeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DateTimeSerializer(); + var y = new DateTimeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("dateOnly")] + [InlineData("kind")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new DateTimeSerializer(); + var y = notEqualFieldName switch + { + "dateOnly" => new DateTimeSerializer(dateOnly: true), + "kind" => new DateTimeSerializer(kind: DateTimeKind.Unspecified), + "representation" => new DateTimeSerializer(representation: BsonType.String), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DateTimeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDateTimeSerializer : DateTimeSerializer + { + } + } + public class LocalTests { public class C diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/Decimal128SerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/Decimal128SerializerTests.cs new file mode 100644 index 00000000000..fd32dd0a510 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/Decimal128SerializerTests.cs @@ -0,0 +1,115 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization.Options; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class Decimal128SerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new Decimal128Serializer(); + var y = new DerivedFromDecimal128Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new Decimal128Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new Decimal128Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new Decimal128Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new Decimal128Serializer(); + var y = new Decimal128Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var representation1 = BsonType.String; + var representation2 = BsonType.Decimal128; + var x = new Decimal128Serializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new Decimal128Serializer(representation2, converter1), + "converter" => new Decimal128Serializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new Decimal128Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDecimal128Serializer : Decimal128Serializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryInterfaceImplementerSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryInterfaceImplementerSerializerTests.cs new file mode 100644 index 00000000000..de69b5ff966 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryInterfaceImplementerSerializerTests.cs @@ -0,0 +1,221 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections; +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization.Options; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class DictionaryInterfaceImplementerSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DictionaryInterfaceImplementerSerializer(); + var y = new DerivedFromDictionaryInterfaceImplementerSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DictionaryInterfaceImplementerSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DictionaryInterfaceImplementerSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DictionaryInterfaceImplementerSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DictionaryInterfaceImplementerSerializer(); + var y = new DictionaryInterfaceImplementerSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("dictionaryRepresentation")] + [InlineData("keySerializer")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_fields_should_return_true(string notEqualFieldName) + { + var dictionaryRepresentation1 = DictionaryRepresentation.ArrayOfArrays; + var dictionaryRepresentation2 = DictionaryRepresentation.ArrayOfDocuments; + var keySerializer1 = new Int32Serializer(BsonType.Int32); + var keySerializer2 = new Int32Serializer(BsonType.String); + var valueSerializer1 = new Int32Serializer(BsonType.Int32); + var valueSerializer2 = new Int32Serializer(BsonType.String); + + var x = new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation1, keySerializer1, valueSerializer1); + var y = notEqualFieldName switch + { + "dictionaryRepresentation" => new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation2, keySerializer1, valueSerializer1), + "keySerializer" => new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation1, keySerializer2, valueSerializer1), + "valueSerializer" => new DictionaryInterfaceImplementerSerializer(dictionaryRepresentation1, keySerializer1, valueSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DictionaryInterfaceImplementerSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDictionaryInterfaceImplementerSerializer : DictionaryInterfaceImplementerSerializer + where TDictionary : class, IDictionary, new() + { + } + } + + public class DictionaryInterfaceImplementerSerializerGenericTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DictionaryInterfaceImplementerSerializer, int, int>(); + var y = new DerivedFromDictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DictionaryInterfaceImplementerSerializer, int, int>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DictionaryInterfaceImplementerSerializer, int, int>(); + var y = new DictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("dictionaryRepresentation")] + [InlineData("keySerializer")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_fields_should_return_true(string notEqualFieldName) + { + var dictionaryRepresentation1 = DictionaryRepresentation.ArrayOfArrays; + var dictionaryRepresentation2 = DictionaryRepresentation.ArrayOfDocuments; + var keySerializer1 = new Int32Serializer(BsonType.Int32); + var keySerializer2 = new Int32Serializer(BsonType.String); + var valueSerializer1 = new Int32Serializer(BsonType.Int32); + var valueSerializer2 = new Int32Serializer(BsonType.String); + + var x = new DictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation1, keySerializer1, valueSerializer1); + var y = notEqualFieldName switch + { + "dictionaryRepresentation" => new DictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation2, keySerializer1, valueSerializer1), + "keySerializer" => new DictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation1, keySerializer2, valueSerializer1), + "valueSerializer" => new DictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation1, keySerializer1, valueSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDictionaryInterfaceImplementerSerializer : DictionaryInterfaceImplementerSerializer + where TDictionary : class, IDictionary + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionarySerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionarySerializerBaseTests.cs new file mode 100644 index 00000000000..b3a3962d4e9 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DictionarySerializerBaseTests.cs @@ -0,0 +1,249 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections; +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Conventions; +using MongoDB.Bson.Serialization.Options; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class DictionarySerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + var y = new DerivedFromConcreteDictionarySerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + var y = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("dictionaryRepresentation")] + [InlineData("keySerializer")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var discriminatorConvention1 = new ScalarDiscriminatorConvention("_t"); + var discriminatorConvention2 = new HierarchicalDiscriminatorConvention("_t"); + var objectSerializer1 = new ObjectSerializer(discriminatorConvention1); + var objectSerializer2 = new ObjectSerializer(discriminatorConvention2); + + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(DictionaryRepresentation.Document, objectSerializer1, objectSerializer1); + var y = (DictionarySerializerBase)(notEqualFieldName switch + { + "dictionaryRepresentation" => new ConcreteDictionarySerializerBase(DictionaryRepresentation.ArrayOfArrays, objectSerializer1, objectSerializer1), + "keySerializer" => new ConcreteDictionarySerializerBase(DictionaryRepresentation.Document, objectSerializer2, objectSerializer1), + "valueSerializer" => new ConcreteDictionarySerializerBase(DictionaryRepresentation.Document, objectSerializer1, objectSerializer2), + _ => throw new Exception() + }); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (DictionarySerializerBase)new ConcreteDictionarySerializerBase(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteDictionarySerializerBase : DictionarySerializerBase + where TDictionary : class, IDictionary + { + public ConcreteDictionarySerializerBase() { } + + public ConcreteDictionarySerializerBase( + DictionaryRepresentation dictionaryRepresentation, + IBsonSerializer keySerializer, + IBsonSerializer valueSerializer) + : base(dictionaryRepresentation, keySerializer, valueSerializer) + { + } + + protected override TDictionary CreateInstance() => throw new NotImplementedException(); + } + + public class DerivedFromConcreteDictionarySerializerBase : ConcreteDictionarySerializerBase + where TDictionary : class, IDictionary + { + } + } + + public class DictionarySerializerBaseGenericTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + var y = new DerivedFromConcreteDictionarySerializerBase, int, int>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + var y = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("dictionaryRepresentation")] + [InlineData("keySerializer")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = DictionaryRepresentation.Document; + var representation2 = DictionaryRepresentation.ArrayOfDocuments; + var int32Serializer1 = new Int32Serializer(BsonType.Int32); + var int32Serializer2 = new Int32Serializer(BsonType.String); + + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(representation1, int32Serializer1, int32Serializer1); + var y = (DictionarySerializerBase, int, int>)(notEqualFieldName switch + { + "dictionaryRepresentation" => new ConcreteDictionarySerializerBase, int, int>(representation2, int32Serializer1, int32Serializer1), + "keySerializer" => new ConcreteDictionarySerializerBase, int, int>(representation1, int32Serializer2, int32Serializer1), + "valueSerializer" => new ConcreteDictionarySerializerBase, int, int>(representation1, int32Serializer1, int32Serializer2), + _ => throw new Exception() + }); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (DictionarySerializerBase, int, int>)new ConcreteDictionarySerializerBase, int, int>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteDictionarySerializerBase : DictionarySerializerBase + where TDictionary : class, IEnumerable> + { + public ConcreteDictionarySerializerBase() { } + + public ConcreteDictionarySerializerBase( + DictionaryRepresentation dictionaryRepresentation, + IBsonSerializer keySerializer, + IBsonSerializer valueSerializer) + : base(dictionaryRepresentation, keySerializer, valueSerializer) + { + } + } + + public class DerivedFromConcreteDictionarySerializerBase : ConcreteDictionarySerializerBase + where TDictionary : class, IEnumerable> + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DiscriminatedInterfaceSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DiscriminatedInterfaceSerializerTests.cs new file mode 100644 index 00000000000..01fbebf8357 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DiscriminatedInterfaceSerializerTests.cs @@ -0,0 +1,124 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Conventions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class DiscriminatedInterfaceSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DiscriminatedInterfaceSerializer(); + var y = new DerivedFromDiscriminatedInterfaceSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DiscriminatedInterfaceSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DiscriminatedInterfaceSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DiscriminatedInterfaceSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DiscriminatedInterfaceSerializer(); + var y = new DiscriminatedInterfaceSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("discriminatorConvention")] + [InlineData("interfaceSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var discriminatorConvention1 = new ScalarDiscriminatorConvention("_t"); + var discriminatorConvention2 = new ScalarDiscriminatorConvention("_u"); + var interfaceSerializer1 = new InterfaceSerializer1(); + var interfaceSerializer2 = new InterfaceSerializer2(); + + var x = new DiscriminatedInterfaceSerializer(); + var y = notEqualFieldName switch + { + "discriminatorConvention" => new DiscriminatedInterfaceSerializer(discriminatorConvention2, interfaceSerializer1), + "interfaceSerializer" => new DiscriminatedInterfaceSerializer(discriminatorConvention1, interfaceSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DiscriminatedInterfaceSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDiscriminatedInterfaceSerializer : DiscriminatedInterfaceSerializer + { + public DerivedFromDiscriminatedInterfaceSerializer() { } + } + + public interface I { } + + public class InterfaceSerializer1 : SerializerBase { } + + public class InterfaceSerializer2 : SerializerBase { } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DiscriminatedWrapperSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DiscriminatedWrapperSerializerTests.cs new file mode 100644 index 00000000000..3243378195c --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DiscriminatedWrapperSerializerTests.cs @@ -0,0 +1,127 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization.Conventions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class DiscriminatedWrapperSerializerTests + { + private static readonly IDiscriminatorConvention __discriminatorConvention1 = new ScalarDiscriminatorConvention("_t"); + private static readonly IDiscriminatorConvention __discriminatorConvention2 = new ScalarDiscriminatorConvention("_u"); + private static readonly IBsonSerializer __wrappedSerializer1 = new CSerializer1(); + private static readonly IBsonSerializer __wrappedSerializer2 = new CSerializer2(); + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + var y = new DerivedFromDiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + var y = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("discriminatorConvention")] + [InlineData("wrappedSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + var y = notEqualFieldName switch + { + "discriminatorConvention" => new DiscriminatedWrapperSerializer(__discriminatorConvention2, __wrappedSerializer1), + "wrappedSerializer" => new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DiscriminatedWrapperSerializer(__discriminatorConvention1, __wrappedSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDiscriminatedWrapperSerializer : DiscriminatedWrapperSerializer + { + public DerivedFromDiscriminatedWrapperSerializer( + IDiscriminatorConvention discriminatorConvention, + IBsonSerializer wrappedSerializer) + : base(discriminatorConvention, wrappedSerializer) + { + } + } + + public class C { } + + public class CSerializer1 : SerializerBase { } + + public class CSerializer2 : SerializerBase { } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DoubleSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DoubleSerializerTests.cs new file mode 100644 index 00000000000..d14db413ae3 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DoubleSerializerTests.cs @@ -0,0 +1,102 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class DoubleSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DoubleSerializer(); + var y = new DerivedFromDoubleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DoubleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DoubleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DoubleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DoubleSerializer(); + var y = new DoubleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new DoubleSerializer(BsonType.Double); + var y = new DoubleSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DoubleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDoubleSerializer : DoubleSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DowncastingSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DowncastingSerializerTests.cs new file mode 100644 index 00000000000..b2c5f2ea018 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DowncastingSerializerTests.cs @@ -0,0 +1,108 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class DowncastingSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DowncastingSerializer(Int32Serializer.Instance); + var y = new DerivedFromDowncastingSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DowncastingSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DowncastingSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DowncastingSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DowncastingSerializer(Int32Serializer.Instance); + var y = new DowncastingSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new DowncastingSerializer(new Int32Serializer(BsonType.Int32)); + var y = new DowncastingSerializer(new Int32Serializer(BsonType.String)); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DowncastingSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDowncastingSerializer : DowncastingSerializer + where TDerived : TBase + { + public DerivedFromDowncastingSerializer(IBsonSerializer derivedSerializer) + : base(derivedSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/DynamicDocumentBaseSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DynamicDocumentBaseSerializerTests.cs new file mode 100644 index 00000000000..91c7e11d0aa --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/DynamicDocumentBaseSerializerTests.cs @@ -0,0 +1,104 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Dynamic; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class DynamicDocumentBaseSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + var y = new DerivedFromDynamicDocumentBaseSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + var y = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (DynamicDocumentBaseSerializer)new ConcreteDynamicDocumentBaseSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteDynamicDocumentBaseSerializer : DynamicDocumentBaseSerializer + where T : class, IDynamicMetaObjectProvider + { + protected override void ConfigureDeserializationContext(BsonDeserializationContext.Builder builder) => throw new System.NotImplementedException(); + protected override void ConfigureSerializationContext(BsonSerializationContext.Builder builder) => throw new System.NotImplementedException(); + protected override T CreateDocument() => throw new System.NotImplementedException(); + protected override void SetValueForMember(T document, string memberName, object value) => throw new System.NotImplementedException(); + protected override bool TryGetValueForMember(T document, string memberName, out object value) => throw new System.NotImplementedException(); + } + + public class DerivedFromDynamicDocumentBaseSerializer : ConcreteDynamicDocumentBaseSerializer + where T : class, IDynamicMetaObjectProvider + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ElementAppendingSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ElementAppendingSerializerTests.cs index d99e1422699..792f496f6ad 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ElementAppendingSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ElementAppendingSerializerTests.cs @@ -31,6 +31,17 @@ namespace MongoDB.Bson.Tests.Serialization.Serializers { public class ElementAppendingSerializerTests { + public class C { public int X { get; set; } } + public class CSerializer1 : SerializerBase { } + public class CSerializer2 : SerializerBase { } + + private static readonly IBsonSerializer __documentSerializer1 = new CSerializer1(); + private static readonly IBsonSerializer __documentSerializer2 = new CSerializer2(); + private static readonly List __elements1 = new List { new BsonElement("x", 1) }; + private static readonly List __elements2 = new List { new BsonElement("x", 2) }; + private static readonly Action __writerSettingsConfigurator1 = s => { }; + private static readonly Action __writerSettingsConfigurator2 = s => { }; + [Fact] public void constructor_should_initialize_instance() { @@ -283,6 +294,100 @@ public void Serialize_should_preserve_IsDynamicType_when_creating_new_context(bo capturedIsDynamicType.Should().Be(isDynamicType); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + var y = new DerivedFromElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + var y = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("documentSerializer")] + [InlineData("elements")] + [InlineData("writerSettingsConfigurator")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + var y = notEqualFieldName switch + { + "documentSerializer" => new ElementAppendingSerializer(__documentSerializer2, __elements1, __writerSettingsConfigurator1), + "elements" => new ElementAppendingSerializer(__documentSerializer1, __elements2, __writerSettingsConfigurator1), + "writerSettingsConfigurator" => new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ElementAppendingSerializer(__documentSerializer1, __elements1, __writerSettingsConfigurator1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromElementAppendingSerializer : ElementAppendingSerializer + { + public DerivedFromElementAppendingSerializer( + IBsonSerializer documentSerializer, + IEnumerable elements, + Action writerSettingsConfigurator) + : base(documentSerializer, elements, writerSettingsConfigurator) + { + } + } + // private methods private ElementAppendingSerializer CreateSubject( IEnumerable elements = null, diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumSerializerTests.cs index e18902601bf..eb36a2574f7 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumSerializerTests.cs @@ -504,6 +504,85 @@ public void WithRepresentation_should_return_expected_result( } } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new EnumSerializer(); + var y = new DerivedFromEnumSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new EnumSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new EnumSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new EnumSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new EnumSerializer(); + var y = new EnumSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new EnumSerializer(BsonType.Int32); + var y = new EnumSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new EnumSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromEnumSerializer : EnumSerializer + where TEnum : struct, Enum + { + } + // private methods private TEnum Deserialize(IBsonSerializer serializer, byte[] bson) { diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerBaseTests.cs new file mode 100644 index 00000000000..04636cec630 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerBaseTests.cs @@ -0,0 +1,237 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections; +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class EnumerableInterfaceImplementerSerializerBaseTests + { + private static readonly IBsonSerializer __itemSerializer1; + private static readonly IBsonSerializer __itemSerializer2; + + static EnumerableInterfaceImplementerSerializerBaseTests() + { + __itemSerializer1 = new Int32Serializer(BsonType.Int32); + __itemSerializer2 = new Int32Serializer(BsonType.String); + } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + var y = new DerivedFromConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + var y = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + var y = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (EnumerableInterfaceImplementerSerializerBase>)new ConcreteEnumerableInterfaceImplementerSerializerBase>(__itemSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteEnumerableInterfaceImplementerSerializerBase : EnumerableInterfaceImplementerSerializerBase + where TValue : class, IList, new() + { + public ConcreteEnumerableInterfaceImplementerSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + + protected override object CreateAccumulator() => throw new NotImplementedException(); + } + + public class DerivedFromConcreteEnumerableInterfaceImplementerSerializerBase : ConcreteEnumerableInterfaceImplementerSerializerBase + where TValue : class, IList, new() + { + public DerivedFromConcreteEnumerableInterfaceImplementerSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + } + } + + public class EnumerableInterfaceImplementerSerializerBaseGenericTests + { + private static readonly IBsonSerializer __itemSerializer1; + private static readonly IBsonSerializer __itemSerializer2; + + static EnumerableInterfaceImplementerSerializerBaseGenericTests() + { + __itemSerializer1 = new Int32Serializer(BsonType.Int32); + __itemSerializer2 = new Int32Serializer(BsonType.String); + } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + var y = new DerivedFromConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + var y = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + var y = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (EnumerableInterfaceImplementerSerializerBase, int>)new ConcreteEnumerableInterfaceImplementerSerializerBase, int>(__itemSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteEnumerableInterfaceImplementerSerializerBase : EnumerableInterfaceImplementerSerializerBase + where TValue : class, IEnumerable + { + public ConcreteEnumerableInterfaceImplementerSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + + protected override object CreateAccumulator() => throw new NotImplementedException(); + } + + public class DerivedFromConcreteEnumerableInterfaceImplementerSerializerBase : ConcreteEnumerableInterfaceImplementerSerializerBase + where TValue : class, IEnumerable + { + public DerivedFromConcreteEnumerableInterfaceImplementerSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerTests.cs index 0b3b26e889f..447b5a2a982 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableInterfaceImplementerSerializerTests.cs @@ -13,6 +13,7 @@ * limitations under the License. */ +using System; using System.Collections; using System.Collections.Generic; using System.IO; @@ -26,6 +27,110 @@ namespace MongoDB.Bson.Tests.Serialization.Serializers { public class EnumerableInterfaceImplementerSerializerTests { + private static readonly IBsonSerializer __itemSerializer1; + private static readonly IBsonSerializer __itemSerializer2; + + static EnumerableInterfaceImplementerSerializerTests() + { + __itemSerializer1 = new Int32Serializer(BsonType.Int32); + __itemSerializer2 = new Int32Serializer(BsonType.String); + } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + var y = new DerivedFromEnumerableInterfaceImplementerSerializer>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + var y = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + var y = new EnumerableInterfaceImplementerSerializer>(__itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new EnumerableInterfaceImplementerSerializer>(__itemSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromEnumerableInterfaceImplementerSerializer : EnumerableInterfaceImplementerSerializer + where TValue : class, IList, new() + { + public DerivedFromEnumerableInterfaceImplementerSerializer(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + } + } + + public class EnumerableInterfaceImplementerSerializerGenericTests + { + private static readonly IBsonSerializer __itemSerializer1; + private static readonly IBsonSerializer __itemSerializer2; + + static EnumerableInterfaceImplementerSerializerGenericTests() + { + __itemSerializer1 = new Int32Serializer(BsonType.Int32); + __itemSerializer2 = new Int32Serializer(BsonType.String); + } + public class C : IEnumerable { public int Id; @@ -70,6 +175,89 @@ public void Serialize_should_return_expected_result() } } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + var y = new DerivedFromEnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + var y = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + var y = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new EnumerableInterfaceImplementerSerializer, int>(__itemSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromEnumerableInterfaceImplementerSerializer : EnumerableInterfaceImplementerSerializer + where TValue : class, IEnumerable + { + public DerivedFromEnumerableInterfaceImplementerSerializer(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + } + private IBsonSerializer CreateSubject() { // create subject without using the global serializer registry diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableSerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableSerializerBaseTests.cs new file mode 100644 index 00000000000..6c0f40e5371 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/EnumerableSerializerBaseTests.cs @@ -0,0 +1,231 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections; +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class EnumerableSerializerBaseTests + { + private static readonly IBsonSerializer __itemSerializer1 = new Int32Serializer(BsonType.Int32); + private static readonly IBsonSerializer __itemSerializer2 = new Int32Serializer(BsonType.String); + + [Fact] + public void Equals_derived_should_return_false() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + var y = new DerivedFromConcreteEnumerableSerializerBase>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + var y = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + var y = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (EnumerableSerializerBase>)new ConcreteEnumerableSerializerBase>(__itemSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteEnumerableSerializerBase : EnumerableSerializerBase + where TValue : class, IList, new() + { + public ConcreteEnumerableSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + + protected override void AddItem(object accumulator, object item) => throw new NotImplementedException(); + protected override object CreateAccumulator() => throw new NotImplementedException(); + protected override IEnumerable EnumerateItemsInSerializationOrder(TValue value) => throw new NotImplementedException(); + protected override TValue FinalizeResult(object accumulator) => throw new NotImplementedException(); + } + + public class DerivedFromConcreteEnumerableSerializerBase : ConcreteEnumerableSerializerBase + where TValue : class, IList, new() + { + public DerivedFromConcreteEnumerableSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + } + } + + public class EnumerableSerializerBaseGenericTests + { + private static readonly IBsonSerializer __itemSerializer1 = new Int32Serializer(BsonType.Int32); + private static readonly IBsonSerializer __itemSerializer2 = new Int32Serializer(BsonType.String); + + [Fact] + public void Equals_derived_should_return_false() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + var y = new DerivedFromConcreteEnumerableSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + var y = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + var y = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (EnumerableSerializerBase, int>)new ConcreteEnumerableSerializerBase, int>(__itemSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteEnumerableSerializerBase : EnumerableSerializerBase + where TValue : class, IEnumerable + { + public ConcreteEnumerableSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + + protected override void AddItem(object accumulator, TItem item) => throw new NotImplementedException(); + protected override object CreateAccumulator() => throw new NotImplementedException(); + protected override IEnumerable EnumerateItemsInSerializationOrder(TValue value) => throw new NotImplementedException(); + protected override TValue FinalizeResult(object accumulator) => throw new NotImplementedException(); + } + + public class DerivedFromConcreteEnumerableSerializerBase : ConcreteEnumerableSerializerBase + where TValue : class, IEnumerable + { + public DerivedFromConcreteEnumerableSerializerBase(IBsonSerializer itemSerializer) + : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ExpandoObjectSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ExpandoObjectSerializerTests.cs index 4d489ed4c08..65c03d593d9 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ExpandoObjectSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ExpandoObjectSerializerTests.cs @@ -16,7 +16,9 @@ using System.Collections.Generic; using System.Dynamic; using System.Linq; +using FluentAssertions; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using MongoDB.Bson.TestHelpers; using Xunit; @@ -95,4 +97,74 @@ public void TestDeserializingDiscriminatedVersion() } #endif } + + public class ExpandoObjectSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ExpandoObjectSerializer(); + var y = new DerivedFromExpandoObjectSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ExpandoObjectSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ExpandoObjectSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ExpandoObjectSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ExpandoObjectSerializer(); + var y = new ExpandoObjectSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ExpandoObjectSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromExpandoObjectSerializer : ExpandoObjectSerializer + { + } + } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/GuidSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/GuidSerializerTests.cs index c90a0ab3ddb..e62dd886fbc 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/GuidSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/GuidSerializerTests.cs @@ -601,5 +601,91 @@ public void WithRepresentation_should_return_expected_result() result.Representation.Should().Be(BsonType.String); result.GuidRepresentation.Should().Be(GuidRepresentation.Unspecified); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new GuidSerializer(); + var y = new DerivedFromGuidSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new GuidSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new GuidSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new GuidSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new GuidSerializer(); + var y = new GuidSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("guidRepresentation")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new GuidSerializer(); + var y = notEqualFieldName switch + { + "guidRepresentation" => new GuidSerializer(GuidRepresentation.JavaLegacy), + "representation" => new GuidSerializer(BsonType.String), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new GuidSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromGuidSerializer : GuidSerializer + { + public DerivedFromGuidSerializer() : base() { } + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializerTests.cs new file mode 100644 index 00000000000..711d6359344 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializerTests.cs @@ -0,0 +1,107 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class IEnumerableDeserializingAsCollectionSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + var y = new DerivedFromIEnumerableDeserializingAsCollectionSerializer, int, List>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + var y = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var int32Serializer1 = new Int32Serializer(BsonType.Int32); + var int32Serializer2 = new Int32Serializer(BsonType.String); + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(int32Serializer1); + var y = new IEnumerableDeserializingAsCollectionSerializer, int, List>(int32Serializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IEnumerableDeserializingAsCollectionSerializer, int, List>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromIEnumerableDeserializingAsCollectionSerializer : IEnumerableDeserializingAsCollectionSerializer + where TIEnumerable : class, IEnumerable // TIEnumerable must be an interface + where TCollection : class, ICollection, new() + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/IOrderedEnumerableSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/IOrderedEnumerableSerializerTests.cs new file mode 100644 index 00000000000..9306f087636 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/IOrderedEnumerableSerializerTests.cs @@ -0,0 +1,117 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class IOrderedEnumerableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + var y = new DerivedFromIOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + var y = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("itemSerializer")] + [InlineData("message")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new IOrderedEnumerableSerializer(itemSerializer1, "message1"); + var y = notEqualFieldName switch + { + "itemSerializer" => new IOrderedEnumerableSerializer(itemSerializer2, "message1"), + "message" => new IOrderedEnumerableSerializer(itemSerializer1, "message2"), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IOrderedEnumerableSerializer(Int32Serializer.Instance, "message"); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromIOrderedEnumerableSerializer : IOrderedEnumerableSerializer + { + public DerivedFromIOrderedEnumerableSerializer(IBsonSerializer itemSerializer, string thenByExceptionMessage) + : base(itemSerializer, thenByExceptionMessage) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ImpliedImplementationInterfaceSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ImpliedImplementationInterfaceSerializerTests.cs new file mode 100644 index 00000000000..8f68efdb1df --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ImpliedImplementationInterfaceSerializerTests.cs @@ -0,0 +1,108 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class ImpliedImplementationInterfaceSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ImpliedImplementationInterfaceSerializer, List>(); + var y = new DerivedFromImpliedImplementationInterfaceSerializer, List>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ImpliedImplementationInterfaceSerializer, List>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ImpliedImplementationInterfaceSerializer, List>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ImpliedImplementationInterfaceSerializer, List>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ImpliedImplementationInterfaceSerializer, List>(); + var y = new ImpliedImplementationInterfaceSerializer, List>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var int32Serializer1 = new Int32Serializer(BsonType.Int32); + var int32Serializer2 = new Int32Serializer(BsonType.String); + var implementationSerializer1 = new EnumerableInterfaceImplementerSerializer, int>(int32Serializer1); + var implementationSerializer2 = new EnumerableInterfaceImplementerSerializer, int>(int32Serializer2); + var x = new ImpliedImplementationInterfaceSerializer, List>(implementationSerializer1); + var y = new ImpliedImplementationInterfaceSerializer, List>(implementationSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ImpliedImplementationInterfaceSerializer, List>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromImpliedImplementationInterfaceSerializer : ImpliedImplementationInterfaceSerializer + where TImplementation : class, TInterface + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/KeyValuePairSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/KeyValuePairSerializerTests.cs index 89526506a1b..36d3bf4641a 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/KeyValuePairSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/KeyValuePairSerializerTests.cs @@ -13,10 +13,12 @@ * limitations under the License. */ +using System; using System.Collections.Generic; using System.Linq; -using MongoDB.Bson; +using FluentAssertions; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.DefaultSerializer.Serializers @@ -48,5 +50,98 @@ public void TestNullValue() var rehydrated = BsonSerializer.Deserialize>(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new KeyValuePairSerializer(); + var y = new DerivedFromKeyValuePairSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new KeyValuePairSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new KeyValuePairSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new KeyValuePairSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new KeyValuePairSerializer(); + var y = new KeyValuePairSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("representation")] + [InlineData("keySerializer")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.Array; + var representation2 = BsonType.Document; + var keySerializer1 = new Int32Serializer(BsonType.Int32); + var keySerializer2 = new Int32Serializer(BsonType.String); + var valueSerializer1 = new Int32Serializer(BsonType.Int32); + var valueSerializer2 = new Int32Serializer(BsonType.String); + var x = new KeyValuePairSerializer(representation1, keySerializer1, valueSerializer1); + var y = notEqualFieldName switch + { + "representation" => new KeyValuePairSerializer(representation2, keySerializer1, valueSerializer1), + "keySerializer" => new KeyValuePairSerializer(representation1, keySerializer2, valueSerializer1), + "valueSerializer" => new KeyValuePairSerializer(representation1, keySerializer1, valueSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new KeyValuePairSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromKeyValuePairSerializer : KeyValuePairSerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonArraySerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonArraySerializerTests.cs index 66064f0b1a3..a2c8eb052bb 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonArraySerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonArraySerializerTests.cs @@ -15,8 +15,10 @@ using System; using System.Linq; +using FluentAssertions; using MongoDB.Bson; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -48,5 +50,72 @@ public void TestRoundTrip() Assert.True(bson.SequenceEqual(c.ToBson())); } } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new LazyBsonArraySerializer(); + var y = new DerivedFromLazyBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new LazyBsonArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new LazyBsonArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new LazyBsonArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new LazyBsonArraySerializer(); + var y = new LazyBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new LazyBsonArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromLazyBsonArraySerializer : LazyBsonArraySerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonDocumentSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonDocumentSerializerTests.cs index 9fabbef4173..b305d97d988 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonDocumentSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/LazyBsonDocumentSerializerTests.cs @@ -15,8 +15,9 @@ using System; using System.Linq; -using MongoDB.Bson; +using FluentAssertions; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -48,5 +49,72 @@ public void TestRoundTrip() Assert.True(bson.SequenceEqual(c.ToBson())); } } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new LazyBsonDocumentSerializer(); + var y = new DerivedFromLazyBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new LazyBsonDocumentSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new LazyBsonDocumentSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new LazyBsonDocumentSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new LazyBsonDocumentSerializer(); + var y = new LazyBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new LazyBsonDocumentSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromLazyBsonDocumentSerializer : LazyBsonDocumentSerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/MemorySerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/MemorySerializerTests.cs index 48f28c6fcc7..1c4499239a6 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/MemorySerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/MemorySerializerTests.cs @@ -331,4 +331,228 @@ private static T[] GetArray(Func converter) => _ => (new ArrayHolder() { Items = array }).ToBson(), }; } + + public class ReadOnlyMemorySerializerEqualsTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ReadonlyMemorySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ReadonlyMemorySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ReadonlyMemorySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ReadonlyMemorySerializer(); + var y = new ReadonlyMemorySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ReadonlyMemorySerializer(BsonType.Binary); + var y = new ReadonlyMemorySerializer(BsonType.Array); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ReadonlyMemorySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class MemorySerializerEqualsTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new MemorySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new MemorySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new MemorySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new MemorySerializer(); + var y = new MemorySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new MemorySerializer(BsonType.Binary); + var y = new MemorySerializer(BsonType.Array); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new MemorySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class MemorySerializerBaseEqualsTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ConcreteMemorySerializerBase>(); + var y = new DerivedFromConcreteMemorySerializerBase>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ConcreteMemorySerializerBase>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ConcreteMemorySerializerBase>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ConcreteMemorySerializerBase>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ConcreteMemorySerializerBase>(); + var y = new ConcreteMemorySerializerBase>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ConcreteMemorySerializerBase>(BsonType.Binary); + var y = new ConcreteMemorySerializerBase>(BsonType.Array); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ConcreteMemorySerializerBase>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteMemorySerializerBase : MemorySerializerBase + where TMemory : struct + { + public ConcreteMemorySerializerBase() : base() { } + public ConcreteMemorySerializerBase(BsonType representation) : base(representation) { } + public override MemorySerializerBase WithRepresentation(BsonType representation) => throw new NotImplementedException(); + protected override TMemory CreateMemory(TItem[] items) => throw new NotImplementedException(); + protected override Memory GetMemory(TMemory memory) => throw new NotImplementedException(); + } + + public class DerivedFromConcreteMemorySerializerBase : ConcreteMemorySerializerBase + where TMemory : struct + { + } + } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/NetPrimitiveSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/NetPrimitiveSerializerTests.cs index bb449fe9d0f..50768dc6306 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/NetPrimitiveSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/NetPrimitiveSerializerTests.cs @@ -18,10 +18,13 @@ using System.Globalization; using System.Linq; using System.Net; +using FluentAssertions; using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson.Serialization.Options; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -181,6 +184,88 @@ public void TestLength9() Assert.False(rehydrated.B[7]); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new BitArraySerializer(BsonType.Binary); + var y = new DerivedFromBitArraySerializer(BsonType.Binary); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new BitArraySerializer(BsonType.Binary); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new BitArraySerializer(BsonType.Binary); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new BitArraySerializer(BsonType.Binary); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new BitArraySerializer(BsonType.Binary); + var y = new BitArraySerializer(BsonType.Binary); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new BitArraySerializer(BsonType.Binary); + var y = new BitArraySerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new BitArraySerializer(BsonType.Binary); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromBitArraySerializer : BitArraySerializer + { + public DerivedFromBitArraySerializer(BsonType representation) + : base(representation) + { + } + } } public class ByteArraySerializerTests @@ -271,6 +356,88 @@ public void TestLengthNine() Assert.True(c.B.SequenceEqual(rehydrated.B)); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ByteArraySerializer(BsonType.Binary); + var y = new DerivedFromByteArraySerializer(BsonType.Binary); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ByteArraySerializer(BsonType.Binary); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ByteArraySerializer(BsonType.Binary); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ByteArraySerializer(BsonType.Binary); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ByteArraySerializer(BsonType.Binary); + var y = new ByteArraySerializer(BsonType.Binary); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ByteArraySerializer(BsonType.Binary); + var y = new ByteArraySerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ByteArraySerializer(BsonType.Binary); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromByteArraySerializer : ByteArraySerializer + { + public DerivedFromByteArraySerializer(BsonType representation) + : base(representation) + { + } + } } public class ByteSerializerTests @@ -362,6 +529,88 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ByteSerializer(BsonType.Int32); + var y = new DerivedFromByteSerializer(BsonType.Int32); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ByteSerializer(BsonType.Int32); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ByteSerializer(BsonType.Int32); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ByteSerializer(BsonType.Int32); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ByteSerializer(BsonType.Int32); + var y = new ByteSerializer(BsonType.Int32); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ByteSerializer(BsonType.Int32); + var y = new ByteSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ByteSerializer(BsonType.Int32); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromByteSerializer : ByteSerializer + { + public DerivedFromByteSerializer(BsonType representation) + : base(representation) + { + } + } } public class CharSerializerTests @@ -457,6 +706,88 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new CharSerializer(BsonType.Int32); + var y = new DerivedFromCharSerializer(BsonType.Int32); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new CharSerializer(BsonType.Int32); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new CharSerializer(BsonType.Int32); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new CharSerializer(BsonType.Int32); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new CharSerializer(BsonType.Int32); + var y = new CharSerializer(BsonType.Int32); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new CharSerializer(BsonType.Int32); + var y = new CharSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new CharSerializer(BsonType.Int32); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromCharSerializer : CharSerializer + { + public DerivedFromCharSerializer(BsonType representation) + : base(representation) + { + } + } } public class CultureInfoSerializerTests @@ -533,6 +864,76 @@ public void TestEnUsUseUserOverrideTrue() Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } #endif + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new CultureInfoSerializer(); + var y = new DerivedFromCultureInfoSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new CultureInfoSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new CultureInfoSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new CultureInfoSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new CultureInfoSerializer(); + var y = new CultureInfoSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new CultureInfoSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromCultureInfoSerializer : CultureInfoSerializer + { + public DerivedFromCultureInfoSerializer() + { + } + } } public class DateTimeOffsetSerializerTests @@ -576,6 +977,88 @@ public void TestSerializeDateTimeOffset() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + var y = new DerivedFromDateTimeOffsetSerializer(BsonType.Array); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + var y = new DateTimeOffsetSerializer(BsonType.Array); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + var y = new DateTimeOffsetSerializer(BsonType.DateTime); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DateTimeOffsetSerializer(BsonType.Array); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDateTimeOffsetSerializer : DateTimeOffsetSerializer + { + public DerivedFromDateTimeOffsetSerializer(BsonType representation) + : base(representation) + { + } + } } public class DecimalSerializerTests @@ -762,6 +1245,95 @@ public void TestOnePointFive() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new DecimalSerializer(); + var y = new DerivedFromDecimalSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new DecimalSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new DecimalSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new DecimalSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new DecimalSerializer(); + var y = new DecimalSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.String; + var representation2 = BsonType.Decimal128; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new DecimalSerializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new DecimalSerializer(representation2, converter1), + "converter" => new DecimalSerializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new DecimalSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromDecimalSerializer : DecimalSerializer + { + } } public class Int16SerializerTests @@ -801,83 +1373,356 @@ public void TestMin() } [Fact] - public void TestMinusOne() + public void TestMinusOne() + { + var obj = new TestClass + { + D128 = -1, + D = -1, + I = -1, + L = -1, + S = -1 + }; + var json = obj.ToJson(); + var expected = ("{ 'D128' : NumberDecimal('-1'), 'D' : -1.0, 'I' : -1, 'L' : NumberLong(-1), 'S' : '-1' }").Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void TestZero() + { + var obj = new TestClass + { + D128 = 0, + D = 0, + I = 0, + L = 0, + S = 0 + }; + var json = obj.ToJson(); + var expected = ("{ 'D128' : NumberDecimal('0'), 'D' : 0.0, 'I' : 0, 'L' : NumberLong(0), 'S' : '0' }").Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void TestOne() + { + var obj = new TestClass + { + D128 = 1, + D = 1, + I = 1, + L = 1, + S = 1 + }; + var json = obj.ToJson(); + var expected = ("{ 'D128' : NumberDecimal('1'), 'D' : 1.0, 'I' : 1, 'L' : NumberLong(1), 'S' : '1' }").Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void TestMax() + { + var obj = new TestClass + { + D128 = short.MaxValue, + D = short.MaxValue, + I = short.MaxValue, + L = short.MaxValue, + S = short.MaxValue + }; + var json = obj.ToJson(); + var expected = ("{ 'D128' : NumberDecimal('32767'), 'D' : 32767.0, 'I' : 32767, 'L' : NumberLong(32767), 'S' : '32767' }").Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new Int16Serializer(); + var y = new DerivedFromInt16Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new Int16Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new Int16Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new Int16Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new Int16Serializer(); + var y = new Int16Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.Int32; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new Int16Serializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new Int16Serializer(representation2, converter1), + "converter" => new Int16Serializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new Int16Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromInt16Serializer : Int16Serializer + { + } + } + + public class Int32SerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new Int32Serializer(); + var y = new DerivedFromInt32Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new Int32Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new Int32Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new Int32Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new Int32Serializer(); + var y = new Int32Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.Int32; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new Int32Serializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new Int32Serializer(representation2, converter1), + "converter" => new Int32Serializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new Int32Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromInt32Serializer : Int32Serializer + { + } + } + + public class Int64SerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new Int64Serializer(); + var y = new DerivedFromInt64Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new Int64Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new Int64Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() { - var obj = new TestClass - { - D128 = -1, - D = -1, - I = -1, - L = -1, - S = -1 - }; - var json = obj.ToJson(); - var expected = ("{ 'D128' : NumberDecimal('-1'), 'D' : -1.0, 'I' : -1, 'L' : NumberLong(-1), 'S' : '-1' }").Replace("'", "\""); - Assert.Equal(expected, json); + var x = new Int64Serializer(); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.Equals(x); + + result.Should().Be(true); } [Fact] - public void TestZero() + public void Equals_with_equal_fields_should_return_true() { - var obj = new TestClass - { - D128 = 0, - D = 0, - I = 0, - L = 0, - S = 0 - }; - var json = obj.ToJson(); - var expected = ("{ 'D128' : NumberDecimal('0'), 'D' : 0.0, 'I' : 0, 'L' : NumberLong(0), 'S' : '0' }").Replace("'", "\""); - Assert.Equal(expected, json); + var x = new Int64Serializer(); + var y = new Int64Serializer(); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.Equals(y); + + result.Should().Be(true); } - [Fact] - public void TestOne() + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) { - var obj = new TestClass + var representation1 = BsonType.Int32; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new Int64Serializer(representation1, converter1); + var y = notEqualFieldName switch { - D128 = 1, - D = 1, - I = 1, - L = 1, - S = 1 + "representation" => new Int64Serializer(representation2, converter1), + "converter" => new Int64Serializer(representation1, converter2), + _ => throw new Exception() }; - var json = obj.ToJson(); - var expected = ("{ 'D128' : NumberDecimal('1'), 'D' : 1.0, 'I' : 1, 'L' : NumberLong(1), 'S' : '1' }").Replace("'", "\""); - Assert.Equal(expected, json); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.Equals(y); + + result.Should().Be(false); } [Fact] - public void TestMax() + public void GetHashCode_should_return_zero() { - var obj = new TestClass - { - D128 = short.MaxValue, - D = short.MaxValue, - I = short.MaxValue, - L = short.MaxValue, - S = short.MaxValue - }; - var json = obj.ToJson(); - var expected = ("{ 'D128' : NumberDecimal('32767'), 'D' : 32767.0, 'I' : 32767, 'L' : NumberLong(32767), 'S' : '32767' }").Replace("'", "\""); - Assert.Equal(expected, json); + var x = new Int64Serializer(); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromInt64Serializer : Int64Serializer + { } } @@ -935,6 +1780,73 @@ public void TestIPv6() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IPAddressSerializer(); + var y = new DerivedFromIPAddressSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IPAddressSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IPAddressSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IPAddressSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IPAddressSerializer(); + var y = new IPAddressSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IPAddressSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromIPAddressSerializer : IPAddressSerializer + { + } } public class IPEndPointSerializerTests @@ -991,6 +1903,73 @@ public void TestIPv6() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IPEndPointSerializer(); + var y = new DerivedFromIPEndPointSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IPEndPointSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IPEndPointSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IPEndPointSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IPEndPointSerializer(); + var y = new IPEndPointSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IPEndPointSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromIPEndPointSerializer : IPEndPointSerializer + { + } } public class SByteSerializerTests @@ -1101,6 +2080,84 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new SByteSerializer(); + var y = new DerivedFromSByteSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new SByteSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new SByteSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new SByteSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new SByteSerializer(); + var y = new SByteSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new SByteSerializer(BsonType.Int32); + var y = new SByteSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new SByteSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromSByteSerializer : SByteSerializer + { + } } public class SingleSerializerTests @@ -1265,42 +2322,131 @@ public void TestNaN() Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } - [Fact] - public void TestNegativeInfinity() + [Fact] + public void TestNegativeInfinity() + { + var obj = new TestClass + { + D = float.NegativeInfinity, + I = 0, + L = 0, + S = float.NegativeInfinity + }; + var json = obj.ToJson(); + var expected = "{ 'D' : -Infinity, 'I' : 0, 'L' : NumberLong(0), 'S' : '-Infinity' }".Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void TestPositiveInfinity() + { + var obj = new TestClass + { + D = float.PositiveInfinity, + I = 0, + L = 0, + S = float.PositiveInfinity + }; + var json = obj.ToJson(); + var expected = "{ 'D' : Infinity, 'I' : 0, 'L' : NumberLong(0), 'S' : 'Infinity' }".Replace("'", "\""); + Assert.Equal(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new SingleSerializer(); + var y = new DerivedFromSingleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new SingleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new SingleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new SingleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new SingleSerializer(); + var y = new SingleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) { - var obj = new TestClass + var representation1 = BsonType.Double; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new SingleSerializer(representation1, converter1); + var y = notEqualFieldName switch { - D = float.NegativeInfinity, - I = 0, - L = 0, - S = float.NegativeInfinity + "representation" => new SingleSerializer(representation2, converter1), + "converter" => new SingleSerializer(representation1, converter2), + _ => throw new Exception() }; - var json = obj.ToJson(); - var expected = "{ 'D' : -Infinity, 'I' : 0, 'L' : NumberLong(0), 'S' : '-Infinity' }".Replace("'", "\""); - Assert.Equal(expected, json); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.Equals(y); + + result.Should().Be(false); } [Fact] - public void TestPositiveInfinity() + public void GetHashCode_should_return_zero() { - var obj = new TestClass - { - D = float.PositiveInfinity, - I = 0, - L = 0, - S = float.PositiveInfinity - }; - var json = obj.ToJson(); - var expected = "{ 'D' : Infinity, 'I' : 0, 'L' : NumberLong(0), 'S' : 'Infinity' }".Replace("'", "\""); - Assert.Equal(expected, json); + var x = new SingleSerializer(BsonType.Double); - var bson = obj.ToBson(); - var rehydrated = BsonSerializer.Deserialize(bson); - Assert.True(bson.SequenceEqual(rehydrated.ToBson())); + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromSingleSerializer : SingleSerializer + { } } @@ -1442,6 +2588,95 @@ public void TestMaxValue() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TimeSpanSerializer(); + var y = new DerivedFromTimeSpanSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TimeSpanSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TimeSpanSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TimeSpanSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TimeSpanSerializer(); + var y = new TimeSpanSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("representation")] + [InlineData("units")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.String; + var representation2 = BsonType.Int64; + var units1 = TimeSpanUnits.Ticks; + var units2 = TimeSpanUnits.Microseconds; + var x = new TimeSpanSerializer(representation1, units1); + var y = notEqualFieldName switch + { + "representation" => new TimeSpanSerializer(representation2, units1), + "units" => new TimeSpanSerializer(representation1, units2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TimeSpanSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTimeSpanSerializer : TimeSpanSerializer + { + } } public class UInt16SerializerTests @@ -1539,6 +2774,95 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new UInt16Serializer(); + var y = new DerivedFromUInt16Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new UInt16Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new UInt16Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new UInt16Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new UInt16Serializer(); + var y = new UInt16Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.Int32; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new UInt16Serializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new UInt16Serializer(representation2, converter1), + "converter" => new UInt16Serializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new UInt16Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromUInt16Serializer : UInt16Serializer + { + } } public class UInt32SerializerTests @@ -1636,6 +2960,95 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new UInt32Serializer(); + var y = new DerivedFromUInt32Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new UInt32Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new UInt32Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new UInt32Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new UInt32Serializer(); + var y = new UInt32Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.Int32; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new UInt32Serializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new UInt32Serializer(representation2, converter1), + "converter" => new UInt32Serializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new UInt32Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromUInt32Serializer : UInt32Serializer + { + } } public class UInt64SerializerTests @@ -1733,6 +3146,95 @@ public void TestMax() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new UInt64Serializer(); + var y = new DerivedFromUInt64Serializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new UInt64Serializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new UInt64Serializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new UInt64Serializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new UInt64Serializer(); + var y = new UInt64Serializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("converter")] + [InlineData("representation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var representation1 = BsonType.Int32; + var representation2 = BsonType.String; + var converter1 = new RepresentationConverter(false, false); + var converter2 = new RepresentationConverter(true, true); + var x = new UInt64Serializer(representation1, converter1); + var y = notEqualFieldName switch + { + "representation" => new UInt64Serializer(representation2, converter1), + "converter" => new UInt64Serializer(representation1, converter2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new UInt64Serializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromUInt64Serializer : UInt64Serializer + { + } } public class UriSerializerTests @@ -1805,6 +3307,73 @@ public void TestMongoDB() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new UriSerializer(); + var y = new DerivedFromUriSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new UriSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new UriSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new UriSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new UriSerializer(); + var y = new UriSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new UriSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromUriSerializer : UriSerializer + { + } } public class VersionSerializerTests diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/NullableSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/NullableSerializerTests.cs new file mode 100644 index 00000000000..23e0e38561d --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/NullableSerializerTests.cs @@ -0,0 +1,105 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class NullableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new NullableSerializer(); + var y = new DerivedFromNullableSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new NullableSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new NullableSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new NullableSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new NullableSerializer(); + var y = new NullableSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var serializer1 = new Int32Serializer(BsonType.Int32); + var serializer2 = new Int32Serializer(BsonType.String); + var x = new NullableSerializer(serializer1); + var y = new NullableSerializer(serializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new NullableSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromNullableSerializer : NullableSerializer + where T : struct + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectIdSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectIdSerializerTests.cs new file mode 100644 index 00000000000..9ba3fbdfdaa --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectIdSerializerTests.cs @@ -0,0 +1,102 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class ObjectIdSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ObjectIdSerializer(); + var y = new DerivedFromObjectIdSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ObjectIdSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ObjectIdSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ObjectIdSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ObjectIdSerializer(); + var y = new ObjectIdSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ObjectIdSerializer(BsonType.ObjectId); + var y = new ObjectIdSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ObjectIdSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromObjectIdSerializer : ObjectIdSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectSerializerTests.cs index 802af6e0e5d..48349c19117 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ObjectSerializerTests.cs @@ -569,53 +569,100 @@ public void Deserialize_binary_data_should_return_expected_result_when_guid_repr } [Fact] - public void Equals_should_return_true_when_instances_are_equal() + public void Equals_derived_should_return_false() { - var discriminatorConvention = new ScalarDiscriminatorConvention("_t"); - var subject1 = new ObjectSerializer(discriminatorConvention, GuidRepresentation.Standard, ObjectSerializer.DefaultAllowedTypes); - var subject2 = new ObjectSerializer(discriminatorConvention, GuidRepresentation.Standard, ObjectSerializer.DefaultAllowedTypes); + var x = new ObjectSerializer(); + var y = new DerivedFromObjectSerializer(); - var result = subject1.Equals(subject2); - var hashCode1 = subject1.GetHashCode(); - var hashCode2 = subject2.GetHashCode(); + var result = x.Equals(y); - result.Should().BeTrue(); - hashCode2.Should().Be(hashCode1); // required by the contract of Equals + result.Should().Be(false); } - [Theory] - [ParameterAttributeData] - public void Equals_should_return_false_when_instances_are_not_equal( - [Values("allowedTypes", "discriminatorConvention", "guidRepresentation")] - string notEqualFieldName) + [Fact] + public void Equals_null_should_return_false() { - IDiscriminatorConvention discriminatorConvention = new ScalarDiscriminatorConvention("_t"); - var guidRepresentation = GuidRepresentation.Standard; - var allowedTypes = ObjectSerializer.DefaultAllowedTypes; - var subject1 = new ObjectSerializer(discriminatorConvention, guidRepresentation, allowedTypes); + var x = new ObjectSerializer(); - switch (notEqualFieldName) - { - case "allowedTypes": allowedTypes = ObjectSerializer.NoAllowedTypes; break; - case "discriminatorConvention": discriminatorConvention = new HierarchicalDiscriminatorConvention("_t"); break; - case "guidRepresentation": guidRepresentation = GuidRepresentation.CSharpLegacy; break; - default: throw new ArgumentException($"Invalid notEqualFieldName: {notEqualFieldName}.", nameof(notEqualFieldName)); - } - var subject2 = new ObjectSerializer(discriminatorConvention, guidRepresentation, allowedTypes); + var result = x.Equals(null); - var result = subject1.Equals(subject2); - var hashCode1 = subject1.GetHashCode(); - var hashCode2 = subject2.GetHashCode(); + result.Should().Be(false); + } - result.Should().BeFalse(); - if (notEqualFieldName == "allowedTypes") - { - hashCode2.Should().Be(hashCode1); // because allowedTypes is not part of the hash code computation - } - else + [Fact] + public void Equals_object_should_return_false() + { + var x = new ObjectSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ObjectSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ObjectSerializer(); + var y = new ObjectSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("allowedDeserializationTypes")] + [InlineData("allowedSerializationTypes")] + [InlineData("discriminatorConvention")] + [InlineData("guidRepresentation")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var discriminatorConvention1 = new ScalarDiscriminatorConvention("_t"); + var discriminatorConvention2 = new ScalarDiscriminatorConvention("_u"); + var guidRepresentation1 = GuidRepresentation.Standard; + var guidRepresentation2 = GuidRepresentation.CSharpLegacy; + Func allowedDeserializationTypes1 = t => false; + Func allowedDeserializationTypes2 = t => true; + Func allowedSerializationTypes1 = t => false; + Func allowedSerializationTypes2 = t => true; + var x = new ObjectSerializer(discriminatorConvention1, guidRepresentation1, allowedDeserializationTypes1, allowedSerializationTypes1); + var y = notEqualFieldName switch { - hashCode2.Should().NotBe(hashCode1); // not strictly required but desirable - } + "discriminatorConvention" => new ObjectSerializer(discriminatorConvention2, guidRepresentation1, allowedDeserializationTypes1, allowedSerializationTypes1), + "guidRepresentation" => new ObjectSerializer(discriminatorConvention1, guidRepresentation2, allowedDeserializationTypes1, allowedSerializationTypes1), + "allowedDeserializationTypes" => new ObjectSerializer(discriminatorConvention1, guidRepresentation1, allowedDeserializationTypes2, allowedSerializationTypes1), + "allowedSerializationTypes" => new ObjectSerializer(discriminatorConvention1, guidRepresentation1, allowedDeserializationTypes1, allowedSerializationTypes2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ObjectSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromObjectSerializer : ObjectSerializer + { } [Theory] diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/PartiallyRawBsonDocumentSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/PartiallyRawBsonDocumentSerializerTests.cs index 259b2958093..cb96ecfb198 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/PartiallyRawBsonDocumentSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/PartiallyRawBsonDocumentSerializerTests.cs @@ -97,6 +97,94 @@ public void Deserialize_should_return_nested_partially_raw_BsonDocument() result["b"]["f"].Should().BeOfType(); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + var y = new DerivedFromPartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + var y = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("name")] + [InlineData("rawSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var rawSerializer1 = new RawBsonArraySerializer(); + var rawSerializer2 = new RawBsonDocumentSerializer(); + var x = new PartiallyRawBsonDocumentSerializer("name1", rawSerializer1); + var y = notEqualFieldName switch + { + "name" => new PartiallyRawBsonDocumentSerializer("name2", rawSerializer1), + "rawSerializer" => new PartiallyRawBsonDocumentSerializer("name1", rawSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new PartiallyRawBsonDocumentSerializer("name", BsonValueSerializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromPartiallyRawBsonDocumentSerializer : PartiallyRawBsonDocumentSerializer + { + public DerivedFromPartiallyRawBsonDocumentSerializer(string name, IBsonSerializer rawSerializer) : base(name, rawSerializer) { } + } + // private methods private BsonDocument Deserialize(byte[] bson, PartiallyRawBsonDocumentSerializer serializer) { diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ProjectingDeserializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ProjectingDeserializerTests.cs new file mode 100644 index 00000000000..3321c3441f9 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ProjectingDeserializerTests.cs @@ -0,0 +1,121 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class ProjectingDeserializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + Func projector = f => 1; + var x = new ProjectingDeserializer(Int32Serializer.Instance, projector); + var y = new DerivedFromProjectingDeserializer(Int32Serializer.Instance, projector); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ProjectingDeserializer(Int32Serializer.Instance, f => 1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ProjectingDeserializer(Int32Serializer.Instance, f => 1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ProjectingDeserializer(Int32Serializer.Instance, f => 1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + Func projector = f => 1; + var x = new ProjectingDeserializer(Int32Serializer.Instance, projector); + var y = new ProjectingDeserializer(Int32Serializer.Instance, projector); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("fromSerializer")] + [InlineData("projector")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var fromSerializer1 = new Int32Serializer(BsonType.Int32); + var fromSerializer2 = new Int32Serializer(BsonType.String); + Func projector1 = f => 1; + Func projector2 = f => 2; + var x = new ProjectingDeserializer(fromSerializer1, projector1); + var y = notEqualFieldName switch + { + "fromSerializer" => new ProjectingDeserializer(fromSerializer2, projector1), + "projector" => new ProjectingDeserializer(fromSerializer1, projector2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ProjectingDeserializer(Int32Serializer.Instance, f => 1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromProjectingDeserializer : ProjectingDeserializer + { + public DerivedFromProjectingDeserializer(IBsonSerializer fromSerializer, Func projector) + : base(fromSerializer, projector) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/QueueSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/QueueSerializerTests.cs new file mode 100644 index 00000000000..53ed3c4d941 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/QueueSerializerTests.cs @@ -0,0 +1,187 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class QueueSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new QueueSerializer(); + var y = new DerivedFromQueueSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new QueueSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new QueueSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new QueueSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new QueueSerializer(); + var y = new QueueSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new QueueSerializer(itemSerializer1); + var y = new QueueSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new QueueSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromQueueSerializer : QueueSerializer + { + } + } + + public class QueueSerializerGenericTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new QueueSerializer(); + var y = new DerivedFromQueueSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new QueueSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new QueueSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new QueueSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new QueueSerializer(); + var y = new QueueSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new QueueSerializer(itemSerializer1); + var y = new QueueSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new QueueSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromQueueSerializer : QueueSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonArraySerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonArraySerializerTests.cs index b4988f94d61..341b06d5a68 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonArraySerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonArraySerializerTests.cs @@ -15,8 +15,9 @@ using System; using System.Linq; -using MongoDB.Bson; +using FluentAssertions; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -59,5 +60,72 @@ public void TestRoundTrip() Assert.Equal(json, c.ToJson()); } } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new RawBsonArraySerializer(); + var y = new DerivedFromRawBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new RawBsonArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new RawBsonArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new RawBsonArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new RawBsonArraySerializer(); + var y = new RawBsonArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new RawBsonArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromRawBsonArraySerializer : RawBsonArraySerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonDocumentSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonDocumentSerializerTests.cs index acb1d070786..9c279c17854 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonDocumentSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/RawBsonDocumentSerializerTests.cs @@ -15,8 +15,10 @@ using System; using System.Linq; +using FluentAssertions; using MongoDB.Bson; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization @@ -59,5 +61,72 @@ public void TestRoundTrip() Assert.Equal(json, c.ToJson()); } } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new RawBsonDocumentSerializer(); + var y = new DerivedFromRawBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new RawBsonDocumentSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new RawBsonDocumentSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new RawBsonDocumentSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new RawBsonDocumentSerializer(); + var y = new RawBsonDocumentSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new RawBsonDocumentSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromRawBsonDocumentSerializer : RawBsonDocumentSerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyCollectionSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyCollectionSerializerTests.cs new file mode 100644 index 00000000000..3457af6a77b --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyCollectionSerializerTests.cs @@ -0,0 +1,104 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class ReadOnlyCollectionSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ReadOnlyCollectionSerializer(); + var y = new DerivedFromReadOnlyCollectionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ReadOnlyCollectionSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ReadOnlyCollectionSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ReadOnlyCollectionSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ReadOnlyCollectionSerializer(); + var y = new ReadOnlyCollectionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ReadOnlyCollectionSerializer(itemSerializer1); + var y = new ReadOnlyCollectionSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ReadOnlyCollectionSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromReadOnlyCollectionSerializer : ReadOnlyCollectionSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyCollectionSubclassSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyCollectionSubclassSerializerTests.cs new file mode 100644 index 00000000000..5cb4a0873ed --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyCollectionSubclassSerializerTests.cs @@ -0,0 +1,114 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Collections.Generic; +using System.Collections.ObjectModel; +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class ReadOnlyCollectionSubclassSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ReadOnlyCollectionSubclassSerializer, int>(); + var y = new DerivedFromReadOnlyCollectionSubclassSerializer, int>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ReadOnlyCollectionSubclassSerializer, int>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ReadOnlyCollectionSubclassSerializer, int>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ReadOnlyCollectionSubclassSerializer, int>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ReadOnlyCollectionSubclassSerializer, int>(); + var y = new ReadOnlyCollectionSubclassSerializer, int>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ReadOnlyCollectionSubclassSerializer, int>(itemSerializer1); + var y = new ReadOnlyCollectionSubclassSerializer, int>(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ReadOnlyCollectionSubclassSerializer, int>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromReadOnlyCollectionSubclassSerializer : ReadOnlyCollectionSubclassSerializer + where TValue : ReadOnlyCollection + { + } + + public class ReadOnlyCollectionSubclass : ReadOnlyCollection + { + public ReadOnlyCollectionSubclass(IList list) : base(list) + { + } + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializerTests.cs new file mode 100644 index 00000000000..6b517803c45 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ReadOnlyDictionaryInterfaceImplementerSerializerTests.cs @@ -0,0 +1,121 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization.Options; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class ReadOnlyDictionaryInterfaceImplementerSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + var y = new DerivedFromReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + var y = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("dictionaryRepresentation")] + [InlineData("keySerializer")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var dictionaryRepresentation1 = DictionaryRepresentation.Document; + var dictionaryRepresentation2 = DictionaryRepresentation.ArrayOfArrays; + var keySerializer1 = new Int32Serializer(BsonType.Int32); + var keySerializer2 = new Int32Serializer(BsonType.String); + var valueSerializer1 = new Int32Serializer(BsonType.Int32); + var valueSerializer2 = new Int32Serializer(BsonType.String); + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation1, keySerializer1, valueSerializer1); + var y = notEqualFieldName switch + { + "dictionaryRepresentation" => new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation2, keySerializer1, valueSerializer1), + "keySerializer" => new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation1, keySerializer2, valueSerializer1), + "valueSerializer" => new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(dictionaryRepresentation1, keySerializer1, valueSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ReadOnlyDictionaryInterfaceImplementerSerializer, int, int>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromReadOnlyDictionaryInterfaceImplementerSerializer : ReadOnlyDictionaryInterfaceImplementerSerializer + where TDictionary : class, IReadOnlyDictionary + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/RegexSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/RegexSerializerTests.cs new file mode 100644 index 00000000000..382897e6ace --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/RegexSerializerTests.cs @@ -0,0 +1,102 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class RegexSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new RegexSerializer(); + var y = new DerivedFromRegexSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new RegexSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new RegexSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new RegexSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new RegexSerializer(); + var y = new RegexSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new RegexSerializer(BsonType.RegularExpression); + var y = new RegexSerializer(BsonType.String); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new RegexSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromRegexSerializer : RegexSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/SealedClassSerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/SealedClassSerializerBaseTests.cs new file mode 100644 index 00000000000..41432692a54 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/SealedClassSerializerBaseTests.cs @@ -0,0 +1,99 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Bson.Tests.Serialization.Serializers +{ + public class SealedClassSerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + var y = new DerivedFromConcreteSealedClassSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + var y = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (SealedClassSerializerBase)new ConcreteSealedClassSerializerBase(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteSealedClassSerializerBase : SealedClassSerializerBase + where TValue : class + { + } + + public class DerivedFromConcreteSealedClassSerializerBase : ConcreteSealedClassSerializerBase + where TValue : class + { + } + + public sealed class SealedClass { } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/SerializeAsNominalTypeSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/SerializeAsNominalTypeSerializerTests.cs new file mode 100644 index 00000000000..0e82059e197 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/SerializeAsNominalTypeSerializerTests.cs @@ -0,0 +1,112 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class SerializeAsNominalTypeSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new SerializeAsNominalTypeSerializer(); + var y = new DerivedFromSerializeAsNominalTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new SerializeAsNominalTypeSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new SerializeAsNominalTypeSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new SerializeAsNominalTypeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new SerializeAsNominalTypeSerializer(); + var y = new SerializeAsNominalTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var nominalTypeSerializer1 = new CSerializer1(); + var nominalTypeSerializer2 = new CSerializer2(); + var x = new SerializeAsNominalTypeSerializer(nominalTypeSerializer1); + var y = new SerializeAsNominalTypeSerializer(nominalTypeSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new SerializeAsNominalTypeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromSerializeAsNominalTypeSerializer : SerializeAsNominalTypeSerializer + where TActualType : class, TNominalType + { + } + + public class C { } + + public class D : C { } + + public class CSerializer1 : SerializerBase { } + + public class CSerializer2 : SerializerBase { } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/SerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/SerializerBaseTests.cs new file mode 100644 index 00000000000..daff9c1133a --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/SerializerBaseTests.cs @@ -0,0 +1,94 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class SerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (SerializerBase)new ConcreteSerializerBase(); + var y = new DerivedFromConcreteSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (SerializerBase)new ConcreteSerializerBase(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (SerializerBase)new ConcreteSerializerBase(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (SerializerBase)new ConcreteSerializerBase(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (SerializerBase)new ConcreteSerializerBase(); + var y = (SerializerBase)new ConcreteSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (SerializerBase)new ConcreteSerializerBase(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteSerializerBase : SerializerBase + { + } + + public class DerivedFromConcreteSerializerBase : ConcreteSerializerBase + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/StackSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StackSerializerTests.cs new file mode 100644 index 00000000000..9b40339147b --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StackSerializerTests.cs @@ -0,0 +1,186 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class StackSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new StackSerializer(); + var y = new DerivedFromStackSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new StackSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new StackSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new StackSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new StackSerializer(); + var y = new StackSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new StackSerializer(itemSerializer1); + var y = new StackSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new StackSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromStackSerializer : StackSerializer + { + } + } + + public class StackSerializerGenericTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new StackSerializer(); + var y = new DerivedFromStackSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new StackSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new StackSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new StackSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new StackSerializer(); + var y = new StackSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new StackSerializer(itemSerializer1); + var y = new StackSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new StackSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromStackSerializer : StackSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/StringSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StringSerializerTests.cs new file mode 100644 index 00000000000..f4afebca83f --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StringSerializerTests.cs @@ -0,0 +1,101 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class StringSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new StringSerializer(); + var y = new DerivedFromStringSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new StringSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new StringSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new StringSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new StringSerializer(); + var y = new StringSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new StringSerializer(BsonType.String); + var y = new StringSerializer(BsonType.ObjectId); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new StringSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromStringSerializer : StringSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/StructSerailizerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StructSerailizerBaseTests.cs new file mode 100644 index 00000000000..d897b2dc672 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StructSerailizerBaseTests.cs @@ -0,0 +1,96 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class StructSerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + var y = new DerivedFromConcreteStructSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + var y = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteStructSerializerBase : StructSerializerBase + where TValue : struct + { + } + + public class DerivedFromConcreteStructSerializerBase : ConcreteStructSerializerBase + where TValue : struct + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/StructSerializerBaseTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StructSerializerBaseTests.cs new file mode 100644 index 00000000000..b94254fcc78 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/StructSerializerBaseTests.cs @@ -0,0 +1,96 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class StructSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + var y = new DerivedFromConcreteStructSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + var y = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (StructSerializerBase)new ConcreteStructSerializerBase(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class ConcreteStructSerializerBase : StructSerializerBase + where TValue : struct + { + } + + public class DerivedFromConcreteStructSerializerBase : ConcreteStructSerializerBase + where TValue : struct + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ThreeDimensionalArraySerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ThreeDimensionalArraySerializerTests.cs index 9a24eb30f30..9929f38b90c 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ThreeDimensionalArraySerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ThreeDimensionalArraySerializerTests.cs @@ -14,8 +14,10 @@ */ using System.Linq; +using FluentAssertions; using MongoDB.Bson; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization.ArraySerializer @@ -234,5 +236,85 @@ public void Test2x2x2() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ThreeDimensionalArraySerializer(); + var y = new DerivedFromThreeDimensionalArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ThreeDimensionalArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ThreeDimensionalArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ThreeDimensionalArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ThreeDimensionalArraySerializer(); + var y = new ThreeDimensionalArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ThreeDimensionalArraySerializer(itemSerializer1); + var y = new ThreeDimensionalArraySerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ThreeDimensionalArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromThreeDimensionalArraySerializer : ThreeDimensionalArraySerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/TupleSerializersEqualsTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/TupleSerializersEqualsTests.cs new file mode 100644 index 00000000000..f8c1b7b4c15 --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/TupleSerializersEqualsTests.cs @@ -0,0 +1,778 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class TupleWith1ItemSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1); + var y = new TupleSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith2ItemsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer(itemSerializer2, itemSerializer1), + "item2Serializer" => new TupleSerializer(itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith3ItemsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1), + "item2Serializer" => new TupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1), + "item3Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith4ItemsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new TupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item3Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item4Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith5ItemsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new TupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item3Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item4Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item5Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith6ItemsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + [InlineData("item6Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new TupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item3Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item4Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item5Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item6Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith7ItemsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer(); + var y = new DerivedFromTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer(); + var y = new TupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + [InlineData("item6Serializer")] + [InlineData("item7Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new TupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item3Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item4Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item5Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item6Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item7Serializer" => new TupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } + + public class TupleWith7ItemsAndRestSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TupleSerializer>(); + var y = new DerivedFromTupleSerializer>(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TupleSerializer>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TupleSerializer>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TupleSerializer>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TupleSerializer>(); + var y = new TupleSerializer>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + [InlineData("item6Serializer")] + [InlineData("item7Serializer")] + [InlineData("restSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var restSerializer1 = new TupleSerializer(itemSerializer1); + var restSerializer2 = new TupleSerializer(itemSerializer2); + var x = new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new TupleSerializer>(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item2Serializer" => new TupleSerializer>(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item3Serializer" => new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item4Serializer" => new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item5Serializer" => new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, restSerializer1), + "item6Serializer" => new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, restSerializer1), + "item7Serializer" => new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, restSerializer1), + "restSerializer" => new TupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TupleSerializer>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTupleSerializer : TupleSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/TwoDimensionalArraySerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/TwoDimensionalArraySerializerTests.cs index b905573b302..743e6950918 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/TwoDimensionalArraySerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/TwoDimensionalArraySerializerTests.cs @@ -14,8 +14,9 @@ */ using System.Linq; -using MongoDB.Bson; +using FluentAssertions; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization.ArraySerializer @@ -208,5 +209,85 @@ public void Test3x3() var rehydrated = BsonSerializer.Deserialize(bson); Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new TwoDimensionalArraySerializer(); + var y = new DerivedFromTwoDimensionalArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new TwoDimensionalArraySerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new TwoDimensionalArraySerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new TwoDimensionalArraySerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new TwoDimensionalArraySerializer(); + var y = new TwoDimensionalArraySerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new TwoDimensionalArraySerializer(itemSerializer1); + var y = new TwoDimensionalArraySerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new TwoDimensionalArraySerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromTwoDimensionalArraySerializer : TwoDimensionalArraySerializer + { + } } } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/UndiscriminatedActualTypeSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/UndiscriminatedActualTypeSerializerTests.cs new file mode 100644 index 00000000000..50505d65a4a --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/UndiscriminatedActualTypeSerializerTests.cs @@ -0,0 +1,90 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class UndiscriminatedActualTypeSerializerrTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new UndiscriminatedActualTypeSerializer(); + var y = new DerivedFromUndiscriminatedActualTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new UndiscriminatedActualTypeSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new UndiscriminatedActualTypeSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new UndiscriminatedActualTypeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new UndiscriminatedActualTypeSerializer(); + var y = new UndiscriminatedActualTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new UndiscriminatedActualTypeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromUndiscriminatedActualTypeSerializer : UndiscriminatedActualTypeSerializer + { + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/ValueTupleSerializersEqualsTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ValueTupleSerializersEqualsTests.cs new file mode 100644 index 00000000000..680a8bcbffd --- /dev/null +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/ValueTupleSerializersEqualsTests.cs @@ -0,0 +1,658 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class ValueTupleWith1ItemSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1); + var y = new ValueTupleSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith2ItemsSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer(itemSerializer2, itemSerializer1), + "item2Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith3ItemsSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1), + "item2Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1), + "item3Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith4ItemsSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item3Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item4Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith5ItemsSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item3Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item4Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item5Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith6ItemsSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + [InlineData("item6Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item3Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item4Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item5Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item6Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith7ItemsSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer(); + var y = new ValueTupleSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + [InlineData("item6Serializer")] + [InlineData("item7Serializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item2Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item3Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1), + "item4Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1), + "item5Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1), + "item6Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1), + "item7Serializer" => new ValueTupleSerializer(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } + + public class ValueTupleWith7ItemsAndRestSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueTupleSerializer>(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueTupleSerializer>(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueTupleSerializer>(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueTupleSerializer>(); + var y = new ValueTupleSerializer>(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("item1Serializer")] + [InlineData("item2Serializer")] + [InlineData("item3Serializer")] + [InlineData("item4Serializer")] + [InlineData("item5Serializer")] + [InlineData("item6Serializer")] + [InlineData("item7Serializer")] + [InlineData("restSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var restSerializer1 = new ValueTupleSerializer(itemSerializer1); + var restSerializer2 = new ValueTupleSerializer(itemSerializer2); + var x = new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1); + var y = notEqualFieldName switch + { + "item1Serializer" => new ValueTupleSerializer>(itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item2Serializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item3Serializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item4Serializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer1), + "item5Serializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, itemSerializer1, restSerializer1), + "item6Serializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, itemSerializer1, restSerializer1), + "item7Serializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer2, restSerializer1), + "restSerializer" => new ValueTupleSerializer>(itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, itemSerializer1, restSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueTupleSerializer>(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } +} diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/VersionSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/VersionSerializerTests.cs index d4153f2f0a9..e820e623e09 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/VersionSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/VersionSerializerTests.cs @@ -14,11 +14,9 @@ */ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using FluentAssertions; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using Xunit; namespace MongoDB.Bson.Tests.Serialization.Serializers @@ -117,5 +115,83 @@ public void TestDeserializeMajorMinorBuildRevisionOutOfOrder() Assert.Equal(3, version.Build); Assert.Equal(4, version.Revision); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new VersionSerializer(); + var y = new DerivedFromVersionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new VersionSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new VersionSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new VersionSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new VersionSerializer(); + var y = new VersionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new VersionSerializer(BsonType.String); + var y = new VersionSerializer(BsonType.Document); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new VersionSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromVersionSerializer : VersionSerializer + { + } } } diff --git a/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentCollectionNamespaceSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentCollectionNamespaceSerializerTests.cs new file mode 100644 index 00000000000..b7ce8a0bcda --- /dev/null +++ b/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentCollectionNamespaceSerializerTests.cs @@ -0,0 +1,91 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Driver; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class ChangeStreamDocumentCollectionNamespaceSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ChangeStreamDocumentCollectionNamespaceSerializer(); + var y = new DerivedFromChangeStreamDocumentCollectionNamespaceSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ChangeStreamDocumentCollectionNamespaceSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ChangeStreamDocumentCollectionNamespaceSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ChangeStreamDocumentCollectionNamespaceSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ChangeStreamDocumentCollectionNamespaceSerializer(); + var y = new ChangeStreamDocumentCollectionNamespaceSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ChangeStreamDocumentCollectionNamespaceSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromChangeStreamDocumentCollectionNamespaceSerializer : ChangeStreamDocumentCollectionNamespaceSerializer + { + } + } +} diff --git a/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentDatabaseNamespaceSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentDatabaseNamespaceSerializerTests.cs new file mode 100644 index 00000000000..8e82a1deacb --- /dev/null +++ b/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentDatabaseNamespaceSerializerTests.cs @@ -0,0 +1,91 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Driver; +using Xunit; + +namespace MongoDB.Bson.Serialization.Serializers +{ + public class ChangeStreamDocumentDatabaseNamespaceSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + var y = new DerivedFromChangeStreamDocumentDatabaseNamespaceSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + var y = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ChangeStreamDocumentDatabaseNamespaceSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromChangeStreamDocumentDatabaseNamespaceSerializer : ChangeStreamDocumentDatabaseNamespaceSerializer + { + } + } +} diff --git a/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentSerializerTests.cs index a69709961a7..1d63495d8b1 100644 --- a/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentSerializerTests.cs +++ b/tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentSerializerTests.cs @@ -177,6 +177,93 @@ public void Serialize_should_have_expected_result_when_value_is_null() json.Should().Be("null"); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + var y = new DerivedFromChangeStreamDocumentSerializer(new DocumentSerializer1()); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + var y = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + var y = new ChangeStreamDocumentSerializer(new DocumentSerializer2()); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ChangeStreamDocumentSerializer(new DocumentSerializer1()); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromChangeStreamDocumentSerializer : ChangeStreamDocumentSerializer + { + public DerivedFromChangeStreamDocumentSerializer(IBsonSerializer documentSerializer) : base(documentSerializer) + { + } + } + + public class Document { } + + public class DocumentSerializer1 : ClassSerializerBase { } + + public class DocumentSerializer2 : ClassSerializerBase { } + // private methods private void AssertRegisteredMember(ChangeStreamDocumentSerializer changeStreamDocumentSerializer, string memberName, string elementName, IBsonSerializer memberSerializer) { diff --git a/tests/MongoDB.Driver.Core.Tests/ChangeStreamOperationTypeSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/ChangeStreamOperationTypeSerializerTests.cs index 536e80696cf..f48ca7a0bfe 100644 --- a/tests/MongoDB.Driver.Core.Tests/ChangeStreamOperationTypeSerializerTests.cs +++ b/tests/MongoDB.Driver.Core.Tests/ChangeStreamOperationTypeSerializerTests.cs @@ -123,6 +123,73 @@ public void Serialize_should_throw_when_value_is_invalid(int valueAsInt) argumentException.ParamName.Should().Be("value"); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ChangeStreamOperationTypeSerializer(); + var y = new DerivedFromChangeStreamOperationTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ChangeStreamOperationTypeSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ChangeStreamOperationTypeSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ChangeStreamOperationTypeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ChangeStreamOperationTypeSerializer(); + var y = new ChangeStreamOperationTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ChangeStreamOperationTypeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromChangeStreamOperationTypeSerializer : ChangeStreamOperationTypeSerializer + { + } + // private methods private ChangeStreamOperationTypeSerializer CreateSubject() { diff --git a/tests/MongoDB.Driver.Core.Tests/ChangeStreamSplitEventSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/ChangeStreamSplitEventSerializerTests.cs index 73af4f6fbff..c14790854a8 100644 --- a/tests/MongoDB.Driver.Core.Tests/ChangeStreamSplitEventSerializerTests.cs +++ b/tests/MongoDB.Driver.Core.Tests/ChangeStreamSplitEventSerializerTests.cs @@ -86,6 +86,73 @@ public void Serialize_should_have_expected_result_when_value_is_null() result.Should().Be(expectedResult); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ChangeStreamOperationTypeSerializer(); + var y = new DerivedFromChangeStreamOperationTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ChangeStreamSplitEventSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ChangeStreamSplitEventSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ChangeStreamOperationTypeSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ChangeStreamOperationTypeSerializer(); + var y = new ChangeStreamOperationTypeSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ChangeStreamOperationTypeSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromChangeStreamOperationTypeSerializer : ChangeStreamOperationTypeSerializer + { + } + private ChangeStreamSplitEvent Deserialize(string json) { var serializer = new ChangeStreamSplitEventSerializer(); diff --git a/tests/MongoDB.Driver.Core.Tests/ChangeStreamUpdateDescriptionSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/ChangeStreamUpdateDescriptionSerializerTests.cs index f33756414f3..0d3547f3642 100644 --- a/tests/MongoDB.Driver.Core.Tests/ChangeStreamUpdateDescriptionSerializerTests.cs +++ b/tests/MongoDB.Driver.Core.Tests/ChangeStreamUpdateDescriptionSerializerTests.cs @@ -118,6 +118,73 @@ public void Serialize_should_have_expected_result_when_value_is_null() result.Should().Be("null"); } + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ChangeStreamUpdateDescriptionSerializer(); + var y = new DerivedFromChangeStreamUpdateDescriptionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ChangeStreamSplitEventSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ChangeStreamSplitEventSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ChangeStreamUpdateDescriptionSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ChangeStreamUpdateDescriptionSerializer(); + var y = new ChangeStreamUpdateDescriptionSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ChangeStreamUpdateDescriptionSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromChangeStreamUpdateDescriptionSerializer : ChangeStreamUpdateDescriptionSerializer + { + } + // private methods private ChangeStreamUpdateDescriptionSerializer CreateSubject() { diff --git a/tests/MongoDB.Driver.Core.Tests/Core/Misc/FixedCountBatchableSourceSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/Core/Misc/FixedCountBatchableSourceSerializerTests.cs new file mode 100644 index 00000000000..d4beeb98840 --- /dev/null +++ b/tests/MongoDB.Driver.Core.Tests/Core/Misc/FixedCountBatchableSourceSerializerTests.cs @@ -0,0 +1,128 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.IO; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Core.Misc; +using MongoDB.Driver.Core.Operations.ElementNameValidators; +using Xunit; + +namespace MongoDB.Driver.Core.Tests.Core.Misc +{ + public class FixedCountBatchableSourceSerializerTests + { + private static readonly IBsonSerializer __itemSerializer1 = new Int32Serializer(BsonType.Int32); + private static readonly IBsonSerializer __itemSerializer2 = new Int32Serializer(BsonType.String); + private static IElementNameValidator __itemElementNameValidator1 = new NoOpElementNameValidator(); + private static IElementNameValidator __itemElementNameValidator2 = new UpdateElementNameValidator(); + private static int __count1 = 1; + private static int __count2 = 2; + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + var y = new DerivedFromFixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + var y = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("count")] + [InlineData("itemElementNameValidator")] + [InlineData("itemSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + var y = notEqualFieldName switch + { + "itemSerializer" => new FixedCountBatchableSourceSerializer(__itemSerializer2, __itemElementNameValidator1, __count1), + "itemElementNameValidator" => new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator2, __count1), + "count" => new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new FixedCountBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __count1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromFixedCountBatchableSourceSerializer : FixedCountBatchableSourceSerializer + { + public DerivedFromFixedCountBatchableSourceSerializer(IBsonSerializer itemSerializer, IElementNameValidator itemElementNameValidator, int count) + : base(itemSerializer, itemElementNameValidator, count) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Core.Tests/Core/Misc/SizeLimitingBatchableSourceSerializerTests.cs b/tests/MongoDB.Driver.Core.Tests/Core/Misc/SizeLimitingBatchableSourceSerializerTests.cs new file mode 100644 index 00000000000..c34324bcbe1 --- /dev/null +++ b/tests/MongoDB.Driver.Core.Tests/Core/Misc/SizeLimitingBatchableSourceSerializerTests.cs @@ -0,0 +1,136 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.IO; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Core.Misc; +using MongoDB.Driver.Core.Operations.ElementNameValidators; +using Xunit; + +namespace MongoDB.Driver.Core.Tests.Core.Misc +{ + public class SizeLimitingBatchableSourceSerializerTests + { + private static readonly IBsonSerializer __itemSerializer1 = new Int32Serializer(BsonType.Int32); + private static readonly IBsonSerializer __itemSerializer2 = new Int32Serializer(BsonType.String); + private static IElementNameValidator __itemElementNameValidator1 = new NoOpElementNameValidator(); + private static IElementNameValidator __itemElementNameValidator2 = new UpdateElementNameValidator(); + private static int __maxBatchCount1 = 1; + private static int __maxBatchCount2 = 2; + private static int __maxBatchSize1 = 1; + private static int __maxBatchSize2 = 2; + private static int __maxItemSize1 = 1; + private static int __maxItemSize2 = 2; + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + var y = new DerivedFromSizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + var y = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("itemElementNameValidator")] + [InlineData("itemSerializer")] + [InlineData("maxBatchCount")] + [InlineData("maxBatchSize")] + [InlineData("maxItemSize")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + var y = notEqualFieldName switch + { + "itemSerializer" => new SizeLimitingBatchableSourceSerializer(__itemSerializer2, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1), + "itemElementNameValidator" => new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator2, __maxBatchCount1, __maxItemSize1, __maxBatchSize1), + "maxBatchCount" => new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount2, __maxItemSize1, __maxBatchSize1), + "maxBatchSize" => new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize2, __maxBatchSize1), + "maxItemSize" => new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new SizeLimitingBatchableSourceSerializer(__itemSerializer1, __itemElementNameValidator1, __maxBatchCount1, __maxItemSize1, __maxBatchSize1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromSizeLimitingBatchableSourceSerializer : SizeLimitingBatchableSourceSerializer + { + public DerivedFromSizeLimitingBatchableSourceSerializer(IBsonSerializer itemSerializer, IElementNameValidator itemElementNameValidator, int maxBatchCount, int maxItemSize, int maxBatchSize) + : base(itemSerializer, itemElementNameValidator, maxBatchCount, maxItemSize, maxBatchSize) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Core.Tests/Core/Operations/AggregateOperationTests.cs b/tests/MongoDB.Driver.Core.Tests/Core/Operations/AggregateOperationTests.cs index b2f43ad1f36..9e20b7d8fa0 100644 --- a/tests/MongoDB.Driver.Core.Tests/Core/Operations/AggregateOperationTests.cs +++ b/tests/MongoDB.Driver.Core.Tests/Core/Operations/AggregateOperationTests.cs @@ -929,4 +929,176 @@ private void EnsureTestData() }); } } + + public class AggregateOperation_AggregateResultDeserializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + var y = new DerivedFromAggregateResultDeserializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + var y = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var resultSerializer1 = new Int32Serializer(BsonType.Int32); + var resultSerializer2 = new Int32Serializer(BsonType.String); + var x = new AggregateOperation.AggregateResultDeserializer(resultSerializer1); + var y = new AggregateOperation.AggregateResultDeserializer(resultSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new AggregateOperation.AggregateResultDeserializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromAggregateResultDeserializer : AggregateOperation.AggregateResultDeserializer + { + public DerivedFromAggregateResultDeserializer(IBsonSerializer resultSerializer) : base(resultSerializer) + { + } + } + } + + public class AggregateOperation_AggregateCursorDeserializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + var y = new DerivedFromCursorDeserializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + var y = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var resultSerializer1 = new Int32Serializer(BsonType.Int32); + var resultSerializer2 = new Int32Serializer(BsonType.String); + var x = new AggregateOperation.CursorDeserializer(resultSerializer1); + var y = new AggregateOperation.CursorDeserializer(resultSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new AggregateOperation.CursorDeserializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromCursorDeserializer : AggregateOperation.CursorDeserializer + { + public DerivedFromCursorDeserializer(IBsonSerializer resultSerializer) : base(resultSerializer) + { + } + } + } } diff --git a/tests/MongoDB.Driver.Core.Tests/Core/Operations/ElementDeserializerTests.cs b/tests/MongoDB.Driver.Core.Tests/Core/Operations/ElementDeserializerTests.cs new file mode 100644 index 00000000000..7c2fe52af1c --- /dev/null +++ b/tests/MongoDB.Driver.Core.Tests/Core/Operations/ElementDeserializerTests.cs @@ -0,0 +1,122 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Core.Operations; +using Xunit; + +namespace MongoDB.Driver.Core.Tests.Operations +{ + public class ElementDeserializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ElementDeserializer("name", Int32Serializer.Instance); + var y = new DerivedFromElementDeserializer("name", Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ElementDeserializer("name", Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ElementDeserializer("name", Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ElementDeserializer("name", Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ElementDeserializer("name", Int32Serializer.Instance); + var y = new ElementDeserializer("name", Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("deserializeNull")] + [InlineData("elementName")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var valueSerializer1 = new Int32Serializer(BsonType.Int32); + var valueSerializer2 = new Int32Serializer(BsonType.String); + var deserializeNull1 = true; + var deserializeNull2 = false; + var x = new ElementDeserializer("name1", valueSerializer1, deserializeNull1); + var y = notEqualFieldName switch + { + "elementName" => new ElementDeserializer("name2", valueSerializer1, deserializeNull1), + "valueSerializer" => new ElementDeserializer("name1", valueSerializer2, deserializeNull1), + "deserializeNull" => new ElementDeserializer("name1", valueSerializer1, deserializeNull2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ElementDeserializer("name", Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromElementDeserializer : ElementDeserializer + { + public DerivedFromElementDeserializer(string elementName, IBsonSerializer valueSerializer) : base(elementName, valueSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Core.Tests/Core/Operations/FindAndModifyValueDeserializerTests.cs b/tests/MongoDB.Driver.Core.Tests/Core/Operations/FindAndModifyValueDeserializerTests.cs new file mode 100644 index 00000000000..1b3fb34706a --- /dev/null +++ b/tests/MongoDB.Driver.Core.Tests/Core/Operations/FindAndModifyValueDeserializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Core.Operations; +using Xunit; + +namespace MongoDB.Driver.Core.Tests.Operations +{ + public class FindAndModifyValueDeserializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + var y = new DerivedFromFindAndModifyValueDeserializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + var y = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var resultSerializer1 = new Int32Serializer(BsonType.Int32); + var resultSerializer2 = new Int32Serializer(BsonType.String); + var x = new FindAndModifyValueDeserializer(resultSerializer1); + var y = new FindAndModifyValueDeserializer(resultSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new FindAndModifyValueDeserializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromFindAndModifyValueDeserializer : FindAndModifyValueDeserializer + { + public DerivedFromFindAndModifyValueDeserializer(IBsonSerializer valueSerializer) : base(valueSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/AggregateBucketAutoResultIdSerializerTests.cs b/tests/MongoDB.Driver.Tests/AggregateBucketAutoResultIdSerializerTests.cs new file mode 100644 index 00000000000..6dd4942d24f --- /dev/null +++ b/tests/MongoDB.Driver.Tests/AggregateBucketAutoResultIdSerializerTests.cs @@ -0,0 +1,108 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class AggregateBucketAutoResultIdSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + var y = new DerivedFromAggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + var y = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var valueSerializer1 = new Int32Serializer(Bson.BsonType.Int32); + var valueSerializer2 = new Int32Serializer(Bson.BsonType.String); + var x = new AggregateBucketAutoResultIdSerializer(valueSerializer1); + var y = new AggregateBucketAutoResultIdSerializer(valueSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new AggregateBucketAutoResultIdSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class DerivedFromAggregateBucketAutoResultIdSerializer : AggregateBucketAutoResultIdSerializer + { + public DerivedFromAggregateBucketAutoResultIdSerializer(IBsonSerializer valueSerializer) : base(valueSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/AggregateFacetResultsSerializerTests.cs b/tests/MongoDB.Driver.Tests/AggregateFacetResultsSerializerTests.cs new file mode 100644 index 00000000000..1a5543a3ed3 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/AggregateFacetResultsSerializerTests.cs @@ -0,0 +1,118 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class AggregateFacetResultsSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + var y = new DerivedFromAggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + var y = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("names")] + [InlineData("serializers")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var serializer1 = new Int32Serializer(Bson.BsonType.Int32); + var serializer2 = new Int32Serializer(Bson.BsonType.String); + var x = new AggregateFacetResultsSerializer(new[] { "name1" }, new[] { serializer1 }); + var y = notEqualFieldName switch + { + "names" => new AggregateFacetResultsSerializer(new[] { "name2" }, new[] { serializer1 }), + "serializers" => new AggregateFacetResultsSerializer(new[] { "name1" }, new[] { serializer2 }), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new AggregateFacetResultsSerializer(new[] { "name" }, new[] { Int32Serializer.Instance }); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromAggregateFacetResultsSerializer : AggregateFacetResultsSerializer + { + public DerivedFromAggregateFacetResultsSerializer(IEnumerable names, IEnumerable serializers) + : base(names, serializers) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/ConvertIfPossibleSerializerTests.cs b/tests/MongoDB.Driver.Tests/ConvertIfPossibleSerializerTests.cs new file mode 100644 index 00000000000..ab03edcf224 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/ConvertIfPossibleSerializerTests.cs @@ -0,0 +1,118 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class ConvertIfPossibleSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + var y = new DerivedFromConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + var y = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("serializer")] + [InlineData("serializerRegistry")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var serializer1 = new Int32Serializer(Bson.BsonType.Int32); + var serializer2 = new Int32Serializer(Bson.BsonType.String); + var serializerRegistry1 = new BsonSerializerRegistry(); + var serializerRegistry2 = new BsonSerializerRegistry(); + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(serializer1, serializerRegistry1); + var y = notEqualFieldName switch + { + "serializer" => new FieldValueSerializerHelper.ConvertIfPossibleSerializer(serializer2, serializerRegistry1), + "serializerRegistry" => new FieldValueSerializerHelper.ConvertIfPossibleSerializer(serializer1, serializerRegistry2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new FieldValueSerializerHelper.ConvertIfPossibleSerializer(Int32Serializer.Instance, BsonSerializer.SerializerRegistry); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromConvertIfPossibleSerializer : FieldValueSerializerHelper.ConvertIfPossibleSerializer + { + public DerivedFromConvertIfPossibleSerializer(IBsonSerializer serializer, IBsonSerializerRegistry serializerRegistry) : base(serializer, serializerRegistry) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/EnumConvertingSerializerTests.cs b/tests/MongoDB.Driver.Tests/EnumConvertingSerializerTests.cs new file mode 100644 index 00000000000..876cac80905 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/EnumConvertingSerializerTests.cs @@ -0,0 +1,108 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class EnumConvertingSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + var y = new DerivedFromEnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + var y = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var serializer1 = new Int32Serializer(Bson.BsonType.Int32); + var serializer2 = new Int32Serializer(Bson.BsonType.String); + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(serializer1); + var y = new FieldValueSerializerHelper.EnumConvertingSerializer(serializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new FieldValueSerializerHelper.EnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromEnumConvertingSerializer : FieldValueSerializerHelper.EnumConvertingSerializer + { + public DerivedFromEnumConvertingSerializer(IBsonSerializer serializer) : base(serializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/FieldValueSerializerHelperTests.cs b/tests/MongoDB.Driver.Tests/FieldValueSerializerHelperTests.cs new file mode 100644 index 00000000000..b540e8faedf --- /dev/null +++ b/tests/MongoDB.Driver.Tests/FieldValueSerializerHelperTests.cs @@ -0,0 +1,108 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class FieldValueSerializerHelper_IEnumerableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + var y = new DerivedFromIEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + var y = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var serializer1 = new Int32Serializer(Bson.BsonType.Int32); + var serializer2 = new Int32Serializer(Bson.BsonType.String); + var x = new FieldValueSerializerHelper.IEnumerableSerializer(serializer1); + var y = new FieldValueSerializerHelper.IEnumerableSerializer(serializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new FieldValueSerializerHelper.IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromIEnumerableSerializer : FieldValueSerializerHelper.IEnumerableSerializer + { + public DerivedFromIEnumerableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializerTests.cs new file mode 100644 index 00000000000..f6f7ace6d48 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/EnumUnderlyingTypeSerializerTests.cs @@ -0,0 +1,119 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class EnumUnderlyingTypeSerializerTests + { + private static readonly IBsonSerializer __enumSerializer1 = new ESerializer1(); + private static readonly IBsonSerializer __enumSerializer2 = new ESerializer2(); + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + var y = new DerivedFromEnumUnderlyingTypeSerializer(__enumSerializer1); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + var y = new EnumUnderlyingTypeSerializer(__enumSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + var y = new EnumUnderlyingTypeSerializer(__enumSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new EnumUnderlyingTypeSerializer(__enumSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromEnumUnderlyingTypeSerializer : EnumUnderlyingTypeSerializer + where TEnum : Enum + where TEnumUnderlyingType : struct + { + public DerivedFromEnumUnderlyingTypeSerializer(IBsonSerializer enumSerializer) : base(enumSerializer) + { + } + } + + internal enum E { } + + internal class ESerializer1 : StructSerializerBase { } + + internal class ESerializer2 : StructSerializerBase { } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBaseTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBaseTests.cs new file mode 100644 index 00000000000..feeb8d4e689 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerBaseTests.cs @@ -0,0 +1,122 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System.Collections.Generic; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class IEnumerableSerializerBaseTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + var y = new DerivedFromConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + var y = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(itemSerializer1); + var y = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = (IEnumerableSerializerBase, int>)new ConcreteIEnumerableSerializerBase, int>(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class ConcreteIEnumerableSerializerBase : IEnumerableSerializerBase + where TEnumerable : IEnumerable + { + public ConcreteIEnumerableSerializerBase(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + + protected override TEnumerable CreateDeserializedValue(List items) => throw new System.NotImplementedException(); + } + + internal class DerivedFromConcreteIEnumerableSerializerBase : ConcreteIEnumerableSerializerBase + where TEnumerable : IEnumerable + { + public DerivedFromConcreteIEnumerableSerializerBase(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerTests.cs new file mode 100644 index 00000000000..8b1801cfae4 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IEnumerableSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class IEnumerableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IEnumerableSerializer(Int32Serializer.Instance); + var y = new DerivedFromIEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IEnumerableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IEnumerableSerializer(Int32Serializer.Instance); + var y = new IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new IEnumerableSerializer(itemSerializer1); + var y = new IEnumerableSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IEnumerableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromIEnumerableSerializer : IEnumerableSerializer + { + public DerivedFromIEnumerableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IGroupingSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IGroupingSerializerTests.cs new file mode 100644 index 00000000000..0e6af0544b0 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IGroupingSerializerTests.cs @@ -0,0 +1,118 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class IGroupingSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + var y = new DerivedFromIGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + var y = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("elementSerializer")] + [InlineData("keySerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var int32Serializer1 = new Int32Serializer(BsonType.Int32); + var int32Serializer2 = new Int32Serializer(BsonType.String); + var x = new IGroupingSerializer(int32Serializer1, int32Serializer1); + var y = notEqualFieldName switch + { + "keySerializer" => new IGroupingSerializer(int32Serializer2, int32Serializer1), + "elementSerializer" => new IGroupingSerializer(int32Serializer1, int32Serializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IGroupingSerializer(Int32Serializer.Instance, Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromIGroupingSerializer : IGroupingSerializer + { + public DerivedFromIGroupingSerializer(IBsonSerializer keySerializer, IBsonSerializer elementSerializer) : base(keySerializer, elementSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IOrderedEnumerableSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IOrderedEnumerableSerializerTests.cs new file mode 100644 index 00000000000..4f7d417a8a9 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IOrderedEnumerableSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; +using Linq3Serializers = MongoDB.Driver.Linq.Linq3Implementation.Serializers; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class IOrderedEnumerableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + var y = new DerivedFromIOrderedEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + var y = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new Linq3Serializers.IOrderedEnumerableSerializer(itemSerializer1); + var y = new Linq3Serializers.IOrderedEnumerableSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new Linq3Serializers.IOrderedEnumerableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromIOrderedEnumerableSerializer : Linq3Serializers.IOrderedEnumerableSerializer + { + public DerivedFromIOrderedEnumerableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IOrderedQueryableSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IOrderedQueryableSerializerTests.cs new file mode 100644 index 00000000000..4f51b9c6f68 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IOrderedQueryableSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class IOrderedQueryableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IOrderedQueryableSerializer(Int32Serializer.Instance); + var y = new DerivedFromIOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IOrderedQueryableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IOrderedQueryableSerializer(Int32Serializer.Instance); + var y = new IOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new IOrderedQueryableSerializer(itemSerializer1); + var y = new IOrderedQueryableSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromIOrderedQueryableSerializer : IOrderedQueryableSerializer + { + public DerivedFromIOrderedQueryableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IQueryableSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IQueryableSerializerTests.cs new file mode 100644 index 00000000000..837185c86c3 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/IQueryableSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class IQueryableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new IQueryableSerializer(Int32Serializer.Instance); + var y = new DerivedFromIQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new IQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new IQueryableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new IQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new IQueryableSerializer(Int32Serializer.Instance); + var y = new IQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new IQueryableSerializer(itemSerializer1); + var y = new IQueryableSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new IQueryableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromIQueryableSerializer : IQueryableSerializer + { + public DerivedFromIQueryableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializerTests.cs new file mode 100644 index 00000000000..02fb89e3ad0 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/ISetWindowFieldsPartitionSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class ISetWindowFieldsPartitionSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + var y = new DerivedFromISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + var y = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var inputSerializer1 = new Int32Serializer(BsonType.Int32); + var inputSerializer2 = new Int32Serializer(BsonType.String); + var x = new ISetWindowFieldsPartitionSerializer(inputSerializer1); + var y = new ISetWindowFieldsPartitionSerializer(inputSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ISetWindowFieldsPartitionSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromISetWindowFieldsPartitionSerializer : ISetWindowFieldsPartitionSerializer + { + public DerivedFromISetWindowFieldsPartitionSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/NestedAsOrderedQueryableSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/NestedAsOrderedQueryableSerializerTests.cs new file mode 100644 index 00000000000..1174a7b5aaa --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/NestedAsOrderedQueryableSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class NestedAsOrderedQueryableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + var y = new DerivedFromNestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + var y = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new NestedAsOrderedQueryableSerializer(itemSerializer1); + var y = new NestedAsOrderedQueryableSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new NestedAsOrderedQueryableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromNestedAsOrderedQueryableSerializer : NestedAsOrderedQueryableSerializer + { + public DerivedFromNestedAsOrderedQueryableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/NestedAsQueryableSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/NestedAsQueryableSerializerTests.cs new file mode 100644 index 00000000000..4069f59e03d --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/NestedAsQueryableSerializerTests.cs @@ -0,0 +1,110 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Serializers +{ + public class NestedAsQueryableSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new NestedAsQueryableSerializer(Int32Serializer.Instance); + var y = new DerivedFromNestedAsQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new NestedAsQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new NestedAsQueryableSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new NestedAsQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new NestedAsQueryableSerializer(Int32Serializer.Instance); + var y = new NestedAsQueryableSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new NestedAsQueryableSerializer(itemSerializer1); + var y = new NestedAsQueryableSerializer(itemSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new NestedAsQueryableSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromNestedAsQueryableSerializer : NestedAsQueryableSerializer + { + public DerivedFromNestedAsQueryableSerializer(IBsonSerializer itemSerializer) : base(itemSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/WrappedValueSerializerTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/WrappedValueSerializerTests.cs index 1f6d3ed30e7..0a64e58513a 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/WrappedValueSerializerTests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Serializers/WrappedValueSerializerTests.cs @@ -13,8 +13,11 @@ * limitations under the License. */ +using System; using FluentAssertions; +using MongoDB.Bson; using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; using MongoDB.Driver.Linq.Linq3Implementation.Serializers; using Moq; using Xunit; @@ -34,5 +37,95 @@ public void constructor_should_initialize_instance() subject.ValueSerializer.Should().BeSameAs(valueSerializer); subject.ValueType.Should().BeSameAs(typeof(int)); } + + [Fact] + public void Equals_derived_should_return_false() + { + var x = new WrappedValueSerializer("name", Int32Serializer.Instance); + var y = new DerivedFromWrappedValueSerializer("name", Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new WrappedValueSerializer("name", Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new WrappedValueSerializer("name", Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new WrappedValueSerializer("name", Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new WrappedValueSerializer("name", Int32Serializer.Instance); + var y = new WrappedValueSerializer("name", Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Theory] + [InlineData("fieldName")] + [InlineData("valueSerializer")] + public void Equals_with_not_equal_field_should_return_false(string notEqualFieldName) + { + var itemSerializer1 = new Int32Serializer(BsonType.Int32); + var itemSerializer2 = new Int32Serializer(BsonType.String); + var x = new WrappedValueSerializer("name1", itemSerializer1); + var y = notEqualFieldName switch + { + "fieldName" => new WrappedValueSerializer("name2", itemSerializer1), + "valueSerializer" => new WrappedValueSerializer("name1", itemSerializer2), + _ => throw new Exception() + }; + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new WrappedValueSerializer("name", Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromWrappedValueSerializer : WrappedValueSerializer + { + public DerivedFromWrappedValueSerializer(string fieldName, IBsonSerializer valueSerializer) : base(fieldName, valueSerializer) + { + } + } } } diff --git a/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/FindProjectionTranslatorTests.cs b/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/FindProjectionTranslatorTests.cs index e9381843077..dd13bc9ac51 100644 --- a/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/FindProjectionTranslatorTests.cs +++ b/tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationWithLinq2Tests/Translators/FindProjectionTranslatorTests.cs @@ -23,7 +23,6 @@ using MongoDB.Bson.Serialization; using MongoDB.Driver.Linq.Linq3Implementation.Ast; using MongoDB.Driver.Linq.Linq3Implementation.Translators; -using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFindProjectionTranslators; using Xunit; namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationWithLinq2Tests.Translators diff --git a/tests/MongoDB.Driver.Tests/MongoDBRefTests.cs b/tests/MongoDB.Driver.Tests/MongoDBRefTests.cs index 04faef99397..154ce51f516 100644 --- a/tests/MongoDB.Driver.Tests/MongoDBRefTests.cs +++ b/tests/MongoDB.Driver.Tests/MongoDBRefTests.cs @@ -292,4 +292,74 @@ public void TestWithDatabase() Assert.True(bson.SequenceEqual(rehydrated.ToBson())); } } + + public class MongoDBRefSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new MongoDBRefSerializer(); + var y = new DerivedFromMongoDBRefSerializer(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new MongoDBRefSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new MongoDBRefSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new MongoDBRefSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new MongoDBRefSerializer(); + var y = new MongoDBRefSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new MongoDBRefSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromMongoDBRefSerializer : MongoDBRefSerializer + { + } + } } diff --git a/tests/MongoDB.Driver.Tests/NoPipelineInputTests.cs b/tests/MongoDB.Driver.Tests/NoPipelineInputTests.cs new file mode 100644 index 00000000000..8b0326adc2c --- /dev/null +++ b/tests/MongoDB.Driver.Tests/NoPipelineInputTests.cs @@ -0,0 +1,75 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class NoPipelineInputSerializerTests + { + [Fact] + public void Equals_null_should_return_false() + { + var x = new NoPipelineInputSerializer(); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new NoPipelineInputSerializer(); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new NoPipelineInputSerializer(); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new NoPipelineInputSerializer(); + var y = new NoPipelineInputSerializer(); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new NoPipelineInputSerializer(); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + } +} diff --git a/tests/MongoDB.Driver.Tests/NullableEnumConvertingSerializerTests.cs b/tests/MongoDB.Driver.Tests/NullableEnumConvertingSerializerTests.cs new file mode 100644 index 00000000000..7373667a97a --- /dev/null +++ b/tests/MongoDB.Driver.Tests/NullableEnumConvertingSerializerTests.cs @@ -0,0 +1,109 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class NullableEnumConvertingSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + var y = new DerivedFromNullableEnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + var y = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var serializer1 = new Int32Serializer(Bson.BsonType.Int32); + var serializer2 = new Int32Serializer(Bson.BsonType.String); + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(serializer1); + var y = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(serializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new FieldValueSerializerHelper.NullableEnumConvertingSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromNullableEnumConvertingSerializer : FieldValueSerializerHelper.NullableEnumConvertingSerializer + where TFrom : struct where TTo : struct + { + public DerivedFromNullableEnumConvertingSerializer(IBsonSerializer nonNullableEnumSerializer) : base(nonNullableEnumSerializer) + { + } + } + } +} diff --git a/tests/MongoDB.Driver.Tests/OfTypeSerializerTests.cs b/tests/MongoDB.Driver.Tests/OfTypeSerializerTests.cs new file mode 100644 index 00000000000..7dc3c6a234e --- /dev/null +++ b/tests/MongoDB.Driver.Tests/OfTypeSerializerTests.cs @@ -0,0 +1,99 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class OfTypeSerializerTests + { + private static IBsonSerializer __derivedDocumentSerializer1 = new DSerializer1(); + private static IBsonSerializer __derivedDocumentSerializer2 = new DSerializer2(); + + [Fact] + public void Equals_null_should_return_false() + { + var x = new OfTypeSerializer(__derivedDocumentSerializer1); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new OfTypeSerializer(__derivedDocumentSerializer1); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new OfTypeSerializer(__derivedDocumentSerializer1); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new OfTypeSerializer(__derivedDocumentSerializer1); + var y= new OfTypeSerializer(__derivedDocumentSerializer1); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var x = new OfTypeSerializer(__derivedDocumentSerializer1); + var y = new OfTypeSerializer(__derivedDocumentSerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new OfTypeSerializer(__derivedDocumentSerializer1); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + public class C { } + + public class D : C { } + + public class DSerializer1 : SerializerBase { } + + public class DSerializer2 : SerializerBase { } + } +} diff --git a/tests/MongoDB.Driver.Tests/RangeWindowBoundaryTests.cs b/tests/MongoDB.Driver.Tests/RangeWindowBoundaryTests.cs new file mode 100644 index 00000000000..7936a8be5c6 --- /dev/null +++ b/tests/MongoDB.Driver.Tests/RangeWindowBoundaryTests.cs @@ -0,0 +1,109 @@ +/* Copyright 2010-present MongoDB Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Bson.Serialization; +using MongoDB.Bson.Serialization.Serializers; +using Xunit; + +namespace MongoDB.Driver.Tests +{ + public class ValueRangeWindowBoundaryConvertingValueSerializerTests + { + [Fact] + public void Equals_derived_should_return_false() + { + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + var y = new DerivedFromValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_null_should_return_false() + { + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + + var result = x.Equals(null); + + result.Should().Be(false); + } + + [Fact] + public void Equals_object_should_return_false() + { + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + var y = new object(); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void Equals_self_should_return_true() + { + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + + var result = x.Equals(x); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_equal_fields_should_return_true() + { + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + var y = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + + var result = x.Equals(y); + + result.Should().Be(true); + } + + [Fact] + public void Equals_with_not_equal_field_should_return_false() + { + var sortBySerializer1 = new Int32Serializer(BsonType.Int32); + var sortBySerializer2 = new Int32Serializer(BsonType.String); + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(sortBySerializer1); + var y = new ValueRangeWindowBoundaryConvertingValueSerializer(sortBySerializer2); + + var result = x.Equals(y); + + result.Should().Be(false); + } + + [Fact] + public void GetHashCode_should_return_zero() + { + var x = new ValueRangeWindowBoundaryConvertingValueSerializer(Int32Serializer.Instance); + + var result = x.GetHashCode(); + + result.Should().Be(0); + } + + internal class DerivedFromValueRangeWindowBoundaryConvertingValueSerializer : ValueRangeWindowBoundaryConvertingValueSerializer + { + public DerivedFromValueRangeWindowBoundaryConvertingValueSerializer(IBsonSerializer sortBySerializer) : base(sortBySerializer) + { + } + } + } +}