public ScriptLoaderTests() { scope = new NestedScope(); errorCache = new ErrorCacheMock(); loader = new ScriptLoader { Scope = scope, ErrorCache = errorCache }; scope.Store(new Variable { PackageId = Constants.PackageManagerIdentity, Name = "var1", Default = "1", Type = SupportedTypes.Integer.GetStringValue(), Units = Units.None.GetStringValue() }); scope.Store(new Variable { PackageId = Constants.PackageManagerIdentity, Name = "var_2", Default = "60s", Type = SupportedTypes.String.GetStringValue(), Units = Units.None.GetStringValue() }); }
public void TestBuildNestedHierarchy() { var names = new[] { "a.b", "a.d.e", "a", "a.b.c", "a.c", "a.B.c", "a.d", "a" }; var errorMock = new ErrorCacheMock(); var scopes = NestedScope.BuildHeirarchy(names, errorMock); errorMock.Count.Should().Be(0); scopes.Count.Should().Be(6); foreach (var pair in scopes) { pair.Value.Name.Should().Be(pair.Key); } var a = scopes["a"]; var ab = scopes["a.b"]; var ac = scopes["a.c"]; var ad = scopes["a.d"]; var abc = scopes["a.b.c"]; var ade = scopes["a.d.e"]; a.IsTopMost.Should().BeTrue(); a.Children.Should().Contain(new[] { ab, ac, ad }); ab.Children.Should().Contain(abc); ad.Children.Should().Contain(ade); }
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 void TestFirstVarOfName(int lookupLevel, string[] shouldFind) { // Store three variables at the top and bottom of the stack var var1 = new Variable { PackageId = "top", Name = "var1", Units = Units.None.GetStringValue(), Type = SupportedTypes.Integer.GetStringValue() }; scopeStack[0].Store(var1); var var1A = new Variable { PackageId = "middle", Name = "var1", Units = Units.None.GetStringValue(), Type = SupportedTypes.Integer.GetStringValue() }; scopeStack[1].Store(var1A); var var2 = new Variable { PackageId = "bottom", Name = "var2", Units = Units.None.GetStringValue(), Type = SupportedTypes.Integer.GetStringValue() }; scopeStack[2].Store(var2); // Lookup names var1 and var2 at the specified level in the scope nesting var lookupScope = scopeStack[lookupLevel]; var errorCache = new ErrorCacheMock(); var found = new[] { lookupScope.FirstVarOfName("var1", errorCache), lookupScope.FirstVarOfName("var2", errorCache) } .Where(v => v != null) .Select(v => v.Ref.ToFullName()) .ToArray(); foreach (var message in errorCache) { output.WriteLine(message); } found.Should().Equal(shouldFind); errorCache.Count.Should().Be(2 - shouldFind.Length); }
public TimeSeriesSettingsTests() { errorCache = new ErrorCacheMock(); globalScope = new NestedScope { Name = Constants.PackageManagerIdentity }; packageScope = new NestedScope(globalScope) { Name = "TestPackage" }; AddVariable(globalScope, Constants.GlobalSettings.NoOfShards, DefaultShards); AddVariable(globalScope, Constants.GlobalSettings.NoOfReplicas, DefaultReplicas); AddVariable(globalScope, Constants.GlobalSettings.RefreshInterval, DefaultRefreshInterval, SupportedTypes.String); }
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(); }
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); }
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); }
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); }
public void TestMergeWithExistingHierarchy() { var names = new[] { "a.b", "a.d.e", "a", "a.b.c", "a.c", "a.B.c", "a.d", "a" }; var errorMock = new ErrorCacheMock(); var scopes = NestedScope.BuildHeirarchy(names, errorMock); names = new[] { "a.b", "a.b.c", "a.d.f", "a.z" }; scopes = NestedScope.BuildHeirarchy(names, errorMock, scopes); errorMock.Count.Should().Be(0); scopes.Count.Should().Be(8); foreach (var pair in scopes) { pair.Value.Name.Should().Be(pair.Key); } var a = scopes["a"]; var ab = scopes["a.b"]; var ac = scopes["a.c"]; var ad = scopes["a.d"]; var abc = scopes["a.b.c"]; var ade = scopes["a.d.e"]; var adf = scopes["a.d.f"]; var az = scopes["a.z"]; a.IsTopMost.Should().BeTrue(); a.Children.Should().Contain(new[] { ab, ac, ad, az }); ab.Children.Should().Contain(abc); ad.Children.Should().Contain(new[] { ade, adf }); az.Children.Should().BeEmpty(); }