Example #1
0
        /// <summary>
        /// Gets a metadata entry with the specified key.
        /// </summary>
        /// <param name="descriptor">The <see cref="CommandDescriptor"/>.</param>
        /// <param name="key">The metadata key.</param>
        /// <typeparam name="T">The type of the metadata value.</typeparam>
        /// <returns>The metadata value.</returns>
        /// <exception cref="InvalidCastException">The metadata value is not of the specified type.</exception>
        /// <exception cref="KeyNotFoundException">The metadata entry was not found.</exception>
        public static T GetMetadata <T>(this CommandDescriptor descriptor, string key)
        {
            if (!TryGetMetadata(descriptor, key, out T value))
            {
                throw new KeyNotFoundException($"Metadata value with key {key} is unknown.");
            }

            return(value);
        }
Example #2
0
        /// <summary>
        /// Gets a metadata entry with the specified key. Returns the <paramref name="defaultValue"/> if not found.
        /// </summary>
        /// <param name="descriptor">The <see cref="CommandDescriptor"/>.</param>
        /// <param name="key">The metadata key.</param>
        /// <param name="defaultValue">The metadata default value.</param>
        /// <typeparam name="T">The type of the metadata value.</typeparam>
        /// <returns>The metadata value if the entry was found; <paramref name="defaultValue"/> otherwise.</returns>
        /// <exception cref="InvalidCastException">The metadata value is not of the specified type.</exception>
        public static T GetMetadata <T>(this CommandDescriptor descriptor, string key, T defaultValue)
        {
            if (!TryGetMetadata(descriptor, key, out T result))
            {
                result = defaultValue;
            }

            return(result);
        }
        public void TryGetMetadataReturnsFalseIfNotFound()
        {
            string testKey    = "test";
            var    metadata   = new Dictionary <string, object>();
            var    descriptor = new CommandDescriptor(typeof(TestCommand), metadata);

            descriptor.TryGetMetadata(testKey, out int value)
            .Should()
            .BeFalse();
        }
        public void GetMetadataReturnsDefaultValueIfNotFound()
        {
            int    defaultValue = 123;
            string testKey      = "test";
            var    metadata     = new Dictionary <string, object>();
            var    descriptor   = new CommandDescriptor(typeof(TestCommand), metadata);

            descriptor.GetMetadata(testKey, defaultValue)
            .Should()
            .Be(defaultValue);
        }
        public void GetMetadataThrowsIfNotFound()
        {
            var metadata   = new Dictionary <string, object>();
            var descriptor = new CommandDescriptor(typeof(TestCommand), metadata);

            Action action = () =>
            {
                descriptor.GetMetadata <int>("key");
            };

            action.Should()
            .Throw <KeyNotFoundException>();
        }
        public void GetMetadataReturnsValue()
        {
            string testKey   = "test";
            int    testValue = 1;

            var metadata = new Dictionary <string, object> {
                { testKey, testValue }
            };
            var descriptor = new CommandDescriptor(typeof(TestCommand), metadata);

            descriptor.GetMetadata <int>(testKey)
            .Should()
            .Be(testValue);
        }
        public void TryGetMetadataThrowsIfOfWrongType()
        {
            string testKey   = "test";
            int    testValue = 1;

            var metadata = new Dictionary <string, object> {
                { testKey, testValue }
            };
            var descriptor = new CommandDescriptor(typeof(TestCommand), metadata);

            Action action = () =>
            {
                descriptor.TryGetMetadata(testKey, out string wrongTypeValue);
            };

            action.Should()
            .Throw <InvalidCastException>();
        }
Example #8
0
        /// <summary>
        /// Tries to get an metadata entry with the specified key.
        /// </summary>
        /// <param name="descriptor">The <see cref="CommandDescriptor"/>.</param>
        /// <param name="key">The metadata key.</param>
        /// <param name="value">The metadata value.</param>
        /// <typeparam name="T">The type of the metadata value.</typeparam>
        /// <returns><c>true</c> if the metadata entry was found; <c>false</c> otherwise.</returns>
        /// <exception cref="InvalidCastException">The metadata value is not of the specified type.</exception>
        public static bool TryGetMetadata <T>(this CommandDescriptor descriptor, string key, out T value)
        {
            Ensure.Arg.NotNull(descriptor, nameof(descriptor));
            Ensure.Arg.NotEmpty(key, nameof(key));

            if (!descriptor.Metadata.TryGetValue(key, out object tmp))
            {
                value = default;
                return(false);
            }

            if (!(tmp is T))
            {
                throw new InvalidCastException(
                          $"Metadata value with key {key} is not of the expected type {typeof(T).GetDisplayName()}");
            }

            value = (T)tmp;
            return(true);
        }
        public void PopulatesCommandDescriptorWithMetadata()
        {
            var provider1 = Substitute.For <ICommandMetadataProvider>();

            provider1.When(p => p.GetMetadata(Arg.Any <Type>(), Arg.Any <IDictionary <string, object> >()))
            .Do(
                ci =>
            {
                var metadata = ci.ArgAt <IDictionary <string, object> >(1);
                metadata.Add("1", 1);
            });

            var provider2 = Substitute.For <ICommandMetadataProvider>();

            provider2.When(p => p.GetMetadata(Arg.Any <Type>(), Arg.Any <IDictionary <string, object> >()))
            .Do(
                ci =>
            {
                var metadata = ci.ArgAt <IDictionary <string, object> >(1);
                metadata.Add("2", 2);
            });

            var factory = new CommandDescriptorFactory(
                new[]
            {
                provider1,
                provider2
            });

            CommandDescriptor descriptor = factory.CreateDescriptor(typeof(TestCommand));

            descriptor.Metadata.Should()
            .BeEquivalentTo(
                new KeyValuePair <string, object>("1", 1),
                new KeyValuePair <string, object>("2", 2));
        }