コード例 #1
0
        public void EmptyHierarchy_GetParent_ShouldThrow()
        {
            var    hierarchy = new SyntaxHierarchy();
            Action fail      = () => hierarchy.GetParent(TestSyntaxFactory.CreateNull());

            fail.Should().Throw <ArgumentException>().WithMessage("Unable to determine parent of specified node of type 'NullLiteralSyntax' at span '[0:0]' because it has not been indexed.");
        }
コード例 #2
0
        protected virtual void TestQueryAllReturnsExpected(string code, TestSourceKind codeKind, string query, params string[] expected)
        {
            var current = TestSyntaxFactory.Parse(code, codeKind);
            var results = new RoslynCSharpSyntaxQueryExecutor().QueryAll(current, ParseQuery(query));

            Assert.Equal(expected, results.Select(r => r.ToString()).ToArray());
        }
コード例 #3
0
        public void ValidArrayParameterModifierShouldProduceNoDiagnostics()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("default", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateBool(true))
                })),

                TestSyntaxFactory.CreateProperty("allowed", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateArray(Enumerable.Empty <ArrayItemSyntax>()))
                })),

                TestSyntaxFactory.CreateProperty("minLength", TestSyntaxFactory.CreateInt(33)),
                TestSyntaxFactory.CreateProperty("maxLength", TestSyntaxFactory.CreateInt(25)),

                TestSyntaxFactory.CreateProperty("metadata", TestSyntaxFactory.CreateObject(new[]
                {
                    TestSyntaxFactory.CreateProperty("description", TestSyntaxFactory.CreateString("my description")),
                    TestSyntaxFactory.CreateProperty("extra1", TestSyntaxFactory.CreateString("extra")),
                    TestSyntaxFactory.CreateProperty("extra2", TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateProperty("extra3", TestSyntaxFactory.CreateInt(100))
                }))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.Array)).Should().BeEmpty();
        }
コード例 #4
0
        public void ValidStringParameterModifierShouldProduceNoDiagnostics()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("secure", TestSyntaxFactory.CreateBool(false)),
                TestSyntaxFactory.CreateProperty("default", TestSyntaxFactory.CreateString("One")),

                TestSyntaxFactory.CreateProperty("allowed", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("One")),
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("Two")),
                })),

                TestSyntaxFactory.CreateProperty("minLength", TestSyntaxFactory.CreateInt(33)),
                TestSyntaxFactory.CreateProperty("maxLength", TestSyntaxFactory.CreateInt(25)),

                TestSyntaxFactory.CreateProperty("metadata", TestSyntaxFactory.CreateObject(new[]
                {
                    TestSyntaxFactory.CreateProperty("description", TestSyntaxFactory.CreateString("my description")),
                    TestSyntaxFactory.CreateProperty("extra1", TestSyntaxFactory.CreateString("extra")),
                    TestSyntaxFactory.CreateProperty("extra2", TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateProperty("extra3", TestSyntaxFactory.CreateInt(100))
                }))
            });

            var allowedValuesType = UnionType.Create(new StringLiteralType("One"), new StringLiteralType("Two"));

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.String, allowedValuesType)).Should().BeEmpty();
        }
コード例 #5
0
 public void QueryAll_PrimitiveType(string type)
 {
     TestQueryAll(
         new[] { type },
         TestSyntaxFactory.ParseStatement($"{type} x;"),
         $"//{type}"
         );
 }
コード例 #6
0
        public void MinimalResourceShouldBeValid()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test"))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType()).Should().BeEmpty();
        }
コード例 #7
0
        public void ListMustHaveOneFewerSeparatorThanElements(int elementCount, int separatorCount)
        {
            var elements   = Enumerable.Repeat(TestSyntaxFactory.CreateInt(42), elementCount);
            var separators = Enumerable.Repeat(TestSyntaxFactory.CreateToken(TokenType.Colon), separatorCount);

            Action wrongSeparators = () => new SeparatedSyntaxList(elements, separators, new TextSpan(0, 0));

            wrongSeparators.Should().Throw <ArgumentException>().WithMessage($"The number of separators ({separatorCount}) must be the same or one less than the number of elements ({elementCount}).");
        }
