Пример #1
0
        public void DefaultImplementationCanBeResolved()
        {
            var context = TomlSerializerContext.Create(
                TomlSerializerOptions.Default
                .AddDefaultImplementation <IInterface, DefaultImplementation>());
            var implementation = context.GetDefaultImplementation(typeof(IInterface));

            Assert.Equal(typeof(DefaultImplementation), implementation);
        }
Пример #2
0
        public void TheLatterDefaultImplementationTakesPrecedence()
        {
            var context = TomlSerializerContext.Create(
                TomlSerializerOptions.Default
                .AddDefaultImplementation <IInterface, DefaultImplementation>()
                .AddDefaultImplementation <IInterface, OtherDefaultImplementation>());
            var implementation = context.GetDefaultImplementation(typeof(IInterface));

            Assert.Equal(typeof(OtherDefaultImplementation), implementation);
        }
Пример #3
0
        public void OpenGenericDefaultImplementationCanBeResolved()
        {
            var context = TomlSerializerContext.Create(
                TomlSerializerOptions.Default
                .AddOpenGenericDefaultImplementation(
                    typeof(IGeneric <>),
                    @params => typeof(GenericImplementation <>).MakeGenericType(@params)));
            var implementation = context.GetDefaultImplementation(typeof(IGeneric <string>));

            Assert.Equal(typeof(GenericImplementation <string>), implementation);
        }
Пример #4
0
        public void DefaultImplementationCanBeResolvedThroughProvider()
        {
            var provider = Mock.Of <IDefaultImplementationProvider>(
                p => p.HasDefaultImplementation(typeof(string)) == true &&
                p.GetDefaultImplementation(typeof(string)) == typeof(int));

            var context = TomlSerializerContext.Create(
                TomlSerializerOptions.Default.AddDefaultImplementation(provider));
            var implementation = context.GetDefaultImplementation(typeof(string));

            Assert.Equal(typeof(int), implementation);
        }
Пример #5
0
        public void NonSupportingDefaultImplementationProviderIsIgnored()
        {
            var nonSupportingProvider = Mock.Of <IDefaultImplementationProvider>(
                p => p.HasDefaultImplementation(It.IsAny <Type>()) == false);

            var context = TomlSerializerContext.Create(
                TomlSerializerOptions.Default
                .AddDefaultImplementation <IInterface, DefaultImplementation>()
                .AddDefaultImplementation(nonSupportingProvider));
            var implementation = context.GetDefaultImplementation(typeof(IInterface));

            Assert.Equal(typeof(DefaultImplementation), implementation);
        }
Пример #6
0
        public void CreatingTheConverterForNewTypeWithNoSuitableConstructorThrows(Type typeToConvert)
        {
            var context = TomlSerializerContext.Create(TomlSerializerOptions.Default.AddNewTypeConverter());

            Assert.Throws <TomlException>(() => context.GetConverter(typeToConvert));
        }