public void StaticAccessorOnUnknownTypeThrowsTypeLoadException(string input, Type targetType)
        {
            var stringArgumentValue = new StringArgumentValue(() => $"{input}");

            Assert.Throws <TypeLoadException>(() =>
                                              stringArgumentValue.ConvertTo(targetType, new Dictionary <string, LoggingLevelSwitch>())
                                              );
        }
예제 #2
0
        public void StaticAccessorOnUnknownTypeThrowsTypeLoadException(string input, Type targetType)
        {
            var stringArgumentValue = new StringArgumentValue($"{input}");

            Assert.Throws <TypeLoadException>(() =>
                                              stringArgumentValue.ConvertTo(targetType, new ResolutionContext())
                                              );
        }
        public void StringValuesConvertToDefaultInstancesIfTargetIsAbstractClass()
        {
            var stringArgumentValue = new StringArgumentValue(() => "Serilog.Settings.Configuration.Tests.Support.ConcreteClass, Serilog.Settings.Configuration.Tests");

            var result = stringArgumentValue.ConvertTo(typeof(AbstractClass), new Dictionary <string, LoggingLevelSwitch>());

            Assert.IsType <ConcreteClass>(result);
        }
        public void StringValuesConvertToDefaultInstancesIfTargetIsInterface()
        {
            var stringArgumentValue = new StringArgumentValue(() => "Serilog.Formatting.Json.JsonFormatter, Serilog");

            var result = stringArgumentValue.ConvertTo(typeof(ITextFormatter), new Dictionary <string, LoggingLevelSwitch>());

            Assert.IsType <JsonFormatter>(result);
        }
예제 #5
0
        public void StringValuesConvertToDefaultInstancesIfTargetIsInterface()
        {
            var stringArgumentValue = new StringArgumentValue("Serilog.Formatting.Json.JsonFormatter, Serilog");

            var result = stringArgumentValue.ConvertTo(typeof(ITextFormatter), new ResolutionContext());

            Assert.IsType <JsonFormatter>(result);
        }
예제 #6
0
        public void StringValuesConvertToDefaultInstancesIfTargetIsAbstractClass()
        {
            var stringArgumentValue = new StringArgumentValue("Serilog.Settings.Configuration.Tests.Support.ConcreteClass, Serilog.Settings.Configuration.Tests");

            var result = stringArgumentValue.ConvertTo(typeof(AbstractClass), new ResolutionContext());

            Assert.IsType <ConcreteClass>(result);
        }
        private void StaticMembersAccessorsCanBeUsedForReferenceTypes(string input, Type targetType)
        {
            var stringArgumentValue = new StringArgumentValue(() => $"{input}");

            var actual = stringArgumentValue.ConvertTo(targetType, new Dictionary <string, LoggingLevelSwitch>());

            Assert.IsAssignableFrom(targetType, actual);
            Assert.Equal(ConcreteImpl.Instance, actual);
        }
예제 #8
0
        public void StringValuesConvertToTypeFromAssemblyQualifiedName()
        {
            var assemblyQualifiedName = typeof(Version).AssemblyQualifiedName;
            var stringArgumentValue   = new StringArgumentValue(() => assemblyQualifiedName);

            var actual = (Type)stringArgumentValue.ConvertTo(typeof(Type), new Dictionary <string, LoggingLevelSwitch>());

            Assert.Equal(typeof(Version), actual);
        }
예제 #9
0
        public void StaticMembersAccessorsCanBeUsedForReferenceTypes(string input, Type targetType)
        {
            var stringArgumentValue = new StringArgumentValue($"{input}");

            var actual = stringArgumentValue.ConvertTo(targetType, new ResolutionContext());

            Assert.IsAssignableFrom(targetType, actual);
            Assert.Equal(ConcreteImpl.Instance, actual);
        }
예제 #10
0
        public void StringValuesConvertToTypeFromShortTypeName()
        {
            var shortTypeName       = "System.Version";
            var stringArgumentValue = new StringArgumentValue(() => shortTypeName);

            var actual = (Type)stringArgumentValue.ConvertTo(typeof(Type), new Dictionary <string, LoggingLevelSwitch>());

            Assert.Equal(typeof(Version), actual);
        }
예제 #11
0
        public void StringValuesConvertToTypeFromAssemblyQualifiedName()
        {
            var assemblyQualifiedName = typeof(Version).AssemblyQualifiedName;
            var stringArgumentValue   = new StringArgumentValue(assemblyQualifiedName);

            var actual = (Type)stringArgumentValue.ConvertTo(typeof(Type), new ResolutionContext());

            Assert.Equal(typeof(Version), actual);
        }