コード例 #8
0
        public void EmptyListMustHaveZeroSeparators(int separatorCount)
        {
            var separators = Enumerable.Repeat(TestSyntaxFactory.CreateToken(TokenType.Colon), separatorCount);

            // ReSharper disable once ObjectCreationAsStatement
            Action wrongSeparators = () => new SeparatedSyntaxList(Enumerable.Empty <SyntaxBase>(), separators, new TextSpan(53, 1));

            wrongSeparators.Should().Throw <ArgumentException>().WithMessage("With zero elements, the number of separators must also be zero.");
        }
コード例 #9
0
        public void RequiredPropertyShouldBeRequired()
        {
            var obj = TestSyntaxFactory.CreateObject(new ObjectPropertySyntax[0]);

            var errors = TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType()).ToList();

            errors.Should().HaveCount(1);

            errors.Single().Message.Should().Be("The specified object is missing the following required properties: name.");
        }
コード例 #10
0
        public void RequiredPropertyWithParseErrorsShouldProduceNoErrors()
        {
            var obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("dupe", TestSyntaxFactory.CreateString("a")),
                TestSyntaxFactory.CreateProperty("dupe", TestSyntaxFactory.CreateString("a"))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType()).Should().BeEmpty();
        }
コード例 #11
0
        public void ResourceWithValidZonesShouldBeAccepted()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test")),
                TestSyntaxFactory.CreateProperty("zones", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("1")),
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("2"))
                }))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType()).Should().BeEmpty();
        }
コード例 #12
0
        public void WrongTypeOfAdditionalPropertiesWithParseErrorsShouldProduceNoErrors()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test")),
                TestSyntaxFactory.CreateProperty("tags", TestSyntaxFactory.CreateObject(new[]
                {
                    TestSyntaxFactory.CreateProperty("wrongTagType", TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateProperty("wrongTagType", TestSyntaxFactory.CreateInt(3))
                }))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType()).Should().BeEmpty();
        }
コード例 #13
0
        public void ParameterModifierShouldRejectAdditionalProperties()
        {
            var obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("extra", TestSyntaxFactory.CreateString("foo")),
                TestSyntaxFactory.CreateProperty("extra2", TestSyntaxFactory.CreateString("foo"))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.String))
            .Select(e => e.Message)
            .Should()
            .Equal(
                "The property 'extra' is not allowed on objects of type ParameterModifier_string.",
                "The property 'extra2' is not allowed on objects of type ParameterModifier_string.");
        }
コード例 #14
0
        public void MinimalResourceShouldBeValid()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test"))
            });

            var hierarchy = new SyntaxHierarchy();

            hierarchy.AddRoot(obj);

            var(_, diagnostics) = NarrowTypeAndCollectDiagnostics(hierarchy, obj, CreateDummyResourceType());

            diagnostics.Should().BeEmpty();
        }
コード例 #15
0
        public void ParameterModifierShouldRejectAdditionalProperties()
        {
            var obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("extra", TestSyntaxFactory.CreateString("foo")),
                TestSyntaxFactory.CreateProperty("extra2", TestSyntaxFactory.CreateString("foo"))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.String, LanguageConstants.String))
            .Select(e => e.Message)
            .Should()
            .Equal(
                "The property 'extra' is not allowed on objects of type ParameterModifier<string>. Permissible properties include 'allowed', 'default', 'maxLength', 'metadata', 'minLength', 'secure'.",
                "The property 'extra2' is not allowed on objects of type ParameterModifier<string>. Permissible properties include 'allowed', 'default', 'maxLength', 'metadata', 'minLength', 'secure'.");
        }
