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();
        }
        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'.");
        }
        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.");
        }
Exemplo n.º 4
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.");
        }
Exemplo n.º 5
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.");
        }
Exemplo n.º 6
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();
        }
        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'."));
        }
        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'.");
        }
Exemplo n.º 9
0
        public void ValidBoolParameterModifierShouldProduceNoDiagnostics()
        {
            var obj = TestSyntaxFactory.CreateObject(new[]
            {
                TestSyntaxFactory.CreateProperty("default", TestSyntaxFactory.CreateBool(true)),

                TestSyntaxFactory.CreateProperty("allowed", TestSyntaxFactory.CreateArray(new []
                {
                    TestSyntaxFactory.CreateArrayItem(TestSyntaxFactory.CreateBool(false))
                })),

                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.Bool)).Should().BeEmpty();
        }
Exemplo n.º 10
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.");
        }
Exemplo n.º 11
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[] {
Exemplo n.º 12
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();
        }
Exemplo n.º 13
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)
                })
            })));
        }