Beispiel #1
0
        public void Serialize_should_preserve_IsDynamicType_when_creating_new_context(bool isDynamicType, bool useGenericInterface)
        {
            var mockDocumentSerializer = new Mock <IBsonSerializer <BsonDocument> >();
            var subject = new ElementAppendingSerializer <BsonDocument>(mockDocumentSerializer.Object, new BsonElement[0]);
            var writer  = new Mock <IBsonWriter>().Object;
            var context = BsonSerializationContext.CreateRoot(writer, b => { b.IsDynamicType = t => isDynamicType; });
            var args    = new BsonSerializationArgs {
                NominalType = typeof(BsonDocument)
            };
            var  value = new BsonDocument();
            bool?capturedIsDynamicType = null;

            mockDocumentSerializer
            .Setup(m => m.Serialize(It.IsAny <BsonSerializationContext>(), args, value))
            .Callback((BsonSerializationContext c, BsonSerializationArgs a, BsonDocument v) => capturedIsDynamicType = c.IsDynamicType(typeof(BsonDocument)));

            if (useGenericInterface)
            {
                subject.Serialize(context, args, value);
            }
            else
            {
                ((IBsonSerializer)subject).Serialize(context, args, value);
            }

            mockDocumentSerializer.Verify(
                m => m.Serialize(It.Is <BsonSerializationContext>(c => c.IsDynamicType(typeof(BsonDocument)) == isDynamicType), args, value),
                Times.Once);

            capturedIsDynamicType.Should().Be(isDynamicType);
        }
Beispiel #2
0
        public void Serialize_should_configure_GuidRepresentation(
            [ClassValues(typeof(GuidModeValues))] GuidMode mode,
            [Values(false, true)] bool useGenericInterface)
        {
            mode.Set();

#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
            {
                var mockDocumentSerializer     = new Mock <IBsonSerializer <BsonDocument> >();
                var writerSettingsConfigurator = (Action <BsonWriterSettings>)(s => s.GuidRepresentation = GuidRepresentation.Unspecified);
                var subject  = new ElementAppendingSerializer <BsonDocument>(mockDocumentSerializer.Object, new BsonElement[0], writerSettingsConfigurator);
                var stream   = new MemoryStream();
                var settings = new BsonBinaryWriterSettings {
                    GuidRepresentation = GuidRepresentation.CSharpLegacy
                };
                var writer  = new BsonBinaryWriter(stream, settings);
                var context = BsonSerializationContext.CreateRoot(writer);
                var args    = new BsonSerializationArgs {
                    NominalType = typeof(BsonDocument)
                };
                var value = new BsonDocument();
                GuidRepresentation?capturedGuidRepresentation = null;
                mockDocumentSerializer
                .Setup(m => m.Serialize(It.IsAny <BsonSerializationContext>(), args, value))
                .Callback((BsonSerializationContext c, BsonSerializationArgs a, BsonDocument v) =>
                {
                    var elementAppendingWriter = (ElementAppendingBsonWriter)c.Writer;
                    var configurator           = elementAppendingWriter._settingsConfigurator();
                    var configuredSettings     = new BsonBinaryWriterSettings {
                        GuidRepresentation = GuidRepresentation.CSharpLegacy
                    };
                    configurator(configuredSettings);
                    capturedGuidRepresentation = configuredSettings.GuidRepresentation;
                });

                if (useGenericInterface)
                {
                    subject.Serialize(context, args, value);
                }
                else
                {
                    ((IBsonSerializer)subject).Serialize(context, args, value);
                }

                capturedGuidRepresentation.Should().Be(GuidRepresentation.Unspecified);
            }
#pragma warning restore 618
        }
Beispiel #3
0
        public void Serialize_should_configure_GuidRepresentation(bool useGenericInterface)
        {
            var mockDocumentSerializer = new Mock <IBsonSerializer <BsonDocument> >();
            var subject  = new ElementAppendingSerializer <BsonDocument>(mockDocumentSerializer.Object, new BsonElement[0]);
            var stream   = new MemoryStream();
            var settings = new BsonBinaryWriterSettings {
                GuidRepresentation = GuidRepresentation.CSharpLegacy
            };
            var writer  = new BsonBinaryWriter(stream, settings);
            var context = BsonSerializationContext.CreateRoot(writer);
            var args    = new BsonSerializationArgs {
                NominalType = typeof(BsonDocument)
            };
            var value = new BsonDocument();
            GuidRepresentation?capturedGuidRepresentation = null;

            mockDocumentSerializer
            .Setup(m => m.Serialize(It.IsAny <BsonSerializationContext>(), args, value))
            .Callback((BsonSerializationContext c, BsonSerializationArgs a, BsonDocument v) =>
            {
                var elementAppendingWriter = (ElementAppendingBsonWriter)c.Writer;
                var configurator           = elementAppendingWriter._settingsConfigurator();
                var configuredSettings     = new BsonBinaryWriterSettings {
                    GuidRepresentation = GuidRepresentation.CSharpLegacy
                };
                configurator(configuredSettings);
                capturedGuidRepresentation = configuredSettings.GuidRepresentation;
            });

            if (useGenericInterface)
            {
                subject.Serialize(context, args, value);
            }
            else
            {
                ((IBsonSerializer)subject).Serialize(context, args, value);
            }

            capturedGuidRepresentation.Should().Be(GuidRepresentation.Unspecified);
        }