コード例 #16
0
        public void CompletelyInvalidBoolParameterModifier_ShouldLogExpectedErrors()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                // not a bool and not allowed
                TestSyntaxFactory.CreateProperty("secure", TestSyntaxFactory.CreateInt(1)),

                // default value of wrong type
                TestSyntaxFactory.CreateProperty("default", TestSyntaxFactory.CreateInt(1231)),

                // not an array
                TestSyntaxFactory.CreateProperty("allowed", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateInt(22))
                })),

                // not allowed
                TestSyntaxFactory.CreateProperty("minValue", TestSyntaxFactory.CreateBool(true)),
                TestSyntaxFactory.CreateProperty("maxValue", TestSyntaxFactory.CreateString("11")),
                TestSyntaxFactory.CreateProperty("minLength", TestSyntaxFactory.CreateObject(new ObjectPropertySyntax[0])),
                TestSyntaxFactory.CreateProperty("maxLength", TestSyntaxFactory.CreateBool(false)),

                // extra property
                TestSyntaxFactory.CreateProperty("extra", TestSyntaxFactory.CreateBool(false)),

                TestSyntaxFactory.CreateProperty("metadata", TestSyntaxFactory.CreateObject(new[]
                {
                    // wrong type of description
                    TestSyntaxFactory.CreateProperty("description", TestSyntaxFactory.CreateInt(155))
                }))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.Bool))
            .Select(d => d.Message)
            .Should().BeEquivalentTo(
                "The property 'default' expected a value of type bool but the provided value is of type int.",
                "The enclosing array expected an item of type bool, but the provided item was of type int.",
                "The property 'description' expected a value of type string but the provided value is of type int.",
                "The property 'secure' is not allowed on objects of type ParameterModifier_bool.",
                "The property 'minValue' is not allowed on objects of type ParameterModifier_bool.",
                "The property 'maxValue' is not allowed on objects of type ParameterModifier_bool.",
                "The property 'minLength' is not allowed on objects of type ParameterModifier_bool.",
                "The property 'maxLength' is not allowed on objects of type ParameterModifier_bool.",
                "The property 'extra' is not allowed on objects of type ParameterModifier_bool.");
        }
コード例 #17
0
        public void Valid_parameter_modifier_should_ensure_default_value_is_assignable_to_allowed_values()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("default", TestSyntaxFactory.CreateString("Three")),

                TestSyntaxFactory.CreateProperty("allowed", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("One")),
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("Two")),
                })),
            });

            var allowedValuesType = UnionType.Create(new StringLiteralType("One"), new StringLiteralType("Two"));

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.String, allowedValuesType))
            .Should().SatisfyRespectively(
                x => x.Message.Should().Be("The property 'default' expected a value of type 'One' | 'Two' but the provided value is of type 'Three'."));
        }
コード例 #18
0
        public void CompletelyInvalidArrayParameterModifier_ShouldLogExpectedErrors()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                // not a bool
                TestSyntaxFactory.CreateProperty("secure", TestSyntaxFactory.CreateInt(1)),

                // default value of wrong type
                TestSyntaxFactory.CreateProperty("default", TestSyntaxFactory.CreateBool(true)),

                // not an array
                TestSyntaxFactory.CreateProperty("allowed", TestSyntaxFactory.CreateObject(new ObjectPropertySyntax[0])),

                // not ints
                TestSyntaxFactory.CreateProperty("minValue", TestSyntaxFactory.CreateBool(true)),
                TestSyntaxFactory.CreateProperty("maxValue", TestSyntaxFactory.CreateString("11")),
                TestSyntaxFactory.CreateProperty("minLength", TestSyntaxFactory.CreateObject(new ObjectPropertySyntax[0])),
                TestSyntaxFactory.CreateProperty("maxLength", TestSyntaxFactory.CreateBool(false)),

                // extra property
                TestSyntaxFactory.CreateProperty("extra", TestSyntaxFactory.CreateBool(false)),

                TestSyntaxFactory.CreateProperty("metadata", TestSyntaxFactory.CreateObject(new[]
                {
                    // wrong type of description
                    TestSyntaxFactory.CreateProperty("description", TestSyntaxFactory.CreateInt(155))
                }))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.Array))
            .Select(d => d.Message)
            .Should()
            .BeEquivalentTo(
                "The property 'default' expected a value of type 'array' but the provided value is of type 'bool'.",
                "The property 'maxLength' expected a value of type 'int' but the provided value is of type 'bool'.",
                "The property 'allowed' expected a value of type 'array[]' but the provided value is of type 'object'.",
                "The property 'minLength' expected a value of type 'int' but the provided value is of type 'object'.",
                "The property 'description' expected a value of type 'string' but the provided value is of type 'int'.",
                "The property 'secure' is not allowed on objects of type 'ParameterModifier_array'.",
                "The property 'minValue' is not allowed on objects of type 'ParameterModifier_array'.",
                "The property 'maxValue' is not allowed on objects of type 'ParameterModifier_array'.",
                "The property 'extra' is not allowed on objects of type 'ParameterModifier_array'.");
        }
