/// <summary>
 /// Gets the value associated with the specified <paramref name="key"/>,
 /// recursively resolving placeholders according to specified <paramref name="options"/>
 /// </summary>
 /// <param name="configuration">
 /// The <paramref name="configuration"/> providing the values.
 /// </param>
 /// <param name="key">
 /// The key whose value to resolve.
 /// </param>
 /// <param name="options">
 /// The options used in the resolver process.
 /// </param>
 /// <param name="mapUnresolvable">
 /// A function used to map placeholders, which could not be resolved.
 /// </param>
 public static string ResolveValue(
     this IConfiguration configuration, string key,
     SubstitutionOptions options, Func <string, string> mapUnresolvable) =>
 configuration
 .ToConfigurationValueProvider()
 .ToResolverValueProvider(options, mapUnresolvable)
 .GetValue(key);
            public void TwoResolveSteps_OneKeyPerStep_SecondStepUnresolvable_InvokesMapValueFunction(
                SubstitutionOptions options, string prefix, string suffix
                )
            {
                // Arrange
                const string KEY_1   = "key1";
                const string KEY_2   = "key2";
                const string KEY_3   = "key3";
                string       VALUE_1 = prefix + KEY_2 + suffix;
                string       VALUE_2 = prefix + KEY_3 + suffix;

                configurationMock = Mock.Of <IConfiguration>(c =>
                                                             c[KEY_1] == VALUE_1 &&
                                                             c[KEY_2] == VALUE_2
                                                             );

                var mapValueMock = Mock.Of <Func <string, string> >();

                // Act
                configurationMock.ResolveValue(KEY_1, options, mapValueMock);

                // Assert
                Mock.Get(mapValueMock)
                .Verify(f => f(It.Is <string>(v => v == VALUE_2)));
            }
 public void AddSubstitution(
     string search,
     string replacement,
     SubstitutionOptions options)
 {
     AddSubstitution(Project.Settings, search, replacement, options);
 }
 /// <summary>
 /// Gets the value associated with the specified <paramref name="key"/>,
 /// recursively resolving placeholders according to specified <paramref name="options"/>
 /// </summary>
 /// <param name="configuration">
 /// The <paramref name="configuration"/> providing the values.
 /// </param>
 /// <param name="key">
 /// The key whose value to resolve.
 /// </param>
 /// <param name="options">
 /// The options used in the resolver process.
 /// </param>
 /// <throws>
 /// Throws <see cref="ValueUnresolvableException"/> when a value can
 /// not be resolved.
 /// </throws>
 public static string ResolveValue(
     this IConfiguration configuration, string key,
     SubstitutionOptions options = SubstitutionOptions.All) =>
 configuration
 .ToConfigurationValueProvider()
 .ToResolverValueProvider(options, ThrowValueUnresolvableException)
 .GetValue(key);
        public void AddSubstitution(
			string search,
			string replacement,
			SubstitutionOptions options)
        {
            AddSubstitution(Project.Settings, search, replacement, options);
        }
 public void KeyNotExists_ThrowsKeyNotFoundException(
     SubstitutionOptions options, string prefix, string suffix
     )
 {
     Assert.Throws <KeyNotFoundException>(
         () => configurationMock.ResolveValue(prefix + suffix, options)
         );
 }
 public RegisteredSubstitution(
     string search,
     string replacement,
     SubstitutionOptions options)
 {
     // Save the fields for the substitution.
     Search      = search;
     Replacement = replacement;
     Options     = options;
 }
        /// <summary>
        /// Gets the value associated with the specified <paramref name="key"/>,
        /// recursively resolving placeholders according to specified <paramref name="options"/>.
        /// </summary>
        /// <param name="configuration">
        /// The <paramref name="configuration"/> providing the values.
        /// </param>
        /// <param name="key">
        /// The key whose value to resolve.
        /// </param>
        /// <param name="value">
        /// When this method returns, the value associated with the specified key,
        /// if the key can be resolved; otherwise, null.
        /// This parameter is passed uninitialized.
        /// </param>
        /// <param name="options">
        /// The options used in the resolver process.
        /// </param>
        public static bool TryResolveValue(this IConfiguration configuration, string key,
                                           out string?value, SubstitutionOptions options = SubstitutionOptions.All
                                           )
        {
            var result = configuration
                         .ToConfigurationValueProvider()
                         .ToResolverValueProvider(options, ThrowValueUnresolvableException)
                         .TryGetValue(key);

            value = result.value;
            return(result.success);
        }
            public void InfiniteSteps_TwoKeysPerStep_ThrowsValueUnresolvableException(
                SubstitutionOptions options, string prefix, string suffix
                )
            {
                // Arrange
                const string KEY_1   = "key1";
                string       VALUE_1 = prefix + KEY_1 + suffix + prefix + KEY_1 + suffix;

                configurationMock = Mock.Of <IConfiguration>(
                    c => c[KEY_1] == VALUE_1
                    );

                // AssertThrows
                Assert.Throws <ValueUnresolvableException>(
                    () => configurationMock.ResolveValue(KEY_1, options)
                    );
            }
            public void ZeroResolveSteps_OneKeyPerStep_KeyEmptyString_ReturnsResolved(
                SubstitutionOptions options
                )
            {
                // Arrange
                const string KEY_1   = "";
                const string VALUE_1 = "val1";

                configurationMock = Mock.Of <IConfiguration>(
                    c => c[KEY_1] == VALUE_1
                    );

                // Act
                var actualValue = configurationMock.ResolveValue(KEY_1, options);

                // Assert
                Assert.AreEqual(VALUE_1, actualValue);
            }
        public void AddSubstitution(
            SettingsManager settingsManager,
            string search,
            string replacement,
            SubstitutionOptions options)
        {
            // Create the substitution we'll be registering.
            var substitution = new RegisteredSubstitution(search, replacement, options);

            // Grab the configuration object for this settings manager or create one if
            // it doesn't already exist.
            var settings =
                settingsManager.Get <ImmediateCorrectionSettings>(
                    ImmediateCorrectionSettings.SettingsPath);

            settings.Substitutions.Add(substitution);

            // Mark that our substituions are out of date.
            optimizedSubstitions = false;
        }
        /// <summary>
        /// Creates an instance of <see cref="IConfiguration"/>, wherein
        /// entries with placeholders are resolved according to specified <paramref name="options"/>.
        /// <param name="configuration">
        /// The <paramref name="configuration"/> providing the values.
        /// </param>
        /// </summary>
        /// <param name="options">
        /// The options used in the resolver process.
        /// </param>
        public static IConfiguration Resolved(
            this IConfiguration configuration,
            SubstitutionOptions options = SubstitutionOptions.All
            )
        {
            if (configuration is null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            return(new ConfigurationProxy(
                       configuration: configuration,
                       valueProvider: (config, key) => TryResolveValue(
                           configuration: config,
                           key: key,
                           value: out var val,
                           options: options
                           ) ? val ! : null ! // Required by signature
                       ));
        }
        public void AddSubstitution(
			SettingsManager settingsManager,
			string search,
			string replacement,
			SubstitutionOptions options)
        {
            // Create the substitution we'll be registering.
            var substitution = new RegisteredSubstitution(search, replacement, options);

            // Grab the configuration object for this settings manager or create one if
            // it doesn't already exist.
            var settings =
                settingsManager.Get<ImmediateCorrectionSettings>(
                    ImmediateCorrectionSettings.SettingsPath);

            settings.Substitutions.Add(substitution);

            // Mark that our substituions are out of date.
            optimizedSubstitions = false;
        }
            public void OneResolveStep_OneKeyPerStep_ReturnsResolved(
                SubstitutionOptions options, string prefix, string suffix
                )
            {
                // Arrange
                const string KEY_1   = "key1";
                const string KEY_2   = "key2";
                string       VALUE_1 = prefix + KEY_2 + suffix;
                const string VALUE_2 = "val2";

                configurationMock = Mock.Of <IConfiguration>(c =>
                                                             c[KEY_1] == VALUE_1 &&
                                                             c[KEY_2] == VALUE_2
                                                             );

                // Act
                var actualValue = configurationMock.ResolveValue(KEY_1, options);

                // Assert
                Assert.AreEqual(VALUE_2, actualValue);
            }
Exemplo n.º 15
0
        /// <summary>
        /// Converts the specified <paramref name="options"/> to their corresponding
        /// Regular Expressions.
        /// </summary>
        public static IEnumerable <string> ToRegexPatterns(this SubstitutionOptions options)
        {
            if ((options & SubstitutionOptions.CurlyBracketsDollarEnv) != 0)
            {
                yield return(@"\{\$env:([\s\S]*?)\}");
            }

            if ((options & SubstitutionOptions.DollarCurlyBrackets) != 0)
            {
                yield return(@"\$\{([\s\S]*?)\}");
            }

            if ((options & SubstitutionOptions.DollarBrackets) != 0)
            {
                yield return(@"\$\(([\s\S]*?)\)");
            }

            if ((options & SubstitutionOptions.Percent) != 0)
            {
                yield return(@"\%([\s\S]*?)\%");
            }
        }
            public void OneResolveStep_OneKeyPerStep_FirstStepUnresolvable_ReturnsMapValueFunctionResult(
                SubstitutionOptions options, string prefix, string suffix
                )
            {
                // Arrange
                const string KEY_1          = "key1";
                const string KEY_2          = "key2";
                string       VALUE_1        = prefix + KEY_2 + suffix;
                const string VALUE_1_MAPPED = "val1";

                configurationMock = Mock.Of <IConfiguration>(
                    c => c[KEY_1] == VALUE_1
                    );

                var mapValueMock = Mock.Of <Func <string, string> >(
                    f => f(VALUE_1) == VALUE_1_MAPPED
                    );

                // Act
                var result = configurationMock.ResolveValue(KEY_1, options, mapValueMock);

                // Assert
                Assert.AreEqual(VALUE_1_MAPPED, result);
            }
Exemplo n.º 17
0
 /// <summary>
 /// Initiaizes a new instance of the <see cref="ResolverValueProvider"/>class.
 /// </summary>
 /// <param name="provider">The provider from which to get the raw values.</param>
 /// <param name="options">The options to use.</param>
 /// <param name="mapUnresolvable">
 /// A function used to map placeholders, which could not be resolved.
 /// </param>
 public ResolverValueProvider(IValueProvider provider, SubstitutionOptions options, Func <string, string> mapUnresolvable)
 {
     this.provider        = provider ?? throw new ArgumentNullException(nameof(provider));
     this.options         = options;
     this.mapUnresolvable = mapUnresolvable ?? throw new ArgumentNullException(nameof(mapUnresolvable));
 }
 /// <summary>
 /// Creates an instance of <see cref="ResolverValueProvider"/> from the specified
 /// <paramref name="provider"/>.
 /// </summary>
 public static ResolverValueProvider ToResolverValueProvider(
     this IValueProvider provider, SubstitutionOptions options, Func <string, string> mapUnresolvable) =>
 new ResolverValueProvider(provider, options, mapUnresolvable);