public void AddDirectiveWithArgument()
        {
            // arrange
            var descriptor = EnumTypeDescriptor.New(Context);

            // act
            IEnumTypeDescriptor desc = descriptor;

            desc.Directive("Bar",
                           new ArgumentNode("a", new StringValueNode("b")));

            // assert
            EnumTypeDefinition description = descriptor.CreateDefinition();

            Assert.Collection(description.Directives,
                              t =>
            {
                Assert.Equal("Bar", t.ParsedDirective.Name.Value);
                Assert.Collection(t.ParsedDirective.Arguments,
                                  x =>
                {
                    Assert.Equal("a", x.Name.Value);
                    Assert.IsType <StringValueNode>(x.Value);
                    Assert.Equal("b", ((StringValueNode)x.Value).Value);
                });
            });
        }
Example #2
0
        private void Initialize(EnumTypeDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            if (string.IsNullOrEmpty(descriptor.Name))
            {
                throw new ArgumentException(
                          "Am enum type name must not be null or empty.");
            }

            foreach (EnumValue enumValue in descriptor.GetItems()
                     .Select(t => new EnumValue(t)))
            {
                _nameToValues[enumValue.Name]   = enumValue;
                _valueToValues[enumValue.Value] = enumValue;
            }

            SyntaxNode  = descriptor.SyntaxNode;
            Name        = descriptor.Name;
            Description = descriptor.Description;
            NativeType  = descriptor.NativeType;
        }
        public void SpecifyOneValueInferTheOthers()
        {
            // arrange
            var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));

            // act
            IEnumTypeDescriptor desc = descriptor;

            desc.Item(FooEnum.Bar1).Name("FOOBAR");

            // assert
            EnumTypeDefinition description = descriptor.CreateDefinition();

            Assert.Collection(description.Values,
                              t =>
            {
                Assert.Equal("FOOBAR", t.Name);
                Assert.Equal(FooEnum.Bar1, t.Value);
            },
                              t =>
            {
                Assert.Equal("BAR2", t.Name);
                Assert.Equal(FooEnum.Bar2, t.Value);
            });
        }
Example #4
0
        private void Initialize(Action <IEnumTypeDescriptor> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            EnumTypeDescriptor descriptor = CreateDescriptor();

            configure(descriptor);

            EnumTypeDescription description = descriptor.CreateDescription();

            foreach (EnumValue enumValue in description.Values
                     .Select(t => new EnumValue(t)))
            {
                _nameToValues[enumValue.Name]   = enumValue;
                _valueToValues[enumValue.Value] = enumValue;
            }

            SyntaxNode = description.SyntaxNode;
            ClrType    = description.ClrType;

            Initialize(description.Name, description.Description,
                       new DirectiveCollection(
                           this,
                           DirectiveLocation.Enum,
                           description.Directives));
        }
        public void InferNameFromType()
        {
            // act
            var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));

            // assert
            Assert.Equal("FooEnum", descriptor.CreateDefinition().Name);
        }
Example #6
0
        public void InitialName()
        {
            // act
            var descriptor = new EnumTypeDescriptor("Foo");

            // assert
            Assert.Equal("Foo", descriptor.CreateDescription().Name);
        }
        public void NoTypeProvided()
        {
            // act
            Action a = () => EnumTypeDescriptor.New(Context, (Type)null);

            // assert
            Assert.Throws <ArgumentNullException>(a);
        }
Example #8
0
        public void InferNameFromType()
        {
            // act
            var descriptor = new EnumTypeDescriptor(typeof(FooEnum));

            // assert
            Assert.Equal("FooEnum", descriptor.CreateDescription().Name);
        }
Example #9
0
        protected override EnumTypeDefinition CreateDefinition(
            IInitializationContext context)
        {
            var descriptor = EnumTypeDescriptor.New(
                context.DescriptorContext);

            _configure(descriptor);
            return(descriptor.CreateDefinition());
        }
Example #10
0
        protected override EnumTypeDefinition CreateDefinition(
            ITypeDiscoveryContext context)
        {
            var descriptor = EnumTypeDescriptor.New <T>(
                context.DescriptorContext);

            _configure(descriptor);
            return(descriptor.CreateDefinition());
        }
Example #11
0
        protected override EnumTypeDefinition CreateDefinition(
            ITypeDiscoveryContext context)
        {
            var descriptor = EnumTypeDescriptor.FromSchemaType(
                context.DescriptorContext,
                GetType());

            _configure(descriptor);
            return(descriptor.CreateDefinition());
        }
Example #12
0
        private void Initialize(Action <IEnumTypeDescriptor> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            EnumTypeDescriptor descriptor = CreateDescriptor();

            configure(descriptor);
            Initialize(descriptor);
        }
Example #13
0
        public void AddDirective()
        {
            // arrange
            var descriptor = new EnumTypeDescriptor("Foo");

            // act
            IEnumTypeDescriptor desc = descriptor;

            desc.Directive("Bar");

            // assert
            EnumTypeDescription description = descriptor.CreateDescription();

            Assert.Collection(description.Directives,
                              t => Assert.Equal("Bar", t.ParsedDirective.Name.Value));
        }
        public void AddDirectiveWithDirectiveNode()
        {
            // arrange
            var descriptor = EnumTypeDescriptor.New(Context);

            // act
            IEnumTypeDescriptor desc = descriptor;

            desc.Directive(new DirectiveNode("Bar"));

            // assert
            EnumTypeDefinition description = descriptor.CreateDefinition();

            Assert.Collection(description.Directives,
                              t => Assert.Equal("Bar", t.ParsedDirective.Name.Value));
        }
        public void InferValuesFromType()
        {
            // act
            var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));

            // assert
            EnumTypeDefinition description = descriptor.CreateDefinition();

            Assert.Collection(description.Values,
                              t =>
            {
                Assert.Equal("BAR1", t.Name);
                Assert.Equal(FooEnum.Bar1, t.Value);
            },
                              t =>
            {
                Assert.Equal("BAR2", t.Name);
                Assert.Equal(FooEnum.Bar2, t.Value);
            });
        }
        public void ExplicitValueBinding()
        {
            // arrange
            var descriptor = EnumTypeDescriptor.New(Context, typeof(FooEnum));

            // act
            IEnumTypeDescriptor desc = descriptor;

            desc.Item(FooEnum.Bar1).Name("FOOBAR");
            desc.BindItems(BindingBehavior.Explicit);

            // assert
            EnumTypeDefinition description = descriptor.CreateDefinition();

            Assert.Collection(description.Values,
                              t =>
            {
                Assert.Equal("FOOBAR", t.Name);
                Assert.Equal(FooEnum.Bar1, t.Value);
            });
        }
Example #17
0
        public EnumType()
        {
            EnumTypeDescriptor descriptor = CreateDescriptor();

            Configure(descriptor);

            if (string.IsNullOrEmpty(descriptor.Name))
            {
                throw new ArgumentException(
                          "Am enum type name must not be null or empty.");
            }

            foreach (EnumValue enumValue in descriptor.CreateEnumValues())
            {
                _nameToValues[enumValue.Name]   = enumValue;
                _valueToValues[enumValue.Value] = enumValue;
            }

            Name        = descriptor.Name;
            Description = descriptor.Description;
            NativeType  = descriptor.NativeType;
        }