コード例 #19
0
        public void WrongTypeOfAdditionalPropertiesShouldBeRejected()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test")),
                TestSyntaxFactory.CreateProperty("tags", TestSyntaxFactory.CreateObject(new[]
                {
                    TestSyntaxFactory.CreateProperty("wrongTagType", TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateProperty("wrongTagType2", TestSyntaxFactory.CreateInt(3))
                }))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType())
            .Select(d => d.Message)
            .Should()
            .BeEquivalentTo(
                "The property 'wrongTagType' expected a value of type string but the provided value is of type bool.",
                "The property 'wrongTagType2' expected a value of type string but the provided value is of type int.");
        }
コード例 #20
0
        public void ResourceWithValidZonesShouldBeAccepted()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test")),
                TestSyntaxFactory.CreateProperty("zones", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("1")),
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateString("2"))
                }))
            });

            var hierarchy = new SyntaxHierarchy();

            hierarchy.AddRoot(obj);

            var(narrowedType, diagnostics) = NarrowTypeAndCollectDiagnostics(hierarchy, obj, CreateDummyResourceType());
            diagnostics.Should().BeEmpty();
        }
コード例 #21
0
        public void InvalidArrayValuesShouldBeRejected()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test")),

                // zones is an array of strings - set wrong item types
                TestSyntaxFactory.CreateProperty("zones", TestSyntaxFactory.CreateArray(new[]
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateInt(2))
                })),

                // this property is an array - specify a string instead
                TestSyntaxFactory.CreateProperty("managedByExtended", TestSyntaxFactory.CreateString("not an array"))
            });

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, CreateDummyResourceType())
            .Select(d => d.Message)
            .Should().BeEquivalentTo(
                "The enclosing array expected an item of type string, but the provided item was of type bool.",
                "The property 'managedByExtended' expected a value of type string[] but the provided value is of type 'not an array'.",
                "The enclosing array expected an item of type string, but the provided item was of type int.");
        }
コード例 #22
0
        public void InvalidArrayValuesShouldBeRejected()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("name", TestSyntaxFactory.CreateString("test")),

                // zones is an array of strings - set wrong item types
                TestSyntaxFactory.CreateProperty("zones", TestSyntaxFactory.CreateArray(new[]
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateInt(2))
                })),

                // this property is an array - specify a string instead
                TestSyntaxFactory.CreateProperty("managedByExtended", TestSyntaxFactory.CreateString("not an array"))
            });

            var hierarchy = new SyntaxHierarchy();

            hierarchy.AddRoot(obj);

            var(narrowedType, diagnostics) = NarrowTypeAndCollectDiagnostics(hierarchy, obj, CreateDummyResourceType());

            diagnostics.OrderBy(x => x.Message).Should().HaveDiagnostics(new[] {
コード例 #23
0
 public void QueryAll_SwitchSection(string query, string code, string[] expected)
 {
     TestQueryAll(expected, TestSyntaxFactory.ParseStatement(code), query);
 }
コード例 #24
0
 public void QueryAll_Declaration_SpecialCategory(string query, string code, string expected)
 {
     TestQueryAll(new[] { expected }, TestSyntaxFactory.ParseCompilationUnit(code), query);
 }
コード例 #25
0
 public void QueryAll_Statement(string query, string code, string expected)
 {
     TestQueryAll(new[] { expected }, TestSyntaxFactory.ParseStatement(code), query);
 }
コード例 #26
0
        public void DiscriminatedObjectType_raises_appropriate_diagnostics_for_matches()
        {
            var discriminatedType = new DiscriminatedObjectType(
                "discObj",
                "myDiscriminator",
                new []
            {
                new NamedObjectType("typeA", new []
                {
                    new TypeProperty("myDiscriminator", new StringLiteralType("valA")),
                    new TypeProperty("fieldA", LanguageConstants.Any, TypePropertyFlags.Required),
                }, null),
                new NamedObjectType("typeB", new []
                {
                    new TypeProperty("myDiscriminator", new StringLiteralType("valB")),
                    new TypeProperty("fieldB", LanguageConstants.Any, TypePropertyFlags.Required),
                }, null),
            });

            // no discriminator field supplied
            var obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("fieldA", TestSyntaxFactory.CreateString("someVal")),
            });

            var errors = TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, discriminatedType);

            errors.Should().SatisfyRespectively(
                x => {
                x.Message.Should().Be("The property 'myDiscriminator' requires a value of type 'valA' | 'valB', but none was supplied.");
            });

            // incorrect type specified for the discriminator field
            obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("myDiscriminator", TestSyntaxFactory.CreateObject(Enumerable.Empty <ObjectPropertySyntax>())),
                TestSyntaxFactory.CreateProperty("fieldB", TestSyntaxFactory.CreateString("someVal")),
            });

            errors = TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, discriminatedType);
            errors.Should().SatisfyRespectively(
                x => {
                x.Message.Should().Be("The property 'myDiscriminator' expected a value of type 'valA' | 'valB' but the provided value is of type object.");
            });

            // discriminator value that matches neither option supplied
            obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("myDiscriminator", TestSyntaxFactory.CreateString("valC")),
            });

            errors = TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, discriminatedType);
            errors.Should().SatisfyRespectively(
                x => {
                x.Message.Should().Be("The property 'myDiscriminator' expected a value of type 'valA' | 'valB' but the provided value is of type 'valC'.");
            });

            // missing required property for the 'valB' branch
            obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("myDiscriminator", TestSyntaxFactory.CreateString("valB")),
            });

            errors = TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, discriminatedType);
            errors.Should().SatisfyRespectively(
                x => {
                x.Message.Should().Be("The specified object is missing the following required properties: fieldB.");
            });

            // supplied the required property for the 'valB' branch
            obj = TestSyntaxFactory.CreateObject(new []
            {
                TestSyntaxFactory.CreateProperty("myDiscriminator", TestSyntaxFactory.CreateString("valB")),
                TestSyntaxFactory.CreateProperty("fieldB", TestSyntaxFactory.CreateString("someVal")),
            });

            errors = TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, discriminatedType);
            errors.Should().BeEmpty();
        }
