// Passes initial value overrides (presumably from ConfigurationSettings)
        public ConfigurationSettings(
            IExecutionState executionState,
            IConfiguration configuration,
            IEnumerable <KeyValuePair <string, object> > settings)
            : base(configuration)
        {
            _executionState = executionState ?? throw new ArgumentNullException(nameof(executionState));

            _settings = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);
            if (settings != null)
            {
                foreach (KeyValuePair <string, object> setting in settings)
                {
                    this[setting.Key] = setting.Value;
                }
            }

            // Iterate over configuration and convert them, but only if we don't already have a setting for that key
            foreach (KeyValuePair <string, string> item in configuration.AsEnumerable())
            {
                if (!_settings.ContainsKey(item.Key) &&
                    ScriptMetadataValue.TryGetScriptMetadataValue(item.Key, item.Value, executionState, out ScriptMetadataValue metadataValue))
                {
                    _settings[item.Key] = metadataValue;
                }
            }
        }
Beispiel #2
0
            public void ReturnsTrueForValidScript(string value)
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                // When
                bool result = ScriptMetadataValue.TryGetScriptMetadataValue(string.Empty, value, context, out ScriptMetadataValue metadataValue);

                // Then
                result.ShouldBeTrue();
                metadataValue.ShouldNotBeNull();
            }
Beispiel #3
0
            public void ReturnsFalseForNormalString()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                // When
                bool result = ScriptMetadataValue.TryGetScriptMetadataValue(string.Empty, "1234", context, out ScriptMetadataValue metadataValue);

                // Then
                result.ShouldBeFalse();
                metadataValue.ShouldBeNull();
            }
Beispiel #4
0
            public void ReturnsFalseForStringBuilder()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                // When
                bool result = ScriptMetadataValue.TryGetScriptMetadataValue(string.Empty, new StringBuilder("=> $\"ABC {1+2} XYZ\""), context, out ScriptMetadataValue metadataValue);

                // Then
                result.ShouldBeFalse();
                metadataValue.ShouldBeNull();
            }
        /// <inheritdoc/>
        public new object this[string key]
        {
            get => base[key];
            set
            {
                _ = key ?? throw new ArgumentNullException(nameof(key));

                _settings[key] = ScriptMetadataValue.TryGetScriptMetadataValue(key, value, _executionState, out ScriptMetadataValue metadataValue)
                    ? metadataValue
                    : value;
            }
        }
Beispiel #6
0
            public void EvaluatesIntScriptResult()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                ScriptMetadataValue.TryGetScriptMetadataValue("Foo", "=> 1 + 2", context, out ScriptMetadataValue scriptMetadataValue);
                TestDocument document = new TestDocument
                {
                    { "Foo", scriptMetadataValue }
                };

                // When
                object result = document.Get("Foo");

                // Then
                result.ShouldBe(3);
            }
Beispiel #7
0
            public void EvaluatesScriptMetadata(string value)
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                ScriptMetadataValue.TryGetScriptMetadataValue("Foo", value, context, out ScriptMetadataValue scriptMetadataValue);
                TestDocument document = new TestDocument
                {
                    { "Foo", scriptMetadataValue }
                };

                // When
                string result = document.GetString("Foo");

                // Then
                result.ShouldBe("ABC 3 XYZ");
            }
        // Passes initial value overrides (presumably from ConfigurationSettings)
        public ReadOnlyConfigurationSettings(
            IExecutionState executionState,
            IConfiguration configuration,
            IDictionary <string, object> valueOverrides)
            : base(configuration)
        {
            _ = executionState ?? throw new ArgumentNullException(nameof(executionState));

            _valueOverrides = valueOverrides ?? new Dictionary <string, object>();
            foreach (KeyValuePair <string, string> item in configuration.AsEnumerable())
            {
                if (!_valueOverrides.ContainsKey(item.Key) &&
                    ScriptMetadataValue.TryGetScriptMetadataValue(item.Key, item.Value, executionState, out ScriptMetadataValue metadataValue))
                {
                    _valueOverrides[item.Key] = metadataValue;
                }
            }
        }
Beispiel #9
0
            public void DoesNotExcludeAllScriptEvaluation()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                ScriptMetadataValue.TryGetScriptMetadataValue("Bar", "=> 1 + 2", context, out ScriptMetadataValue fooValue);
                ScriptMetadataValue.TryGetScriptMetadataValue("Foo", "=> 3 + 4", context, out ScriptMetadataValue barValue);
                TestDocument document = new TestDocument
                {
                    { "Foo", fooValue },
                    { "Bar", barValue },
                    { Keys.ExcludeFromEvaluation, false }
                };

                // When
                object fooResult = document.Get("Foo");
                object barResult = document.Get("bar");

                // Then
                fooResult.ShouldBe(3);
                barResult.ShouldBe(7);
            }
Beispiel #10
0
            public void ExcludesMultipleValuesFromScriptEvaluation()
            {
                // Given
                TestExecutionContext context = new TestExecutionContext();

                context.ScriptHelper = new ScriptHelper(context);
                ScriptMetadataValue.TryGetScriptMetadataValue("Foo", "=> 1 + 2", context, out ScriptMetadataValue fooValue);
                ScriptMetadataValue.TryGetScriptMetadataValue("Bar", "=> 3 + 4", context, out ScriptMetadataValue barValue);
                TestDocument document = new TestDocument
                {
                    { "Foo", fooValue },
                    { "Bar", barValue },
                    { Keys.ExcludeFromEvaluation, new string[] { "foo", "Bar" } }
                };

                // When
                object fooResult = document.Get("Foo");
                object barResult = document.Get("bar");

                // Then
                fooResult.ShouldBe("=> 1 + 2");
                barResult.ShouldBe("=> 3 + 4");
            }