public void Convert_returns_empty_dictionary_when_null()
        {
            var dictionary = ObjectToDictionary.Convert(null);

            Assert.That(dictionary, Is.Not.Null);
            Assert.That(dictionary, Is.Empty);
        }
        public void Convert_returns_original_object_if_dictionary()
        {
            var data = fixture.Create <Dictionary <string, object> >();

            var dictionary = ObjectToDictionary.Convert(data);

            Assert.That(dictionary, Is.SameAs(data));
        }
Exemple #3
0
        public static void Log <T>(this ILogger logger, LogLevel level, T state, Exception error, Func <T, Exception, string> messageFormatter)
        {
            var dictionary = ObjectToDictionary.Convert(state);

            if (messageFormatter != null)
            {
                var formattedMessage = messageFormatter(state, error);

                dictionary[MessageKey] = formattedMessage;
            }

            logger.Log(level, dictionary, error);
        }
        private bool LogDataContains(object data, string key, object expectedValue)
        {
            var dictionary = ObjectToDictionary.Convert(data);

            if (!dictionary.ContainsKey(key))
            {
                return(false);
            }

            var value = dictionary[key];

            Assert.That(value, Is.EqualTo(expectedValue));

            return(true);
        }
        public void Convert_uses_cache_to_spare_calls()
        {
            var data = new
            {
                text  = fixture.Create <string>(),
                value = fixture.Create <int>(),
                time  = fixture.Create <DateTimeOffset>(),
                flag  = fixture.Create <bool>()
            };

            var dictionary = ObjectToDictionary.Convert(data);

            Assert.That(ObjectToDictionary.Cache, Is.Not.Empty);
            Assert.That(ObjectToDictionary.Cache[data.GetType()], Is.Not.Null);
        }
        public void Convert_returns_a_dictionary_with_items()
        {
            var data = new
            {
                text  = fixture.Create <string>(),
                value = fixture.Create <int>(),
                time  = fixture.Create <DateTimeOffset>(),
                flag  = fixture.Create <bool>()
            };

            var dictionary = ObjectToDictionary.Convert(data);

            Assert.That(dictionary, Is.Not.Null);
            Assert.That(dictionary[nameof(data.text)], Is.EqualTo(data.text));
            Assert.That(dictionary[nameof(data.value)], Is.EqualTo(data.value));
            Assert.That(dictionary[nameof(data.time)], Is.EqualTo(data.time));
            Assert.That(dictionary[nameof(data.flag)], Is.EqualTo(data.flag));
        }