コード例 #27
0
        public void EmptyModifierIsValid()
        {
            var obj = TestSyntaxFactory.CreateObject(new ObjectPropertySyntax[0]);

            TypeValidator.GetExpressionAssignmentDiagnostics(CreateTypeManager(), obj, LanguageConstants.CreateParameterModifierType(LanguageConstants.Int)).Should().BeEmpty();
        }
コード例 #28
0
ファイル: ErrorBuilderTests.cs プロジェクト: husains/bicep
        private static object CreateMockParameter(ParameterInfo parameter, int index)
        {
            if (parameter.ParameterType == typeof(TypeSymbol))
            {
                return(new PrimitiveType($"<type_{index}>", TypeSymbolValidationFlags.Default));
            }

            if (parameter.ParameterType == typeof(IList <TypeSymbol>))
            {
                return(new List <TypeSymbol> {
                    new PrimitiveType($"<list_type_{index}>", TypeSymbolValidationFlags.Default)
                });
            }

            if (parameter.ParameterType == typeof(IEnumerable <string>))
            {
                return(new List <string> {
                    $"<value_{index}"
                });
            }

            if (parameter.ParameterType == typeof(IList <string>))
            {
                return(new List <string> {
                    $"<value_{index}"
                });
            }

            if (parameter.ParameterType == typeof(ImmutableArray <string>))
            {
                return(new[] { $"<value_{index}" }.ToImmutableArray());
            }

            if (parameter.ParameterType == typeof(Uri))
            {
                return(new Uri("file:///path/to/main.bicep"));
            }

            if (parameter.ParameterType == typeof(Symbol))
            {
                // just using this one as it's easy to construct
                return(ErrorType.Create(Enumerable.Empty <ErrorDiagnostic>()));
            }

            if (parameter.ParameterType == typeof(int) || parameter.ParameterType == typeof(int?))
            {
                return(0);
            }

            if (parameter.ParameterType == typeof(long) || parameter.ParameterType == typeof(long?))
            {
                return(0);
            }

            if (parameter.ParameterType == typeof(bool) || parameter.ParameterType == typeof(bool?))
            {
                return(false);
            }

            if (parameter.ParameterType == typeof(SymbolKind))
            {
                return(SymbolKind.Variable);
            }

            if (parameter.ParameterType == typeof(ResourceTypeReference))
            {
                return(ResourceTypeReference.Parse("Mock.ErrorParam/mockResources@2020-01-01"));
            }

            if (parameter.ParameterType == typeof(ResourceScope))
            {
                return(ResourceScope.ResourceGroup);
            }

            if (parameter.ParameterType == typeof(string) || parameter.ParameterType == typeof(IEnumerable <char>))
            {
                return($"<param_{index}>");
            }

            if (parameter.ParameterType == typeof(ObjectSyntax))
            {
                return(TestSyntaxFactory.CreateObject(Array.Empty <ObjectPropertySyntax>()));
            }

            throw new AssertFailedException($"Unable to generate mock parameter value of type '{parameter.ParameterType}' for the diagnostic builder method.");
        }
