Beispiel #1
0
        public void TryParseEnumMemberOfOneValueShouldBeTrue()
        {
            var    enumType             = new EdmEnumType("Ns", "Color");
            var    blue                 = enumType.AddMember("Blue", new EdmEnumMemberValue(0));
            var    white                = enumType.AddMember("White", new EdmEnumMemberValue(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "  Ns.Color/Blue  ";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            Assert.True(EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember));
            Assert.Equal(blue, parsedMember.Single());

            // JSON
            string jsonEnumPath = "Blue";

            Assert.True(EdmEnumValueParser.TryParseJsonEnumMember(jsonEnumPath, enumType, null, out parsedMember));
            Assert.Equal(blue, parsedMember.Single());

            jsonEnumPath = "1";
            Assert.True(EdmEnumValueParser.TryParseJsonEnumMember(jsonEnumPath, enumType, null, out parsedMember));
            Assert.Equal(white, parsedMember.Single());
        }
Beispiel #2
0
        public void TryParseEnumMemberWithFlagsOfTwoValuesShouldBeTrue()
        {
            var    enumType             = new EdmEnumType("Ns", "Permission", true);
            var    read                 = enumType.AddMember("Read", new EdmEnumMemberValue(0));
            var    write                = enumType.AddMember("Write", new EdmEnumMemberValue(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = " Ns.Permission/Read   Ns.Permission/Write ";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            Assert.True(EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember));
            Assert.Equal(2, parsedMember.Count());
            Assert.Equal(read, parsedMember.First());
            Assert.Equal(write, parsedMember.Last());

            // JSON
            string jsonEnumPath = " Read , Write ";

            Assert.True(EdmEnumValueParser.TryParseJsonEnumMember(jsonEnumPath, enumType, null, out parsedMember));
            Assert.Equal(2, parsedMember.Count());
            Assert.Equal(read, parsedMember.First());
            Assert.Equal(write, parsedMember.Last());
        }
        private IEnumerable <EdmError> ComputeErrors()
        {
            IEnumerable <IEdmEnumMember> member;

            if (!EdmEnumValueParser.TryParseEnumMember(this.expression.EnumMemberPath, this.Schema.Model, this.Location, out member))
            {
                return(new EdmError[] { new EdmError(this.Location, EdmErrorCode.InvalidEnumMemberPath, Strings.CsdlParser_InvalidEnumMemberPath(this.expression.EnumMemberPath)) });
            }

            return(Enumerable.Empty <EdmError>());
        }
Beispiel #4
0
        public void TryParseEnumMemberOfMultipleInvalidTypeShouldBeTrue()
        {
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "Ns.Color/Blue Ns.Color/Red";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            Assert.True(EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember));
            Assert.Equal(2, parsedMember.Count());
            Assert.Equal("Blue", parsedMember.First().Name);
            Assert.Equal("Red", parsedMember.ElementAt(1).Name);
        }
Beispiel #5
0
        private IEnumerable <IEdmEnumMember> ComputeReferenced()
        {
            IEnumerable <IEdmEnumMember> member;

            // In OData Json CSDL, a enum member expression is a string value. "@self.HasPattern": "Red"
            // But in OData XML CSDL, a enum member expression is an element like: <EnumMember>org.example.Pattern/Red</EnumMember>
            // So, in OData JSON CSDL, we have to use the Term type to construct EnumMember.
            if (this.expression.EnumType != null)
            {
                return(EdmEnumValueParser.TryParseJsonEnumMember(this.expression.EnumMemberPath, this.expression.EnumType, this.Location, out member) ? member : null);
            }

            return(EdmEnumValueParser.TryParseEnumMember(this.expression.EnumMemberPath, this.Schema.Model, this.Location, out member) ? member : null);
        }
Beispiel #6
0
        public void TryParseEnumMemberOfInvalidPathShouldBeFalse()
        {
            var enumType = new EdmEnumType("Ns", "Color");

            enumType.AddMember("Blue", new EdmEnumMemberValue(0));
            enumType.AddMember("White", new EdmEnumMemberValue(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "Ns.Color//Blue";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            Assert.False(EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember));
        }
Beispiel #7
0
        public void TryParseEnumMemberWithoutFlagsOfTwoValueShouldBeFalse()
        {
            var enumType = new EdmEnumType("Ns", "Permission");

            enumType.AddMember("Read", new EdmEnumMemberValue(0));
            enumType.AddMember("Write", new EdmEnumMemberValue(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "Ns.Permission/Read Ns.Permission/Write";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            Assert.False(EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember));
        }
Beispiel #8
0
        public void TryParseEnumMemberOfTwoValuesWithInvalidEnumTypeShouldBeFalse()
        {
            var enumType = new EdmEnumType("Ns", "Permission", EdmPrimitiveTypeKind.String, true);

            enumType.AddMember("Read", new EdmStringConstant("1"));
            enumType.AddMember("Write", new EdmStringConstant("2"));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "Ns.Permission/Read Ns.Permissions/Write";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember).Should().BeFalse();
        }
Beispiel #9
0
        public void TryParseEnumMemberOfInvalidEnumMemberShouldBeFalse()
        {
            var enumType = new EdmEnumType("Ns", "Color");

            enumType.AddMember("Blue", new EdmIntegerConstant(0));
            enumType.AddMember("White", new EdmIntegerConstant(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "Ns.Color/Green";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember).Should().BeFalse();
        }
        public void TryParseEnumMemberOfOneValueShouldBeTrue()
        {
            var enumType = new EdmEnumType("Ns", "Color");
            var blue     = enumType.AddMember("Blue", new EdmEnumMemberValue(0));

            enumType.AddMember("White", new EdmEnumMemberValue(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "  Ns.Color/Blue  ";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember).Should().BeTrue();
            parsedMember.Single().Should().Be(blue);
        }
        public void TryParseEnumMemberWithFlagsOfMultiValueShouldBeTrue()
        {
            var    enumType             = new EdmEnumType("Ns", "Permission", true);
            var    read                 = enumType.AddMember("Read", new EdmEnumMemberValue(1));
            var    write                = enumType.AddMember("Write", new EdmEnumMemberValue(2));
            var    readwrite            = enumType.AddMember("ReadWrite", new EdmEnumMemberValue(3));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = "Ns.Permission/Read  Ns.Permission/Write  Ns.Permission/ReadWrite";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember).Should().BeTrue();
            parsedMember.Count().Should().Be(3);
            parsedMember.Should().Contain(read).And.Contain(write).And.Contain(readwrite);
        }
Beispiel #12
0
        public void TryParseEnumMemberWithFlagsOfTwoValuesShouldBeTrue()
        {
            var    enumType             = new EdmEnumType("Ns", "Permission", true);
            var    read                 = enumType.AddMember("Read", new EdmIntegerConstant(0));
            var    write                = enumType.AddMember("Write", new EdmIntegerConstant(1));
            var    complexType          = new EdmComplexType("Ns", "Address");
            string enumPath             = " Ns.Permission/Read   Ns.Permission/Write ";
            List <IEdmSchemaType> types = new List <IEdmSchemaType> {
                enumType, complexType
            };
            IEnumerable <IEdmEnumMember> parsedMember;

            EdmEnumValueParser.TryParseEnumMember(enumPath, BuildModelFromTypes(types), null, out parsedMember).Should().BeTrue();
            parsedMember.Count().Should().Be(2);
            parsedMember.First().Should().Be(read);
            parsedMember.Last().Should().Be(write);
        }
        private IEnumerable <IEdmEnumMember> ComputeReferenced()
        {
            IEnumerable <IEdmEnumMember> member;

            return(EdmEnumValueParser.TryParseEnumMember(this.expression.EnumMemberPath, this.Schema.Model, this.Location, out member) ? member : null);
        }