Exemplo n.º 1
0
        public void ExtractVariableWorks(object value, object defaultValue, SupportedTypes supportedType, Units unit, object max = null, object min = null)
        {
            var variable = new Variable
            {
                Name    = "Test",
                Value   = value?.ToString(),
                Default = defaultValue?.ToString(),
                Max     = max?.ToString(),
                Min     = min?.ToString(),
                Type    = supportedType.GetStringValue(),
                Units   = unit.GetStringValue()
            };

            var errorCache = new ErrorCacheMock();
            var info       = VariableInfo.Extract(variable, "myPackage", errorCache);

            errorCache.ShouldHaveNoErrors();

            info.Ref.VariableName.Should().Be("Test");
            info.Ref.PackageId.Should().Be("myPackage");

            info.HasErrors.Should().BeFalse();
            info.Value.ShouldBeEquivalentTo(value);
            info.Default.ShouldBeEquivalentTo(defaultValue);
            info.Min.ShouldBeEquivalentTo(min);
            info.Max.ShouldBeEquivalentTo(max);
            info.SupportedType.Should().Be(supportedType);
            info.Units.Should().Be(unit);
        }
        public VariableInfo LoadConstantSetting(Constant constant, string settingName)
        {
            // Lookup a previously stored version of this variable
            var variableRef = new VariableRef
            {
                PackageId    = PackageId,
                VariableName = $"constant.{settingName}"
            };

            if (!Scope.TryFindVariable(variableRef, out var variable))
            {
                // This is the first time a constant has been used here
                // so create a new variable for it
                variable = new Variable {
                    Name = variableRef.VariableName
                };
            }

            variable.Value       = variable.Default = constant.Value.ToString();
            variable.Units       = constant.Units.GetStringValue();
            variable.Type        = constant.SupportedType.GetStringValue();
            variable.IsGenerated = true;

            return(VariableInfo.Extract(variable, PackageId, ErrorCache));
        }
 public VariableInfo LookupReferencedVariable(VariableRef variableRef)
 {
     if (!Scope.TryFindVariable(variableRef, out var variable))
     {
         ErrorCache.AddError($"Could not find variable: '{variableRef.ToFullName()}'");
         return(null);
     }
     return(VariableInfo.Extract(variable, variableRef.PackageId, ErrorCache));
 }
        public VariableInfo LookupGlobalDefaultSetting(string settingName)
        {
            var globalRef = new VariableRef
            {
                PackageId    = Constants.PackageManagerIdentity,
                VariableName = settingName
            };

            if (!Scope.TryFindVariable(globalRef, out var variable))
            {
                ErrorCache.AddError($"Could not find global default variable: '{globalRef.ToFullName()}'");
                return(null);
            }
            return(VariableInfo.Extract(variable, globalRef.PackageId, ErrorCache));
        }
Exemplo n.º 5
0
        public virtual VariableInfo FirstVarOfName(string variableName, IErrorCache errorCache)
        {
            if (byLocalName.TryGetValue(variableName, out var result))
            {
                return(VariableInfo.Extract(result, result.PackageId, errorCache));
            }

            if (Scope?.Parent != null)
            {
                return(Scope.Parent.FirstVarOfName(variableName, errorCache));
            }

            errorCache.AddError($"Could not FirstVarOfName for variable called {variableName}");
            return(null);
        }
Exemplo n.º 6
0
        public void ExtractVariableWithErrors(object value, object defaultValue, string supportedType, string unit, string errorMessage)
        {
            var variable = new Variable
            {
                Name    = "Test",
                Value   = value?.ToString(),
                Default = defaultValue?.ToString(),
                Type    = supportedType,
                Units   = unit
            };

            var errorCache = new ErrorCacheMock();
            var info       = VariableInfo.Extract(variable, "myPackage", errorCache);

            errorCache.ShouldContain(errorMessage);
            info.HasErrors.Should().BeTrue();
        }
Exemplo n.º 7
0
        public void StringValueTypeConversionWorks(string value, string defaultValue, string expected)
        {
            var variable = new Variable
            {
                Name    = "sample",
                Value   = value,
                Default = defaultValue,
                Type    = SupportedTypes.String.GetStringValue(),
                Units   = Units.None.GetStringValue()
            };

            var errorCache = new ErrorCacheMock();
            var info       = VariableInfo.Extract(variable, "myPackage", errorCache);

            info.GetValueOrDefault <string>(errorCache).Should().Be(expected);
            errorCache.Count.Should().Be(0);
        }
Exemplo n.º 8
0
        public void IntegerValueTypeConversionWorks(int?value, int?defaultValue, int expected)
        {
            var variable = new Variable
            {
                Name    = "sample",
                Value   = value?.ToString(),
                Default = defaultValue?.ToString(),
                Type    = SupportedTypes.Integer.GetStringValue(),
                Units   = Units.None.GetStringValue()
            };

            var errorCache = new ErrorCacheMock();
            var info       = VariableInfo.Extract(variable, "myPackage", errorCache);

            info.GetValueOrDefault <int>(errorCache).Should().Be(expected);
            errorCache.Count.Should().Be(0);
        }
Exemplo n.º 9
0
        public void IntegerValueTypeConversionFailsCorrectly()
        {
            var variable = new Variable
            {
                Name  = "sample",
                Value = "abc",
                Type  = SupportedTypes.Integer.GetStringValue(),
                Units = Units.None.GetStringValue()
            };

            var errorCache = new ErrorCacheMock();
            var info       = VariableInfo.Extract(variable, "myPackage", errorCache);

            errorCache.Count.Should().Be(1);

            errorCache.Clear();
            Assert.Throws <InvalidOperationException>(() => info.GetValueOrDefault <int>(errorCache));
            errorCache.Count.Should().Be(1);
        }