コード例 #29
0
        private static IEnumerable <object[]> GetLiteralExpressionData()
        {
            // simple types
            yield return(CreateRow("null", TestSyntaxFactory.CreateNull()));

            yield return(CreateRow("true", TestSyntaxFactory.CreateBool(true)));

            yield return(CreateRow("false", TestSyntaxFactory.CreateBool(false)));

            yield return(CreateRow("string", TestSyntaxFactory.CreateString("hello")));

            yield return(CreateRow("int", TestSyntaxFactory.CreateInt(42)));

            yield return(CreateRow("empty object", TestSyntaxFactory.CreateObject(new ObjectPropertySyntax[0])));

            yield return(CreateRow("object literal", TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("one", TestSyntaxFactory.CreateNull()),
                TestSyntaxFactory.CreateProperty("two", TestSyntaxFactory.CreateBool(true)),
                TestSyntaxFactory.CreateProperty("three", TestSyntaxFactory.CreateBool(false)),
                TestSyntaxFactory.CreateProperty("four", TestSyntaxFactory.CreateString("hello")),
                TestSyntaxFactory.CreateProperty("five", TestSyntaxFactory.CreateInt(42)),
                TestSyntaxFactory.CreateProperty("six", TestSyntaxFactory.CreateObject(new []
                {
                    TestSyntaxFactory.CreateProperty("one", TestSyntaxFactory.CreateNull()),
                    TestSyntaxFactory.CreateProperty("two", TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateProperty("three", TestSyntaxFactory.CreateBool(false)),
                    TestSyntaxFactory.CreateProperty("four", TestSyntaxFactory.CreateString("test")),
                    TestSyntaxFactory.CreateProperty("five", TestSyntaxFactory.CreateInt(100)),
                    TestSyntaxFactory.CreateProperty("six", TestSyntaxFactory.CreateArray(new SyntaxBase[]
                    {
                        TestSyntaxFactory.CreateNull(),
                        TestSyntaxFactory.CreateBool(true),
                        TestSyntaxFactory.CreateBool(false),
                        TestSyntaxFactory.CreateString("other"),
                        TestSyntaxFactory.CreateInt(103)
                    }))
                }))
            })));

            yield return(CreateRow("empty array", TestSyntaxFactory.CreateArray(new ArrayItemSyntax[0])));

            yield return(CreateRow("array literal", TestSyntaxFactory.CreateArray(new SyntaxBase[]
            {
                TestSyntaxFactory.CreateNull(),
                TestSyntaxFactory.CreateBool(true),
                TestSyntaxFactory.CreateBool(false),
                TestSyntaxFactory.CreateString("other"),
                TestSyntaxFactory.CreateInt(103),
                TestSyntaxFactory.CreateObject(new[]
                {
                    TestSyntaxFactory.CreateProperty("one", TestSyntaxFactory.CreateNull()),
                    TestSyntaxFactory.CreateProperty("two", TestSyntaxFactory.CreateBool(true)),
                    TestSyntaxFactory.CreateProperty("three", TestSyntaxFactory.CreateBool(false)),
                    TestSyntaxFactory.CreateProperty("four", TestSyntaxFactory.CreateString("test")),
                    TestSyntaxFactory.CreateProperty("five", TestSyntaxFactory.CreateInt(100)),
                    TestSyntaxFactory.CreateProperty("six", TestSyntaxFactory.CreateArray(new SyntaxBase[]
                    {
                        TestSyntaxFactory.CreateNull(),
                        TestSyntaxFactory.CreateBool(true),
                        TestSyntaxFactory.CreateBool(false),
                        TestSyntaxFactory.CreateString("other"),
                        TestSyntaxFactory.CreateInt(103)
                    }))
                }),
                TestSyntaxFactory.CreateArray(new SyntaxBase[]
                {
                    TestSyntaxFactory.CreateNull(),
                    TestSyntaxFactory.CreateBool(true),
                    TestSyntaxFactory.CreateBool(false),
                    TestSyntaxFactory.CreateString("other"),
                    TestSyntaxFactory.CreateInt(103)
                })
            })));
        }