예제 #12
0
        public void StringValuesConvertToTypeFromShortTypeName()
        {
            var shortTypeName       = "System.Version";
            var stringArgumentValue = new StringArgumentValue(shortTypeName);

            var actual = (Type)stringArgumentValue.ConvertTo(typeof(Type), new ResolutionContext());

            Assert.Equal(typeof(Version), actual);
        }
예제 #13
0
        public void StaticAccessorWithInvalidMemberThrowsInvalidOperationException(string input, Type targetType)
        {
            var stringArgumentValue = new StringArgumentValue($"{input}");
            var exception           = Assert.Throws <InvalidOperationException>(() =>
                                                                                stringArgumentValue.ConvertTo(targetType, new ResolutionContext())
                                                                                );

            Assert.Contains("Could not find a public static property or field ", exception.Message);
            Assert.Contains("on type `Serilog.Settings.Configuration.Tests.Support.ClassWithStaticAccessors, Serilog.Settings.Configuration.Tests`", exception.Message);
        }
예제 #14
0
        public void ReferencingUndeclaredLevelSwitchThrows()
        {
            var resolutionContext = new ResolutionContext();

            resolutionContext.AddLevelSwitch("$anotherSwitch", new LoggingLevelSwitch(LogEventLevel.Verbose));

            var stringArgumentValue = new StringArgumentValue("$mySwitch");

            var ex = Assert.Throws <InvalidOperationException>(() =>
                                                               stringArgumentValue.ConvertTo(typeof(LoggingLevelSwitch), resolutionContext)
                                                               );

            Assert.Contains("$mySwitch", ex.Message);
            Assert.Contains("\"LevelSwitches\":{\"$mySwitch\":", ex.Message);
        }
예제 #15
0
        public void LevelSwitchesCanBeLookedUpByName()
        {
            var @switch           = new LoggingLevelSwitch(LogEventLevel.Verbose);
            var switchName        = "$theSwitch";
            var resolutionContext = new ResolutionContext();

            resolutionContext.AddLevelSwitch(switchName, @switch);

            var stringArgumentValue = new StringArgumentValue(switchName);

            var resolvedSwitch = stringArgumentValue.ConvertTo(typeof(LoggingLevelSwitch), resolutionContext);

            Assert.IsType <LoggingLevelSwitch>(resolvedSwitch);
            Assert.Same(@switch, resolvedSwitch);
        }
        public void LevelSwitchesCanBeLookedUpByName()
        {
            var @switch          = new LoggingLevelSwitch(LogEventLevel.Verbose);
            var switchName       = "$theSwitch";
            var declaredSwitches = new Dictionary <string, LoggingLevelSwitch>()
            {
                { switchName, @switch }
            };

            var stringArgumentValue = new StringArgumentValue(() => switchName);

            var resolvedSwitch = stringArgumentValue.ConvertTo(typeof(LoggingLevelSwitch), declaredSwitches);

            Assert.IsType <LoggingLevelSwitch>(resolvedSwitch);
            Assert.Same(@switch, resolvedSwitch);
        }
        public void ReferencingUndeclaredLevelSwitchThrows()
        {
            var declaredSwitches = new Dictionary <string, LoggingLevelSwitch>()
            {
                { "$anotherSwitch", new LoggingLevelSwitch(LogEventLevel.Verbose) }
            };

            var stringArgumentValue = new StringArgumentValue(() => "$mySwitch");

            var ex = Assert.Throws <InvalidOperationException>(() =>
                                                               stringArgumentValue.ConvertTo(typeof(LoggingLevelSwitch), declaredSwitches)
                                                               );

            Assert.Contains("$mySwitch", ex.Message);
            Assert.Contains("\"LevelSwitches\":{\"$mySwitch\":", ex.Message);
        }
예제 #18
0
        public void TryParseStaticMemberAccessorReturnsExpectedResults(string input, string expectedAccessorType, string expectedPropertyName)
        {
            var actual = StringArgumentValue.TryParseStaticMemberAccessor(input,
                                                                          out var actualAccessorType,
                                                                          out var actualMemberName);

            if (expectedAccessorType == null)
            {
                Assert.False(actual, $"Should not parse {input}");
            }
            else
            {
                Assert.True(actual, $"should successfully parse {input}");
                Assert.Equal(expectedAccessorType, actualAccessorType);
                Assert.Equal(expectedPropertyName, actualMemberName);
            }
        }
예제 #19
0
        public void FindTypeSupportsSimpleNamesForSerilogTypes(string input, Type targetType)
        {
            var type = StringArgumentValue.FindType(input);

            Assert.Equal(targetType, type);
        }